Bug 29844: (QA follow-up) Fix Koha::Library::Groups->all_libraries
[koha.git] / Koha / Library / Group.pm
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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22
23 use Koha::Database;
24 use Koha::DateUtils qw( dt_from_string );
25 use Koha::Libraries;
26
27 use base qw(Koha::Object);
28
29 =head1 NAME
30
31 Koha::Library::Group - Koha Library::Group object class
32
33 =head1 API
34
35 =head2 Class methods
36
37 =cut
38
39 =head3 my @parent = $self->parent()
40
41 =cut
42
43 sub parent {
44     my ($self) = @_;
45
46     $self->{_parent} ||= Koha::Library::Groups->find( $self->parent_id );
47
48     return $self->{_parent};
49 }
50
51 =head3 my @children = $self->children()
52
53 =cut
54
55 sub children {
56     my ($self) = @_;
57
58     my $children =
59       Koha::Library::Groups->search( { parent_id => $self->id }, { order_by => [ 'title', 'branchcode' ] } );
60
61     return wantarray ? $children->as_list : $children;
62 }
63
64 =head3 has_child
65
66 my $has_child = $group->has_child( $branchcode );
67
68 Return true if the given branchcode library is a child of this group.
69
70 =cut
71
72 sub has_child {
73     my ( $self, $branchcode ) = @_;
74     return unless $branchcode; # Does not support group of libraries.
75     return ( grep { $_ and $_ eq $branchcode }
76           $self->children->get_column('branchcode') ) ? 1 : 0;
77 }
78
79 =head3 library
80
81 my $library = $group->library();
82
83 Returns the library for this group if one exists
84
85 =cut
86
87 sub library {
88     my ($self) = @_;
89
90     return unless $self->branchcode;
91
92     $self->{_library} ||= Koha::Libraries->find( $self->branchcode );
93
94     return $self->{_library};
95 }
96
97 =head3 libraries
98
99 my $libraries = $group->libraries( { [invert => 1] } );
100
101 Returns the libraries set as direct children of this group.
102
103 If invert param is true, the returned list will be libraries
104 that are *not* direct children of this group.
105
106 =cut
107
108 sub libraries {
109     my ($self, $params) = @_;
110     my $invert = $params->{invert};
111
112     my $in_or_not = $invert ? '-not_in' : '-in';
113
114     my @children = Koha::Library::Groups->search(
115         {
116             parent_id  => $self->id,
117             branchcode => { '!=' => undef },
118         },
119         { order_by => 'branchcode' }
120     )->as_list;
121
122     my @branchcodes = map { $_->branchcode } @children;
123
124     return Koha::Libraries->search(
125         {
126             branchcode => { $in_or_not => \@branchcodes }
127         },
128         {
129             order_by => 'branchname'
130         }
131     );
132 }
133
134 =head3 all_libraries
135
136 my @libraries = $group->all_libraries( { [invert => 1] } );
137
138 Returns the libraries set as children of this group or any subgroup.
139
140 =cut
141
142 sub all_libraries {
143     my ( $self, $params ) = @_;
144
145     my @libraries;
146
147     push (@libraries, $self->libraries->as_list);
148     my @children = $self->children->search({ branchcode => undef })->as_list;
149     foreach my $c (@children) {
150         push( @libraries, $c->all_libraries );
151     }
152
153     my %seen;
154     @libraries =
155       grep { !$seen{ $_->id }++ } @libraries;
156
157     return @libraries;
158 }
159
160 =head3 libraries_not_direct_children
161
162 my $libraries = $group->libraries_not_direct_children();
163
164 Returns the libraries *not* set as direct children of this group
165
166 =cut
167
168 sub libraries_not_direct_children {
169     my ($self) = @_;
170
171     return $self->libraries( { invert => 1 } );
172 }
173
174 =head3 store
175
176 =cut
177
178 sub store {
179     my ($self) = @_;
180
181     $self->created_on( dt_from_string() ) unless $self->in_storage();
182
183     return $self->SUPER::store(@_);
184 }
185
186 =head2 Internal methods
187
188 =head3 _type
189
190 =cut
191
192 sub _type {
193     return 'LibraryGroup';
194 }
195
196 =head1 AUTHOR
197
198 Kyle M Hall <kyle@bywatersolutions.com>
199
200 =cut
201
202 1;