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