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 $exclude_from_local_holds_priority = $input->param('exclude_from_local_holds_priority');
97 my @branches = grep { $_ ne q{} } $input->multi_param('branches');
99 $reset_password = undef if $reset_password eq -1;
100 $change_password = undef if $change_password eq -1;
102 my $is_a_modif = $input->param("is_a_modif");
104 if ($enrolmentperioddate) {
105 $enrolmentperioddate = output_pref(
107 dt => dt_from_string($enrolmentperioddate),
115 my $category = Koha::Patron::Categories->find( $categorycode );
116 $category->categorycode($categorycode);
117 $category->description($description);
118 $category->enrolmentperiod($enrolmentperiod);
119 $category->enrolmentperioddate($enrolmentperioddate);
120 $category->upperagelimit($upperagelimit);
121 $category->dateofbirthrequired($dateofbirthrequired);
122 $category->enrolmentfee($enrolmentfee);
123 $category->reservefee($reservefee);
124 $category->hidelostitems($hidelostitems);
125 $category->overduenoticerequired($overduenoticerequired);
126 $category->category_type($category_type);
127 $category->BlockExpiredPatronOpacActions($BlockExpiredPatronOpacActions);
128 $category->checkprevcheckout($checkPrevCheckout);
129 $category->default_privacy($default_privacy);
130 $category->reset_password($reset_password);
131 $category->change_password($change_password);
132 $category->exclude_from_local_holds_priority($exclude_from_local_holds_priority);
135 $category->replace_branch_limitations( \@branches );
138 push @messages, {type => 'error', code => 'error_on_update' };
140 push @messages, { type => 'message', code => 'success_on_update' };
144 my $category = Koha::Patron::Category->new({
145 categorycode => $categorycode,
146 description => $description,
147 enrolmentperiod => $enrolmentperiod,
148 enrolmentperioddate => $enrolmentperioddate,
149 upperagelimit => $upperagelimit,
150 dateofbirthrequired => $dateofbirthrequired,
151 enrolmentfee => $enrolmentfee,
152 reservefee => $reservefee,
153 hidelostitems => $hidelostitems,
154 overduenoticerequired => $overduenoticerequired,
155 category_type => $category_type,
156 BlockExpiredPatronOpacActions => $BlockExpiredPatronOpacActions,
157 checkprevcheckout => $checkPrevCheckout,
158 default_privacy => $default_privacy,
159 reset_password => $reset_password,
160 change_password => $change_password,
161 exclude_from_local_holds_priority => $exclude_from_local_holds_priority,
165 $category->replace_branch_limitations( \@branches );
169 push @messages, { type => 'error', code => 'error_on_insert' };
171 push @messages, { type => 'message', code => 'success_on_insert' };
175 if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
176 C4::Form::MessagingPreferences::handle_form_action( $input,
177 { categorycode => scalar $input->param('categorycode') }, $template );
183 elsif ( $op eq 'delete_confirm' ) {
185 my $count = Koha::Patrons->search({
186 categorycode => $categorycode
189 my $category = Koha::Patron::Categories->find($categorycode);
192 category => $category,
193 patrons_in_category => $count,
197 elsif ( $op eq 'delete_confirmed' ) {
198 my $categorycode = uc( $input->param('categorycode') );
200 my $category = Koha::Patron::Categories->find( $categorycode );
201 my $deleted = eval { $category->delete; };
203 if ( $@ or not $deleted ) {
204 push @messages, {type => 'error', code => 'error_on_delete' };
206 push @messages, { type => 'message', code => 'success_on_delete' };
212 if ( $op eq 'list' ) {
213 my $categories = Koha::Patron::Categories->search(
215 description => { -like => "$searchfield%" }
218 order_by => ['category_type', 'description', 'categorycode' ]
223 categories => $categories,
228 categorycode => $categorycode,
229 searchfield => $searchfield,
230 messages => \@messages,
234 output_html_with_http_headers $input, $cookie, $template->output;