Bug 35961: Add missing includes
[koha.git] / Koha / AuthorisedValues.pm
1 package Koha::AuthorisedValues;
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
23 use Koha::Database;
24
25 use Koha::AuthorisedValue;
26 use Koha::MarcSubfieldStructures;
27 use Koha::Cache::Memory::Lite;
28
29 use base qw(Koha::Objects Koha::Objects::Limit::Library);
30
31 =head1 NAME
32
33 Koha::AuthorisedValues - Koha Authorised value Object set class
34
35 =head1 API
36
37 =head2 Class Methods
38
39 =cut
40
41 sub search_by_marc_field {
42     my ( $self, $params ) = @_;
43     my $frameworkcode = $params->{frameworkcode} || '';
44     my $tagfield      = $params->{tagfield};
45     my $tagsubfield   = $params->{tagsubfield};
46
47     return unless $tagfield or $tagsubfield;
48
49     return $self->SUPER::search(
50         {   'marc_subfield_structures.frameworkcode' => $frameworkcode,
51             ( defined $tagfield    ? ( 'marc_subfield_structures.tagfield'    => $tagfield )    : () ),
52             ( defined $tagsubfield ? ( 'marc_subfield_structures.tagsubfield' => $tagsubfield ) : () ),
53         },
54         {
55             join => { category => 'marc_subfield_structures' },
56             order_by => [ 'category', 'lib', 'lib_opac' ],
57         }
58     );
59 }
60
61 sub search_by_koha_field {
62     my ( $self, $params ) = @_;
63     my $frameworkcode    = $params->{frameworkcode} || '';
64     my $kohafield        = $params->{kohafield};
65     my $category         = $params->{category};
66
67     return unless $kohafield;
68
69     return $self->SUPER::search(
70         {   'marc_subfield_structures.frameworkcode' => $frameworkcode,
71             'marc_subfield_structures.kohafield'     => $kohafield,
72             ( defined $category ? ( category_name    => $category )         : () ),
73         },
74         {   join     => { category => 'marc_subfield_structures' },
75             distinct => 1,
76             order_by => [ 'category', 'lib', 'lib_opac' ],
77         }
78     );
79 }
80
81 sub find_by_koha_field {
82     my ( $self, $params ) = @_;
83     my $frameworkcode    = $params->{frameworkcode} || '';
84     my $kohafield        = $params->{kohafield};
85     my $authorised_value = $params->{authorised_value};
86
87     my $av = $self->SUPER::search(
88         {   'marc_subfield_structures.frameworkcode' => $frameworkcode,
89             'marc_subfield_structures.kohafield'     => $kohafield,
90             'me.authorised_value'                    => $authorised_value,
91         },
92         {   join     => { category => 'marc_subfield_structures' },
93             distinct => 1,
94         }
95     );
96     return $av->count ? $av->next : undef;
97 }
98
99 sub get_description_by_koha_field {
100     my ( $self, $params ) = @_;
101     my $frameworkcode    = $params->{frameworkcode} || '';
102     my $kohafield        = $params->{kohafield};
103     my $authorised_value = $params->{authorised_value};
104
105     return {} unless defined $authorised_value;
106
107     my $memory_cache = Koha::Cache::Memory::Lite->get_instance;
108     my $cache_key    = "AV_descriptions:$frameworkcode:$kohafield:$authorised_value";
109     my $cached       = $memory_cache->get_from_cache($cache_key);
110     return $cached if $cached;
111
112     my $av = $self->find_by_koha_field($params);
113     if ( ! defined $av ){
114         $memory_cache->set_in_cache( $cache_key, {} );
115         return {};
116     }
117     my $descriptions = { lib => $av->lib, opac_description => $av->opac_description };
118     $memory_cache->set_in_cache( $cache_key, $descriptions );
119     return $descriptions;
120 }
121
122 sub get_descriptions_by_koha_field {
123     my ( $self, $params ) = @_;
124     my $frameworkcode = $params->{frameworkcode} || '';
125     my $kohafield = $params->{kohafield};
126
127     my $memory_cache = Koha::Cache::Memory::Lite->get_instance;
128     my $cache_key    = "AV_descriptions:$frameworkcode:$kohafield";
129     my $cached       = $memory_cache->get_from_cache($cache_key);
130     return @$cached if $cached;
131
132     my @avs          = $self->search_by_koha_field($params)->as_list;
133     my @descriptions = map {
134         {
135             authorised_value => $_->authorised_value,
136             lib              => $_->lib,
137             opac_description => $_->opac_description
138         }
139     } @avs;
140     $memory_cache->set_in_cache( $cache_key, \@descriptions );
141     return @descriptions;
142 }
143
144 =head3 get_descriptions_by_marc_field
145
146     Return cached descriptions when looking up by MARC field/subfield
147
148 =cut
149
150 sub get_descriptions_by_marc_field {
151     my ( $self, $params ) = @_;
152     my $frameworkcode = $params->{frameworkcode} || '';
153     my $tagfield      = $params->{tagfield};
154     my $tagsubfield   = $params->{tagsubfield};
155
156     return {} unless defined $params->{tagfield};
157
158     my $memory_cache = Koha::Cache::Memory::Lite->get_instance;
159     my $cache_key    = "AV_descriptions_by_MARC:$frameworkcode:$tagfield";
160     if ($tagsubfield) {
161         $cache_key .= ":$tagsubfield";
162     }
163
164     my $cached = $memory_cache->get_from_cache($cache_key);
165     return $cached if $cached;
166
167     my $descriptions = {};
168     my @avs          = $self->search_by_marc_field($params)->as_list;
169     foreach my $av (@avs) {
170         $descriptions->{ $av->authorised_value } = $av->lib;
171     }
172     $memory_cache->set_in_cache( $cache_key, $descriptions );
173     return $descriptions;
174 }
175
176 sub categories {
177     my ( $self ) = @_;
178     my $rs = $self->_resultset->search(
179         undef,
180         {
181             select => ['category'],
182             distinct => 1,
183             order_by => 'category',
184         },
185     );
186     return map $_->get_column('category'), $rs->all;
187 }
188
189 =head3 type
190
191 =cut
192
193 sub _type {
194     return 'AuthorisedValue';
195 }
196
197 sub object_class {
198     return 'Koha::AuthorisedValue';
199 }
200
201 =head1 AUTHOR
202
203 Kyle M Hall <kyle@bywatersolutions.com>
204
205 =cut
206
207 1;