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