Bug 30650: Be more flexible with opening slots
[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 qw( get_template_and_user );
26 use C4::Output qw( output_html_with_http_headers );
27 use C4::Form::MessagingPreferences;
28 use Koha::Patrons;
29 use Koha::Database;
30 use Koha::DateUtils qw( dt_from_string output_pref );
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     }
47 );
48
49 if ( $op eq 'add_form' ) {
50
51     $template->param(
52         category => scalar Koha::Patron::Categories->find($categorycode),
53     );
54
55     if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
56         C4::Form::MessagingPreferences::set_form_values(
57             { categorycode => $categorycode }, $template );
58     }
59 }
60 elsif ( $op eq 'add_validate' ) {
61
62     my $categorycode = $input->param('categorycode');
63     my $description = $input->param('description');
64     my $enrolmentperiod = $input->param('enrolmentperiod');
65     my $enrolmentperioddate = $input->param('enrolmentperioddate') || undef;
66     my $password_expiry_days = $input->param('password_expiry_days') || undef;
67     my $upperagelimit = $input->param('upperagelimit');
68     my $dateofbirthrequired = $input->param('dateofbirthrequired');
69     my $enrolmentfee = $input->param('enrolmentfee');
70     my $reservefee = $input->param('reservefee');
71     my $hidelostitems = $input->param('hidelostitems');
72     my $overduenoticerequired = $input->param('overduenoticerequired');
73     my $category_type = $input->param('category_type');
74     my $BlockExpiredPatronOpacActions = $input->param('BlockExpiredPatronOpacActions');
75     my $checkPrevCheckout = $input->param('checkprevcheckout');
76     my $default_privacy = $input->param('default_privacy');
77     my $reset_password = $input->param('reset_password');
78     my $change_password = $input->param('change_password');
79     my $exclude_from_local_holds_priority = $input->param('exclude_from_local_holds_priority');
80     my $min_password_length = $input->param('min_password_length');
81     my $require_strong_password = $input->param('require_strong_password');
82     my @branches = grep { $_ ne q{} } $input->multi_param('branches');
83     my $can_be_guarantee = $input->param('can_be_guarantee');
84
85     $reset_password = undef if $reset_password eq -1;
86     $change_password = undef if $change_password eq -1;
87     $min_password_length = undef unless length($min_password_length);
88     $require_strong_password = undef if $require_strong_password eq -1;
89
90     my $is_a_modif = $input->param("is_a_modif");
91
92     if ($enrolmentperioddate) {
93         $enrolmentperioddate = output_pref(
94             {
95                 dt         => dt_from_string($enrolmentperioddate),
96                 dateformat => 'iso',
97                 dateonly   => 1,
98             }
99         );
100     }
101
102     if ($is_a_modif) {
103         my $category = Koha::Patron::Categories->find( $categorycode );
104         $category->categorycode($categorycode);
105         $category->description($description);
106         $category->enrolmentperiod($enrolmentperiod);
107         $category->enrolmentperioddate($enrolmentperioddate);
108         $category->password_expiry_days($password_expiry_days);
109         $category->upperagelimit($upperagelimit);
110         $category->dateofbirthrequired($dateofbirthrequired);
111         $category->enrolmentfee($enrolmentfee);
112         $category->reservefee($reservefee);
113         $category->hidelostitems($hidelostitems);
114         $category->overduenoticerequired($overduenoticerequired);
115         $category->category_type($category_type);
116         $category->can_be_guarantee($can_be_guarantee);
117         $category->BlockExpiredPatronOpacActions($BlockExpiredPatronOpacActions);
118         $category->checkprevcheckout($checkPrevCheckout);
119         $category->default_privacy($default_privacy);
120         $category->reset_password($reset_password);
121         $category->change_password($change_password);
122         $category->exclude_from_local_holds_priority($exclude_from_local_holds_priority);
123         $category->min_password_length($min_password_length);
124         $category->require_strong_password($require_strong_password);
125         eval {
126             $category->store;
127             $category->replace_library_limits( \@branches );
128         };
129         if ( $@ ) {
130             push @messages, {type => 'error', code => 'error_on_update' };
131         } else {
132             push @messages, { type => 'message', code => 'success_on_update' };
133         }
134     }
135     else {
136         my $category = Koha::Patron::Category->new({
137             categorycode => $categorycode,
138             description => $description,
139             enrolmentperiod => $enrolmentperiod,
140             enrolmentperioddate => $enrolmentperioddate,
141             password_expiry_days => $password_expiry_days,
142             upperagelimit => $upperagelimit,
143             dateofbirthrequired => $dateofbirthrequired,
144             enrolmentfee => $enrolmentfee,
145             reservefee => $reservefee,
146             hidelostitems => $hidelostitems,
147             overduenoticerequired => $overduenoticerequired,
148             category_type => $category_type,
149             can_be_guarantee => $can_be_guarantee,
150             BlockExpiredPatronOpacActions => $BlockExpiredPatronOpacActions,
151             checkprevcheckout => $checkPrevCheckout,
152             default_privacy => $default_privacy,
153             reset_password => $reset_password,
154             change_password => $change_password,
155             exclude_from_local_holds_priority => $exclude_from_local_holds_priority,
156             min_password_length => $min_password_length,
157             require_strong_password => $require_strong_password,
158         });
159         eval {
160             $category->store;
161             $category->replace_library_limits( \@branches );
162         };
163
164         if ( $@ ) {
165             push @messages, { type => 'error', code => 'error_on_insert' };
166         } else {
167             push @messages, { type => 'message', code => 'success_on_insert' };
168         }
169     }
170
171     if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
172         C4::Form::MessagingPreferences::handle_form_action( $input,
173             { categorycode => scalar $input->param('categorycode') }, $template );
174     }
175
176     $searchfield = q||;
177     $op = 'list';
178 }
179 elsif ( $op eq 'delete_confirm' ) {
180
181     my $count = Koha::Patrons->search({
182         categorycode => $categorycode
183     })->count;
184
185     my $category = Koha::Patron::Categories->find($categorycode);
186
187     $template->param(
188         category => $category,
189         patrons_in_category => $count,
190     );
191
192 }
193 elsif ( $op eq 'delete_confirmed' ) {
194     my $categorycode = uc( $input->param('categorycode') );
195
196     my $category = Koha::Patron::Categories->find( $categorycode );
197     my $deleted = eval { $category->delete; };
198
199     if ( $@ or not $deleted ) {
200         push @messages, {type => 'error', code => 'error_on_delete' };
201     } else {
202         push @messages, { type => 'message', code => 'success_on_delete' };
203     }
204
205     $op = 'list';
206 }
207
208 if ( $op eq 'list' ) {
209     my $categories = Koha::Patron::Categories->search(
210         {
211             description => { -like => "$searchfield%" }
212         },
213         {
214             order_by => ['category_type', 'description', 'categorycode' ]
215         }
216     );
217
218     $template->param(
219         categories => $categories,
220     )
221 }
222
223 $template->param(
224     categorycode => $categorycode,
225     searchfield  => $searchfield,
226     messages     => \@messages,
227     op => $op,
228 );
229
230 output_html_with_http_headers $input, $cookie, $template->output;
231
232 exit 0;