1 package Koha::Object::Limit::Library;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 Koha::Object::Limit::Library - Generic library limit handling class
31 use base qw(Koha::Object Koha::Object::Limit::Library);
32 my $object = Koha::Object->new({ property1 => $property1, property2 => $property2, etc... } );
36 This class is provided as a generic way of handling library limits for Koha::Object-based classes
39 This class must always be subclassed.
49 my $limits = $object->library_limits();
51 $object->library_limits( \@branchcodes );
56 my ( $self, $branchcodes ) = @_;
59 return $self->replace_library_limits($branchcodes);
62 return $self->get_library_limits();
66 =head3 get_library_limits
68 my $limits = $object->get_library_limits();
72 sub get_library_limits {
76 = $self->_library_limit_rs->search(
77 { $self->_library_limits->{id} => $self->id } )
78 ->get_column( $self->_library_limits->{library} )->all();
83 =head3 add_library_limit
85 $object->add_library_limit( $branchcode );
89 sub add_library_limit {
90 my ( $self, $branchcode ) = @_;
92 Koha::Exceptions::MissingParameter->throw(
93 "Required parameter 'branchcode' missing")
98 $limitation = $self->_library_limit_rs->update_or_create(
99 { $self->_library_limits->{id} => $self->id,
100 $self->_library_limits->{library} => $branchcode
105 Koha::Exceptions::CannotAddLibraryLimit->throw( $_->{msg} );
108 return $limitation ? 1 : undef;
111 =head3 del_library_limit
113 $object->del_library_limit( $branchcode );
117 sub del_library_limit {
118 my ( $self, $branchcode ) = @_;
120 Koha::Exceptions::MissingParameter->throw(
121 "Required parameter 'branchcode' missing")
124 my $limitation = $self->_library_limit_rs->search(
125 { $self->_library_limits->{id} => $self->id,
126 $self->_library_limits->{library} => $branchcode
130 Koha::Exceptions::ObjectNotFound->throw(
131 "No branch limit for branch $branchcode found for id "
134 unless ($limitation->count);
136 return $limitation->delete();
139 =head3 replace_library_limits
141 $object->replace_library_limits( \@branchcodes );
145 sub replace_library_limits {
146 my ( $self, $branchcodes ) = @_;
148 $self->_library_limit_rs->search(
149 { $self->_library_limits->{id} => $self->id } )->delete;
151 my @return_values = map { $self->add_library_limit($_) } @$branchcodes;
153 return \@return_values;
156 =head3 Koha::Objects->_library_limit_rs
158 Returns the internal resultset for the branch limitation table or creates it if undefined
162 sub _library_limit_rs {
165 $self->{_library_limit_rs} ||= Koha::Database->new->schema->resultset(
166 $self->_library_limits->{class} );
168 return $self->{_library_limit_rs};