Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[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         = 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;
39
40 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
41     {
42         template_name   => "admin/categories.tt",
43         query           => $input,
44         type            => "intranet",
45         authnotrequired => 0,
46         flagsrequired   => { parameters => 'manage_patron_categories' },
47         debug           => 1,
48     }
49 );
50
51 if ( $op eq 'add_form' ) {
52     my ( $category, $selected_branches );
53     if ($categorycode) {
54         $category          = Koha::Patron::Categories->find($categorycode);
55         $selected_branches = $category->branch_limitations;
56     }
57
58     my $branches = Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed;
59     my @branches_loop;
60     foreach my $branch ( @$branches ) {
61         my $selected = ( grep { $_ eq $branch->{branchcode} } @$selected_branches ) ? 1 : 0;
62         push @branches_loop,
63           { branchcode => $branch->{branchcode},
64             branchname => $branch->{branchname},
65             selected   => $selected,
66           };
67     }
68
69     $template->param(
70         category => $category,
71         branches_loop       => \@branches_loop,
72     );
73
74     if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
75         C4::Form::MessagingPreferences::set_form_values(
76             { categorycode => $categorycode }, $template );
77     }
78 }
79 elsif ( $op eq 'add_validate' ) {
80
81     my $categorycode = $input->param('categorycode');
82     my $description = $input->param('description');
83     my $enrolmentperiod = $input->param('enrolmentperiod');
84     my $enrolmentperioddate = $input->param('enrolmentperioddate') || undef;
85     my $upperagelimit = $input->param('upperagelimit');
86     my $dateofbirthrequired = $input->param('dateofbirthrequired');
87     my $enrolmentfee = $input->param('enrolmentfee');
88     my $reservefee = $input->param('reservefee');
89     my $hidelostitems = $input->param('hidelostitems');
90     my $overduenoticerequired = $input->param('overduenoticerequired');
91     my $category_type = $input->param('category_type');
92     my $BlockExpiredPatronOpacActions = $input->param('BlockExpiredPatronOpacActions');
93     my $checkPrevCheckout = $input->param('checkprevcheckout');
94     my $default_privacy = $input->param('default_privacy');
95     my $reset_password = $input->param('reset_password');
96     my $change_password = $input->param('change_password');
97     my @branches = grep { $_ ne q{} } $input->multi_param('branches');
98
99     $reset_password = undef if $reset_password eq -1;
100     $change_password = undef if $change_password eq -1;
101
102     my $is_a_modif = $input->param("is_a_modif");
103
104     if ($enrolmentperioddate) {
105         $enrolmentperioddate = output_pref(
106             {
107                 dt         => dt_from_string($enrolmentperioddate),
108                 dateformat => 'iso',
109                 dateonly   => 1,
110             }
111         );
112     }
113
114     if ($is_a_modif) {
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         eval {
133             $category->store;
134             $category->replace_branch_limitations( \@branches );
135         };
136         if ( $@ ) {
137             push @messages, {type => 'error', code => 'error_on_update' };
138         } else {
139             push @messages, { type => 'message', code => 'success_on_update' };
140         }
141     }
142     else {
143         my $category = Koha::Patron::Category->new({
144             categorycode => $categorycode,
145             description => $description,
146             enrolmentperiod => $enrolmentperiod,
147             enrolmentperioddate => $enrolmentperioddate,
148             upperagelimit => $upperagelimit,
149             dateofbirthrequired => $dateofbirthrequired,
150             enrolmentfee => $enrolmentfee,
151             reservefee => $reservefee,
152             hidelostitems => $hidelostitems,
153             overduenoticerequired => $overduenoticerequired,
154             category_type => $category_type,
155             BlockExpiredPatronOpacActions => $BlockExpiredPatronOpacActions,
156             checkprevcheckout => $checkPrevCheckout,
157             default_privacy => $default_privacy,
158             reset_password => $reset_password,
159             change_password => $change_password,
160         });
161         eval {
162             $category->store;
163             $category->replace_branch_limitations( \@branches );
164         };
165
166         if ( $@ ) {
167             push @messages, { type => 'error', code => 'error_on_insert' };
168         } else {
169             push @messages, { type => 'message', code => 'success_on_insert' };
170         }
171     }
172
173     if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
174         C4::Form::MessagingPreferences::handle_form_action( $input,
175             { categorycode => scalar $input->param('categorycode') }, $template );
176     }
177
178     $searchfield = q||;
179     $op = 'list';
180 }
181 elsif ( $op eq 'delete_confirm' ) {
182
183     my $count = Koha::Patrons->search({
184         categorycode => $categorycode
185     })->count;
186
187     my $category = Koha::Patron::Categories->find($categorycode);
188
189     $template->param(
190         category => $category,
191         patrons_in_category => $count,
192     );
193
194 }
195 elsif ( $op eq 'delete_confirmed' ) {
196     my $categorycode = uc( $input->param('categorycode') );
197
198     my $category = Koha::Patron::Categories->find( $categorycode );
199     my $deleted = eval { $category->delete; };
200
201     if ( $@ or not $deleted ) {
202         push @messages, {type => 'error', code => 'error_on_delete' };
203     } else {
204         push @messages, { type => 'message', code => 'success_on_delete' };
205     }
206
207     $op = 'list';
208 }
209
210 if ( $op eq 'list' ) {
211     my $categories = Koha::Patron::Categories->search(
212         {
213             description => { -like => "$searchfield%" }
214         },
215         {
216             order_by => ['category_type', 'description', 'categorycode' ]
217         }
218     );
219
220     $template->param(
221         categories => $categories,
222     )
223 }
224
225 $template->param(
226     categorycode => $categorycode,
227     searchfield  => $searchfield,
228     messages     => \@messages,
229     op => $op,
230 );
231
232 output_html_with_http_headers $input, $cookie, $template->output;
233
234 exit 0;