Bug 32336: (QA follow-up) Use $metadata->schema
[srvgit] / 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
25 use base qw(Koha::Object Koha::Object::Limit::Library);
26
27 my $cache = Koha::Caches->get_instance();
28
29 =head1 NAME
30
31 Koha::AuthorisedValue - Koha Authorised value Object class
32
33 =head1 API
34
35 =head2 Class methods
36
37 =cut
38
39 =head3 store
40
41 AuthorisedValue specific store to ensure relevant caches are flushed on change
42
43 =cut
44
45 sub store {
46     my ($self) = @_;
47
48     my $flush = 0;
49
50     if ( !$self->in_storage ) {
51         $flush = 1;
52     }
53     else {
54         my %updated_columns = $self->_result->get_dirty_columns;
55
56         if (   exists $updated_columns{lib}
57             or exists $updated_columns{lib_opac} )
58         {
59             $flush = 1;
60         }
61     }
62
63     $self = $self->SUPER::store;
64
65     if ($flush) {
66         my $key = "AV_descriptions:".$self->category;
67         $cache->clear_from_cache($key);
68     }
69
70     return $self;
71 }
72
73 =head2 delete
74
75 AuthorisedValue specific C<delete> to clear relevant caches on delete.
76
77 =cut
78
79 sub delete {
80     my $self = shift @_;
81     my $key = "AV_descriptions:".$self->category;
82     $cache->clear_from_cache($key);
83     $self->SUPER::delete(@_);
84 }
85
86 =head3 opac_description
87
88 my $description = $av->opac_description();
89
90 =cut
91
92 sub opac_description {
93     my ( $self, $value ) = @_;
94
95     return $self->lib_opac() || $self->lib();
96 }
97
98 =head3 to_api_mapping
99
100 This method returns the mapping for representing a Koha::AuthorisedValue object
101 on the API.
102
103 =cut
104
105 sub to_api_mapping {
106     return {
107         id               => 'authorised_value_id',
108         category         => 'category_name',
109         authorised_value => 'value',
110         lib              => 'description',
111         lib_opac         => 'opac_description',
112         imageurl         => 'image_url',
113     };
114 }
115
116 =head2 Internal methods
117
118 =head3 _type
119
120 =cut
121
122 sub _type {
123     return 'AuthorisedValue';
124 }
125
126 =head3 _library_limits
127
128 =cut
129
130 sub _library_limits {
131     return {
132         class   => 'AuthorisedValuesBranch',
133         id      => 'av_id',
134         library => 'branchcode'
135     };
136 }
137
138 =head1 AUTHOR
139
140 Kyle M Hall <kyle@bywatersolutions.com>
141
142 =cut
143
144 1;