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 ) : () ),
56 join => { category => 'marc_subfield_structures' },
57 order_by => [ 'category', 'lib', 'lib_opac' ],
62 sub search_by_koha_field {
63 my ( $self, $params ) = @_;
64 my $frameworkcode = $params->{frameworkcode} || '';
65 my $kohafield = $params->{kohafield};
66 my $category = $params->{category};
68 return unless $kohafield;
70 return $self->SUPER::search(
71 { 'marc_subfield_structures.frameworkcode' => $frameworkcode,
72 'marc_subfield_structures.kohafield' => $kohafield,
73 ( defined $category ? ( category_name => $category ) : () ),
75 { join => { category => 'marc_subfield_structures' },
77 order_by => [ 'category', 'lib', 'lib_opac' ],
82 sub find_by_koha_field {
83 my ( $self, $params ) = @_;
84 my $frameworkcode = $params->{frameworkcode} || '';
85 my $kohafield = $params->{kohafield};
86 my $authorised_value = $params->{authorised_value};
88 my $av = $self->SUPER::search(
89 { 'marc_subfield_structures.frameworkcode' => $frameworkcode,
90 'marc_subfield_structures.kohafield' => $kohafield,
91 'me.authorised_value' => $authorised_value,
93 { join => { category => 'marc_subfield_structures' },
97 return $av->count ? $av->next : undef;
100 sub get_description_by_koha_field {
101 my ( $self, $params ) = @_;
102 my $frameworkcode = $params->{frameworkcode} || '';
103 my $kohafield = $params->{kohafield};
104 my $authorised_value = $params->{authorised_value};
106 return {} unless defined $authorised_value;
108 my $memory_cache = Koha::Cache::Memory::Lite->get_instance;
109 my $cache_key = "AV_descriptions:$frameworkcode:$kohafield:$authorised_value";
110 my $cached = $memory_cache->get_from_cache($cache_key);
111 return $cached if $cached;
113 my $av = $self->find_by_koha_field($params);
114 return {} unless defined $av;
115 my $descriptions = { lib => $av->lib, opac_description => $av->opac_description };
116 $memory_cache->set_in_cache( $cache_key, $descriptions );
117 return $descriptions;
120 sub get_descriptions_by_koha_field {
121 my ( $self, $params ) = @_;
122 my $frameworkcode = $params->{frameworkcode} || '';
123 my $kohafield = $params->{kohafield};
125 my $memory_cache = Koha::Cache::Memory::Lite->get_instance;
126 my $cache_key = "AV_descriptions:$frameworkcode:$kohafield";
127 my $cached = $memory_cache->get_from_cache($cache_key);
128 return @$cached if $cached;
130 my @avs = $self->search_by_koha_field($params);
131 my @descriptions = map {
133 authorised_value => $_->authorised_value,
135 opac_description => $_->opac_description
138 $memory_cache->set_in_cache( $cache_key, \@descriptions );
139 return @descriptions;
144 my $rs = $self->_resultset->search(
147 select => ['category'],
149 order_by => 'category',
152 return map $_->get_column('category'), $rs->all;
160 return 'AuthorisedValue';
164 return 'Koha::AuthorisedValue';
169 Kyle M Hall <kyle@bywatersolutions.com>