Bug 25261: (QA follow-up) Add unit tests
[koha.git] / admin / categories.pl
blob749db2d0a0a73c69dfde3c82bbd28bd87201d612
1 #!/usr/bin/perl
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>.
21 use Modern::Perl;
23 use CGI qw ( -utf8 );
24 use C4::Context;
25 use C4::Auth;
26 use C4::Output;
27 use C4::Form::MessagingPreferences;
28 use Koha::Patrons;
29 use Koha::Database;
30 use Koha::DateUtils;
31 use Koha::Patron::Categories;
32 use Koha::Libraries;
34 my $input = new CGI;
35 my $searchfield = $input->param('description') // q||;
36 my $categorycode = $input->param('categorycode');
37 my $op = $input->param('op') // 'list';
38 my @messages;
40 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
42 template_name => "admin/categories.tt",
43 query => $input,
44 type => "intranet",
45 flagsrequired => { parameters => 'manage_patron_categories' },
46 debug => 1,
50 if ( $op eq 'add_form' ) {
51 my ( $category, $selected_branches );
52 if ($categorycode) {
53 $category = Koha::Patron::Categories->find($categorycode);
54 $selected_branches = $category->branch_limitations;
57 my $branches = Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed;
58 my @branches_loop;
59 foreach my $branch ( @$branches ) {
60 my $selected = ( grep { $_ eq $branch->{branchcode} } @$selected_branches ) ? 1 : 0;
61 push @branches_loop,
62 { branchcode => $branch->{branchcode},
63 branchname => $branch->{branchname},
64 selected => $selected,
68 $template->param(
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 $min_password_length = $input->param('min_password_length');
98 my $require_strong_password = $input->param('require_strong_password');
99 my @branches = grep { $_ ne q{} } $input->multi_param('branches');
101 $reset_password = undef if $reset_password eq -1;
102 $change_password = undef if $change_password eq -1;
103 $min_password_length = undef unless length($min_password_length);
104 $require_strong_password = undef if $require_strong_password eq -1;
106 my $is_a_modif = $input->param("is_a_modif");
108 if ($enrolmentperioddate) {
109 $enrolmentperioddate = output_pref(
111 dt => dt_from_string($enrolmentperioddate),
112 dateformat => 'iso',
113 dateonly => 1,
118 if ($is_a_modif) {
119 my $category = Koha::Patron::Categories->find( $categorycode );
120 $category->categorycode($categorycode);
121 $category->description($description);
122 $category->enrolmentperiod($enrolmentperiod);
123 $category->enrolmentperioddate($enrolmentperioddate);
124 $category->upperagelimit($upperagelimit);
125 $category->dateofbirthrequired($dateofbirthrequired);
126 $category->enrolmentfee($enrolmentfee);
127 $category->reservefee($reservefee);
128 $category->hidelostitems($hidelostitems);
129 $category->overduenoticerequired($overduenoticerequired);
130 $category->category_type($category_type);
131 $category->BlockExpiredPatronOpacActions($BlockExpiredPatronOpacActions);
132 $category->checkprevcheckout($checkPrevCheckout);
133 $category->default_privacy($default_privacy);
134 $category->reset_password($reset_password);
135 $category->change_password($change_password);
136 $category->exclude_from_local_holds_priority($exclude_from_local_holds_priority);
137 $category->min_password_length($min_password_length);
138 $category->require_strong_password($require_strong_password);
139 eval {
140 $category->store;
141 $category->replace_branch_limitations( \@branches );
143 if ( $@ ) {
144 push @messages, {type => 'error', code => 'error_on_update' };
145 } else {
146 push @messages, { type => 'message', code => 'success_on_update' };
149 else {
150 my $category = Koha::Patron::Category->new({
151 categorycode => $categorycode,
152 description => $description,
153 enrolmentperiod => $enrolmentperiod,
154 enrolmentperioddate => $enrolmentperioddate,
155 upperagelimit => $upperagelimit,
156 dateofbirthrequired => $dateofbirthrequired,
157 enrolmentfee => $enrolmentfee,
158 reservefee => $reservefee,
159 hidelostitems => $hidelostitems,
160 overduenoticerequired => $overduenoticerequired,
161 category_type => $category_type,
162 BlockExpiredPatronOpacActions => $BlockExpiredPatronOpacActions,
163 checkprevcheckout => $checkPrevCheckout,
164 default_privacy => $default_privacy,
165 reset_password => $reset_password,
166 change_password => $change_password,
167 exclude_from_local_holds_priority => $exclude_from_local_holds_priority,
168 min_password_length => $min_password_length,
169 require_strong_password => $require_strong_password,
171 eval {
172 $category->store;
173 $category->replace_branch_limitations( \@branches );
176 if ( $@ ) {
177 push @messages, { type => 'error', code => 'error_on_insert' };
178 } else {
179 push @messages, { type => 'message', code => 'success_on_insert' };
183 if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
184 C4::Form::MessagingPreferences::handle_form_action( $input,
185 { categorycode => scalar $input->param('categorycode') }, $template );
188 $searchfield = q||;
189 $op = 'list';
191 elsif ( $op eq 'delete_confirm' ) {
193 my $count = Koha::Patrons->search({
194 categorycode => $categorycode
195 })->count;
197 my $category = Koha::Patron::Categories->find($categorycode);
199 $template->param(
200 category => $category,
201 patrons_in_category => $count,
205 elsif ( $op eq 'delete_confirmed' ) {
206 my $categorycode = uc( $input->param('categorycode') );
208 my $category = Koha::Patron::Categories->find( $categorycode );
209 my $deleted = eval { $category->delete; };
211 if ( $@ or not $deleted ) {
212 push @messages, {type => 'error', code => 'error_on_delete' };
213 } else {
214 push @messages, { type => 'message', code => 'success_on_delete' };
217 $op = 'list';
220 if ( $op eq 'list' ) {
221 my $categories = Koha::Patron::Categories->search(
223 description => { -like => "$searchfield%" }
226 order_by => ['category_type', 'description', 'categorycode' ]
230 $template->param(
231 categories => $categories,
235 $template->param(
236 categorycode => $categorycode,
237 searchfield => $searchfield,
238 messages => \@messages,
239 op => $op,
242 output_html_with_http_headers $input, $cookie, $template->output;
244 exit 0;