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

(-)a/Koha/Library/Group.pm (+129 lines)
Line 0 Link Here
1
package Koha::Library::Group;
2
3
# Copyright ByWater Solutions 2016
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use Modern::Perl;
21
22
use Carp;
23
24
use Koha::Database;
25
26
use Koha::Libraries;
27
28
use base qw(Koha::Object);
29
30
=head1 NAME
31
32
Koha::Library::Group - Koha Library::Group object class
33
34
=head1 API
35
36
=head2 Class Methods
37
38
=cut
39
40
=head3 my @children = $self->get_children()
41
42
=cut
43
44
sub children {
45
    my ($self) = @_;
46
47
    my $children =
48
      Koha::Library::Groups->search( { parent_id => $self->id }, { order_by => [ 'title', 'branchcode' ] } );
49
50
    return $children;
51
}
52
53
=head3 library
54
55
my $library = $group->library();
56
57
Returns the library for this group if one exists
58
59
=cut
60
61
sub library {
62
    my ($self) = @_;
63
64
    return unless $self->branchcode;
65
66
    return Koha::Libraries->find( $self->branchcode );
67
}
68
69
=head3 libraries
70
71
my @libraries = $group->libraries();
72
73
Returns the libraries set as direct children of this group
74
75
=cut
76
77
sub libraries {
78
    my ($self) = @_;
79
80
    my @children = Koha::Library::Groups->search(
81
        {
82
            parent_id  => $self->id,
83
            branchcode => { '!=' => undef },
84
        },
85
        { order_by => 'branchcode' }
86
    );
87
88
    my @libraries = map { $_->library() } @children;
89
}
90
91
=head3 libraries_not_direct_children
92
93
my @libraries = $group->libraries_not_direct_children();
94
95
Returns the libraries set as direct children of this group
96
97
=cut
98
99
sub libraries_not_direct_children {
100
    my ($self) = @_;
101
102
    my @children = Koha::Library::Groups->search(
103
        {
104
            parent_id  => $self->id,
105
            branchcode => { '!=' => undef },
106
        },
107
        { order_by => 'branchcode' }
108
    );
109
110
    my @branchcodes = map { $_->branchcode } @children;
111
112
    return Koha::Libraries->search( { branchcode => { -not_in => \@branchcodes } } );
113
}
114
115
=head3 type
116
117
=cut
118
119
sub type {
120
    return 'LibraryGroup';
121
}
122
123
=head1 AUTHOR
124
125
Kyle M Hall <kyle@bywatersolutions.com>
126
127
=cut
128
129
1;
(-)a/Koha/Library/Groups.pm (-1 / +109 lines)
Line 0 Link Here
0
- 
1
package Koha::Library::Groups;
2
3
# Copyright ByWater Solutions 2016
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use Modern::Perl;
21
22
use Carp;
23
24
use Koha::Database;
25
26
use Koha::Library::Group;
27
28
use base qw(Koha::Objects);
29
30
=head1 NAME
31
32
Koha::Library::Groups - Koha Library::Group object set class
33
34
=head1 API
35
36
=head2 Class Methods
37
38
=cut
39
40
=head3 my $tree_hashref = $self->get_tree()
41
42
=cut
43
44
sub get_tree {
45
    my ( $self ) = @_;
46
47
    my $root_group = $self->get_root_group();
48
49
    my $tree;
50
51
    $tree->{ $root_group->id }->{object} = $root_group;
52
    $tree->{ $root_group->id }->{children} = $root_group->_sub_tree( $root_group );
53
54
    return $tree;
55
}
56
57
=head3 my $tree_hashref = $self->_sub_tree()
58
59
=cut
60
61
sub _sub_tree {
62
    my ( $self, $parent ) = @_;
63
64
    my @children = $parent->children();
65
66
    my $subtree;
67
68
    foreach my $child ( @children ) {
69
        $subtree->{ $child->id }->{object} = $child;
70
        $subtree->{ $child->id }->{children} = $child->sub_tree($parent);
71
    }
72
73
    return $subtree;
74
75
}
76
77
=head3 my @root_groups = $self->get_root_group()
78
79
=cut
80
81
sub get_root_groups {
82
    my ( $self ) = @_;
83
84
    return $self->search( { parent_id => undef }, { order_by => 'title' } );
85
}
86
87
=head3 type
88
89
=cut
90
91
sub type {
92
    return 'LibraryGroup';
93
}
94
95
=head3 object_class
96
97
=cut
98
99
sub object_class {
100
    return 'Koha::Library::Group';
101
}
102
103
=head1 AUTHOR
104
105
Kyle M Hall <kyle@bywatersolutions.com>
106
107
=cut
108
109
1;

Return to bug 15707