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
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.
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.
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>.
24 use Koha::DateUtils qw( dt_from_string );
27 use base qw(Koha::Object);
31 Koha::Library::Group - Koha Library::Group object class
39 =head3 my @parent = $self->parent()
46 $self->{_parent} ||= Koha::Library::Groups->find( $self->parent_id );
48 return $self->{_parent};
51 =head3 my @children = $self->children()
59 Koha::Library::Groups->search( { parent_id => $self->id }, { order_by => [ 'title', 'branchcode' ] } );
61 return wantarray ? $children->as_list : $children;
66 my $has_child = $group->has_child( $branchcode );
68 Return true if the given branchcode library is a child of this group.
73 my ( $self, $branchcode ) = @_;
74 return unless $branchcode; # Does not support group of libraries.
75 return ( grep { $_ and $_ eq $branchcode }
76 $self->children->get_column('branchcode') ) ? 1 : 0;
81 my $library = $group->library();
83 Returns the library for this group if one exists
90 return unless $self->branchcode;
92 $self->{_library} ||= Koha::Libraries->find( $self->branchcode );
94 return $self->{_library};
99 my $libraries = $group->libraries( { [invert => 1] } );
101 Returns the libraries set as direct children of this group.
103 If invert param is true, the returned list will be libraries
104 that are *not* direct children of this group.
109 my ($self, $params) = @_;
110 my $invert = $params->{invert};
112 my $in_or_not = $invert ? '-not_in' : '-in';
114 my @branchcodes = Koha::Library::Groups->search(
116 parent_id => $self->id,
117 branchcode => { '!=' => undef },
119 { order_by => 'branchcode' }
120 )->get_column('branchcode');
122 return Koha::Libraries->search(
124 branchcode => { $in_or_not => \@branchcodes }
127 order_by => 'branchname'
134 my @libraries = $group->all_libraries( { [invert => 1] } );
136 Returns the libraries set as children of this group or any subgroup.
141 my ( $self, $params ) = @_;
145 push (@libraries, $self->libraries->as_list);
146 my @children = $self->children->search({ branchcode => undef })->as_list;
147 foreach my $c (@children) {
148 push( @libraries, $c->all_libraries );
153 grep { !$seen{ $_->id }++ } @libraries;
158 =head3 libraries_not_direct_children
160 my $libraries = $group->libraries_not_direct_children();
162 Returns the libraries *not* set as direct children of this group
166 sub libraries_not_direct_children {
169 return $self->libraries( { invert => 1 } );
179 $self->created_on( dt_from_string() ) unless $self->in_storage();
181 return $self->SUPER::store(@_);
184 =head2 Internal methods
191 return 'LibraryGroup';
196 Kyle M Hall <kyle@bywatersolutions.com>