261b232d33981ecb30faa772ba9f36fae84bfed9
[koha.git] / Koha / Patron / Category.pm
1 package Koha::Patron::Category;
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 Carp;
21
22 use C4::Members::Messaging;
23
24 use Koha::Database;
25 use Koha::DateUtils;
26
27 use base qw(Koha::Object);
28
29 =head1 NAME
30
31 Koha::Patron;;Category - Koha Patron;;Category Object class
32
33 =head1 API
34
35 =head2 Class Methods
36
37 =cut
38
39 =head3 effective_BlockExpiredPatronOpacActions
40
41 my $BlockExpiredPatronOpacActions = $category->effective_BlockExpiredPatronOpacActions
42
43 Return the effective BlockExpiredPatronOpacActions value.
44
45 =cut
46
47 sub effective_BlockExpiredPatronOpacActions {
48     my( $self) = @_;
49     return C4::Context->preference('BlockExpiredPatronOpacActions') if $self->BlockExpiredPatronOpacActions == -1;
50     return $self->BlockExpiredPatronOpacActions
51 }
52
53 =head3 store
54
55 =cut
56
57 sub store {
58     my ($self) = @_;
59
60     $self->dateofbirthrequired(undef)
61       if not defined $self->dateofbirthrequired
62       or $self->dateofbirthrequired eq '';
63
64     $self->upperagelimit(undef)
65       if not defined $self->upperagelimit
66       or $self->upperagelimit eq '';
67
68     $self->checkprevcheckout('inherit')
69       unless defined $self->checkprevcheckout;
70
71     return $self->SUPER::store;
72 }
73
74 =head3 default_messaging
75
76 my $messaging = $category->default_messaging();
77
78 =cut
79
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(
86             {
87                 categorycode => $self->categorycode,
88                 message_name => $option->{message_name}
89             }
90         );
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,
96         };
97         foreach my $transport ( keys %{ $pref->{transports} } ) {
98             push @{ $brief_pref->{transports} }, { transport => $transport };
99         }
100         push @messaging, $brief_pref;
101     }
102     return \@messaging;
103 }
104
105 =head3 branch_limitations
106
107 my $limitations = $category->branch_limitations();
108
109 $category->branch_limitations( \@branchcodes );
110
111 =cut
112
113 sub branch_limitations {
114     my ( $self, $branchcodes ) = @_;
115
116     if ($branchcodes) {
117         return $self->replace_branch_limitations($branchcodes);
118     }
119     else {
120         return $self->get_branch_limitations();
121     }
122
123 }
124
125 =head3 get_branch_limitations
126
127 my $limitations = $category->get_branch_limitations();
128
129 =cut
130
131 sub get_branch_limitations {
132     my ($self) = @_;
133
134     my @branchcodes =
135       $self->_catb_resultset->search( { categorycode => $self->categorycode } )
136       ->get_column('branchcode')->all();
137
138     return \@branchcodes;
139 }
140
141 =head3 add_branch_limitation
142
143 $category->add_branch_limitation( $branchcode );
144
145 =cut
146
147 sub add_branch_limitation {
148     my ( $self, $branchcode ) = @_;
149
150     croak("No branchcode passed in!") unless $branchcode;
151
152     my $limitation = $self->_catb_resultset->update_or_create(
153         { categorycode => $self->categorycode, branchcode => $branchcode } );
154
155     return $limitation ? 1 : undef;
156 }
157
158 =head3 del_branch_limitation
159
160 $category->del_branch_limitation( $branchcode );
161
162 =cut
163
164 sub del_branch_limitation {
165     my ( $self, $branchcode ) = @_;
166
167     croak("No branchcode passed in!") unless $branchcode;
168
169     my $limitation =
170       $self->_catb_resultset->find(
171         { categorycode => $self->categorycode, branchcode => $branchcode } );
172
173     unless ($limitation) {
174         my $categorycode = $self->categorycode;
175         carp(
176 "No branch limit for branch $branchcode found for categorycode $categorycode to delete!"
177         );
178         return;
179     }
180
181     return $limitation->delete();
182 }
183
184 =head3 replace_branch_limitations
185
186 $category->replace_branch_limitations( \@branchcodes );
187
188 =cut
189
190 sub replace_branch_limitations {
191     my ( $self, $branchcodes ) = @_;
192
193     $self->_catb_resultset->search( { categorycode => $self->categorycode } )->delete;
194
195     my @return_values =
196       map { $self->add_branch_limitation($_) } @$branchcodes;
197
198     return \@return_values;
199 }
200
201 =head3 Koha::Objects->_catb_resultset
202
203 Returns the internal resultset or creates it if undefined
204
205 =cut
206
207 sub _catb_resultset {
208     my ($self) = @_;
209
210     $self->{_catb_resultset} ||=
211       Koha::Database->new->schema->resultset('CategoriesBranch');
212
213     return $self->{_catb_resultset};
214 }
215
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;
224     }
225 }
226
227 =head3 effective_reset_password
228
229 Returns if patrons in this category can reset their password. If set in $self->reset_password
230 or, if undef, falls back to the OpacResetPassword system preference.
231
232 =cut
233
234 sub effective_reset_password {
235     my ($self) = @_;
236
237     return ( defined $self->reset_password )
238         ? $self->reset_password
239         : C4::Context->preference('OpacResetPassword');
240 }
241
242 =head3 effective_change_password
243
244 Returns if patrons in this category can change their password. If set in $self->change_password
245 or, if undef, falls back to the OpacPasswordChange system preference.
246
247 =cut
248
249 sub effective_change_password {
250     my ($self) = @_;
251
252     return ( defined $self->change_password )
253         ? $self->change_password
254         : C4::Context->preference('OpacPasswordChange');
255 }
256
257 =head2 Internal methods
258
259 =head3 type
260
261 =cut
262
263 sub _type {
264     return 'Category';
265 }
266
267 1;