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>.
27 use C4::Form::MessagingPreferences;
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' },
50 if ( $op eq 'add_form' ) {
53 category => scalar Koha::Patron::Categories->find($categorycode),
56 if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
57 C4::Form::MessagingPreferences::set_form_values(
58 { categorycode => $categorycode }, $template );
61 elsif ( $op eq 'add_validate' ) {
63 my $categorycode = $input->param('categorycode');
64 my $description = $input->param('description');
65 my $enrolmentperiod = $input->param('enrolmentperiod');
66 my $enrolmentperioddate = $input->param('enrolmentperioddate') || 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->upperagelimit($upperagelimit);
108 $category->dateofbirthrequired($dateofbirthrequired);
109 $category->enrolmentfee($enrolmentfee);
110 $category->reservefee($reservefee);
111 $category->hidelostitems($hidelostitems);
112 $category->overduenoticerequired($overduenoticerequired);
113 $category->category_type($category_type);
114 $category->BlockExpiredPatronOpacActions($BlockExpiredPatronOpacActions);
115 $category->checkprevcheckout($checkPrevCheckout);
116 $category->default_privacy($default_privacy);
117 $category->reset_password($reset_password);
118 $category->change_password($change_password);
119 $category->exclude_from_local_holds_priority($exclude_from_local_holds_priority);
120 $category->min_password_length($min_password_length);
121 $category->require_strong_password($require_strong_password);
124 $category->replace_library_limits( \@branches );
127 push @messages, {type => 'error', code => 'error_on_update' };
129 push @messages, { type => 'message', code => 'success_on_update' };
133 my $category = Koha::Patron::Category->new({
134 categorycode => $categorycode,
135 description => $description,
136 enrolmentperiod => $enrolmentperiod,
137 enrolmentperioddate => $enrolmentperioddate,
138 upperagelimit => $upperagelimit,
139 dateofbirthrequired => $dateofbirthrequired,
140 enrolmentfee => $enrolmentfee,
141 reservefee => $reservefee,
142 hidelostitems => $hidelostitems,
143 overduenoticerequired => $overduenoticerequired,
144 category_type => $category_type,
145 BlockExpiredPatronOpacActions => $BlockExpiredPatronOpacActions,
146 checkprevcheckout => $checkPrevCheckout,
147 default_privacy => $default_privacy,
148 reset_password => $reset_password,
149 change_password => $change_password,
150 exclude_from_local_holds_priority => $exclude_from_local_holds_priority,
151 min_password_length => $min_password_length,
152 require_strong_password => $require_strong_password,
156 $category->replace_library_limits( \@branches );
160 push @messages, { type => 'error', code => 'error_on_insert' };
162 push @messages, { type => 'message', code => 'success_on_insert' };
166 if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
167 C4::Form::MessagingPreferences::handle_form_action( $input,
168 { categorycode => scalar $input->param('categorycode') }, $template );
174 elsif ( $op eq 'delete_confirm' ) {
176 my $count = Koha::Patrons->search({
177 categorycode => $categorycode
180 my $category = Koha::Patron::Categories->find($categorycode);
183 category => $category,
184 patrons_in_category => $count,
188 elsif ( $op eq 'delete_confirmed' ) {
189 my $categorycode = uc( $input->param('categorycode') );
191 my $category = Koha::Patron::Categories->find( $categorycode );
192 my $deleted = eval { $category->delete; };
194 if ( $@ or not $deleted ) {
195 push @messages, {type => 'error', code => 'error_on_delete' };
197 push @messages, { type => 'message', code => 'success_on_delete' };
203 if ( $op eq 'list' ) {
204 my $categories = Koha::Patron::Categories->search(
206 description => { -like => "$searchfield%" }
209 order_by => ['category_type', 'description', 'categorycode' ]
214 categories => $categories,
219 categorycode => $categorycode,
220 searchfield => $searchfield,
221 messages => \@messages,
225 output_html_with_http_headers $input, $cookie, $template->output;