Bug 12159: Fix getting extended patron attributes for circ-menu
[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 use Koha::AuthorisedValues;
24
25 use base qw(Koha::Object);
26
27 =head1 NAME
28
29 Koha::Patron::Attribute - Koha Patron Attribute Object class
30
31 =head1 API
32
33 =head2 Class Methods
34
35 =cut
36
37 =head3 store
38
39     my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... });
40     try { $attribute->store }
41     catch { handle_exception };
42
43 =cut
44
45 sub store {
46
47     my $self = shift;
48
49     $self->_check_repeatable;
50     $self->_check_unique_id;
51
52     return $self->SUPER::store();
53 }
54
55 =head3 opac_display
56
57     my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... });
58     if ( $attribute->opac_display ) { ... }
59
60 =cut
61
62 sub opac_display {
63
64     my $self = shift;
65
66     return Koha::Patron::Attribute::Types->find( $self->code )->opac_display;
67 }
68
69 =head3 opac_editable
70
71     my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... });
72     if ( $attribute->is_opac_editable ) { ... }
73
74 =cut
75
76 sub opac_editable {
77
78     my $self = shift;
79
80     return Koha::Patron::Attribute::Types->find( $self->code )->opac_editable;
81 }
82
83 =head3 display_checkout
84
85     my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... });
86     if ( $attribute->display_checkout ) { ... }
87
88 =cut
89
90 sub display_checkout {
91
92     my $self = shift;
93
94     return $self->type->display_checkout;
95 }
96
97 =head3 value_description
98
99     my $description = $attribute->value_description
100
101     Return authorised value description or free text based on attribute type settings
102
103 =cut
104
105 sub value_description {
106
107     my $self = shift;
108
109     if ( $self->type->authorised_value_category ) {
110         my $av = Koha::AuthorisedValues->search({
111             category => $self->type->authorised_value_category,
112             authorised_value => $self->attribute,
113         });
114         return $av->next->lib if $av->count;
115     }
116     return $self->attribute;
117 }
118
119 =head3 type_description
120
121     my $description = $attribute->type_description
122
123     Return description of attribute type
124
125 =cut
126
127 sub type_description {
128
129     my $self = shift;
130
131     return $self->type->description;
132 }
133
134 =head3 type
135
136     my $attribute_type = $attribute->type;
137
138 Returns a C<Koha::Patron::Attribute::Type> object corresponding to the current patron attribute
139
140 =cut
141
142 sub type {
143
144     my $self = shift;
145
146     return Koha::Patron::Attribute::Types->find( $self->code );
147 }
148
149 =head2 Internal methods
150
151 =head3 _check_repeatable
152
153 _check_repeatable checks if the attribute type is repeatable and throws and exception
154 if the attribute type isn't repeatable and there's already an attribute with the same
155 code for the given patron.
156
157 =cut
158
159 sub _check_repeatable {
160
161     my $self = shift;
162
163     if ( !$self->type->repeatable ) {
164         my $attr_count = Koha::Patron::Attributes->search(
165             {   borrowernumber => $self->borrowernumber,
166                 code           => $self->code
167             }
168             )->count;
169         Koha::Exceptions::Patron::Attribute::NonRepeatable->throw()
170             if $attr_count > 0;
171     }
172
173     return $self;
174 }
175
176 =head3 _check_unique_id
177
178 _check_unique_id checks if the attribute type is marked as unique id and throws and exception
179 if the attribute type is a unique id and there's already an attribute with the same
180 code and value on the database.
181
182 =cut
183
184 sub _check_unique_id {
185
186     my $self = shift;
187
188     if ( $self->type->unique_id ) {
189         my $unique_count = Koha::Patron::Attributes
190             ->search( { code => $self->code, attribute => $self->attribute } )
191             ->count;
192         Koha::Exceptions::Patron::Attribute::UniqueIDConstraint->throw()
193             if $unique_count > 0;
194     }
195
196     return $self;
197 }
198
199 =head3 _type
200
201 =cut
202
203 sub _type {
204     return 'BorrowerAttribute';
205 }
206
207 1;