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