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