Bug 17828: Overload Koha::Patron::Attribute->store to check for unique_id and repeatable
[koha.git] / Koha / Patron / Attribute.pm
1 package Koha::Patron::Attribute;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 use Modern::Perl;
19
20 use Koha::Database;
21 use Koha::Exceptions::Patron::Attribute;
22 use Koha::Patron::Attribute::Types;
23
24 use base qw(Koha::Object);
25
26 =head1 NAME
27
28 Koha::Patron::Attribute - Koha Patron Attribute Object class
29
30 =head1 API
31
32 =head2 Class Methods
33
34 =cut
35
36 =head3 store
37
38     my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... });
39     try { $attribute->store }
40     catch { handle_exception };
41
42 =cut
43
44 sub store {
45
46     my $self = shift;
47
48     $self->_check_repeatable;
49     $self->_check_unique_id;
50
51     return $self->SUPER::store();
52 }
53
54 =head3 opac_display
55
56     my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... });
57     if ( $attribute->opac_display ) { ... }
58
59 =cut
60
61 sub opac_display {
62
63     my $self = shift;
64
65     return Koha::Patron::Attribute::Types->find( $self->code )->opac_display;
66 }
67
68 =head3 opac_editable
69
70     my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... });
71     if ( $attribute->is_opac_editable ) { ... }
72
73 =cut
74
75 sub opac_editable {
76
77     my $self = shift;
78
79     return Koha::Patron::Attribute::Types->find( $self->code )->opac_editable;
80 }
81
82 =head2 Internal methods
83
84 =head3 _check_repeatable
85
86 _check_repeatable checks if the attribute type is repeatable and throws and exception
87 if the attribute type isn't repeatable and there's already an attribute with the same
88 code for the given patron.
89
90 =cut
91
92 sub _check_repeatable {
93
94     my $self = shift;
95
96     if ( !Koha::Patron::Attribute::Types->find( $self->code )->repeatable ) {
97         my $attr_count
98             = Koha::Database->new->schema->resultset( $self->_type )->search(
99             {   borrowernumber => $self->borrowernumber,
100                 code           => $self->code
101             }
102             )->count;
103         Koha::Exceptions::Patron::Attribute::NonRepeatable->throw()
104             if $attr_count > 0;
105     }
106
107     return $self;
108 }
109
110 =head3 _check_unique_id
111
112 _check_unique_id checks if the attribute type is marked as unique id and throws and exception
113 if the attribute type is a unique id and there's already an attribute with the same
114 code and value on the database.
115
116 =cut
117
118 sub _check_unique_id {
119
120     my $self = shift;
121
122     if ( Koha::Patron::Attribute::Types->find( $self->code )->unique_id ) {
123         my $unique_count
124             = Koha::Database->new->schema->resultset( $self->_type )
125             ->search( { code => $self->code, attribute => $self->attribute } )
126             ->count;
127         Koha::Exceptions::Patron::Attribute::UniqueIDConstraint->throw()
128             if $unique_count > 0;
129     }
130 }
131
132 =head3 _type
133
134 =cut
135
136 sub _type {
137     return 'BorrowerAttribute';
138 }
139
140 1;