From 82287f842b34128d023c19d03d3862eb9ba4cf6d Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Sun, 31 Jan 2016 11:22:19 +0000 Subject: [PATCH] Bug 15707 - Add new modules --- Koha/Library/Group.pm | 129 +++++++++++++++++++++++++++++++++++++++++++++++++ Koha/Library/Groups.pm | 109 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 238 insertions(+) create mode 100644 Koha/Library/Group.pm create mode 100644 Koha/Library/Groups.pm diff --git a/Koha/Library/Group.pm b/Koha/Library/Group.pm new file mode 100644 index 0000000..142992b --- /dev/null +++ b/Koha/Library/Group.pm @@ -0,0 +1,129 @@ +package Koha::Library::Group; + +# Copyright ByWater Solutions 2016 +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Carp; + +use Koha::Database; + +use Koha::Libraries; + +use base qw(Koha::Object); + +=head1 NAME + +Koha::Library::Group - Koha Library::Group object class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 my @children = $self->get_children() + +=cut + +sub children { + my ($self) = @_; + + my $children = + Koha::Library::Groups->search( { parent_id => $self->id }, { order_by => [ 'title', 'branchcode' ] } ); + + return $children; +} + +=head3 library + +my $library = $group->library(); + +Returns the library for this group if one exists + +=cut + +sub library { + my ($self) = @_; + + return unless $self->branchcode; + + return Koha::Libraries->find( $self->branchcode ); +} + +=head3 libraries + +my @libraries = $group->libraries(); + +Returns the libraries set as direct children of this group + +=cut + +sub libraries { + my ($self) = @_; + + my @children = Koha::Library::Groups->search( + { + parent_id => $self->id, + branchcode => { '!=' => undef }, + }, + { order_by => 'branchcode' } + ); + + my @libraries = map { $_->library() } @children; +} + +=head3 libraries_not_direct_children + +my @libraries = $group->libraries_not_direct_children(); + +Returns the libraries set as direct children of this group + +=cut + +sub libraries_not_direct_children { + my ($self) = @_; + + my @children = Koha::Library::Groups->search( + { + parent_id => $self->id, + branchcode => { '!=' => undef }, + }, + { order_by => 'branchcode' } + ); + + my @branchcodes = map { $_->branchcode } @children; + + return Koha::Libraries->search( { branchcode => { -not_in => \@branchcodes } } ); +} + +=head3 type + +=cut + +sub type { + return 'LibraryGroup'; +} + +=head1 AUTHOR + +Kyle M Hall + +=cut + +1; diff --git a/Koha/Library/Groups.pm b/Koha/Library/Groups.pm new file mode 100644 index 0000000..32f8291 --- /dev/null +++ b/Koha/Library/Groups.pm @@ -0,0 +1,109 @@ +package Koha::Library::Groups; + +# Copyright ByWater Solutions 2016 +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Carp; + +use Koha::Database; + +use Koha::Library::Group; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::Library::Groups - Koha Library::Group object set class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 my $tree_hashref = $self->get_tree() + +=cut + +sub get_tree { + my ( $self ) = @_; + + my $root_group = $self->get_root_group(); + + my $tree; + + $tree->{ $root_group->id }->{object} = $root_group; + $tree->{ $root_group->id }->{children} = $root_group->_sub_tree( $root_group ); + + return $tree; +} + +=head3 my $tree_hashref = $self->_sub_tree() + +=cut + +sub _sub_tree { + my ( $self, $parent ) = @_; + + my @children = $parent->children(); + + my $subtree; + + foreach my $child ( @children ) { + $subtree->{ $child->id }->{object} = $child; + $subtree->{ $child->id }->{children} = $child->sub_tree($parent); + } + + return $subtree; + +} + +=head3 my @root_groups = $self->get_root_group() + +=cut + +sub get_root_groups { + my ( $self ) = @_; + + return $self->search( { parent_id => undef }, { order_by => 'title' } ); +} + +=head3 type + +=cut + +sub type { + return 'LibraryGroup'; +} + +=head3 object_class + +=cut + +sub object_class { + return 'Koha::Library::Group'; +} + +=head1 AUTHOR + +Kyle M Hall + +=cut + +1; -- 2.1.4