713dcf924970c0733596c371f14e46178895e04a
[srvgit] / 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
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Carp;
21 use List::MoreUtils qw(any);
22
23 use C4::Members::Messaging;
24
25 use Koha::Database;
26 use Koha::DateUtils;
27
28 use base qw(Koha::Object Koha::Object::Limit::Library);
29
30 =head1 NAME
31
32 Koha::Patron;;Category - Koha Patron;;Category Object class
33
34 =head1 API
35
36 =head2 Class Methods
37
38 =cut
39
40 =head3 effective_BlockExpiredPatronOpacActions
41
42 my $BlockExpiredPatronOpacActions = $category->effective_BlockExpiredPatronOpacActions
43
44 Return the effective BlockExpiredPatronOpacActions value.
45
46 =cut
47
48 sub effective_BlockExpiredPatronOpacActions {
49     my( $self) = @_;
50     return C4::Context->preference('BlockExpiredPatronOpacActions') if $self->BlockExpiredPatronOpacActions == -1;
51     return $self->BlockExpiredPatronOpacActions
52 }
53
54 =head3 store
55
56 =cut
57
58 sub store {
59     my ($self) = @_;
60
61     $self->dateofbirthrequired(undef)
62       if not defined $self->dateofbirthrequired
63       or $self->dateofbirthrequired eq '';
64
65     $self->upperagelimit(undef)
66       if not defined $self->upperagelimit
67       or $self->upperagelimit eq '';
68
69     $self->checkprevcheckout('inherit')
70       unless defined $self->checkprevcheckout;
71
72     return $self->SUPER::store;
73 }
74
75 =head3 default_messaging
76
77 my $messaging = $category->default_messaging();
78
79 =cut
80
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(
87             {
88                 categorycode => $self->categorycode,
89                 message_name => $option->{message_name}
90             }
91         );
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,
97         };
98         foreach my $transport ( keys %{ $pref->{transports} } ) {
99             push @{ $brief_pref->{transports} }, { transport => $transport };
100         }
101         push @messaging, $brief_pref;
102     }
103     return \@messaging;
104 }
105
106 sub get_expiry_date {
107     my ($self, $date ) = @_;
108     if ( $self->enrolmentperiod ) {
109         $date ||= dt_from_string;
110         $date = dt_from_string( $date ) unless ref $date;
111         return $date->add( months => $self->enrolmentperiod, end_of_month => 'limit' );
112     } else {
113         return $self->enrolmentperioddate;
114     }
115 }
116
117 =head3 effective_reset_password
118
119 Returns if patrons in this category can reset their password. If set in $self->reset_password
120 or, if undef, falls back to the OpacResetPassword system preference.
121
122 =cut
123
124 sub effective_reset_password {
125     my ($self) = @_;
126
127     return $self->reset_password // C4::Context->preference('OpacResetPassword');
128 }
129
130 =head3 effective_change_password
131
132 Returns if patrons in this category can change their password. If set in $self->change_password
133 or, if undef, falls back to the OpacPasswordChange system preference.
134
135 =cut
136
137 sub effective_change_password {
138     my ($self) = @_;
139
140     return $self->change_password // C4::Context->preference('OpacPasswordChange');
141 }
142
143 =head3 effective_min_password_length
144
145     $category->effective_min_password_length()
146
147 Retrieve category's password length if set, or minPasswordLength otherwise
148
149 =cut
150
151 sub effective_min_password_length {
152     my ($self) = @_;
153
154     return $self->min_password_length // C4::Context->preference('minPasswordLength');
155 }
156
157 =head3 effective_require_strong_password
158
159     $category->effective_require_strong_password()
160
161 Retrieve category's password strength if set, or RequireStrongPassword otherwise
162
163 =cut
164
165 sub effective_require_strong_password {
166     my ($self) = @_;
167
168     return $self->require_strong_password // C4::Context->preference('RequireStrongPassword');
169 }
170
171 =head3 override_hidden_items
172
173     if ( $patron->category->override_hidden_items ) {
174         ...
175     }
176
177 Returns a boolean that if patrons of this category are exempt from the OPACHiddenItems policies
178
179 TODO: Remove on bug 22547
180
181 =cut
182
183 sub override_hidden_items {
184     my ($self) = @_;
185     return any { $_ eq $self->categorycode }
186     split( /\|/, C4::Context->preference('OpacHiddenItemsExceptions') );
187 }
188
189 =head2 Internal methods
190
191 =head3 _library_limits
192
193  configure library limits
194
195 =cut
196
197 sub _library_limits {
198     return {
199         class => "CategoriesBranch",
200         id => "categorycode",
201         library => "branchcode",
202     };
203 }
204
205 =head3 type
206
207 =cut
208
209 sub _type {
210     return 'Category';
211 }
212
213 1;