Bug 28031: (follow-up) Clarify check methods
[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
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Koha::Database;
21 use Koha::Exceptions::Patron::Attribute;
22 use Koha::Patron::Attribute::Types;
23 use Koha::AuthorisedValues;
24
25 use base qw(Koha::Object);
26
27 =head1 NAME
28
29 Koha::Patron::Attribute - Koha Patron Attribute Object class
30
31 =head1 API
32
33 =head2 Class Methods
34
35 =cut
36
37 =head3 store
38
39     my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... });
40     try { $attribute->store }
41     catch { handle_exception };
42
43 =cut
44
45 sub store {
46
47     my $self = shift;
48
49     my $type = $self->type;
50
51     Koha::Exceptions::Patron::Attribute::InvalidType->throw( type => $self->code )
52         unless $type;
53
54     Koha::Exceptions::Patron::Attribute::NonRepeatable->throw( attribute => $self )
55         unless $self->repeatable_ok($type);
56
57     Koha::Exceptions::Patron::Attribute::UniqueIDConstraint->throw( attribute => $self )
58         unless $self->unique_ok($type);
59
60     return $self->SUPER::store();
61 }
62
63 =head3 type
64
65     my $attribute_type = $attribute->type;
66
67 Returns a C<Koha::Patron::Attribute::Type> object corresponding to the current patron attribute
68
69 =cut
70
71 sub type {
72
73     my $self = shift;
74
75     return scalar Koha::Patron::Attribute::Types->find( $self->code );
76 }
77
78 =head3 authorised_value
79
80 my $authorised_value = $attribute->authorised_value;
81
82 Return the Koha::AuthorisedValue object of this attribute when one is attached.
83
84 Return undef if this attribute is not attached to an authorised value
85
86 =cut
87
88 sub authorised_value {
89     my ($self) = @_;
90
91     return unless $self->type->authorised_value_category;
92
93     my $av = Koha::AuthorisedValues->search(
94         {
95             category         => $self->type->authorised_value_category,
96             authorised_value => $self->attribute,
97         }
98     );
99     return unless $av->count; # Data inconsistency
100     return $av->next;
101 }
102
103 =head3 description
104
105 my $description = $patron_attribute->description;
106
107 Return the value of this attribute or the description of the authorised value (when attached).
108
109 This method must be called when the authorised value's description must be
110 displayed instead of the code.
111
112 =cut
113
114 sub description {
115     my ( $self) = @_;
116     if ( $self->type->authorised_value_category ) {
117         my $av = $self->authorised_value;
118         return $av ? $av->lib : "";
119     }
120     return $self->attribute;
121 }
122
123 =head3 to_api_mapping
124
125 This method returns the mapping for representing a Koha::Patron::Attribute object
126 on the API.
127
128 =cut
129
130 sub to_api_mapping {
131     return {
132         id             => 'extended_attribute_id',
133         attribute      => 'value',
134         borrowernumber => undef,
135         code           => 'type'
136     };
137 }
138
139 =head3 repeatable_ok
140
141 Checks if the attribute type is repeatable and returns a boolean representing
142 whether storing the current object state would break the repeatable constraint.
143
144 =cut
145
146 sub repeatable_ok {
147
148     my ( $self, $type ) = @_;
149
150     my $ok = 1;
151     if ( !$type->repeatable ) {
152         my $params = {
153             borrowernumber => $self->borrowernumber,
154             code           => $self->code
155         };
156
157         $params->{id} = { '!=' => $self->id }
158             if $self->in_storage;
159
160         $ok = 0 if Koha::Patron::Attributes->search($params)->count > 0;
161     }
162
163     return $ok;
164 }
165
166 =head3 unique_ok
167
168 Checks if the attribute type is marked as unique and returns a boolean representing
169 whether storing the current object state would break the unique constraint.
170
171 =cut
172
173 sub unique_ok {
174
175     my ( $self, $type ) = @_;
176
177     my $ok = 1;
178     if ( $type->unique_id ) {
179         my $params = { code => $self->code, attribute => $self->attribute };
180
181         $params->{borrowernumber} = { '!=' => $self->borrowernumber } if $self->borrowernumber;
182         $params->{id}             = { '!=' => $self->id }             if $self->in_storage;
183
184         my $unique_count = Koha::Patron::Attributes
185             ->search( $params )
186             ->count;
187
188         $ok = 0 if $unique_count > 0;
189     }
190
191     return $ok;
192 }
193
194 =head2 Internal methods
195
196 =head3 _type
197
198 =cut
199
200 sub _type {
201     return 'BorrowerAttribute';
202 }
203
204 1;