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