Bug 23281: Add Koha::Objects::Limit::Library

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
This commit is contained in:
Tomas Cohen Arazi 2019-07-08 14:13:12 -03:00 committed by Martin Renvoize
parent f1f66d7300
commit ae4e992dfb
No known key found for this signature in database
GPG Key ID: 422B469130441A0F
1 changed files with 86 additions and 0 deletions

View File

@ -0,0 +1,86 @@
package Koha::Objects::Limit::Library;
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with Koha; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
use Modern::Perl;
use Koha::Database;
=head1 NAME
Koha::Objects::Limit::Library - Generic library limit handling class
=head1 SYNOPSIS
use base qw(Koha::Objects Koha::Object::Limit::Library);
my $objects = Koha::Objects->search_with_library_limits( $params, $attributes, $library_id );
=head1 DESCRIPTION
This class is provided as a generic way of handling library limits for Koha::Objects-based classes
in Koha.
This class must always be subclassed.
=head1 API
=head2 Class methods
=cut
=head3 search_with_library_limits
my $results = $objects->search_with_library_limits( $params, $attributes, $library_id );
Wrapper method for searching objects with library limits, respecting those
limits
=cut
sub search_with_library_limits {
my ( $self, $params, $attributes, $library_id ) = @_;
return $self->SUPER::search( $params, $attributes ) unless $library_id;
my $library_limits = $self->object_class()->_library_limits;
my $library_limits_table
= Koha::Database->new->schema->resultset( $library_limits->{class} )->result_source->name;
my $library_field = $library_limits->{library};
my $where = {
'-or' => [
"$library_limits_table.$library_field" => undef,
"$library_limits_table.$library_field" => $library_id,
]
};
$attributes //= {};
if ( exists $attributes->{join} ) {
if ( ref $attributes->{join} eq 'ARRAY' ) {
push @{ $attributes->{join} }, "$library_limits_table";
}
else {
$attributes->{join} = [ $attributes->{join}, "$library_limits_table" ];
}
}
else {
$attributes->{join} = $library_limits_table;
}
return $self->search( { %$params, %$where, }, $attributes );
}
1;