Bug 24663: Remove authnotrequired if set to 0
[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         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 @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         $category->exclude_from_local_holds_priority($exclude_from_local_holds_priority);
133         eval {
134             $category->store;
135             $category->replace_branch_limitations( \@branches );
136         };
137         if ( $@ ) {
138             push @messages, {type => 'error', code => 'error_on_update' };
139         } else {
140             push @messages, { type => 'message', code => 'success_on_update' };
141         }
142     }
143     else {
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,
162         });
163         eval {
164             $category->store;
165             $category->replace_branch_limitations( \@branches );
166         };
167
168         if ( $@ ) {
169             push @messages, { type => 'error', code => 'error_on_insert' };
170         } else {
171             push @messages, { type => 'message', code => 'success_on_insert' };
172         }
173     }
174
175     if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
176         C4::Form::MessagingPreferences::handle_form_action( $input,
177             { categorycode => scalar $input->param('categorycode') }, $template );
178     }
179
180     $searchfield = q||;
181     $op = 'list';
182 }
183 elsif ( $op eq 'delete_confirm' ) {
184
185     my $count = Koha::Patrons->search({
186         categorycode => $categorycode
187     })->count;
188
189     my $category = Koha::Patron::Categories->find($categorycode);
190
191     $template->param(
192         category => $category,
193         patrons_in_category => $count,
194     );
195
196 }
197 elsif ( $op eq 'delete_confirmed' ) {
198     my $categorycode = uc( $input->param('categorycode') );
199
200     my $category = Koha::Patron::Categories->find( $categorycode );
201     my $deleted = eval { $category->delete; };
202
203     if ( $@ or not $deleted ) {
204         push @messages, {type => 'error', code => 'error_on_delete' };
205     } else {
206         push @messages, { type => 'message', code => 'success_on_delete' };
207     }
208
209     $op = 'list';
210 }
211
212 if ( $op eq 'list' ) {
213     my $categories = Koha::Patron::Categories->search(
214         {
215             description => { -like => "$searchfield%" }
216         },
217         {
218             order_by => ['category_type', 'description', 'categorycode' ]
219         }
220     );
221
222     $template->param(
223         categories => $categories,
224     )
225 }
226
227 $template->param(
228     categorycode => $categorycode,
229     searchfield  => $searchfield,
230     messages     => \@messages,
231     op => $op,
232 );
233
234 output_html_with_http_headers $input, $cookie, $template->output;
235
236 exit 0;