Bug 26922: Regression tests
[koha.git] / Koha / Patron / Category.pm
blob7a4541ebc4cc85d1513ab79f480b86e2b1f7526e
1 package Koha::Patron::Category;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18 use Modern::Perl;
20 use Carp;
21 use List::MoreUtils qw(any);
23 use C4::Members::Messaging;
25 use Koha::Database;
26 use Koha::DateUtils;
28 use base qw(Koha::Object);
30 =head1 NAME
32 Koha::Patron;;Category - Koha Patron;;Category Object class
34 =head1 API
36 =head2 Class Methods
38 =cut
40 =head3 effective_BlockExpiredPatronOpacActions
42 my $BlockExpiredPatronOpacActions = $category->effective_BlockExpiredPatronOpacActions
44 Return the effective BlockExpiredPatronOpacActions value.
46 =cut
48 sub effective_BlockExpiredPatronOpacActions {
49 my( $self) = @_;
50 return C4::Context->preference('BlockExpiredPatronOpacActions') if $self->BlockExpiredPatronOpacActions == -1;
51 return $self->BlockExpiredPatronOpacActions
54 =head3 store
56 =cut
58 sub store {
59 my ($self) = @_;
61 $self->dateofbirthrequired(undef)
62 if not defined $self->dateofbirthrequired
63 or $self->dateofbirthrequired eq '';
65 $self->upperagelimit(undef)
66 if not defined $self->upperagelimit
67 or $self->upperagelimit eq '';
69 $self->checkprevcheckout('inherit')
70 unless defined $self->checkprevcheckout;
72 return $self->SUPER::store;
75 =head3 default_messaging
77 my $messaging = $category->default_messaging();
79 =cut
81 sub default_messaging {
82 my ( $self ) = @_;
83 my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
84 my @messaging;
85 foreach my $option (@$messaging_options) {
86 my $pref = C4::Members::Messaging::GetMessagingPreferences(
88 categorycode => $self->categorycode,
89 message_name => $option->{message_name}
92 next unless $pref->{transports};
93 my $brief_pref = {
94 message_attribute_id => $option->{message_attribute_id},
95 message_name => $option->{message_name},
96 $option->{'message_name'} => 1,
98 foreach my $transport ( keys %{ $pref->{transports} } ) {
99 push @{ $brief_pref->{transports} }, { transport => $transport };
101 push @messaging, $brief_pref;
103 return \@messaging;
106 =head3 branch_limitations
108 my $limitations = $category->branch_limitations();
110 $category->branch_limitations( \@branchcodes );
112 =cut
114 sub branch_limitations {
115 my ( $self, $branchcodes ) = @_;
117 if ($branchcodes) {
118 return $self->replace_branch_limitations($branchcodes);
120 else {
121 return $self->get_branch_limitations();
126 =head3 get_branch_limitations
128 my $limitations = $category->get_branch_limitations();
130 =cut
132 sub get_branch_limitations {
133 my ($self) = @_;
135 my @branchcodes =
136 $self->_catb_resultset->search( { categorycode => $self->categorycode } )
137 ->get_column('branchcode')->all();
139 return \@branchcodes;
142 =head3 add_branch_limitation
144 $category->add_branch_limitation( $branchcode );
146 =cut
148 sub add_branch_limitation {
149 my ( $self, $branchcode ) = @_;
151 croak("No branchcode passed in!") unless $branchcode;
153 my $limitation = $self->_catb_resultset->update_or_create(
154 { categorycode => $self->categorycode, branchcode => $branchcode } );
156 return $limitation ? 1 : undef;
159 =head3 del_branch_limitation
161 $category->del_branch_limitation( $branchcode );
163 =cut
165 sub del_branch_limitation {
166 my ( $self, $branchcode ) = @_;
168 croak("No branchcode passed in!") unless $branchcode;
170 my $limitation =
171 $self->_catb_resultset->find(
172 { categorycode => $self->categorycode, branchcode => $branchcode } );
174 unless ($limitation) {
175 my $categorycode = $self->categorycode;
176 carp(
177 "No branch limit for branch $branchcode found for categorycode $categorycode to delete!"
179 return;
182 return $limitation->delete();
185 =head3 replace_branch_limitations
187 $category->replace_branch_limitations( \@branchcodes );
189 =cut
191 sub replace_branch_limitations {
192 my ( $self, $branchcodes ) = @_;
194 $self->_catb_resultset->search( { categorycode => $self->categorycode } )->delete;
196 my @return_values =
197 map { $self->add_branch_limitation($_) } @$branchcodes;
199 return \@return_values;
202 =head3 Koha::Objects->_catb_resultset
204 Returns the internal resultset or creates it if undefined
206 =cut
208 sub _catb_resultset {
209 my ($self) = @_;
211 $self->{_catb_resultset} ||=
212 Koha::Database->new->schema->resultset('CategoriesBranch');
214 return $self->{_catb_resultset};
217 sub get_expiry_date {
218 my ($self, $date ) = @_;
219 if ( $self->enrolmentperiod ) {
220 $date ||= dt_from_string;
221 $date = dt_from_string( $date ) unless ref $date;
222 return $date->add( months => $self->enrolmentperiod, end_of_month => 'limit' );
223 } else {
224 return $self->enrolmentperioddate;
228 =head3 effective_reset_password
230 Returns if patrons in this category can reset their password. If set in $self->reset_password
231 or, if undef, falls back to the OpacResetPassword system preference.
233 =cut
235 sub effective_reset_password {
236 my ($self) = @_;
238 return $self->reset_password // C4::Context->preference('OpacResetPassword');
241 =head3 effective_change_password
243 Returns if patrons in this category can change their password. If set in $self->change_password
244 or, if undef, falls back to the OpacPasswordChange system preference.
246 =cut
248 sub effective_change_password {
249 my ($self) = @_;
251 return $self->change_password // C4::Context->preference('OpacPasswordChange');
254 =head3 effective_min_password_length
256 $category->effective_min_password_length()
258 Retrieve category's password length if set, or minPasswordLength otherwise
260 =cut
262 sub effective_min_password_length {
263 my ($self) = @_;
265 return $self->min_password_length // C4::Context->preference('minPasswordLength');
268 =head3 effective_require_strong_password
270 $category->effective_require_strong_password()
272 Retrieve category's password strength if set, or RequireStrongPassword otherwise
274 =cut
276 sub effective_require_strong_password {
277 my ($self) = @_;
279 return $self->require_strong_password // C4::Context->preference('RequireStrongPassword');
282 =head3 override_hidden_items
284 if ( $patron->category->override_hidden_items ) {
288 Returns a boolean that if patrons of this category are exempt from the OPACHiddenItems policies
290 TODO: Remove on bug 22547
292 =cut
294 sub override_hidden_items {
295 my ($self) = @_;
296 return any { $_ eq $self->categorycode }
297 split( /\|/, C4::Context->preference('OpacHiddenItemsExceptions') );
300 =head2 Internal methods
302 =head3 type
304 =cut
306 sub _type {
307 return 'Category';