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
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24 use Try::Tiny qw( catch try );
28 Koha::Object::Limit::Library - Generic library limit handling class
32 use base qw(Koha::Object Koha::Object::Limit::Library);
33 my $object = Koha::Object->new({ property1 => $property1, property2 => $property2, etc... } );
37 This class is provided as a generic way of handling library limits for Koha::Object-based classes
40 This class must always be subclassed.
50 my $limits = $object->library_limits();
52 $object->library_limits( \@branchcodes );
54 Accessor method for library limits. When updating library limits, it accepts
55 a list of branchcodes. If requested to return the current library limits
56 it returns a Koha::Libraries object with the corresponding libraries.
61 my ( $self, $branchcodes ) = @_;
64 return $self->replace_library_limits($branchcodes);
67 return $self->get_library_limits();
71 =head3 get_library_limits
73 my $limits = $object->get_library_limits();
75 Returns the current library limits in the form of a Koha::Libraries iterator object.
76 It returns undef if no library limits defined.
80 sub get_library_limits {
84 = $self->_library_limit_rs->search(
85 { $self->_library_limits->{id} => $self->id } )
86 ->get_column( $self->_library_limits->{library} )->all();
88 return unless @branchcodes;
90 my $filter = [ map { { branchcode => $_ } } @branchcodes ];
91 my $libraries = Koha::Libraries->search( $filter );
96 =head3 add_library_limit
98 $object->add_library_limit( $branchcode );
102 sub add_library_limit {
103 my ( $self, $branchcode ) = @_;
105 Koha::Exceptions::MissingParameter->throw(
106 "Required parameter 'branchcode' missing")
110 $self->_library_limit_rs->update_or_create(
111 { $self->_library_limits->{id} => $self->id,
112 $self->_library_limits->{library} => $branchcode
117 Koha::Exceptions::CannotAddLibraryLimit->throw( $_->{msg} );
123 =head3 del_library_limit
125 $object->del_library_limit( $branchcode );
129 sub del_library_limit {
130 my ( $self, $branchcode ) = @_;
132 Koha::Exceptions::MissingParameter->throw(
133 "Required parameter 'branchcode' missing")
136 my $limitation = $self->_library_limit_rs->search(
137 { $self->_library_limits->{id} => $self->id,
138 $self->_library_limits->{library} => $branchcode
142 Koha::Exceptions::ObjectNotFound->throw(
143 "No branch limit for branch $branchcode found for id "
146 unless ($limitation->count);
148 return $limitation->delete();
151 =head3 replace_library_limits
153 $object->replace_library_limits( \@branchcodes );
157 sub replace_library_limits {
158 my ( $self, $branchcodes ) = @_;
160 $self->_result->result_source->schema->txn_do(
162 $self->_library_limit_rs->search(
163 { $self->_library_limits->{id} => $self->id } )->delete;
165 map { $self->add_library_limit($_) } @$branchcodes;
173 =head3 Koha::Objects->_library_limit_rs
175 Returns the internal resultset for the branch limitation table or creates it if undefined
179 sub _library_limit_rs {
181 return Koha::Database->new->schema->resultset( $self->_library_limits->{class} );