Bug 16735: (QA follow-up) POD fixes
[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 library
66
67 my $library = $group->library();
68
69 Returns the library for this group if one exists
70
71 =cut
72
73 sub library {
74     my ($self) = @_;
75
76     return unless $self->branchcode;
77
78     $self->{_library} ||= Koha::Libraries->find( $self->branchcode );
79
80     return $self->{_library};
81 }
82
83 =head3 libraries
84
85 my @libraries = $group->libraries( { [invert => 1] } );
86
87 Returns the libraries set as direct children of this group.
88
89 If invert param is true, the returned list will be libraries
90 that are *not* direct children of this group.
91
92 =cut
93
94 sub libraries {
95     my ($self, $params) = @_;
96     my $invert = $params->{invert};
97
98     my $in_or_not = $invert ? '-not_in' : '-in';
99
100     my @children = Koha::Library::Groups->search(
101         {
102             parent_id  => $self->id,
103             branchcode => { '!=' => undef },
104         },
105         { order_by => 'branchcode' }
106     );
107
108     my @branchcodes = map { $_->branchcode } @children;
109
110     return Koha::Libraries->search(
111         {
112             branchcode => { $in_or_not => \@branchcodes }
113         },
114         {
115             order_by => 'branchname'
116         }
117     );
118 }
119
120 =head3 all_libraries
121
122 my @libraries = $group->all_libraries( { [invert => 1] } );
123
124 Returns the libraries set as children of this group or any subgroup.
125
126 =cut
127
128 sub all_libraries {
129     my ( $self, $params ) = @_;
130
131     my @libraries;
132
133     my @children = $self->children;
134     foreach my $c (@children) {
135         if ( $c->branchcode ) {
136             push( @libraries, $c );
137         }
138         else {
139             push( @libraries, $c->all_libraries );
140         }
141     }
142
143     my %seen;
144     @libraries =
145       grep { !$seen{ $_->id }++ } @libraries;
146
147 #    return wantarray ? @li : $children;
148     return @libraries;
149 }
150
151 =head3 libraries_not_direct_children
152
153 my @libraries = $group->libraries_not_direct_children();
154
155 Returns the libraries *not* set as direct children of this group
156
157 =cut
158
159 sub libraries_not_direct_children {
160     my ($self) = @_;
161
162     return $self->libraries( { invert => 1 } );
163 }
164
165 =head3 store
166
167 =cut
168
169 sub store {
170     my ($self) = @_;
171
172     $self->created_on( dt_from_string() ) unless $self->in_storage();
173
174     return $self->SUPER::store(@_);
175 }
176
177 =head2 Internal methods
178
179 =head3 _type
180
181 =cut
182
183 sub _type {
184     return 'LibraryGroup';
185 }
186
187 =head1 AUTHOR
188
189 Kyle M Hall <kyle@bywatersolutions.com>
190
191 =cut
192
193 1;