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.
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
{
182 $self->{_library_limit_rs
} ||= Koha
::Database
->new->schema->resultset(
183 $self->_library_limits->{class} );
185 return $self->{_library_limit_rs
};