Bug 14637: Fix add patron category under MySQL 5.7
[koha.git] / Koha / AuthorisedValue.pm
blob910b3da9a95ddbc7f813dc104670fa8a48142609
1 package Koha::AuthorisedValue;
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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use Modern::Perl;
22 use Carp;
24 use Koha::Database;
26 use base qw(Koha::Object);
28 =head1 NAME
30 Koha::AuthorisedValue - Koha Authorised value Object class
32 =head1 API
34 =head2 Class Methods
36 =cut
38 =head3 branch_limitations
40 my $limitations = $av->branch_limitations();
42 $av->branch_limitations( \@branchcodes );
44 =cut
46 sub branch_limitations {
47 my ( $self, $branchcodes ) = @_;
49 if ($branchcodes) {
50 return $self->replace_branch_limitations($branchcodes);
52 else {
53 return $self->get_branch_limitations();
58 =head3 get_branch_limitations
60 my $limitations = $av->get_branch_limitations();
62 =cut
64 sub get_branch_limitations {
65 my ($self) = @_;
67 my @branchcodes =
68 $self->_avb_resultset->search( { av_id => $self->id() } )
69 ->get_column('branchcode')->all();
71 return \@branchcodes;
74 =head3 add_branch_limitation
76 $av->add_branch_limitation( $branchcode );
78 =cut
80 sub add_branch_limitation {
81 my ( $self, $branchcode ) = @_;
83 croak("No branchcode passed in!") unless $branchcode;
85 my $limitation = $self->_avb_resultset->update_or_create(
86 { av_id => $self->id(), branchcode => $branchcode } );
88 return $limitation ? 1 : undef;
91 =head3 del_branch_limitation
93 $av->del_branch_limitation( $branchcode );
95 =cut
97 sub del_branch_limitation {
98 my ( $self, $branchcode ) = @_;
100 croak("No branchcode passed in!") unless $branchcode;
102 my $limitation =
103 $self->_avb_resultset->find(
104 { av_id => $self->id(), branchcode => $branchcode } );
106 unless ($limitation) {
107 my $id = $self->id();
108 carp(
109 "No branch limit for branch $branchcode found for av_id $id to delete!"
111 return;
114 return $limitation->delete();
117 =head3 replace_branch_limitations
119 $av->replace_branch_limitations( \@branchcodes );
121 =cut
123 sub replace_branch_limitations {
124 my ( $self, $branchcodes ) = @_;
126 $self->_avb_resultset->search( { av_id => $self->id() } )->delete();
128 my @return_values =
129 map { $self->add_branch_limitation($_) } @$branchcodes;
131 return \@return_values;
134 =head3 lib_opac
136 my $description = $av->lib_opac();
138 $av->lib_opac( $description );
140 =cut
142 sub opac_description {
143 my ( $self, $value ) = @_;
145 return $self->lib_opac() || $self->lib();
148 =head3 Koha::Objects->_avb_resultset
150 Returns the internal resultset or creates it if undefined
152 =cut
154 sub _avb_resultset {
155 my ($self) = @_;
157 $self->{_avb_resultset} ||=
158 Koha::Database->new()->schema()->resultset('AuthorisedValuesBranch');
160 $self->{_avb_resultset};
163 =head3 type
165 =cut
167 sub _type {
168 return 'AuthorisedValue';
171 =head1 AUTHOR
173 Kyle M Hall <kyle@bywatersolutions.com>
175 =cut