Bug 22453: Make TestBuilder generates dates taking into account the timezone
[koha.git] / Koha / Library / Group.pm
blobcf40330545ef2e46e0ea3dc34bd564ccc28e8614
1 package Koha::Library::Group;
3 # Copyright ByWater Solutions 2016
5 # This file is part of Koha.
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.
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.
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.
20 use Modern::Perl;
22 use Carp;
24 use Koha::Database;
25 use Koha::DateUtils qw(dt_from_string);
26 use Koha::Libraries;
28 use base qw(Koha::Object);
30 =head1 NAME
32 Koha::Library::Group - Koha Library::Group object class
34 =head1 API
36 =head2 Class methods
38 =cut
40 =head3 my @parent = $self->parent()
42 =cut
44 sub parent {
45 my ($self) = @_;
47 $self->{_parent} ||= Koha::Library::Groups->find( $self->parent_id );
49 return $self->{_parent};
52 =head3 my @children = $self->children()
54 =cut
56 sub children {
57 my ($self) = @_;
59 my $children =
60 Koha::Library::Groups->search( { parent_id => $self->id }, { order_by => [ 'title', 'branchcode' ] } );
62 return wantarray ? $children->as_list : $children;
65 =head3 has_child
67 my $has_child = $group->has_child( $branchcode );
69 Return true if the given branchcode library is a child of this group.
71 =cut
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;
80 =head3 library
82 my $library = $group->library();
84 Returns the library for this group if one exists
86 =cut
88 sub library {
89 my ($self) = @_;
91 return unless $self->branchcode;
93 $self->{_library} ||= Koha::Libraries->find( $self->branchcode );
95 return $self->{_library};
98 =head3 libraries
100 my @libraries = $group->libraries( { [invert => 1] } );
102 Returns the libraries set as direct children of this group.
104 If invert param is true, the returned list will be libraries
105 that are *not* direct children of this group.
107 =cut
109 sub libraries {
110 my ($self, $params) = @_;
111 my $invert = $params->{invert};
113 my $in_or_not = $invert ? '-not_in' : '-in';
115 my @children = Koha::Library::Groups->search(
117 parent_id => $self->id,
118 branchcode => { '!=' => undef },
120 { order_by => 'branchcode' }
123 my @branchcodes = map { $_->branchcode } @children;
125 return Koha::Libraries->search(
127 branchcode => { $in_or_not => \@branchcodes }
130 order_by => 'branchname'
135 =head3 all_libraries
137 my @libraries = $group->all_libraries( { [invert => 1] } );
139 Returns the libraries set as children of this group or any subgroup.
141 =cut
143 sub all_libraries {
144 my ( $self, $params ) = @_;
146 my @libraries;
148 my @children = $self->children;
149 foreach my $c (@children) {
150 if ( $c->branchcode ) {
151 push( @libraries, $c->library );
153 else {
154 push( @libraries, $c->all_libraries );
158 my %seen;
159 @libraries =
160 grep { !$seen{ $_->id }++ } @libraries;
162 return @libraries;
165 =head3 libraries_not_direct_children
167 my @libraries = $group->libraries_not_direct_children();
169 Returns the libraries *not* set as direct children of this group
171 =cut
173 sub libraries_not_direct_children {
174 my ($self) = @_;
176 return $self->libraries( { invert => 1 } );
179 =head3 store
181 =cut
183 sub store {
184 my ($self) = @_;
186 $self->created_on( dt_from_string() ) unless $self->in_storage();
188 return $self->SUPER::store(@_);
191 =head2 Internal methods
193 =head3 _type
195 =cut
197 sub _type {
198 return 'LibraryGroup';
201 =head1 AUTHOR
203 Kyle M Hall <kyle@bywatersolutions.com>
205 =cut