|
Line 0
Link Here
|
| 0 |
- |
1 |
package Koha::Object::Library::Limit; |
|
|
2 |
|
| 3 |
# This file is part of Koha. |
| 4 |
# |
| 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. |
| 9 |
# |
| 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. |
| 13 |
# |
| 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. |
| 17 |
|
| 18 |
use Modern::Perl; |
| 19 |
|
| 20 |
use Koha::Database; |
| 21 |
use Koha::Exceptions; |
| 22 |
|
| 23 |
use Try::Tiny; |
| 24 |
|
| 25 |
=head1 NAME |
| 26 |
|
| 27 |
Koha::Object::Library::Limit - Generic library limit handling class |
| 28 |
|
| 29 |
=head1 SYNOPSIS |
| 30 |
|
| 31 |
use base qw(Koha::Object Koha::Object::Library::Limit); |
| 32 |
my $object = Koha::Object->new({ property1 => $property1, property2 => $property2, etc... } ); |
| 33 |
|
| 34 |
=head1 DESCRIPTION |
| 35 |
|
| 36 |
This class is provided as a generic way of handling library limits for Koha::Object-based classes |
| 37 |
in Koha. |
| 38 |
|
| 39 |
This class must always be subclassed. |
| 40 |
|
| 41 |
=head1 API |
| 42 |
|
| 43 |
=head2 Class Methods |
| 44 |
|
| 45 |
=cut |
| 46 |
|
| 47 |
=head3 library_limits |
| 48 |
|
| 49 |
my $limits = $object->library_limits(); |
| 50 |
|
| 51 |
$object->library_limits( \@branchcodes ); |
| 52 |
|
| 53 |
=cut |
| 54 |
|
| 55 |
sub library_limits { |
| 56 |
my ( $self, $branchcodes ) = @_; |
| 57 |
|
| 58 |
if ($branchcodes) { |
| 59 |
return $self->replace_library_limits($branchcodes); |
| 60 |
} |
| 61 |
else { |
| 62 |
return $self->get_library_limits(); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
=head3 get_library_limits |
| 67 |
|
| 68 |
my $limits = $object->get_library_limits(); |
| 69 |
|
| 70 |
=cut |
| 71 |
|
| 72 |
sub get_library_limits { |
| 73 |
my ($self) = @_; |
| 74 |
|
| 75 |
my @branchcodes |
| 76 |
= $self->_library_limit_rs->search( |
| 77 |
{ $self->_library_limits->{id} => $self->id } ) |
| 78 |
->get_column( $self->_library_limits->{library} )->all(); |
| 79 |
|
| 80 |
return \@branchcodes; |
| 81 |
} |
| 82 |
|
| 83 |
=head3 add_library_limit |
| 84 |
|
| 85 |
$object->add_library_limit( $branchcode ); |
| 86 |
|
| 87 |
=cut |
| 88 |
|
| 89 |
sub add_library_limit { |
| 90 |
my ( $self, $branchcode ) = @_; |
| 91 |
|
| 92 |
Koha::Exceptions::MissingParameter->throw( |
| 93 |
"Required parameter 'branchcode' missing") |
| 94 |
unless $branchcode; |
| 95 |
|
| 96 |
my $limitation; |
| 97 |
try { |
| 98 |
$limitation = $self->_library_limit_rs->update_or_create( |
| 99 |
{ $self->_library_limits->{id} => $self->id, |
| 100 |
$self->_library_limits->{library} => $branchcode |
| 101 |
} |
| 102 |
); |
| 103 |
} |
| 104 |
catch { |
| 105 |
Koha::Exceptions::CannotAddLibraryLimit->throw( $_->{msg} ); |
| 106 |
}; |
| 107 |
|
| 108 |
return $limitation ? 1 : undef; |
| 109 |
} |
| 110 |
|
| 111 |
=head3 del_library_limit |
| 112 |
|
| 113 |
$object->del_library_limit( $branchcode ); |
| 114 |
|
| 115 |
=cut |
| 116 |
|
| 117 |
sub del_library_limit { |
| 118 |
my ( $self, $branchcode ) = @_; |
| 119 |
|
| 120 |
Koha::Exceptions::MissingParameter->throw( |
| 121 |
"Required parameter 'branchcode' missing") |
| 122 |
unless $branchcode; |
| 123 |
|
| 124 |
my $limitation = $self->_library_limit_rs->search( |
| 125 |
{ $self->_library_limits->{id} => $self->id, |
| 126 |
$self->_library_limits->{library} => $branchcode |
| 127 |
} |
| 128 |
); |
| 129 |
|
| 130 |
Koha::Exceptions::ObjectNotFound->throw( |
| 131 |
"No branch limit for branch $branchcode found for id " |
| 132 |
. $self->id |
| 133 |
. " to delete!" ) |
| 134 |
unless ($limitation->count); |
| 135 |
|
| 136 |
return $limitation->delete(); |
| 137 |
} |
| 138 |
|
| 139 |
=head3 replace_library_limits |
| 140 |
|
| 141 |
$object->replace_library_limits( \@branchcodes ); |
| 142 |
|
| 143 |
=cut |
| 144 |
|
| 145 |
sub replace_library_limits { |
| 146 |
my ( $self, $branchcodes ) = @_; |
| 147 |
|
| 148 |
$self->_library_limit_rs->search( |
| 149 |
{ $self->_library_limits->{id} => $self->id } )->delete; |
| 150 |
|
| 151 |
my @return_values = map { $self->add_library_limit($_) } @$branchcodes; |
| 152 |
|
| 153 |
return \@return_values; |
| 154 |
} |
| 155 |
|
| 156 |
=head3 Koha::Objects->_library_limit_rs |
| 157 |
|
| 158 |
Returns the internal resultset for the branch limitation table or creates it if undefined |
| 159 |
|
| 160 |
=cut |
| 161 |
|
| 162 |
sub _library_limit_rs { |
| 163 |
my ($self) = @_; |
| 164 |
|
| 165 |
$self->{_library_limit_rs} ||= Koha::Database->new->schema->resultset( |
| 166 |
$self->_library_limits->{class} ); |
| 167 |
|
| 168 |
return $self->{_library_limit_rs}; |
| 169 |
} |
| 170 |
|
| 171 |
1; |