Bug 18584: Our legacy code contains trailing-spaces
[koha.git] / Koha / Patron / Category.pm
blobc96764bfde3b3f239ebd57165051f6eb2976e91b
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 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.
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.
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.
18 use Modern::Perl;
20 use Carp;
22 use C4::Members::Messaging;
24 use Koha::Database;
25 use Koha::DateUtils;
27 use base qw(Koha::Object);
29 =head1 NAME
31 Koha::Patron;;Category - Koha Patron;;Category Object class
33 =head1 API
35 =head2 Class Methods
37 =cut
39 =head3 effective_BlockExpiredPatronOpacActions
41 my $BlockExpiredPatronOpacActions = $category->effective_BlockExpiredPatronOpacActions
43 Return the effective BlockExpiredPatronOpacActions value.
45 =cut
47 sub effective_BlockExpiredPatronOpacActions {
48 my( $self) = @_;
49 return C4::Context->preference('BlockExpiredPatronOpacActions') if $self->BlockExpiredPatronOpacActions == -1;
50 return $self->BlockExpiredPatronOpacActions
53 =head3 store
55 =cut
57 sub store {
58 my ($self) = @_;
60 $self->dateofbirthrequired(undef)
61 if not defined $self->dateofbirthrequired
62 or $self->dateofbirthrequired eq '';
64 $self->upperagelimit(undef)
65 if not defined $self->upperagelimit
66 or $self->upperagelimit eq '';
68 $self->checkprevcheckout('inherit')
69 unless defined $self->checkprevcheckout;
71 return $self->SUPER::store;
74 =head3 default_messaging
76 my $messaging = $category->default_messaging();
78 =cut
80 sub default_messaging {
81 my ( $self ) = @_;
82 my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
83 my @messaging;
84 foreach my $option (@$messaging_options) {
85 my $pref = C4::Members::Messaging::GetMessagingPreferences(
87 categorycode => $self->categorycode,
88 message_name => $option->{message_name}
91 next unless $pref->{transports};
92 my $brief_pref = {
93 message_attribute_id => $option->{message_attribute_id},
94 message_name => $option->{message_name},
95 $option->{'message_name'} => 1,
97 foreach my $transport ( keys %{ $pref->{transports} } ) {
98 push @{ $brief_pref->{transports} }, { transport => $transport };
100 push @messaging, $brief_pref;
102 return \@messaging;
105 =head3 branch_limitations
107 my $limitations = $category->branch_limitations();
109 $category->branch_limitations( \@branchcodes );
111 =cut
113 sub branch_limitations {
114 my ( $self, $branchcodes ) = @_;
116 if ($branchcodes) {
117 return $self->replace_branch_limitations($branchcodes);
119 else {
120 return $self->get_branch_limitations();
125 =head3 get_branch_limitations
127 my $limitations = $category->get_branch_limitations();
129 =cut
131 sub get_branch_limitations {
132 my ($self) = @_;
134 my @branchcodes =
135 $self->_catb_resultset->search( { categorycode => $self->categorycode } )
136 ->get_column('branchcode')->all();
138 return \@branchcodes;
141 =head3 add_branch_limitation
143 $category->add_branch_limitation( $branchcode );
145 =cut
147 sub add_branch_limitation {
148 my ( $self, $branchcode ) = @_;
150 croak("No branchcode passed in!") unless $branchcode;
152 my $limitation = $self->_catb_resultset->update_or_create(
153 { categorycode => $self->categorycode, branchcode => $branchcode } );
155 return $limitation ? 1 : undef;
158 =head3 del_branch_limitation
160 $category->del_branch_limitation( $branchcode );
162 =cut
164 sub del_branch_limitation {
165 my ( $self, $branchcode ) = @_;
167 croak("No branchcode passed in!") unless $branchcode;
169 my $limitation =
170 $self->_catb_resultset->find(
171 { categorycode => $self->categorycode, branchcode => $branchcode } );
173 unless ($limitation) {
174 my $categorycode = $self->categorycode;
175 carp(
176 "No branch limit for branch $branchcode found for categorycode $categorycode to delete!"
178 return;
181 return $limitation->delete();
184 =head3 replace_branch_limitations
186 $category->replace_branch_limitations( \@branchcodes );
188 =cut
190 sub replace_branch_limitations {
191 my ( $self, $branchcodes ) = @_;
193 $self->_catb_resultset->search( { categorycode => $self->categorycode } )->delete;
195 my @return_values =
196 map { $self->add_branch_limitation($_) } @$branchcodes;
198 return \@return_values;
201 =head3 Koha::Objects->_catb_resultset
203 Returns the internal resultset or creates it if undefined
205 =cut
207 sub _catb_resultset {
208 my ($self) = @_;
210 $self->{_catb_resultset} ||=
211 Koha::Database->new->schema->resultset('CategoriesBranch');
213 return $self->{_catb_resultset};
216 sub get_expiry_date {
217 my ($self, $date ) = @_;
218 if ( $self->enrolmentperiod ) {
219 $date ||= dt_from_string;
220 $date = dt_from_string( $date ) unless ref $date;
221 return $date->add( months => $self->enrolmentperiod, end_of_month => 'limit' );
222 } else {
223 return $self->enrolmentperioddate;
227 =head3 type
229 =cut
231 sub _type {
232 return 'Category';