1 package Koha::AuthorisedValue;
3 # Copyright ByWater Solutions 2014
5 # This file is part of Koha.
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.
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.
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>.
26 use Koha::Object::Limit::Library;
28 use base qw(Koha::Object Koha::Object::Limit::Library);
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
33 my $cache = Koha::Caches->get_instance();
37 Koha::AuthorisedValue - Koha Authorised value Object class
47 AuthorisedValue specific store to ensure relevant caches are flushed on change
56 if ( !$self->in_storage ) {
60 my %updated_columns = $self->_result->get_dirty_columns;
62 if ( exists $updated_columns{lib}
63 or exists $updated_columns{lib_opac} )
69 $self->_check_is_integer_only;
71 $self = $self->SUPER::store;
74 my $key = "AV_descriptions:".$self->category;
75 $cache->clear_from_cache($key);
83 AuthorisedValue specific C<delete> to clear relevant caches on delete.
89 my $key = "AV_descriptions:".$self->category;
90 $cache->clear_from_cache($key);
91 $self->SUPER::delete(@_);
94 =head3 opac_description
96 my $description = $av->opac_description();
100 sub opac_description {
101 my ( $self, $value ) = @_;
103 return $self->lib_opac() || $self->lib();
106 =head3 to_api_mapping
108 This method returns the mapping for representing a Koha::AuthorisedValue object
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',
124 =head3 is_integer_only
126 This helper method tells you if the category for this value allows numbers only.
130 sub is_integer_only {
132 return $self->_result->category->is_integer_only;
135 =head2 Internal methods
137 =head3 _check_is_integer_only
139 Raise an exception if the category only allows integer values and the value is not.
140 Otherwise returns true.
144 sub _check_is_integer_only {
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");
160 return 'AuthorisedValue';
163 =head3 _library_limits
167 sub _library_limits {
169 class => 'AuthorisedValuesBranch',
171 library => 'branchcode'
177 Kyle M Hall <kyle@bywatersolutions.com>