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