Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[koha-equinox.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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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);
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 =head3 Koha::AuthorisedValues->search();
43
44 my @objects = Koha::AuthorisedValues->search($params);
45
46 =cut
47
48 sub search {
49     my ( $self, $params, $attributes ) = @_;
50
51     my $branchcode = $params->{branchcode};
52     delete( $params->{branchcode} );
53
54     my $or =
55       $branchcode
56       ? {
57         '-or' => [
58             'authorised_values_branches.branchcode' => undef,
59             'authorised_values_branches.branchcode' => $branchcode,
60         ]
61       }
62       : {};
63     my $join = $branchcode ? { join => 'authorised_values_branches' } : {};
64     $attributes //= {};
65     $attributes = { %$attributes, %$join };
66     return $self->SUPER::search( { %$params, %$or, }, $attributes );
67 }
68
69 sub search_by_marc_field {
70     my ( $self, $params ) = @_;
71     my $frameworkcode = $params->{frameworkcode} || '';
72     my $tagfield      = $params->{tagfield};
73     my $tagsubfield   = $params->{tagsubfield};
74
75     return unless $tagfield or $tagsubfield;
76
77     return $self->SUPER::search(
78         {   'marc_subfield_structures.frameworkcode' => $frameworkcode,
79             ( defined $tagfield    ? ( 'marc_subfield_structures.tagfield'    => $tagfield )    : () ),
80             ( defined $tagsubfield ? ( 'marc_subfield_structures.tagsubfield' => $tagsubfield ) : () ),
81         },
82         { join => { category => 'marc_subfield_structures' } }
83     );
84 }
85
86 sub search_by_koha_field {
87     my ( $self, $params ) = @_;
88     my $frameworkcode    = $params->{frameworkcode} || '';
89     my $kohafield        = $params->{kohafield};
90     my $category         = $params->{category};
91
92     return unless $kohafield;
93
94     return $self->SUPER::search(
95         {   'marc_subfield_structures.frameworkcode' => $frameworkcode,
96             'marc_subfield_structures.kohafield'     => $kohafield,
97             ( defined $category ? ( category_name    => $category )         : () ),
98         },
99         {   join     => { category => 'marc_subfield_structures' },
100             distinct => 1,
101         }
102     );
103 }
104
105 sub find_by_koha_field {
106     my ( $self, $params ) = @_;
107     my $frameworkcode    = $params->{frameworkcode} || '';
108     my $kohafield        = $params->{kohafield};
109     my $authorised_value = $params->{authorised_value};
110
111     my $av = $self->SUPER::search(
112         {   'marc_subfield_structures.frameworkcode' => $frameworkcode,
113             'marc_subfield_structures.kohafield'     => $kohafield,
114             'me.authorised_value'                    => $authorised_value,
115         },
116         {   join     => { category => 'marc_subfield_structures' },
117             distinct => 1,
118         }
119     );
120     return $av->count ? $av->next : undef;
121 }
122
123 sub get_description_by_koha_field {
124     my ( $self, $params ) = @_;
125     my $frameworkcode    = $params->{frameworkcode} || '';
126     my $kohafield        = $params->{kohafield};
127     my $authorised_value = $params->{authorised_value};
128
129     return {} unless defined $authorised_value;
130
131     my $memory_cache = Koha::Cache::Memory::Lite->get_instance;
132     my $cache_key    = "AV_descriptions:$frameworkcode:$kohafield:$authorised_value";
133     my $cached       = $memory_cache->get_from_cache($cache_key);
134     return $cached if $cached;
135
136     my $av = $self->find_by_koha_field($params);
137     return {} unless defined $av;
138     my $descriptions = { lib => $av->lib, opac_description => $av->opac_description };
139     $memory_cache->set_in_cache( $cache_key, $descriptions );
140     return $descriptions;
141 }
142
143 sub get_descriptions_by_koha_field {
144     my ( $self, $params ) = @_;
145     my $frameworkcode = $params->{frameworkcode} || '';
146     my $kohafield = $params->{kohafield};
147
148     my $memory_cache = Koha::Cache::Memory::Lite->get_instance;
149     my $cache_key    = "AV_descriptions:$frameworkcode:$kohafield";
150     my $cached       = $memory_cache->get_from_cache($cache_key);
151     return @$cached if $cached;
152
153     my @avs          = $self->search_by_koha_field($params);
154     my @descriptions = map {
155         {
156             authorised_value => $_->authorised_value,
157             lib              => $_->lib,
158             opac_description => $_->opac_description
159         }
160     } @avs;
161     $memory_cache->set_in_cache( $cache_key, \@descriptions );
162     return @descriptions;
163 }
164
165 sub categories {
166     my ( $self ) = @_;
167     my $rs = $self->_resultset->search(
168         undef,
169         {
170             select => ['category'],
171             distinct => 1,
172             order_by => 'category',
173         },
174     );
175     return map $_->get_column('category'), $rs->all;
176 }
177
178 =head3 type
179
180 =cut
181
182 sub _type {
183     return 'AuthorisedValue';
184 }
185
186 sub object_class {
187     return 'Koha::AuthorisedValue';
188 }
189
190 =head1 AUTHOR
191
192 Kyle M Hall <kyle@bywatersolutions.com>
193
194 =cut
195
196 1;