1 package Koha::AuthorisedValues;
3 # Copyright ByWater Solutions 2014
5 # This file is part of Koha.
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.
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.
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>.
25 use Koha::AuthorisedValue;
26 use Koha::MarcSubfieldStructures;
27 use Koha::Cache::Memory::Lite;
29 use base qw(Koha::Objects Koha::Objects::Limit::Library);
33 Koha::AuthorisedValues - Koha Authorised value Object set class
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};
47 return unless $tagfield or $tagsubfield;
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 ) : () ),
55 join => { category => 'marc_subfield_structures' },
56 order_by => [ 'category', 'lib', 'lib_opac' ],
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};
67 return unless $kohafield;
69 return $self->SUPER::search(
70 { 'marc_subfield_structures.frameworkcode' => $frameworkcode,
71 'marc_subfield_structures.kohafield' => $kohafield,
72 ( defined $category ? ( category_name => $category ) : () ),
74 { join => { category => 'marc_subfield_structures' },
76 order_by => [ 'category', 'lib', 'lib_opac' ],
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};
87 my $av = $self->SUPER::search(
88 { 'marc_subfield_structures.frameworkcode' => $frameworkcode,
89 'marc_subfield_structures.kohafield' => $kohafield,
90 'me.authorised_value' => $authorised_value,
92 { join => { category => 'marc_subfield_structures' },
96 return $av->count ? $av->next : undef;
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};
105 return {} unless defined $authorised_value;
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;
112 my $av = $self->find_by_koha_field($params);
113 if ( ! defined $av ){
114 $memory_cache->set_in_cache( $cache_key, {} );
117 my $descriptions = { lib => $av->lib, opac_description => $av->opac_description };
118 $memory_cache->set_in_cache( $cache_key, $descriptions );
119 return $descriptions;
122 sub get_descriptions_by_koha_field {
123 my ( $self, $params ) = @_;
124 my $frameworkcode = $params->{frameworkcode} || '';
125 my $kohafield = $params->{kohafield};
127 my $memory_cache = Koha::Cache::Memory::Lite->get_instance;
128 my $cache_key = "AV_descriptions:$frameworkcode:$kohafield";
129 my $cached = $memory_cache->get_from_cache($cache_key);
130 return @$cached if $cached;
132 my @avs = $self->search_by_koha_field($params)->as_list;
133 my @descriptions = map {
135 authorised_value => $_->authorised_value,
137 opac_description => $_->opac_description
140 $memory_cache->set_in_cache( $cache_key, \@descriptions );
141 return @descriptions;
144 =head3 get_descriptions_by_marc_field
146 Return cached descriptions when looking up by MARC field/subfield
150 sub get_descriptions_by_marc_field {
151 my ( $self, $params ) = @_;
152 my $frameworkcode = $params->{frameworkcode} || '';
153 my $tagfield = $params->{tagfield};
154 my $tagsubfield = $params->{tagsubfield};
156 return {} unless defined $params->{tagfield};
158 my $memory_cache = Koha::Cache::Memory::Lite->get_instance;
159 my $cache_key = "AV_descriptions_by_MARC:$frameworkcode:$tagfield";
161 $cache_key .= ":$tagsubfield";
164 my $cached = $memory_cache->get_from_cache($cache_key);
165 return $cached if $cached;
167 my $descriptions = {};
168 my @avs = $self->search_by_marc_field($params)->as_list;
169 foreach my $av (@avs) {
170 $descriptions->{ $av->authorised_value } = $av->lib;
172 $memory_cache->set_in_cache( $cache_key, $descriptions );
173 return $descriptions;
178 my $rs = $self->_resultset->search(
181 select => ['category'],
183 order_by => 'category',
186 return map $_->get_column('category'), $rs->all;
194 return 'AuthorisedValue';
198 return 'Koha::AuthorisedValue';
203 Kyle M Hall <kyle@bywatersolutions.com>