Bug 28554: In Koha::AuthorisedValues sort by description
[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);
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         {
83             join => { category => 'marc_subfield_structures' },
84             order_by => [ 'category', 'lib', 'lib_opac' ],
85         }
86     );
87 }
88
89 sub search_by_koha_field {
90     my ( $self, $params ) = @_;
91     my $frameworkcode    = $params->{frameworkcode} || '';
92     my $kohafield        = $params->{kohafield};
93     my $category         = $params->{category};
94
95     return unless $kohafield;
96
97     return $self->SUPER::search(
98         {   'marc_subfield_structures.frameworkcode' => $frameworkcode,
99             'marc_subfield_structures.kohafield'     => $kohafield,
100             ( defined $category ? ( category_name    => $category )         : () ),
101         },
102         {   join     => { category => 'marc_subfield_structures' },
103             distinct => 1,
104             order_by => [ 'category', 'lib', 'lib_opac' ],
105         }
106     );
107 }
108
109 sub find_by_koha_field {
110     my ( $self, $params ) = @_;
111     my $frameworkcode    = $params->{frameworkcode} || '';
112     my $kohafield        = $params->{kohafield};
113     my $authorised_value = $params->{authorised_value};
114
115     my $av = $self->SUPER::search(
116         {   'marc_subfield_structures.frameworkcode' => $frameworkcode,
117             'marc_subfield_structures.kohafield'     => $kohafield,
118             'me.authorised_value'                    => $authorised_value,
119         },
120         {   join     => { category => 'marc_subfield_structures' },
121             distinct => 1,
122         }
123     );
124     return $av->count ? $av->next : undef;
125 }
126
127 sub get_description_by_koha_field {
128     my ( $self, $params ) = @_;
129     my $frameworkcode    = $params->{frameworkcode} || '';
130     my $kohafield        = $params->{kohafield};
131     my $authorised_value = $params->{authorised_value};
132
133     return {} unless defined $authorised_value;
134
135     my $memory_cache = Koha::Cache::Memory::Lite->get_instance;
136     my $cache_key    = "AV_descriptions:$frameworkcode:$kohafield:$authorised_value";
137     my $cached       = $memory_cache->get_from_cache($cache_key);
138     return $cached if $cached;
139
140     my $av = $self->find_by_koha_field($params);
141     return {} unless defined $av;
142     my $descriptions = { lib => $av->lib, opac_description => $av->opac_description };
143     $memory_cache->set_in_cache( $cache_key, $descriptions );
144     return $descriptions;
145 }
146
147 sub get_descriptions_by_koha_field {
148     my ( $self, $params ) = @_;
149     my $frameworkcode = $params->{frameworkcode} || '';
150     my $kohafield = $params->{kohafield};
151
152     my $memory_cache = Koha::Cache::Memory::Lite->get_instance;
153     my $cache_key    = "AV_descriptions:$frameworkcode:$kohafield";
154     my $cached       = $memory_cache->get_from_cache($cache_key);
155     return @$cached if $cached;
156
157     my @avs          = $self->search_by_koha_field($params);
158     my @descriptions = map {
159         {
160             authorised_value => $_->authorised_value,
161             lib              => $_->lib,
162             opac_description => $_->opac_description
163         }
164     } @avs;
165     $memory_cache->set_in_cache( $cache_key, \@descriptions );
166     return @descriptions;
167 }
168
169 sub categories {
170     my ( $self ) = @_;
171     my $rs = $self->_resultset->search(
172         undef,
173         {
174             select => ['category'],
175             distinct => 1,
176             order_by => 'category',
177         },
178     );
179     return map $_->get_column('category'), $rs->all;
180 }
181
182 =head3 type
183
184 =cut
185
186 sub _type {
187     return 'AuthorisedValue';
188 }
189
190 sub object_class {
191     return 'Koha::AuthorisedValue';
192 }
193
194 =head1 AUTHOR
195
196 Kyle M Hall <kyle@bywatersolutions.com>
197
198 =cut
199
200 1;