View | Details | Raw Unified | Return to bug 38290
Collapse All | Expand All

(-)a/Koha/Acquisition/Bookseller.pm (-1 / +26 lines)
Lines 25-31 use Koha::Subscriptions; Link Here
25
25
26
use C4::Contract qw( GetContracts );
26
use C4::Contract qw( GetContracts );
27
27
28
use base qw( Koha::Object::Mixin::AdditionalFields Koha::Object );
28
use base qw( Koha::Object::Mixin::AdditionalFields Koha::Object Koha::Object::Limit::LibraryGroup );
29
29
30
=head1 NAME
30
=head1 NAME
31
31
Lines 35-40 Koha::Acquisition::Bookseller Object class Link Here
35
35
36
=head2 Class methods
36
=head2 Class methods
37
37
38
=head3 store
39
40
=cut
41
42
sub store {
43
    my ($self) = @_;
44
45
    $self->set_lib_group_visibility() if $self->lib_group_visibility;
46
    $self = $self->SUPER::store();
47
    return $self;
48
}
49
38
=head3 baskets
50
=head3 baskets
39
51
40
    my $vendor  = Koha::Acquisition::Booksellers->find( $id );
52
    my $vendor  = Koha::Acquisition::Booksellers->find( $id );
Lines 222-227 sub to_api_mapping { Link Here
222
    };
234
    };
223
}
235
}
224
236
237
=head3 _library_group_visibility_parameters
238
239
Configure library group limits
240
241
=cut
242
243
sub _library_group_visibility_parameters {
244
    return {
245
        class             => "Aqbookseller",
246
        visibility_column => "lib_group_visibility",
247
    };
248
}
249
225
=head2 Internal methods
250
=head2 Internal methods
226
251
227
=head3 _type
252
=head3 _type
(-)a/Koha/Acquisition/Booksellers.pm (-1 / +17 lines)
Lines 19-25 use Modern::Perl; Link Here
19
19
20
use Koha::Acquisition::Bookseller;
20
use Koha::Acquisition::Bookseller;
21
21
22
use base qw( Koha::Object::Mixin::AdditionalFields Koha::Objects );
22
use base qw( Koha::Objects Koha::Object::Mixin::AdditionalFields Koha::Objects::Limit::LibraryGroup );
23
23
24
=head1 NAME
24
=head1 NAME
25
25
Lines 27-32 Koha::Acquisition::Booksellers object set class Link Here
27
27
28
=head1 API
28
=head1 API
29
29
30
=head2 Class methods
31
32
=head3 search
33
34
=cut
35
36
sub search {
37
    my ( $self, $params, $attributes ) = @_;
38
39
    my $class = ref($self) ? ref($self) : $self;
40
41
    ( $params, $attributes ) = $self->define_library_group_limits( $params, $attributes );
42
43
    return $self->SUPER::search( $params, $attributes );
44
}
45
30
=head2 Internal methods
46
=head2 Internal methods
31
47
32
=head3 _type (internal)
48
=head3 _type (internal)
(-)a/Koha/Object/Limit/LibraryGroup.pm (+82 lines)
Line 0 Link Here
1
package Koha::Object::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 Koha::Database;
21
use Koha::Exceptions;
22
23
use Try::Tiny qw( catch try );
24
25
=head1 NAME
26
27
Koha::Object::Limit::LibraryGroup - Generic library group limit handling class
28
29
=head1 SYNOPSIS
30
31
    use base qw(Koha::Object Koha::Object::Limit::LibraryGroup);
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 group 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 lib_group_limits
48
49
A method that can be used to embed or simply retrieve the library group limits for an object
50
51
=cut
52
53
sub lib_group_limits {
54
    my ( $self ) = @_;
55
56
    my $lib_group_visibility = $self->lib_group_visibility;
57
    return [] if !$lib_group_visibility;
58
59
    my @ids = grep(/[0-9]/, split(/\|/, $lib_group_visibility));
60
61
    my @lib_groups = map { Koha::Library::Groups->find($_) } @ids;
62
63
    return \@lib_groups;
64
}
65
66
=head3 set_lib_group_visibility
67
68
A method that can be used to set the library group visibility for an object
69
70
=cut
71
72
sub set_lib_group_visibility {
73
    my ( $self ) = @_;
74
75
    if ( $self->lib_group_visibility && $self->lib_group_visibility !~ /^\|.*\|$/ ) {
76
        $self->lib_group_visibility( "|" . $self->lib_group_visibility . "|" );
77
    }
78
79
    return $self;
80
}
81
82
1;
(-)a/Koha/Objects/Limit/LibraryGroup.pm (-1 / +144 lines)
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;

Return to bug 38290