Bug 27926: (QA follow-up) Switch to using data-order
[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         { join => { category => 'marc_subfield_structures' } }
56     );
57 }
58
59 sub search_by_koha_field {
60     my ( $self, $params ) = @_;
61     my $frameworkcode    = $params->{frameworkcode} || '';
62     my $kohafield        = $params->{kohafield};
63     my $category         = $params->{category};
64
65     return unless $kohafield;
66
67     return $self->SUPER::search(
68         {   'marc_subfield_structures.frameworkcode' => $frameworkcode,
69             'marc_subfield_structures.kohafield'     => $kohafield,
70             ( defined $category ? ( category_name    => $category )         : () ),
71         },
72         {   join     => { category => 'marc_subfield_structures' },
73             distinct => 1,
74         }
75     );
76 }
77
78 sub find_by_koha_field {
79     my ( $self, $params ) = @_;
80     my $frameworkcode    = $params->{frameworkcode} || '';
81     my $kohafield        = $params->{kohafield};
82     my $authorised_value = $params->{authorised_value};
83
84     my $av = $self->SUPER::search(
85         {   'marc_subfield_structures.frameworkcode' => $frameworkcode,
86             'marc_subfield_structures.kohafield'     => $kohafield,
87             'me.authorised_value'                    => $authorised_value,
88         },
89         {   join     => { category => 'marc_subfield_structures' },
90             distinct => 1,
91         }
92     );
93     return $av->count ? $av->next : undef;
94 }
95
96 sub get_description_by_koha_field {
97     my ( $self, $params ) = @_;
98     my $frameworkcode    = $params->{frameworkcode} || '';
99     my $kohafield        = $params->{kohafield};
100     my $authorised_value = $params->{authorised_value};
101
102     return {} unless defined $authorised_value;
103
104     my $memory_cache = Koha::Cache::Memory::Lite->get_instance;
105     my $cache_key    = "AV_descriptions:$frameworkcode:$kohafield:$authorised_value";
106     my $cached       = $memory_cache->get_from_cache($cache_key);
107     return $cached if $cached;
108
109     my $av = $self->find_by_koha_field($params);
110     return {} unless defined $av;
111     my $descriptions = { lib => $av->lib, opac_description => $av->opac_description };
112     $memory_cache->set_in_cache( $cache_key, $descriptions );
113     return $descriptions;
114 }
115
116 sub get_descriptions_by_koha_field {
117     my ( $self, $params ) = @_;
118     my $frameworkcode = $params->{frameworkcode} || '';
119     my $kohafield = $params->{kohafield};
120
121     my $memory_cache = Koha::Cache::Memory::Lite->get_instance;
122     my $cache_key    = "AV_descriptions:$frameworkcode:$kohafield";
123     my $cached       = $memory_cache->get_from_cache($cache_key);
124     return @$cached if $cached;
125
126     my @avs          = $self->search_by_koha_field($params);
127     my @descriptions = map {
128         {
129             authorised_value => $_->authorised_value,
130             lib              => $_->lib,
131             opac_description => $_->opac_description
132         }
133     } @avs;
134     $memory_cache->set_in_cache( $cache_key, \@descriptions );
135     return @descriptions;
136 }
137
138 sub categories {
139     my ( $self ) = @_;
140     my $rs = $self->_resultset->search(
141         undef,
142         {
143             select => ['category'],
144             distinct => 1,
145             order_by => 'category',
146         },
147     );
148     return map $_->get_column('category'), $rs->all;
149 }
150
151 =head3 type
152
153 =cut
154
155 sub _type {
156     return 'AuthorisedValue';
157 }
158
159 sub object_class {
160     return 'Koha::AuthorisedValue';
161 }
162
163 =head1 AUTHOR
164
165 Kyle M Hall <kyle@bywatersolutions.com>
166
167 =cut
168
169 1;