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
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.
25 use Koha::DateUtils qw(dt_from_string);
28 use base qw(Koha::Object);
32 Koha::Library::Group - Koha Library::Group object class
40 =head3 my @parent = $self->parent()
47 $self->{_parent} ||= Koha::Library::Groups->find( $self->parent_id );
49 return $self->{_parent};
52 =head3 my @children = $self->children()
60 Koha::Library::Groups->search( { parent_id => $self->id }, { order_by => [ 'title', 'branchcode' ] } );
62 return wantarray ? $children->as_list : $children;
67 my $library = $group->library();
69 Returns the library for this group if one exists
76 return unless $self->branchcode;
78 $self->{_library} ||= Koha::Libraries->find( $self->branchcode );
80 return $self->{_library};
85 my @libraries = $group->libraries( { [invert => 1] } );
87 Returns the libraries set as direct children of this group.
89 If invert param is true, the returned list will be libraries
90 that are *not* direct children of this group.
95 my ($self, $params) = @_;
96 my $invert = $params->{invert};
98 my $in_or_not = $invert ? '-not_in' : '-in';
100 my @children = Koha::Library::Groups->search(
102 parent_id => $self->id,
103 branchcode => { '!=' => undef },
105 { order_by => 'branchcode' }
108 my @branchcodes = map { $_->branchcode } @children;
110 return Koha::Libraries->search(
112 branchcode => { $in_or_not => \@branchcodes }
115 order_by => 'branchname'
122 my @libraries = $group->all_libraries( { [invert => 1] } );
124 Returns the libraries set as children of this group or any subgroup.
129 my ( $self, $params ) = @_;
133 my @children = $self->children;
134 foreach my $c (@children) {
135 if ( $c->branchcode ) {
136 push( @libraries, $c );
139 push( @libraries, $c->all_libraries );
145 grep { !$seen{ $_->id }++ } @libraries;
147 # return wantarray ? @li : $children;
151 =head3 libraries_not_direct_children
153 my @libraries = $group->libraries_not_direct_children();
155 Returns the libraries *not* set as direct children of this group
159 sub libraries_not_direct_children {
162 return $self->libraries( { invert => 1 } );
172 $self->created_on( dt_from_string() ) unless $self->in_storage();
174 return $self->SUPER::store(@_);
177 =head2 Internal methods
184 return 'LibraryGroup';
189 Kyle M Hall <kyle@bywatersolutions.com>