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');
84 $reset_password = undef if $reset_password eq -1;
85 $change_password = undef if $change_password eq -1;
86 $min_password_length = undef unless length($min_password_length);
87 $require_strong_password = undef if $require_strong_password eq -1;
89 my $is_a_modif = $input->param("is_a_modif");
91 if ($enrolmentperioddate) {
92 $enrolmentperioddate = output_pref(
94 dt => dt_from_string($enrolmentperioddate),
102 my $category = Koha::Patron::Categories->find( $categorycode );
103 $category->categorycode($categorycode);
104 $category->description($description);
105 $category->enrolmentperiod($enrolmentperiod);
106 $category->enrolmentperioddate($enrolmentperioddate);
107 $category->password_expiry_days($password_expiry_days);
108 $category->upperagelimit($upperagelimit);
109 $category->dateofbirthrequired($dateofbirthrequired);
110 $category->enrolmentfee($enrolmentfee);
111 $category->reservefee($reservefee);
112 $category->hidelostitems($hidelostitems);
113 $category->overduenoticerequired($overduenoticerequired);
114 $category->category_type($category_type);
115 $category->BlockExpiredPatronOpacActions($BlockExpiredPatronOpacActions);
116 $category->checkprevcheckout($checkPrevCheckout);
117 $category->default_privacy($default_privacy);
118 $category->reset_password($reset_password);
119 $category->change_password($change_password);
120 $category->exclude_from_local_holds_priority($exclude_from_local_holds_priority);
121 $category->min_password_length($min_password_length);
122 $category->require_strong_password($require_strong_password);
125 $category->replace_library_limits( \@branches );
128 push @messages, {type => 'error', code => 'error_on_update' };
130 push @messages, { type => 'message', code => 'success_on_update' };
134 my $category = Koha::Patron::Category->new({
135 categorycode => $categorycode,
136 description => $description,
137 enrolmentperiod => $enrolmentperiod,
138 enrolmentperioddate => $enrolmentperioddate,
139 password_expiry_days => $password_expiry_days,
140 upperagelimit => $upperagelimit,
141 dateofbirthrequired => $dateofbirthrequired,
142 enrolmentfee => $enrolmentfee,
143 reservefee => $reservefee,
144 hidelostitems => $hidelostitems,
145 overduenoticerequired => $overduenoticerequired,
146 category_type => $category_type,
147 BlockExpiredPatronOpacActions => $BlockExpiredPatronOpacActions,
148 checkprevcheckout => $checkPrevCheckout,
149 default_privacy => $default_privacy,
150 reset_password => $reset_password,
151 change_password => $change_password,
152 exclude_from_local_holds_priority => $exclude_from_local_holds_priority,
153 min_password_length => $min_password_length,
154 require_strong_password => $require_strong_password,
158 $category->replace_library_limits( \@branches );
162 push @messages, { type => 'error', code => 'error_on_insert' };
164 push @messages, { type => 'message', code => 'success_on_insert' };
168 if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
169 C4::Form::MessagingPreferences::handle_form_action( $input,
170 { categorycode => scalar $input->param('categorycode') }, $template );
176 elsif ( $op eq 'delete_confirm' ) {
178 my $count = Koha::Patrons->search({
179 categorycode => $categorycode
182 my $category = Koha::Patron::Categories->find($categorycode);
185 category => $category,
186 patrons_in_category => $count,
190 elsif ( $op eq 'delete_confirmed' ) {
191 my $categorycode = uc( $input->param('categorycode') );
193 my $category = Koha::Patron::Categories->find( $categorycode );
194 my $deleted = eval { $category->delete; };
196 if ( $@ or not $deleted ) {
197 push @messages, {type => 'error', code => 'error_on_delete' };
199 push @messages, { type => 'message', code => 'success_on_delete' };
205 if ( $op eq 'list' ) {
206 my $categories = Koha::Patron::Categories->search(
208 description => { -like => "$searchfield%" }
211 order_by => ['category_type', 'description', 'categorycode' ]
216 categories => $categories,
221 categorycode => $categorycode,
222 searchfield => $searchfield,
223 messages => \@messages,
227 output_html_with_http_headers $input, $cookie, $template->output;