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;
82 =head2 Internal methods
84 =head3 _check_repeatable
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.
92 sub _check_repeatable {
96 if ( !Koha::Patron::Attribute::Types->find( $self->code )->repeatable ) {
98 = Koha::Database->new->schema->resultset( $self->_type )->search(
99 { borrowernumber => $self->borrowernumber,
103 Koha::Exceptions::Patron::Attribute::NonRepeatable->throw()
110 =head3 _check_unique_id
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.
118 sub _check_unique_id {
122 if ( Koha::Patron::Attribute::Types->find( $self->code )->unique_id ) {
124 = Koha::Database->new->schema->resultset( $self->_type )
125 ->search( { code => $self->code, attribute => $self->attribute } )
127 Koha::Exceptions::Patron::Attribute::UniqueIDConstraint->throw()
128 if $unique_count > 0;
137 return 'BorrowerAttribute';