3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2002 Paul Poulain
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
25 use C4::Auth qw( get_template_and_user );
26 use C4::Output qw( output_html_with_http_headers );
27 use C4::Form::MessagingPreferences;
30 use Koha::DateUtils qw( dt_from_string output_pref );
31 use Koha::Patron::Categories;
35 my $searchfield = $input->param('description') // q||;
36 my $categorycode = $input->param('categorycode');
37 my $op = $input->param('op') // 'list';
40 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
42 template_name => "admin/categories.tt",
45 flagsrequired => { parameters => 'manage_patron_categories' },
49 if ( $op eq 'add_form' ) {
52 category => scalar Koha::Patron::Categories->find($categorycode),
55 if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
56 C4::Form::MessagingPreferences::set_form_values(
57 { categorycode => $categorycode }, $template );
60 elsif ( $op eq 'add_validate' ) {
62 my $categorycode = $input->param('categorycode');
63 my $description = $input->param('description');
64 my $enrolmentperiod = $input->param('enrolmentperiod');
65 my $enrolmentperioddate = $input->param('enrolmentperioddate') || undef;
66 my $password_expiry_days = $input->param('password_expiry_days') || undef;
67 my $upperagelimit = $input->param('upperagelimit');
68 my $dateofbirthrequired = $input->param('dateofbirthrequired');
69 my $enrolmentfee = $input->param('enrolmentfee');
70 my $reservefee = $input->param('reservefee');
71 my $hidelostitems = $input->param('hidelostitems');
72 my $overduenoticerequired = $input->param('overduenoticerequired');
73 my $category_type = $input->param('category_type');
74 my $BlockExpiredPatronOpacActions = $input->param('BlockExpiredPatronOpacActions');
75 my $checkPrevCheckout = $input->param('checkprevcheckout');
76 my $default_privacy = $input->param('default_privacy');
77 my $reset_password = $input->param('reset_password');
78 my $change_password = $input->param('change_password');
79 my $exclude_from_local_holds_priority = $input->param('exclude_from_local_holds_priority');
80 my $min_password_length = $input->param('min_password_length');
81 my $require_strong_password = $input->param('require_strong_password');
82 my @branches = grep { $_ ne q{} } $input->multi_param('branches');
83 my $can_be_guarantee = $input->param('can_be_guarantee');
85 $reset_password = undef if $reset_password eq -1;
86 $change_password = undef if $change_password eq -1;
87 $min_password_length = undef unless length($min_password_length);
88 $require_strong_password = undef if $require_strong_password eq -1;
90 my $is_a_modif = $input->param("is_a_modif");
92 if ($enrolmentperioddate) {
93 $enrolmentperioddate = output_pref(
95 dt => dt_from_string($enrolmentperioddate),
103 my $category = Koha::Patron::Categories->find( $categorycode );
104 $category->categorycode($categorycode);
105 $category->description($description);
106 $category->enrolmentperiod($enrolmentperiod);
107 $category->enrolmentperioddate($enrolmentperioddate);
108 $category->password_expiry_days($password_expiry_days);
109 $category->upperagelimit($upperagelimit);
110 $category->dateofbirthrequired($dateofbirthrequired);
111 $category->enrolmentfee($enrolmentfee);
112 $category->reservefee($reservefee);
113 $category->hidelostitems($hidelostitems);
114 $category->overduenoticerequired($overduenoticerequired);
115 $category->category_type($category_type);
116 $category->can_be_guarantee($can_be_guarantee);
117 $category->BlockExpiredPatronOpacActions($BlockExpiredPatronOpacActions);
118 $category->checkprevcheckout($checkPrevCheckout);
119 $category->default_privacy($default_privacy);
120 $category->reset_password($reset_password);
121 $category->change_password($change_password);
122 $category->exclude_from_local_holds_priority($exclude_from_local_holds_priority);
123 $category->min_password_length($min_password_length);
124 $category->require_strong_password($require_strong_password);
127 $category->replace_library_limits( \@branches );
130 push @messages, {type => 'error', code => 'error_on_update' };
132 push @messages, { type => 'message', code => 'success_on_update' };
136 my $category = Koha::Patron::Category->new({
137 categorycode => $categorycode,
138 description => $description,
139 enrolmentperiod => $enrolmentperiod,
140 enrolmentperioddate => $enrolmentperioddate,
141 password_expiry_days => $password_expiry_days,
142 upperagelimit => $upperagelimit,
143 dateofbirthrequired => $dateofbirthrequired,
144 enrolmentfee => $enrolmentfee,
145 reservefee => $reservefee,
146 hidelostitems => $hidelostitems,
147 overduenoticerequired => $overduenoticerequired,
148 category_type => $category_type,
149 can_be_guarantee => $can_be_guarantee,
150 BlockExpiredPatronOpacActions => $BlockExpiredPatronOpacActions,
151 checkprevcheckout => $checkPrevCheckout,
152 default_privacy => $default_privacy,
153 reset_password => $reset_password,
154 change_password => $change_password,
155 exclude_from_local_holds_priority => $exclude_from_local_holds_priority,
156 min_password_length => $min_password_length,
157 require_strong_password => $require_strong_password,
161 $category->replace_library_limits( \@branches );
165 push @messages, { type => 'error', code => 'error_on_insert' };
167 push @messages, { type => 'message', code => 'success_on_insert' };
171 if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
172 C4::Form::MessagingPreferences::handle_form_action( $input,
173 { categorycode => scalar $input->param('categorycode') }, $template );
179 elsif ( $op eq 'delete_confirm' ) {
181 my $count = Koha::Patrons->search({
182 categorycode => $categorycode
185 my $category = Koha::Patron::Categories->find($categorycode);
188 category => $category,
189 patrons_in_category => $count,
193 elsif ( $op eq 'delete_confirmed' ) {
194 my $categorycode = uc( $input->param('categorycode') );
196 my $category = Koha::Patron::Categories->find( $categorycode );
197 my $deleted = eval { $category->delete; };
199 if ( $@ or not $deleted ) {
200 push @messages, {type => 'error', code => 'error_on_delete' };
202 push @messages, { type => 'message', code => 'success_on_delete' };
208 if ( $op eq 'list' ) {
209 my $categories = Koha::Patron::Categories->search(
211 description => { -like => "$searchfield%" }
214 order_by => ['category_type', 'description', 'categorycode' ]
219 categories => $categories,
224 categorycode => $categorycode,
225 searchfield => $searchfield,
226 messages => \@messages,
230 output_html_with_http_headers $input, $cookie, $template->output;