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
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use Koha
::Exceptions
::Patron
::Attribute
;
22 use Koha
::Patron
::Attribute
::Types
;
23 use Koha
::AuthorisedValues
;
25 use base
qw(Koha::Object);
29 Koha::Patron::Attribute - Koha Patron Attribute Object class
39 my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... });
40 try { $attribute->store }
41 catch { handle_exception };
49 $self->_check_repeatable;
50 $self->_check_unique_id;
52 return $self->SUPER::store
();
57 my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... });
58 if ( $attribute->opac_display ) { ... }
66 return Koha
::Patron
::Attribute
::Types
->find( $self->code )->opac_display;
71 my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... });
72 if ( $attribute->is_opac_editable ) { ... }
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 ) { ... }
90 sub display_checkout
{
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
105 sub value_description
{
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
127 sub type_description
{
131 return $self->type->description;
136 my $attribute_type = $attribute->type;
138 Returns a C<Koha::Patron::Attribute::Type> object corresponding to the current patron attribute
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.
159 sub _check_repeatable
{
163 if ( !$self->type->repeatable ) {
164 my $attr_count = Koha
::Patron
::Attributes
->search(
165 { borrowernumber
=> $self->borrowernumber,
169 Koha
::Exceptions
::Patron
::Attribute
::NonRepeatable
->throw()
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.
184 sub _check_unique_id
{
188 if ( $self->type->unique_id ) {
189 my $unique_count = Koha
::Patron
::Attributes
190 ->search( { code
=> $self->code, attribute
=> $self->attribute } )
192 Koha
::Exceptions
::Patron
::Attribute
::UniqueIDConstraint
->throw()
193 if $unique_count > 0;
204 return 'BorrowerAttribute';