Bug 35197: (QA follow-up): Update tablename
[koha.git] / Koha / AuthorisedValue.pm
1 package Koha::AuthorisedValue;
2
3 # Copyright ByWater Solutions 2014
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Koha::Caches;
23 use Koha::Database;
24 use Koha::Exceptions;
25 use Koha::Object;
26 use Koha::Object::Limit::Library;
27
28 use base qw(Koha::Object Koha::Object::Limit::Library);
29
30 use constant NUM_PATTERN    => q{^(-[1-9][0-9]*|0|[1-9][0-9]*)$};
31 use constant NUM_PATTERN_JS => q{(-[1-9][0-9]*|0|[1-9][0-9]*)};     # ^ and $ removed
32
33 my $cache = Koha::Caches->get_instance();
34
35 =head1 NAME
36
37 Koha::AuthorisedValue - Koha Authorised value Object class
38
39 =head1 API
40
41 =head2 Class methods
42
43 =cut
44
45 =head3 store
46
47 AuthorisedValue specific store to ensure relevant caches are flushed on change
48
49 =cut
50
51 sub store {
52     my ($self) = @_;
53
54     my $flush = 0;
55
56     if ( !$self->in_storage ) {
57         $flush = 1;
58     }
59     else {
60         my %updated_columns = $self->_result->get_dirty_columns;
61
62         if (   exists $updated_columns{lib}
63             or exists $updated_columns{lib_opac} )
64         {
65             $flush = 1;
66         }
67     }
68
69     $self->_check_is_integer_only;
70
71     $self = $self->SUPER::store;
72
73     if ($flush) {
74         my $key = "AV_descriptions:".$self->category;
75         $cache->clear_from_cache($key);
76     }
77
78     return $self;
79 }
80
81 =head2 delete
82
83 AuthorisedValue specific C<delete> to clear relevant caches on delete.
84
85 =cut
86
87 sub delete {
88     my $self = shift @_;
89     my $key = "AV_descriptions:".$self->category;
90     $cache->clear_from_cache($key);
91     $self->SUPER::delete(@_);
92 }
93
94 =head3 opac_description
95
96 my $description = $av->opac_description();
97
98 =cut
99
100 sub opac_description {
101     my ( $self, $value ) = @_;
102
103     return $self->lib_opac() || $self->lib();
104 }
105
106 =head3 to_api_mapping
107
108 This method returns the mapping for representing a Koha::AuthorisedValue object
109 on the API.
110
111 =cut
112
113 sub to_api_mapping {
114     return {
115         id               => 'authorised_value_id',
116         category         => 'category_name',
117         authorised_value => 'value',
118         lib              => 'description',
119         lib_opac         => 'opac_description',
120         imageurl         => 'image_url',
121     };
122 }
123
124 =head3 is_integer_only
125
126 This helper method tells you if the category for this value allows numbers only.
127
128 =cut
129
130 sub is_integer_only {
131     my $self = shift;
132     return $self->_result->category->is_integer_only;
133 }
134
135 =head2 Internal methods
136
137 =head3 _check_is_integer_only
138
139     Raise an exception if the category only allows integer values and the value is not.
140     Otherwise returns true.
141
142 =cut
143
144 sub _check_is_integer_only {
145     my ($self)  = @_;
146     my $pattern = NUM_PATTERN;
147     my $value   = $self->authorised_value // q{};
148     return 1 if $value =~ qr/${pattern}/;    # no need to check category here yet
149     if ( $self->is_integer_only ) {
150         Koha::Exceptions::NoInteger->throw("'$value' is no integer value");
151     }
152     return 1;
153 }
154
155 =head3 _type
156
157 =cut
158
159 sub _type {
160     return 'AuthorisedValue';
161 }
162
163 =head3 _library_limits
164
165 =cut
166
167 sub _library_limits {
168     return {
169         class   => 'AuthorisedValuesBranch',
170         id      => 'av_id',
171         library => 'branchcode'
172     };
173 }
174
175 =head1 AUTHOR
176
177 Kyle M Hall <kyle@bywatersolutions.com>
178
179 =cut
180
181 1;