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);
34 Koha::AuthorisedValues - Koha Authorised value Object set class
42 =head3 Koha::AuthorisedValues->search();
44 my @objects = Koha::AuthorisedValues->search($params);
49 my ( $self, $params, $attributes ) = @_;
51 my $branchcode = $params->{branchcode};
52 delete( $params->{branchcode} );
58 'authorised_values_branches.branchcode' => undef,
59 'authorised_values_branches.branchcode' => $branchcode,
63 my $join = $branchcode ? { join => 'authorised_values_branches' } : {};
65 $attributes = { %$attributes, %$join };
66 return $self->SUPER::search( { %$params, %$or, }, $attributes );
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};
75 return unless $tagfield or $tagsubfield;
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 ) : () ),
83 join => { category => 'marc_subfield_structures' },
84 order_by => [ 'category', 'lib', 'lib_opac' ],
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};
95 return unless $kohafield;
97 return $self->SUPER::search(
98 { 'marc_subfield_structures.frameworkcode' => $frameworkcode,
99 'marc_subfield_structures.kohafield' => $kohafield,
100 ( defined $category ? ( category_name => $category ) : () ),
102 { join => { category => 'marc_subfield_structures' },
104 order_by => [ 'category', 'lib', 'lib_opac' ],
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};
115 my $av = $self->SUPER::search(
116 { 'marc_subfield_structures.frameworkcode' => $frameworkcode,
117 'marc_subfield_structures.kohafield' => $kohafield,
118 'me.authorised_value' => $authorised_value,
120 { join => { category => 'marc_subfield_structures' },
124 return $av->count ? $av->next : undef;
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};
133 return {} unless defined $authorised_value;
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;
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;
147 sub get_descriptions_by_koha_field {
148 my ( $self, $params ) = @_;
149 my $frameworkcode = $params->{frameworkcode} || '';
150 my $kohafield = $params->{kohafield};
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;
157 my @avs = $self->search_by_koha_field($params);
158 my @descriptions = map {
160 authorised_value => $_->authorised_value,
162 opac_description => $_->opac_description
165 $memory_cache->set_in_cache( $cache_key, \@descriptions );
166 return @descriptions;
171 my $rs = $self->_resultset->search(
174 select => ['category'],
176 order_by => 'category',
179 return map $_->get_column('category'), $rs->all;
187 return 'AuthorisedValue';
191 return 'Koha::AuthorisedValue';
196 Kyle M Hall <kyle@bywatersolutions.com>