Bug 19074: Fix category display in Batch patron modification.
[koha.git] / admin / patron-attr-types.pl
1 #! /usr/bin/perl
2 #
3 # Copyright 2008 LibLime
4 # Parts copyright 2010 BibLibre
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
22 use Modern::Perl;
23
24 use CGI qw ( -utf8 );
25 use List::MoreUtils qw/uniq/;
26
27 use C4::Auth;
28 use C4::Context;
29 use C4::Output;
30 use C4::Koha;
31 use C4::Members::AttributeTypes;
32
33 use Koha::AuthorisedValues;
34 use Koha::Libraries;
35 use Koha::Patron::Categories;
36
37 my $script_name = "/cgi-bin/koha/admin/patron-attr-types.pl";
38
39 our $input = new CGI;
40 my $op = $input->param('op') || '';
41
42
43 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
44     {   template_name   => "admin/patron-attr-types.tt",
45         query           => $input,
46         type            => "intranet",
47         authnotrequired => 0,
48         flagsrequired => { parameters => 'parameters_remaining_permissions' }
49     }
50 );
51
52
53 $template->param(script_name => $script_name);
54
55 my $code = $input->param("code");
56
57 my $display_list = 0;
58 if ($op eq "edit_attribute_type") {
59     edit_attribute_type_form($template, $code);
60 } elsif ($op eq "edit_attribute_type_confirmed") {
61     $display_list = add_update_attribute_type('edit', $template, $code);
62 } elsif ($op eq "add_attribute_type") {
63     add_attribute_type_form($template);
64 } elsif ($op eq "add_attribute_type_confirmed") {
65     $display_list = add_update_attribute_type('add', $template, $code);
66 } elsif ($op eq "delete_attribute_type") {
67     $display_list = delete_attribute_type_form($template, $code);
68 } elsif ($op eq "delete_attribute_type_confirmed") {
69     delete_attribute_type($template, $code);
70     $display_list = 1;
71 } else {
72     $display_list = 1;
73 }
74
75 if ($display_list) {
76     unless (C4::Context->preference('ExtendedPatronAttributes')) {
77         $template->param(WARNING_extended_attributes_off => 1); 
78     }
79     patron_attribute_type_list($template);
80 }
81
82 output_html_with_http_headers $input, $cookie, $template->output;
83
84 exit 0;
85
86 sub add_attribute_type_form {
87     my $template = shift;
88
89     my $branches = Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed;
90     my @branches_loop;
91     foreach my $branch (@$branches) {
92         push @branches_loop, {
93             branchcode => $branch->{branchcode},
94             branchname => $branch->{branchname},
95         };
96     }
97
98     my $patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
99     $template->param(
100         attribute_type_form => 1,
101         confirm_op => 'add_attribute_type_confirmed',
102         categories => $patron_categories,
103         branches_loop => \@branches_loop,
104     );
105     $template->param(classes_val_loop => GetAuthorisedValues( 'PA_CLASS'));
106 }
107
108 sub error_add_attribute_type_form {
109     my $template = shift;
110
111     $template->param(description => scalar $input->param('description'));
112
113     if ($input->param('repeatable')) {
114         $template->param(repeatable_checked => 1);
115     }
116     if ($input->param('unique_id')) {
117         $template->param(unique_id_checked => 1);
118     }
119     if ($input->param('opac_display')) {
120         $template->param(opac_display_checked => 1);
121     }
122     if ($input->param('opac_editable')) {
123         $template->param(opac_editable_checked => 1);
124     }
125     if ($input->param('staff_searchable')) {
126         $template->param(staff_searchable_checked => 1);
127     }
128     if ($input->param('display_checkout')) {
129         $template->param(display_checkout_checked => 'checked="checked"');
130     }
131
132     $template->param( category_code => scalar $input->param('category_code') );
133     $template->param( class => scalar $input->param('class') );
134
135     $template->param(
136         attribute_type_form => 1,
137         confirm_op => 'add_attribute_type_confirmed',
138         authorised_value_category => scalar $input->param('authorised_value_category'),
139     );
140 }
141
142 sub add_update_attribute_type {
143     my $op = shift;
144     my $template = shift;
145     my $code = shift;
146
147     my $description = $input->param('description');
148
149     my $attr_type;
150     if ($op eq 'edit') {
151         $attr_type = C4::Members::AttributeTypes->fetch($code);
152         $attr_type->description($description);
153     } else {
154         my $existing = C4::Members::AttributeTypes->fetch($code);
155         if (defined($existing)) {
156             $template->param(duplicate_code_error => $code);
157             error_add_attribute_type_form($template);
158             return 0;
159         }
160         $attr_type = C4::Members::AttributeTypes->new($code, $description);
161         my $repeatable = $input->param('repeatable');
162         $attr_type->repeatable($repeatable);
163         my $unique_id = $input->param('unique_id');
164         $attr_type->unique_id($unique_id);
165     }
166
167     my $opac_display = $input->param('opac_display');
168     $attr_type->opac_display($opac_display);
169     my $opac_editable = $input->param('opac_editable');
170     $attr_type->opac_editable($opac_editable);
171     my $staff_searchable = $input->param('staff_searchable');
172     $attr_type->staff_searchable($staff_searchable);
173     my $authorised_value_category = $input->param('authorised_value_category');
174     $attr_type->authorised_value_category($authorised_value_category);
175     my $display_checkout = $input->param('display_checkout');
176     $attr_type->display_checkout($display_checkout);
177     $attr_type->category_code(scalar $input->param('category_code'));
178     $attr_type->class(scalar $input->param('class'));
179     my @branches = $input->multi_param('branches');
180     $attr_type->branches( \@branches );
181
182     if ($op eq 'edit') {
183         $template->param(edited_attribute_type => $attr_type->code());
184     } else {
185         $template->param(added_attribute_type => $attr_type->code());
186     }
187     $attr_type->store();
188
189     return 1;
190 }
191
192 sub delete_attribute_type_form {
193     my $template = shift;
194     my $code = shift;
195
196     my $attr_type = C4::Members::AttributeTypes->fetch($code);
197     my $display_list = 0;
198     if (defined($attr_type)) {
199         $template->param(
200             delete_attribute_type_form => 1,
201             confirm_op => "delete_attribute_type_confirmed",
202             code => $code,
203             description => $attr_type->description(),
204         );
205     } else {
206         $template->param(ERROR_delete_not_found => $code);
207         $display_list = 1;
208     }
209     return $display_list;
210 }
211
212 sub delete_attribute_type {
213     my $template = shift;
214     my $code = shift;
215
216     my $attr_type = C4::Members::AttributeTypes->fetch($code);
217     if (defined($attr_type)) {
218         if ($attr_type->num_patrons() > 0) {
219             $template->param(ERROR_delete_in_use => $code);
220             $template->param(ERROR_num_patrons => $attr_type->num_patrons());
221         } else {
222             $attr_type->delete();
223             $template->param(deleted_attribute_type => $code);
224         }
225     } else {
226         $template->param(ERROR_delete_not_found => $code);
227     }
228 }
229
230 sub edit_attribute_type_form {
231     my $template = shift;
232     my $code = shift;
233
234     my $attr_type = C4::Members::AttributeTypes->fetch($code);
235
236     $template->param(code => $code);
237     $template->param(description => $attr_type->description());
238     $template->param(class => $attr_type->class());
239
240     if ($attr_type->repeatable()) {
241         $template->param(repeatable_checked => 1);
242     }
243     $template->param(repeatable_disabled => 1);
244     if ($attr_type->unique_id()) {
245         $template->param(unique_id_checked => 1);
246     }
247     $template->param(unique_id_disabled => 1);
248     if ($attr_type->opac_display()) {
249         $template->param(opac_display_checked => 1);
250     }
251     if ($attr_type->opac_editable()) {
252         $template->param(opac_editable_checked => 1);
253     }
254     if ($attr_type->staff_searchable()) {
255         $template->param(staff_searchable_checked => 1);
256     }
257     if ($attr_type->display_checkout()) {
258         $template->param(display_checkout_checked => 'checked="checked"');
259     }
260     $template->param( authorised_value_category => $attr_type->authorised_value_category() );
261     $template->param(classes_val_loop => GetAuthorisedValues( 'PA_CLASS' ));
262
263     my $branches = Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed;
264     my @branches_loop;
265     my $selected_branches = $attr_type->branches;
266     foreach my $branch (@$branches) {
267         my $selected = ( grep {$_->{branchcode} eq $branch->{branchcode}} @$selected_branches ) ? 1 : 0;
268         push @branches_loop, {
269             branchcode => $branch->{branchcode},
270             branchname => $branch->{branchname},
271             selected => $selected,
272         };
273     }
274     $template->param( branches_loop => \@branches_loop );
275
276     $template->param(
277         category_code        => $attr_type->category_code,
278         category_class       => $attr_type->class,
279         category_description => $attr_type->category_description,
280     );
281
282     my $patron_categories = Koha::Patron::Categories->search({}, {order_by => ['description']});
283     $template->param(
284         attribute_type_form => 1,
285         edit_attribute_type => 1,
286         confirm_op => 'edit_attribute_type_confirmed',
287         categories => $patron_categories,
288     );
289
290 }
291
292 sub patron_attribute_type_list {
293     my $template = shift;
294
295     my @attr_types = C4::Members::AttributeTypes::GetAttributeTypes( 1, 1 );
296
297     my @classes = uniq( map { $_->{class} } @attr_types );
298     @classes = sort @classes;
299
300     my @attributes_loop;
301     for my $class (@classes) {
302         my ( @items, $branches );
303         for my $attr (@attr_types) {
304             next if $attr->{class} ne $class;
305             my $attr_type = C4::Members::AttributeTypes->fetch($attr->{code});
306             $attr->{branches} = $attr_type->branches;
307             push @items, $attr;
308         }
309         my $av = Koha::AuthorisedValues->search({ category => 'PA_CLASS', authorised_value => $class });
310         my $lib = $av->count ? $av->next->lib : $class;
311         push @attributes_loop, {
312             class => $class,
313             items => \@items,
314             lib   => $lib,
315             branches => $branches,
316         };
317     }
318     $template->param(available_attribute_types => \@attributes_loop);
319     $template->param(display_list => 1);
320 }