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