Bug 18203: Add per borrower category restrictions on placing ILL requests
[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::Patron::Categories;
31 use Koha::Libraries;
32
33 my $input         = CGI->new;
34 my $searchfield   = $input->param('description') // q||;
35 my $categorycode  = $input->param('categorycode');
36 my $op            = $input->param('op') // 'list';
37 my @messages;
38
39 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
40     {
41         template_name   => "admin/categories.tt",
42         query           => $input,
43         type            => "intranet",
44         flagsrequired   => { parameters => 'manage_patron_categories' },
45     }
46 );
47
48 if ( $op eq 'add_form' ) {
49
50     $template->param(
51         category => scalar Koha::Patron::Categories->find($categorycode),
52     );
53
54     if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
55         C4::Form::MessagingPreferences::set_form_values(
56             { categorycode => $categorycode }, $template );
57     }
58 }
59 elsif ( $op eq 'add_validate' ) {
60
61     my $categorycode = $input->param('categorycode');
62     my $description = $input->param('description');
63     my $enrolmentperiod = $input->param('enrolmentperiod');
64     my $enrolmentperioddate = $input->param('enrolmentperioddate') || undef;
65     my $password_expiry_days = $input->param('password_expiry_days') || undef;
66     my $upperagelimit = $input->param('upperagelimit');
67     my $dateofbirthrequired = $input->param('dateofbirthrequired');
68     my $enrolmentfee = $input->param('enrolmentfee');
69     my $reservefee = $input->param('reservefee');
70     my $hidelostitems = $input->param('hidelostitems');
71     my $overduenoticerequired = $input->param('overduenoticerequired');
72     my $category_type = $input->param('category_type');
73     my $BlockExpiredPatronOpacActions = $input->param('BlockExpiredPatronOpacActions');
74     my $checkPrevCheckout = $input->param('checkprevcheckout');
75     my $canplaceillopac = $input->param('canplaceillopac');
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 ($is_a_modif) {
93         my $category = Koha::Patron::Categories->find( $categorycode );
94         $category->categorycode($categorycode);
95         $category->description($description);
96         $category->enrolmentperiod($enrolmentperiod);
97         $category->enrolmentperioddate($enrolmentperioddate);
98         $category->password_expiry_days($password_expiry_days);
99         $category->upperagelimit($upperagelimit);
100         $category->dateofbirthrequired($dateofbirthrequired);
101         $category->enrolmentfee($enrolmentfee);
102         $category->reservefee($reservefee);
103         $category->hidelostitems($hidelostitems);
104         $category->overduenoticerequired($overduenoticerequired);
105         $category->category_type($category_type);
106         $category->can_be_guarantee($can_be_guarantee);
107         $category->BlockExpiredPatronOpacActions($BlockExpiredPatronOpacActions);
108         $category->checkprevcheckout($checkPrevCheckout);
109         $category->canplaceillopac($canplaceillopac);
110         $category->default_privacy($default_privacy);
111         $category->reset_password($reset_password);
112         $category->change_password($change_password);
113         $category->exclude_from_local_holds_priority($exclude_from_local_holds_priority);
114         $category->min_password_length($min_password_length);
115         $category->require_strong_password($require_strong_password);
116         eval {
117             $category->store;
118             $category->replace_library_limits( \@branches );
119         };
120         if ( $@ ) {
121             push @messages, {type => 'error', code => 'error_on_update' };
122         } else {
123             push @messages, { type => 'message', code => 'success_on_update' };
124         }
125     }
126     else {
127         my $category = Koha::Patron::Category->new({
128             categorycode => $categorycode,
129             description => $description,
130             enrolmentperiod => $enrolmentperiod,
131             enrolmentperioddate => $enrolmentperioddate,
132             password_expiry_days => $password_expiry_days,
133             upperagelimit => $upperagelimit,
134             dateofbirthrequired => $dateofbirthrequired,
135             enrolmentfee => $enrolmentfee,
136             reservefee => $reservefee,
137             hidelostitems => $hidelostitems,
138             overduenoticerequired => $overduenoticerequired,
139             category_type => $category_type,
140             can_be_guarantee => $can_be_guarantee,
141             BlockExpiredPatronOpacActions => $BlockExpiredPatronOpacActions,
142             checkprevcheckout => $checkPrevCheckout,
143             canplaceillopac => $canplaceillopac,
144             default_privacy => $default_privacy,
145             reset_password => $reset_password,
146             change_password => $change_password,
147             exclude_from_local_holds_priority => $exclude_from_local_holds_priority,
148             min_password_length => $min_password_length,
149             require_strong_password => $require_strong_password,
150         });
151         eval {
152             $category->store;
153             $category->replace_library_limits( \@branches );
154         };
155
156         if ( $@ ) {
157             push @messages, { type => 'error', code => 'error_on_insert' };
158         } else {
159             push @messages, { type => 'message', code => 'success_on_insert' };
160         }
161     }
162
163     if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
164         C4::Form::MessagingPreferences::handle_form_action( $input,
165             { categorycode => scalar $input->param('categorycode') }, $template );
166     }
167
168     $searchfield = q||;
169     $op = 'list';
170 }
171 elsif ( $op eq 'delete_confirm' ) {
172
173     my $count = Koha::Patrons->search({
174         categorycode => $categorycode
175     })->count;
176
177     my $category = Koha::Patron::Categories->find($categorycode);
178
179     $template->param(
180         category => $category,
181         patrons_in_category => $count,
182     );
183
184 }
185 elsif ( $op eq 'delete_confirmed' ) {
186     my $categorycode = uc( $input->param('categorycode') );
187
188     my $category = Koha::Patron::Categories->find( $categorycode );
189     my $deleted = eval { $category->delete; };
190
191     if ( $@ or not $deleted ) {
192         push @messages, {type => 'error', code => 'error_on_delete' };
193     } else {
194         push @messages, { type => 'message', code => 'success_on_delete' };
195     }
196
197     $op = 'list';
198 }
199
200 if ( $op eq 'list' ) {
201     my $categories = Koha::Patron::Categories->search(
202         {
203             description => { -like => "$searchfield%" }
204         },
205         {
206             order_by => ['category_type', 'description', 'categorycode' ]
207         }
208     );
209
210     $template->param(
211         categories => $categories,
212     )
213 }
214
215 $template->param(
216     categorycode => $categorycode,
217     searchfield  => $searchfield,
218     messages     => \@messages,
219     op => $op,
220 );
221
222 output_html_with_http_headers $input, $cookie, $template->output;
223
224 exit 0;