Bug 14153: Noisy warns in admin/transport-cost-matrix.pl
[koha.git] / admin / categorie.pl
blobcc153bd24be0969f61c495cf7f66b32fead7c327
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2002 Paul Poulain
6 # This file is part of Koha.
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.
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.
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>.
21 use Modern::Perl;
23 use CGI qw ( -utf8 );
24 use C4::Context;
25 use C4::Auth;
26 use C4::Branch;
27 use C4::Output;
28 use C4::Form::MessagingPreferences;
29 use Koha::Borrowers;
30 use Koha::Database;
31 use Koha::DateUtils;
32 use Koha::Patron::Categories;
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;
40 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
42 template_name => "admin/categorie.tt",
43 query => $input,
44 type => "intranet",
45 authnotrequired => 0,
46 flagsrequired => { parameters => 'parameters_remaining_permissions' },
47 debug => 1,
51 if ( $op eq 'add_form' ) {
52 my ( $category, $selected_branches );
53 if ($categorycode) {
54 $category = Koha::Patron::Categories->find($categorycode);
55 $selected_branches = $category->branch_limitations;
58 my $branches = GetBranches;
59 my @branches_loop;
60 foreach my $branchcode ( sort { uc( $branches->{$a}->{branchname} ) cmp uc( $branches->{$b}->{branchname} ) } keys %$branches ) {
61 my $selected = ( grep { $_ eq $branchcode } @$selected_branches ) ? 1 : 0;
62 push @branches_loop,
63 { branchcode => $branchcode,
64 branchname => $branches->{$branchcode}->{branchname},
65 selected => $selected,
69 $template->param(
70 category => $category,
71 branches_loop => \@branches_loop,
74 if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
75 C4::Form::MessagingPreferences::set_form_values(
76 { categorycode => $categorycode }, $template );
79 elsif ( $op eq 'add_validate' ) {
81 my $categorycode = $input->param('categorycode');
82 my $description = $input->param('description');
83 my $enrolmentperiod = $input->param('enrolmentperiod');
84 my $enrolmentperioddate = $input->param('enrolmentperioddate') || undef;
85 my $upperagelimit = $input->param('upperagelimit');
86 my $dateofbirthrequired = $input->param('dateofbirthrequired');
87 my $enrolmentfee = $input->param('enrolmentfee');
88 my $reservefee = $input->param('reservefee');
89 my $hidelostitems = $input->param('hidelostitems');
90 my $overduenoticerequired = $input->param('overduenoticerequired');
91 my $category_type = $input->param('category_type');
92 my $BlockExpiredPatronOpacActions = $input->param('BlockExpiredPatronOpacActions');
93 my $default_privacy = $input->param('default_privacy');
94 my @branches = grep { $_ ne q{} } $input->param('branches');
96 my $is_a_modif = $input->param("is_a_modif");
98 if ( $enrolmentperioddate ) {
99 $enrolmentperioddate = output_pref({ dt => dt_from_string($enrolmentperioddate), dateformat => 'iso' });
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->upperagelimit($upperagelimit);
109 $category->dateofbirthrequired($dateofbirthrequired);
110 $category->enrolmentfee($enrolmentfee);
111 $category->reservefee($reservefee);
112 $category->hidelostitems($hidelostitems);
113 $category->overduenoticerequired($overduenoticerequired);
114 $category->category_type($category_type);
115 $category->BlockExpiredPatronOpacActions($BlockExpiredPatronOpacActions);
116 $category->default_privacy($default_privacy);
117 eval {
118 $category->store;
119 $category->replace_branch_limitations( \@branches );
121 if ( $@ ) {
122 push @messages, {type => 'error', code => 'error_on_update' };
123 } else {
124 push @messages, { type => 'message', code => 'success_on_update' };
127 else {
128 my $category = Koha::Patron::Category->new({
129 categorycode => $categorycode,
130 description => $description,
131 enrolmentperiod => $enrolmentperiod,
132 enrolmentperioddate => $enrolmentperioddate,
133 upperagelimit => $upperagelimit,
134 dateofbirthrequired => $dateofbirthrequired,
135 enrolmentfee => $enrolmentfee,
136 reservefee => $reservefee,
137 hidelostitems => $hidelostitems,
138 overduenoticerequired => $overduenoticerequired,
139 category_type => $category_type,
140 BlockExpiredPatronOpacActions => $BlockExpiredPatronOpacActions,
141 default_privacy => $default_privacy,
143 eval {
144 $category->store;
145 $category->replace_branch_limitations( \@branches );
148 if ( $@ ) {
149 push @messages, { type => 'error', code => 'error_on_insert' };
150 } else {
151 push @messages, { type => 'message', code => 'success_on_insert' };
155 if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
156 C4::Form::MessagingPreferences::handle_form_action( $input,
157 { categorycode => $input->param('categorycode') }, $template );
160 $searchfield = q||;
161 $op = 'list';
163 elsif ( $op eq 'delete_confirm' ) {
165 my $count = Koha::Borrowers->search({
166 categorycode => $categorycode
167 })->count;
169 my $category = Koha::Patron::Categories->find($categorycode);
171 $template->param(
172 category => $category,
173 patrons_in_category => $count,
177 elsif ( $op eq 'delete_confirmed' ) {
178 my $categorycode = uc( $input->param('categorycode') );
180 my $category = Koha::Patron::Categories->find( $categorycode );
181 my $deleted = eval { $category->delete; };
183 if ( $@ or not $deleted ) {
184 push @messages, {type => 'error', code => 'error_on_delete' };
185 } else {
186 push @messages, { type => 'message', code => 'success_on_delete' };
189 $op = 'list';
192 if ( $op eq 'list' ) {
193 my $categories = Koha::Patron::Categories->search(
195 description => { -like => "$searchfield%" }
198 order_by => ['category_type', 'description', 'categorycode' ]
202 $template->param(
203 categories => $categories,
207 $template->param(
208 categorycode => $categorycode,
209 searchfield => $searchfield,
210 messages => \@messages,
211 op => $op,
214 output_html_with_http_headers $input, $cookie, $template->output;
216 exit 0;