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 ) : () ),
54 { join => { category => 'marc_subfield_structures' } }
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};
64 return unless $kohafield;
66 return $self->SUPER::search(
67 { 'marc_subfield_structures.frameworkcode' => $frameworkcode,
68 'marc_subfield_structures.kohafield' => $kohafield,
69 ( defined $category ? ( category_name => $category ) : () ),
71 { join => { category => 'marc_subfield_structures' },
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};
83 my $av = $self->SUPER::search(
84 { 'marc_subfield_structures.frameworkcode' => $frameworkcode,
85 'marc_subfield_structures.kohafield' => $kohafield,
86 'me.authorised_value' => $authorised_value,
88 { join => { category => 'marc_subfield_structures' },
92 return $av->count ? $av->next : undef;
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};
101 return {} unless defined $authorised_value;
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;
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;
115 sub get_descriptions_by_koha_field {
116 my ( $self, $params ) = @_;
117 my $frameworkcode = $params->{frameworkcode} || '';
118 my $kohafield = $params->{kohafield};
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;
125 my @avs = $self->search_by_koha_field($params);
126 my @descriptions = map {
128 authorised_value => $_->authorised_value,
130 opac_description => $_->opac_description
133 $memory_cache->set_in_cache( $cache_key, \@descriptions );
134 return @descriptions;
139 my $rs = $self->_resultset->search(
142 select => ['category'],
144 order_by => 'category',
147 return map $_->get_column('category'), $rs->all;
155 return 'AuthorisedValue';
159 return 'Koha::AuthorisedValue';
164 Kyle M Hall <kyle@bywatersolutions.com>