Bug 17359: Correct encoding when displaying patron import summary
[koha.git] / Koha / Object / Limit / Library.pm
1 package Koha::Object::Limit::Library;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 use Modern::Perl;
19
20 use Koha::Database;
21 use Koha::Exceptions;
22 use Koha::Libraries;
23
24 use Try::Tiny;
25
26 =head1 NAME
27
28 Koha::Object::Limit::Library - Generic library limit handling class
29
30 =head1 SYNOPSIS
31
32     use base qw(Koha::Object Koha::Object::Limit::Library);
33     my $object = Koha::Object->new({ property1 => $property1, property2 => $property2, etc... } );
34
35 =head1 DESCRIPTION
36
37 This class is provided as a generic way of handling library limits for Koha::Object-based classes
38 in Koha.
39
40 This class must always be subclassed.
41
42 =head1 API
43
44 =head2 Class Methods
45
46 =cut
47
48 =head3 library_limits
49
50 my $limits = $object->library_limits();
51
52 $object->library_limits( \@branchcodes );
53
54 Accessor method for library limits. When updating library limits, it accepts
55 a list of branchcodes. If requested to return the current library limits
56 it returns a Koha::Libraries object with the corresponding libraries.
57
58 =cut
59
60 sub library_limits {
61     my ( $self, $branchcodes ) = @_;
62
63     if ($branchcodes) {
64         return $self->replace_library_limits($branchcodes);
65     }
66     else {
67         return $self->get_library_limits();
68     }
69 }
70
71 =head3 get_library_limits
72
73 my $limits = $object->get_library_limits();
74
75 Returns the current library limits in the form of a Koha::Libraries iterator object.
76 It returns undef if no library limits defined.
77
78 =cut
79
80 sub get_library_limits {
81     my ($self) = @_;
82
83     my @branchcodes
84         = $self->_library_limit_rs->search(
85         { $self->_library_limits->{id} => $self->id } )
86         ->get_column( $self->_library_limits->{library} )->all();
87
88     return unless @branchcodes;
89
90     my $filter = [ map { { branchcode => $_ } } @branchcodes ];
91     my $libraries = Koha::Libraries->search( $filter );
92
93     return $libraries;
94 }
95
96 =head3 add_library_limit
97
98 $object->add_library_limit( $branchcode );
99
100 =cut
101
102 sub add_library_limit {
103     my ( $self, $branchcode ) = @_;
104
105     Koha::Exceptions::MissingParameter->throw(
106         "Required parameter 'branchcode' missing")
107         unless $branchcode;
108
109     try {
110         $self->_library_limit_rs->update_or_create(
111             {   $self->_library_limits->{id}      => $self->id,
112                 $self->_library_limits->{library} => $branchcode
113             }
114         );
115     }
116     catch {
117         Koha::Exceptions::CannotAddLibraryLimit->throw( $_->{msg} );
118     };
119
120     return $self;
121 }
122
123 =head3 del_library_limit
124
125 $object->del_library_limit( $branchcode );
126
127 =cut
128
129 sub del_library_limit {
130     my ( $self, $branchcode ) = @_;
131
132     Koha::Exceptions::MissingParameter->throw(
133         "Required parameter 'branchcode' missing")
134         unless $branchcode;
135
136     my $limitation = $self->_library_limit_rs->search(
137         {   $self->_library_limits->{id}      => $self->id,
138             $self->_library_limits->{library} => $branchcode
139         }
140     );
141
142     Koha::Exceptions::ObjectNotFound->throw(
143               "No branch limit for branch $branchcode found for id "
144             . $self->id
145             . " to delete!" )
146         unless ($limitation->count);
147
148     return $limitation->delete();
149 }
150
151 =head3 replace_library_limits
152
153 $object->replace_library_limits( \@branchcodes );
154
155 =cut
156
157 sub replace_library_limits {
158     my ( $self, $branchcodes ) = @_;
159
160     $self->_result->result_source->schema->txn_do(
161         sub {
162             $self->_library_limit_rs->search(
163                 { $self->_library_limits->{id} => $self->id } )->delete;
164
165             map { $self->add_library_limit($_) } @$branchcodes;
166         }
167     );
168
169     return $self;
170 }
171
172
173 =head3 Koha::Objects->_library_limit_rs
174
175 Returns the internal resultset for the branch limitation table or creates it if undefined
176
177 =cut
178
179 sub _library_limit_rs {
180     my ($self) = @_;
181
182     $self->{_library_limit_rs} ||= Koha::Database->new->schema->resultset(
183         $self->_library_limits->{class} );
184
185     return $self->{_library_limit_rs};
186 }
187
188 1;