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>.
26 use Koha::AuthorisedValue;
27 use Koha::MarcSubfieldStructures;
28 use Koha::Cache::Memory::Lite;
30 use base qw(Koha::Objects Koha::Objects::Limit::Library);
34 Koha::AuthorisedValues - Koha Authorised value Object set class
42 sub search_by_marc_field {
43 my ( $self, $params ) = @_;
44 my $frameworkcode = $params->{frameworkcode} || '';
45 my $tagfield = $params->{tagfield};
46 my $tagsubfield = $params->{tagsubfield};
48 return unless $tagfield or $tagsubfield;
50 return $self->SUPER::search(
51 { 'marc_subfield_structures.frameworkcode' => $frameworkcode,
52 ( defined $tagfield ? ( 'marc_subfield_structures.tagfield' => $tagfield ) : () ),
53 ( defined $tagsubfield ? ( 'marc_subfield_structures.tagsubfield' => $tagsubfield ) : () ),
55 { join => { category => 'marc_subfield_structures' } }
59 sub search_by_koha_field {
60 my ( $self, $params ) = @_;
61 my $frameworkcode = $params->{frameworkcode} || '';
62 my $kohafield = $params->{kohafield};
63 my $category = $params->{category};
65 return unless $kohafield;
67 return $self->SUPER::search(
68 { 'marc_subfield_structures.frameworkcode' => $frameworkcode,
69 'marc_subfield_structures.kohafield' => $kohafield,
70 ( defined $category ? ( category_name => $category ) : () ),
72 { join => { category => 'marc_subfield_structures' },
78 sub find_by_koha_field {
79 my ( $self, $params ) = @_;
80 my $frameworkcode = $params->{frameworkcode} || '';
81 my $kohafield = $params->{kohafield};
82 my $authorised_value = $params->{authorised_value};
84 my $av = $self->SUPER::search(
85 { 'marc_subfield_structures.frameworkcode' => $frameworkcode,
86 'marc_subfield_structures.kohafield' => $kohafield,
87 'me.authorised_value' => $authorised_value,
89 { join => { category => 'marc_subfield_structures' },
93 return $av->count ? $av->next : undef;
96 sub get_description_by_koha_field {
97 my ( $self, $params ) = @_;
98 my $frameworkcode = $params->{frameworkcode} || '';
99 my $kohafield = $params->{kohafield};
100 my $authorised_value = $params->{authorised_value};
102 return {} unless defined $authorised_value;
104 my $memory_cache = Koha::Cache::Memory::Lite->get_instance;
105 my $cache_key = "AV_descriptions:$frameworkcode:$kohafield:$authorised_value";
106 my $cached = $memory_cache->get_from_cache($cache_key);
107 return $cached if $cached;
109 my $av = $self->find_by_koha_field($params);
110 return {} unless defined $av;
111 my $descriptions = { lib => $av->lib, opac_description => $av->opac_description };
112 $memory_cache->set_in_cache( $cache_key, $descriptions );
113 return $descriptions;
116 sub get_descriptions_by_koha_field {
117 my ( $self, $params ) = @_;
118 my $frameworkcode = $params->{frameworkcode} || '';
119 my $kohafield = $params->{kohafield};
121 my $memory_cache = Koha::Cache::Memory::Lite->get_instance;
122 my $cache_key = "AV_descriptions:$frameworkcode:$kohafield";
123 my $cached = $memory_cache->get_from_cache($cache_key);
124 return @$cached if $cached;
126 my @avs = $self->search_by_koha_field($params);
127 my @descriptions = map {
129 authorised_value => $_->authorised_value,
131 opac_description => $_->opac_description
134 $memory_cache->set_in_cache( $cache_key, \@descriptions );
135 return @descriptions;
140 my $rs = $self->_resultset->search(
143 select => ['category'],
145 order_by => 'category',
148 return map $_->get_column('category'), $rs->all;
156 return 'AuthorisedValue';
160 return 'Koha::AuthorisedValue';
165 Kyle M Hall <kyle@bywatersolutions.com>