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
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.
21 use Koha::Exceptions::Patron::Attribute;
22 use Koha::Patron::Attribute::Types;
24 use base qw(Koha::Object);
28 Koha::Patron::Attribute - Koha Patron Attribute Object class
38 my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... });
39 try { $attribute->store }
40 catch { handle_exception };
48 $self->_check_repeatable;
49 $self->_check_unique_id;
51 return $self->SUPER::store();
56 my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... });
57 if ( $attribute->opac_display ) { ... }
65 return Koha::Patron::Attribute::Types->find( $self->code )->opac_display;
70 my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... });
71 if ( $attribute->is_opac_editable ) { ... }
79 return Koha::Patron::Attribute::Types->find( $self->code )->opac_editable;
84 my $attribute_type = $attribute->type;
86 Returns a C<Koha::Patron::Attribute::Type> object corresponding to the current patron attribute
94 return Koha::Patron::Attribute::Types->find( $self->code );
97 =head2 Internal methods
99 =head3 _check_repeatable
101 _check_repeatable checks if the attribute type is repeatable and throws and exception
102 if the attribute type isn't repeatable and there's already an attribute with the same
103 code for the given patron.
107 sub _check_repeatable {
111 if ( !$self->type->repeatable ) {
112 my $attr_count = Koha::Patron::Attributes->search(
113 { borrowernumber => $self->borrowernumber,
117 Koha::Exceptions::Patron::Attribute::NonRepeatable->throw()
124 =head3 _check_unique_id
126 _check_unique_id checks if the attribute type is marked as unique id and throws and exception
127 if the attribute type is a unique id and there's already an attribute with the same
128 code and value on the database.
132 sub _check_unique_id {
136 if ( $self->type->unique_id ) {
137 my $unique_count = Koha::Patron::Attributes
138 ->search( { code => $self->code, attribute => $self->attribute } )
140 Koha::Exceptions::Patron::Attribute::UniqueIDConstraint->throw()
141 if $unique_count > 0;
152 return 'BorrowerAttribute';