Bug 22521: DBRev 18.12.00.055
[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 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 use Koha::DateUtils qw(dt_from_string);
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 @parent = $self->parent()
41
42 =cut
43
44 sub parent {
45     my ($self) = @_;
46
47     $self->{_parent} ||= Koha::Library::Groups->find( $self->parent_id );
48
49     return $self->{_parent};
50 }
51
52 =head3 my @children = $self->children()
53
54 =cut
55
56 sub children {
57     my ($self) = @_;
58
59     my $children =
60       Koha::Library::Groups->search( { parent_id => $self->id }, { order_by => [ 'title', 'branchcode' ] } );
61
62     return wantarray ? $children->as_list : $children;
63 }
64
65 =head3 has_child
66
67 my $has_child = $group->has_child( $branchcode );
68
69 Return true if the given branchcode library is a child of this group.
70
71 =cut
72
73 sub has_child {
74     my ( $self, $branchcode ) = @_;
75     return unless $branchcode; # Does not support group of libraries.
76     return ( grep { $_ and $_ eq $branchcode }
77           $self->children->get_column('branchcode') ) ? 1 : 0;
78 }
79
80 =head3 library
81
82 my $library = $group->library();
83
84 Returns the library for this group if one exists
85
86 =cut
87
88 sub library {
89     my ($self) = @_;
90
91     return unless $self->branchcode;
92
93     $self->{_library} ||= Koha::Libraries->find( $self->branchcode );
94
95     return $self->{_library};
96 }
97
98 =head3 libraries
99
100 my @libraries = $group->libraries( { [invert => 1] } );
101
102 Returns the libraries set as direct children of this group.
103
104 If invert param is true, the returned list will be libraries
105 that are *not* direct children of this group.
106
107 =cut
108
109 sub libraries {
110     my ($self, $params) = @_;
111     my $invert = $params->{invert};
112
113     my $in_or_not = $invert ? '-not_in' : '-in';
114
115     my @children = Koha::Library::Groups->search(
116         {
117             parent_id  => $self->id,
118             branchcode => { '!=' => undef },
119         },
120         { order_by => 'branchcode' }
121     );
122
123     my @branchcodes = map { $_->branchcode } @children;
124
125     return Koha::Libraries->search(
126         {
127             branchcode => { $in_or_not => \@branchcodes }
128         },
129         {
130             order_by => 'branchname'
131         }
132     );
133 }
134
135 =head3 all_libraries
136
137 my @libraries = $group->all_libraries( { [invert => 1] } );
138
139 Returns the libraries set as children of this group or any subgroup.
140
141 =cut
142
143 sub all_libraries {
144     my ( $self, $params ) = @_;
145
146     my @libraries;
147
148     my @children = $self->children;
149     foreach my $c (@children) {
150         if ( $c->branchcode ) {
151             push( @libraries, $c->library );
152         }
153         else {
154             push( @libraries, $c->all_libraries );
155         }
156     }
157
158     my %seen;
159     @libraries =
160       grep { !$seen{ $_->id }++ } @libraries;
161
162     return @libraries;
163 }
164
165 =head3 libraries_not_direct_children
166
167 my @libraries = $group->libraries_not_direct_children();
168
169 Returns the libraries *not* set as direct children of this group
170
171 =cut
172
173 sub libraries_not_direct_children {
174     my ($self) = @_;
175
176     return $self->libraries( { invert => 1 } );
177 }
178
179 =head3 store
180
181 =cut
182
183 sub store {
184     my ($self) = @_;
185
186     $self->created_on( dt_from_string() ) unless $self->in_storage();
187
188     return $self->SUPER::store(@_);
189 }
190
191 =head2 Internal methods
192
193 =head3 _type
194
195 =cut
196
197 sub _type {
198     return 'LibraryGroup';
199 }
200
201 =head1 AUTHOR
202
203 Kyle M Hall <kyle@bywatersolutions.com>
204
205 =cut
206
207 1;