Bug 29407: Make the pickup locations dropdown JS reusable
[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     return {} unless defined $av;
114     my $descriptions = { lib => $av->lib, opac_description => $av->opac_description };
115     $memory_cache->set_in_cache( $cache_key, $descriptions );
116     return $descriptions;
117 }
118
119 sub get_descriptions_by_koha_field {
120     my ( $self, $params ) = @_;
121     my $frameworkcode = $params->{frameworkcode} || '';
122     my $kohafield = $params->{kohafield};
123
124     my $memory_cache = Koha::Cache::Memory::Lite->get_instance;
125     my $cache_key    = "AV_descriptions:$frameworkcode:$kohafield";
126     my $cached       = $memory_cache->get_from_cache($cache_key);
127     return @$cached if $cached;
128
129     my @avs          = $self->search_by_koha_field($params);
130     my @descriptions = map {
131         {
132             authorised_value => $_->authorised_value,
133             lib              => $_->lib,
134             opac_description => $_->opac_description
135         }
136     } @avs;
137     $memory_cache->set_in_cache( $cache_key, \@descriptions );
138     return @descriptions;
139 }
140
141 sub categories {
142     my ( $self ) = @_;
143     my $rs = $self->_resultset->search(
144         undef,
145         {
146             select => ['category'],
147             distinct => 1,
148             order_by => 'category',
149         },
150     );
151     return map $_->get_column('category'), $rs->all;
152 }
153
154 =head3 type
155
156 =cut
157
158 sub _type {
159     return 'AuthorisedValue';
160 }
161
162 sub object_class {
163     return 'Koha::AuthorisedValue';
164 }
165
166 =head1 AUTHOR
167
168 Kyle M Hall <kyle@bywatersolutions.com>
169
170 =cut
171
172 1;