Bug 21156: Add plural translation capabilities to JS files
[koha.git] / Koha / Patron / Attribute.pm
blob09ed98fe86b98604c0fe727f7d552a4eb7c2b2ee
1 package Koha::Patron::Attribute;
3 # This file is part of Koha.
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.
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.
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.
18 use Modern::Perl;
20 use Koha::Database;
21 use Koha::Exceptions::Patron::Attribute;
22 use Koha::Patron::Attribute::Types;
23 use Koha::AuthorisedValues;
25 use base qw(Koha::Object);
27 =head1 NAME
29 Koha::Patron::Attribute - Koha Patron Attribute Object class
31 =head1 API
33 =head2 Class Methods
35 =cut
37 =head3 store
39 my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... });
40 try { $attribute->store }
41 catch { handle_exception };
43 =cut
45 sub store {
47 my $self = shift;
49 $self->_check_repeatable;
50 $self->_check_unique_id;
52 return $self->SUPER::store();
55 =head3 opac_display
57 my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... });
58 if ( $attribute->opac_display ) { ... }
60 =cut
62 sub opac_display {
64 my $self = shift;
66 return Koha::Patron::Attribute::Types->find( $self->code )->opac_display;
69 =head3 opac_editable
71 my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... });
72 if ( $attribute->is_opac_editable ) { ... }
74 =cut
76 sub opac_editable {
78 my $self = shift;
80 return Koha::Patron::Attribute::Types->find( $self->code )->opac_editable;
83 =head3 display_checkout
85 my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... });
86 if ( $attribute->display_checkout ) { ... }
88 =cut
90 sub display_checkout {
92 my $self = shift;
94 return $self->type->display_checkout;
97 =head3 value_description
99 my $description = $attribute->value_description
101 Return authorised value description or free text based on attribute type settings
103 =cut
105 sub value_description {
107 my $self = shift;
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,
114 return $av->next->lib if $av->count;
116 return $self->attribute;
119 =head3 type_description
121 my $description = $attribute->type_description
123 Return description of attribute type
125 =cut
127 sub type_description {
129 my $self = shift;
131 return $self->type->description;
134 =head3 type
136 my $attribute_type = $attribute->type;
138 Returns a C<Koha::Patron::Attribute::Type> object corresponding to the current patron attribute
140 =cut
142 sub type {
144 my $self = shift;
146 return Koha::Patron::Attribute::Types->find( $self->code );
149 =head2 Internal methods
151 =head3 _check_repeatable
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.
157 =cut
159 sub _check_repeatable {
161 my $self = shift;
163 if ( !$self->type->repeatable ) {
164 my $attr_count = Koha::Patron::Attributes->search(
165 { borrowernumber => $self->borrowernumber,
166 code => $self->code
168 )->count;
169 Koha::Exceptions::Patron::Attribute::NonRepeatable->throw()
170 if $attr_count > 0;
173 return $self;
176 =head3 _check_unique_id
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.
182 =cut
184 sub _check_unique_id {
186 my $self = shift;
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;
196 return $self;
199 =head3 _type
201 =cut
203 sub _type {
204 return 'BorrowerAttribute';