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