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' ) {
51 my ( $category, $selected_branches );
53 $category = Koha::Patron::Categories->find($categorycode);
54 $selected_branches = $category->branch_limitations;
57 my $branches = Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed;
59 foreach my $branch ( @$branches ) {
60 my $selected = ( grep { $_ eq $branch->{branchcode} } @$selected_branches ) ? 1 : 0;
62 { branchcode => $branch->{branchcode},
63 branchname => $branch->{branchname},
64 selected => $selected,
69 category => $category,
70 branches_loop => \@branches_loop,
73 if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
74 C4::Form::MessagingPreferences::set_form_values(
75 { categorycode => $categorycode }, $template );
78 elsif ( $op eq 'add_validate' ) {
80 my $categorycode = $input->param('categorycode');
81 my $description = $input->param('description');
82 my $enrolmentperiod = $input->param('enrolmentperiod');
83 my $enrolmentperioddate = $input->param('enrolmentperioddate') || undef;
84 my $upperagelimit = $input->param('upperagelimit');
85 my $dateofbirthrequired = $input->param('dateofbirthrequired');
86 my $enrolmentfee = $input->param('enrolmentfee');
87 my $reservefee = $input->param('reservefee');
88 my $hidelostitems = $input->param('hidelostitems');
89 my $overduenoticerequired = $input->param('overduenoticerequired');
90 my $category_type = $input->param('category_type');
91 my $BlockExpiredPatronOpacActions = $input->param('BlockExpiredPatronOpacActions');
92 my $checkPrevCheckout = $input->param('checkprevcheckout');
93 my $default_privacy = $input->param('default_privacy');
94 my $reset_password = $input->param('reset_password');
95 my $change_password = $input->param('change_password');
96 my @branches = grep { $_ ne q{} } $input->multi_param('branches');
98 $reset_password = undef if $reset_password eq -1;
99 $change_password = undef if $change_password eq -1;
101 my $is_a_modif = $input->param("is_a_modif");
103 if ($enrolmentperioddate) {
104 $enrolmentperioddate = output_pref(
106 dt => dt_from_string($enrolmentperioddate),
114 my $category = Koha::Patron::Categories->find( $categorycode );
115 $category->categorycode($categorycode);
116 $category->description($description);
117 $category->enrolmentperiod($enrolmentperiod);
118 $category->enrolmentperioddate($enrolmentperioddate);
119 $category->upperagelimit($upperagelimit);
120 $category->dateofbirthrequired($dateofbirthrequired);
121 $category->enrolmentfee($enrolmentfee);
122 $category->reservefee($reservefee);
123 $category->hidelostitems($hidelostitems);
124 $category->overduenoticerequired($overduenoticerequired);
125 $category->category_type($category_type);
126 $category->BlockExpiredPatronOpacActions($BlockExpiredPatronOpacActions);
127 $category->checkprevcheckout($checkPrevCheckout);
128 $category->default_privacy($default_privacy);
129 $category->reset_password($reset_password);
130 $category->change_password($change_password);
133 $category->replace_branch_limitations( \@branches );
136 push @messages, {type => 'error', code => 'error_on_update' };
138 push @messages, { type => 'message', code => 'success_on_update' };
142 my $category = Koha::Patron::Category->new({
143 categorycode => $categorycode,
144 description => $description,
145 enrolmentperiod => $enrolmentperiod,
146 enrolmentperioddate => $enrolmentperioddate,
147 upperagelimit => $upperagelimit,
148 dateofbirthrequired => $dateofbirthrequired,
149 enrolmentfee => $enrolmentfee,
150 reservefee => $reservefee,
151 hidelostitems => $hidelostitems,
152 overduenoticerequired => $overduenoticerequired,
153 category_type => $category_type,
154 BlockExpiredPatronOpacActions => $BlockExpiredPatronOpacActions,
155 checkprevcheckout => $checkPrevCheckout,
156 default_privacy => $default_privacy,
157 reset_password => $reset_password,
158 change_password => $change_password,
162 $category->replace_branch_limitations( \@branches );
166 push @messages, { type => 'error', code => 'error_on_insert' };
168 push @messages, { type => 'message', code => 'success_on_insert' };
172 if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
173 C4::Form::MessagingPreferences::handle_form_action( $input,
174 { categorycode => scalar $input->param('categorycode') }, $template );
180 elsif ( $op eq 'delete_confirm' ) {
182 my $count = Koha::Patrons->search({
183 categorycode => $categorycode
186 my $category = Koha::Patron::Categories->find($categorycode);
189 category => $category,
190 patrons_in_category => $count,
194 elsif ( $op eq 'delete_confirmed' ) {
195 my $categorycode = uc( $input->param('categorycode') );
197 my $category = Koha::Patron::Categories->find( $categorycode );
198 my $deleted = eval { $category->delete; };
200 if ( $@ or not $deleted ) {
201 push @messages, {type => 'error', code => 'error_on_delete' };
203 push @messages, { type => 'message', code => 'success_on_delete' };
209 if ( $op eq 'list' ) {
210 my $categories = Koha::Patron::Categories->search(
212 description => { -like => "$searchfield%" }
215 order_by => ['category_type', 'description', 'categorycode' ]
220 categories => $categories,
225 categorycode => $categorycode,
226 searchfield => $searchfield,
227 messages => \@messages,
231 output_html_with_http_headers $input, $cookie, $template->output;