|
Line 0
Link Here
|
| 0 |
- |
1 |
package Koha::Objects::Limit::LibraryGroup; |
|
|
2 |
|
| 3 |
# This file is part of Koha. |
| 4 |
# |
| 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. |
| 9 |
# |
| 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. |
| 14 |
# |
| 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>. |
| 17 |
|
| 18 |
use Modern::Perl; |
| 19 |
|
| 20 |
use C4::Context; |
| 21 |
use Koha::Database; |
| 22 |
|
| 23 |
=head1 NAME |
| 24 |
|
| 25 |
Koha::Objects::Limit::LibraryGroup - Generic library group limit handling class |
| 26 |
|
| 27 |
=head1 SYNOPSIS |
| 28 |
|
| 29 |
use base qw(Koha::Objects Koha::Objects::Limit::LibraryGroup); |
| 30 |
my $objects = Koha::Objects->search_with_library_grouplimits( $params, $attributes, $library_group_id ); |
| 31 |
|
| 32 |
=head1 DESCRIPTION |
| 33 |
|
| 34 |
This class is provided as a generic way of handling library group limits for Koha::Objects-based classes |
| 35 |
in Koha. |
| 36 |
|
| 37 |
This class must always be subclassed. |
| 38 |
|
| 39 |
=head1 API |
| 40 |
|
| 41 |
=head2 Class methods |
| 42 |
|
| 43 |
=cut |
| 44 |
|
| 45 |
=head3 define_library_group_limits |
| 46 |
|
| 47 |
my $results = $objects->define_library_group_limits( $params, $attributes, $library_group_id ); |
| 48 |
|
| 49 |
Wrapper method for searching objects with library limits, respecting those limits |
| 50 |
|
| 51 |
=cut |
| 52 |
|
| 53 |
sub define_library_group_limits { |
| 54 |
my ( $self, $params, $attributes ) = @_; |
| 55 |
|
| 56 |
my $logged_in_branch = C4::Context->userenv()->{'branch'} || undef; |
| 57 |
return ( $params, $attributes ) unless $logged_in_branch; |
| 58 |
|
| 59 |
my $lib_group_visibility_parameters = $self->object_class()->_library_group_visibility_parameters; |
| 60 |
|
| 61 |
my $library = Koha::Libraries->find($logged_in_branch); |
| 62 |
return ( $params, $attributes ) unless $library; |
| 63 |
|
| 64 |
my $visibility_column = $lib_group_visibility_parameters->{visibility_column}; |
| 65 |
my $library_group_limits_table = |
| 66 |
Koha::Database->new->schema->resultset( $lib_group_visibility_parameters->{class} )->result_source->name; |
| 67 |
|
| 68 |
my $where = { |
| 69 |
'-or' => [ |
| 70 |
{ "$visibility_column" => undef }, |
| 71 |
] |
| 72 |
}; |
| 73 |
|
| 74 |
my @library_groups = $library->library_groups->as_list; |
| 75 |
my @parent_ids; |
| 76 |
|
| 77 |
return ( $params, $attributes ) if scalar(@library_groups) == 0; |
| 78 |
|
| 79 |
foreach my $library_group (@library_groups) { |
| 80 |
my $group_id = $library_group->id; |
| 81 |
my $query = {}; |
| 82 |
$query->{"$visibility_column"} = { "-like" => "%|$group_id|%" }; |
| 83 |
push( @{ $where->{'-or'} }, $query ); |
| 84 |
|
| 85 |
_handle_parent_groups( |
| 86 |
{ |
| 87 |
library_group => $library_group, where => $where, parent_ids => \@parent_ids, |
| 88 |
visibility_column => $visibility_column |
| 89 |
} |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
$params //= {}; |
| 94 |
$attributes //= {}; |
| 95 |
|
| 96 |
if ( ref($params) eq 'ARRAY' ) { |
| 97 |
foreach my $query (@$params) { |
| 98 |
$query = { %$query, %$where }; |
| 99 |
} |
| 100 |
} else { |
| 101 |
$params = { %$params, %$where }; |
| 102 |
} |
| 103 |
|
| 104 |
return ( $params, $attributes ); |
| 105 |
} |
| 106 |
|
| 107 |
=head3 _handle_parent_groups |
| 108 |
|
| 109 |
_handle_parent_groups( |
| 110 |
{ |
| 111 |
library_group => $library_group, where => $where, parent_ids => \@parent_ids, |
| 112 |
visibility_column => $visibility_column |
| 113 |
} |
| 114 |
); |
| 115 |
|
| 116 |
Recursively handles parent library groups to account for multiple sub groups |
| 117 |
|
| 118 |
=cut |
| 119 |
|
| 120 |
sub _handle_parent_groups { |
| 121 |
my ($args) = @_; |
| 122 |
|
| 123 |
my $library_group = $args->{library_group}; |
| 124 |
my $where = $args->{where}; |
| 125 |
my $parent_ids = $args->{parent_ids}; |
| 126 |
my $visibility_column = $args->{visibility_column}; |
| 127 |
|
| 128 |
my $parent = $library_group->parent; |
| 129 |
if ( $parent && !grep( $_ eq $parent->id, @$parent_ids ) ) { |
| 130 |
my $parent_query = {}; |
| 131 |
my $parent_id = $parent->id; |
| 132 |
push( @$parent_ids, $parent_id ); |
| 133 |
$parent_query->{"$visibility_column"} = { "-like" => "%|$parent_id|%" }; |
| 134 |
push( @{ $where->{'-or'} }, $parent_query ); |
| 135 |
_handle_parent_groups( |
| 136 |
{ |
| 137 |
library_group => $parent, where => $where, parent_ids => $parent_ids, |
| 138 |
visibility_column => $visibility_column |
| 139 |
} |
| 140 |
); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
1; |