Bug 20434: Update UNIMARC framework - auth (TM)
[koha.git] / Koha / Object / Limit / Library.pm
blob7d86b94d6356dc147ed79e3ee13ee63bbc7dca57
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
8 # version.
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.
18 use Modern::Perl;
20 use Koha::Database;
21 use Koha::Exceptions;
22 use Koha::Libraries;
24 use Try::Tiny;
26 =head1 NAME
28 Koha::Object::Limit::Library - Generic library limit handling class
30 =head1 SYNOPSIS
32 use base qw(Koha::Object Koha::Object::Limit::Library);
33 my $object = Koha::Object->new({ property1 => $property1, property2 => $property2, etc... } );
35 =head1 DESCRIPTION
37 This class is provided as a generic way of handling library limits for Koha::Object-based classes
38 in Koha.
40 This class must always be subclassed.
42 =head1 API
44 =head2 Class Methods
46 =cut
48 =head3 library_limits
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.
58 =cut
60 sub library_limits {
61 my ( $self, $branchcodes ) = @_;
63 if ($branchcodes) {
64 return $self->replace_library_limits($branchcodes);
66 else {
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.
78 =cut
80 sub get_library_limits {
81 my ($self) = @_;
83 my @branchcodes
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 );
93 return $libraries;
96 =head3 add_library_limit
98 $object->add_library_limit( $branchcode );
100 =cut
102 sub add_library_limit {
103 my ( $self, $branchcode ) = @_;
105 Koha::Exceptions::MissingParameter->throw(
106 "Required parameter 'branchcode' missing")
107 unless $branchcode;
109 try {
110 $self->_library_limit_rs->update_or_create(
111 { $self->_library_limits->{id} => $self->id,
112 $self->_library_limits->{library} => $branchcode
116 catch {
117 Koha::Exceptions::CannotAddLibraryLimit->throw( $_->{msg} );
120 return $self;
123 =head3 del_library_limit
125 $object->del_library_limit( $branchcode );
127 =cut
129 sub del_library_limit {
130 my ( $self, $branchcode ) = @_;
132 Koha::Exceptions::MissingParameter->throw(
133 "Required parameter 'branchcode' missing")
134 unless $branchcode;
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 "
144 . $self->id
145 . " to delete!" )
146 unless ($limitation->count);
148 return $limitation->delete();
151 =head3 replace_library_limits
153 $object->replace_library_limits( \@branchcodes );
155 =cut
157 sub replace_library_limits {
158 my ( $self, $branchcodes ) = @_;
160 $self->_result->result_source->schema->txn_do(
161 sub {
162 $self->_library_limit_rs->search(
163 { $self->_library_limits->{id} => $self->id } )->delete;
165 map { $self->add_library_limit($_) } @$branchcodes;
169 return $self;
173 =head3 Koha::Objects->_library_limit_rs
175 Returns the internal resultset for the branch limitation table or creates it if undefined
177 =cut
179 sub _library_limit_rs {
180 my ($self) = @_;
182 $self->{_library_limit_rs} ||= Koha::Database->new->schema->resultset(
183 $self->_library_limits->{class} );
185 return $self->{_library_limit_rs};