Bug 18692 - same with syspref TalkingTechItivaPhone
[koha.git] / Koha / Patron / Attribute.pm
blob906fc6d3c915e8f8180161c0cc5cf3fc23c60caf
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;
24 use base qw(Koha::Object);
26 =head1 NAME
28 Koha::Patron::Attribute - Koha Patron Attribute Object class
30 =head1 API
32 =head2 Class Methods
34 =cut
36 =head3 store
38 my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... });
39 try { $attribute->store }
40 catch { handle_exception };
42 =cut
44 sub store {
46 my $self = shift;
48 $self->_check_repeatable;
49 $self->_check_unique_id;
51 return $self->SUPER::store();
54 =head3 opac_display
56 my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... });
57 if ( $attribute->opac_display ) { ... }
59 =cut
61 sub opac_display {
63 my $self = shift;
65 return Koha::Patron::Attribute::Types->find( $self->code )->opac_display;
68 =head3 opac_editable
70 my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... });
71 if ( $attribute->is_opac_editable ) { ... }
73 =cut
75 sub opac_editable {
77 my $self = shift;
79 return Koha::Patron::Attribute::Types->find( $self->code )->opac_editable;
82 =head3 type
84 my $attribute_type = $attribute->type;
86 Returns a C<Koha::Patron::Attribute::Type> object corresponding to the current patron attribute
88 =cut
90 sub type {
92 my $self = shift;
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.
105 =cut
107 sub _check_repeatable {
109 my $self = shift;
111 if ( !$self->type->repeatable ) {
112 my $attr_count = Koha::Patron::Attributes->search(
113 { borrowernumber => $self->borrowernumber,
114 code => $self->code
116 )->count;
117 Koha::Exceptions::Patron::Attribute::NonRepeatable->throw()
118 if $attr_count > 0;
121 return $self;
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.
130 =cut
132 sub _check_unique_id {
134 my $self = shift;
136 if ( $self->type->unique_id ) {
137 my $unique_count = Koha::Patron::Attributes
138 ->search( { code => $self->code, attribute => $self->attribute } )
139 ->count;
140 Koha::Exceptions::Patron::Attribute::UniqueIDConstraint->throw()
141 if $unique_count > 0;
144 return $self;
147 =head3 _type
149 =cut
151 sub _type {
152 return 'BorrowerAttribute';