Bug 19234: (follow-up) Make Query plugin available to endpoints
[koha.git] / Koha / Patron / Attribute.pm
1 package Koha::Patron::Attribute;
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
13 #
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.
17
18 use Modern::Perl;
19
20 use Koha::Database;
21 use Koha::Exceptions::Patron::Attribute;
22 use Koha::Patron::Attribute::Types;
23
24 use base qw(Koha::Object);
25
26 =head1 NAME
27
28 Koha::Patron::Attribute - Koha Patron Attribute Object class
29
30 =head1 API
31
32 =head2 Class Methods
33
34 =cut
35
36 =head3 store
37
38     my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... });
39     try { $attribute->store }
40     catch { handle_exception };
41
42 =cut
43
44 sub store {
45
46     my $self = shift;
47
48     $self->_check_repeatable;
49     $self->_check_unique_id;
50
51     return $self->SUPER::store();
52 }
53
54 =head3 opac_display
55
56     my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... });
57     if ( $attribute->opac_display ) { ... }
58
59 =cut
60
61 sub opac_display {
62
63     my $self = shift;
64
65     return Koha::Patron::Attribute::Types->find( $self->code )->opac_display;
66 }
67
68 =head3 opac_editable
69
70     my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... });
71     if ( $attribute->is_opac_editable ) { ... }
72
73 =cut
74
75 sub opac_editable {
76
77     my $self = shift;
78
79     return Koha::Patron::Attribute::Types->find( $self->code )->opac_editable;
80 }
81
82 =head3 type
83
84     my $attribute_type = $attribute->type;
85
86 Returns a C<Koha::Patron::Attribute::Type> object corresponding to the current patron attribute
87
88 =cut
89
90 sub type {
91
92     my $self = shift;
93
94     return Koha::Patron::Attribute::Types->find( $self->code );
95 }
96
97 =head2 Internal methods
98
99 =head3 _check_repeatable
100
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.
104
105 =cut
106
107 sub _check_repeatable {
108
109     my $self = shift;
110
111     if ( !$self->type->repeatable ) {
112         my $attr_count = Koha::Patron::Attributes->search(
113             {   borrowernumber => $self->borrowernumber,
114                 code           => $self->code
115             }
116             )->count;
117         Koha::Exceptions::Patron::Attribute::NonRepeatable->throw()
118             if $attr_count > 0;
119     }
120
121     return $self;
122 }
123
124 =head3 _check_unique_id
125
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.
129
130 =cut
131
132 sub _check_unique_id {
133
134     my $self = shift;
135
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;
142     }
143
144     return $self;
145 }
146
147 =head3 _type
148
149 =cut
150
151 sub _type {
152     return 'BorrowerAttribute';
153 }
154
155 1;