cd538995a0363ee3406af753a65d11a0eb62f4a0
[koha_ffzg] / 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 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.
11 #
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.
15 #
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.
19
20 use Modern::Perl;
21
22 use Carp;
23
24 use Koha::Database;
25 use Koha::DateUtils qw(dt_from_string);
26 use Koha::Libraries;
27
28 use base qw(Koha::Object);
29
30 =head1 NAME
31
32 Koha::Library::Group - Koha Library::Group object class
33
34 =head1 API
35
36 =head2 Class Methods
37
38 =cut
39
40 =head3 my @parent = $self->parent()
41
42 =cut
43
44 sub parent {
45     my ($self) = @_;
46
47     $self->{_parent} ||= Koha::Library::Groups->find( $self->parent_id );
48
49     return $self->{_parent};
50 }
51
52 =head3 my @children = $self->children()
53
54 =cut
55
56 sub children {
57     my ($self) = @_;
58
59     my $children =
60       Koha::Library::Groups->search( { parent_id => $self->id }, { order_by => [ 'title', 'branchcode' ] } );
61
62     return $children;
63 }
64
65 =head3 library
66
67 my $library = $group->library();
68
69 Returns the library for this group if one exists
70
71 =cut
72
73 sub library {
74     my ($self) = @_;
75
76     return unless $self->branchcode;
77
78     $self->{_library} ||= Koha::Libraries->find( $self->branchcode );
79
80     return $self->{_library};
81 }
82
83 =head3 libraries_not_direct_children
84
85 my @libraries = $group->libraries_not_direct_children();
86
87 Returns the libraries *not* set as direct children of this group
88
89 =cut
90
91 sub libraries_not_direct_children {
92     my ($self) = @_;
93
94     my @children = Koha::Library::Groups->search(
95         {
96             parent_id  => $self->id,
97             branchcode => { '!=' => undef },
98         },
99         { order_by => 'branchcode' }
100     );
101
102     my @branchcodes = map { $_->branchcode } @children;
103
104     return Koha::Libraries->search(
105         {
106             branchcode => { -not_in => \@branchcodes }
107         },
108         {
109             order_by => 'branchname'
110         }
111     );
112 }
113
114 =head3 store
115
116 =cut
117
118 sub store {
119     my ($self) = @_;
120
121     my $now = dt_from_string;
122     $self->updated_on($now);
123     $self->created_on($now) unless $self->in_storage();
124
125     return $self->SUPER::store(@_);
126 }
127
128 =head3 type
129
130 =cut
131
132 sub _type {
133     return 'LibraryGroup';
134 }
135
136 =head1 AUTHOR
137
138 Kyle M Hall <kyle@bywatersolutions.com>
139
140 =cut
141
142 1;