Bug 26751: Fix fatal exception if only one repo defined
[koha.git] / admin / categories.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2002 Paul Poulain
5 #
6 # This file is part of Koha.
7 #
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.
12 #
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.
17 #
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>.
20
21 use Modern::Perl;
22
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;
33
34 my $input         = CGI->new;
35 my $searchfield   = $input->param('description') // q||;
36 my $categorycode  = $input->param('categorycode');
37 my $op            = $input->param('op') // 'list';
38 my @messages;
39
40 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
41     {
42         template_name   => "admin/categories.tt",
43         query           => $input,
44         type            => "intranet",
45         flagsrequired   => { parameters => 'manage_patron_categories' },
46         debug           => 1,
47     }
48 );
49
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;
55     }
56
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,
65           };
66     }
67
68     $template->param(
69         category => $category,
70         branches_loop       => \@branches_loop,
71     );
72
73     if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
74         C4::Form::MessagingPreferences::set_form_values(
75             { categorycode => $categorycode }, $template );
76     }
77 }
78 elsif ( $op eq 'add_validate' ) {
79
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');
100
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;
105
106     my $is_a_modif = $input->param("is_a_modif");
107
108     if ($enrolmentperioddate) {
109         $enrolmentperioddate = output_pref(
110             {
111                 dt         => dt_from_string($enrolmentperioddate),
112                 dateformat => 'iso',
113                 dateonly   => 1,
114             }
115         );
116     }
117
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 );
142         };
143         if ( $@ ) {
144             push @messages, {type => 'error', code => 'error_on_update' };
145         } else {
146             push @messages, { type => 'message', code => 'success_on_update' };
147         }
148     }
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,
170         });
171         eval {
172             $category->store;
173             $category->replace_branch_limitations( \@branches );
174         };
175
176         if ( $@ ) {
177             push @messages, { type => 'error', code => 'error_on_insert' };
178         } else {
179             push @messages, { type => 'message', code => 'success_on_insert' };
180         }
181     }
182
183     if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
184         C4::Form::MessagingPreferences::handle_form_action( $input,
185             { categorycode => scalar $input->param('categorycode') }, $template );
186     }
187
188     $searchfield = q||;
189     $op = 'list';
190 }
191 elsif ( $op eq 'delete_confirm' ) {
192
193     my $count = Koha::Patrons->search({
194         categorycode => $categorycode
195     })->count;
196
197     my $category = Koha::Patron::Categories->find($categorycode);
198
199     $template->param(
200         category => $category,
201         patrons_in_category => $count,
202     );
203
204 }
205 elsif ( $op eq 'delete_confirmed' ) {
206     my $categorycode = uc( $input->param('categorycode') );
207
208     my $category = Koha::Patron::Categories->find( $categorycode );
209     my $deleted = eval { $category->delete; };
210
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' };
215     }
216
217     $op = 'list';
218 }
219
220 if ( $op eq 'list' ) {
221     my $categories = Koha::Patron::Categories->search(
222         {
223             description => { -like => "$searchfield%" }
224         },
225         {
226             order_by => ['category_type', 'description', 'categorycode' ]
227         }
228     );
229
230     $template->param(
231         categories => $categories,
232     )
233 }
234
235 $template->param(
236     categorycode => $categorycode,
237     searchfield  => $searchfield,
238     messages     => \@messages,
239     op => $op,
240 );
241
242 output_html_with_http_headers $input, $cookie, $template->output;
243
244 exit 0;