Bug 10694: (follow-up) fix various issues
[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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #
21
22 use Modern::Perl;
23
24 use CGI;
25 use List::MoreUtils qw/uniq/;
26
27 use C4::Auth;
28 use C4::Branch;
29 use C4::Context;
30 use C4::Output;
31 use C4::Koha;
32 use C4::Members qw/GetBorrowercategoryList/;
33 use C4::Members::AttributeTypes;
34
35 my $script_name = "/cgi-bin/koha/admin/patron-attr-types.pl";
36
37 our $input = new CGI;
38 my $op = $input->param('op') || '';
39
40
41 our ($template, $loggedinuser, $cookie)
42     = get_template_and_user({template_name => "admin/patron-attr-types.tmpl",
43                  query => $input,
44                  type => "intranet",
45                  authnotrequired => 0,
46                  flagsrequired => {parameters => 'parameters_remaining_permissions'},
47                  debug => 1,
48                  });
49
50 $template->param(script_name => $script_name);
51
52 my $code = $input->param("code");
53
54 my $display_list = 0;
55 if ($op eq "edit_attribute_type") {
56     edit_attribute_type_form($template, $code);
57 } elsif ($op eq "edit_attribute_type_confirmed") {
58     $display_list = add_update_attribute_type('edit', $template, $code);
59 } elsif ($op eq "add_attribute_type") {
60     add_attribute_type_form($template);
61 } elsif ($op eq "add_attribute_type_confirmed") {
62     $display_list = add_update_attribute_type('add', $template, $code);
63 } elsif ($op eq "delete_attribute_type") {
64     $display_list = delete_attribute_type_form($template, $code);
65 } elsif ($op eq "delete_attribute_type_confirmed") {
66     delete_attribute_type($template, $code);
67     $display_list = 1;
68 } else {
69     $display_list = 1;
70 }
71
72 if ($display_list) {
73     unless (C4::Context->preference('ExtendedPatronAttributes')) {
74         $template->param(WARNING_extended_attributes_off => 1); 
75     }
76     patron_attribute_type_list($template);
77 }
78
79 output_html_with_http_headers $input, $cookie, $template->output;
80
81 exit 0;
82
83 sub add_attribute_type_form {
84     my $template = shift;
85
86     my $branches = GetBranches;
87     my @branches_loop;
88     foreach my $branch (sort keys %$branches) {
89         push @branches_loop, {
90             branchcode => $$branches{$branch}{branchcode},
91             branchname => $$branches{$branch}{branchname},
92         };
93     }
94
95     $template->param(
96         attribute_type_form => 1,
97         confirm_op => 'add_attribute_type_confirmed',
98         categories => GetBorrowercategoryList,
99         branches_loop => \@branches_loop,
100     );
101     authorised_value_category_list($template);
102     pa_classes($template);
103 }
104
105 sub error_add_attribute_type_form {
106     my $template = shift;
107
108     $template->param(description => $input->param('description'));
109
110     if ($input->param('repeatable')) {
111         $template->param(repeatable_checked => 1);
112     }
113     if ($input->param('unique_id')) {
114         $template->param(unique_id_checked => 1);
115     }
116     if ($input->param('password_allowed')) {
117         $template->param(password_allowed_checked => 1);
118     }
119     if ($input->param('opac_display')) {
120         $template->param(opac_display_checked => 1);
121     }
122     if ($input->param('staff_searchable')) {
123         $template->param(staff_searchable_checked => 1);
124     }
125     if ($input->param('display_checkout')) {
126         $template->param(display_checkout_checked => 'checked="checked"');
127     }
128
129     $template->param( category_code => $input->param('category_code') );
130     $template->param( class => $input->param('class') );
131
132     $template->param(
133         attribute_type_form => 1,
134         confirm_op => 'add_attribute_type_confirmed',
135     );
136     authorised_value_category_list($template, $input->param('authorised_value_category'));
137 }
138
139 sub add_update_attribute_type {
140     my $op = shift;
141     my $template = shift;
142     my $code = shift;
143
144     my $description = $input->param('description');
145
146     my $attr_type;
147     if ($op eq 'edit') {
148         $attr_type = C4::Members::AttributeTypes->fetch($code);
149         $attr_type->description($description);
150     } else {
151         my $existing = C4::Members::AttributeTypes->fetch($code);
152         if (defined($existing)) {
153             $template->param(duplicate_code_error => $code);
154             error_add_attribute_type_form($template);
155             return 0;
156         }
157         $attr_type = C4::Members::AttributeTypes->new($code, $description);
158         my $repeatable = $input->param('repeatable');
159         $attr_type->repeatable($repeatable);
160         my $unique_id = $input->param('unique_id');
161         $attr_type->unique_id($unique_id);
162     }
163
164     my $opac_display = $input->param('opac_display');
165     $attr_type->opac_display($opac_display);
166     my $staff_searchable = $input->param('staff_searchable');
167     $attr_type->staff_searchable($staff_searchable);
168     my $authorised_value_category = $input->param('authorised_value_category');
169     $attr_type->authorised_value_category($authorised_value_category);
170     my $password_allowed = $input->param('password_allowed');
171     $attr_type->password_allowed($password_allowed);
172     my $display_checkout = $input->param('display_checkout');
173     $attr_type->display_checkout($display_checkout);
174     $attr_type->category_code($input->param('category_code'));
175     $attr_type->class($input->param('class'));
176     my @branches = $input->param('branches');
177     $attr_type->branches( \@branches );
178
179     if ($op eq 'edit') {
180         $template->param(edited_attribute_type => $attr_type->code());
181     } else {
182         $template->param(added_attribute_type => $attr_type->code());
183     }
184     $attr_type->store();
185
186     return 1;
187 }
188
189 sub delete_attribute_type_form {
190     my $template = shift;
191     my $code = shift;
192
193     my $attr_type = C4::Members::AttributeTypes->fetch($code);
194     my $display_list = 0;
195     if (defined($attr_type)) {
196         $template->param(
197             delete_attribute_type_form => 1,
198             confirm_op => "delete_attribute_type_confirmed",
199             code => $code,
200             description => $attr_type->description(),
201         );
202     } else {
203         $template->param(ERROR_delete_not_found => $code);
204         $display_list = 1;
205     }
206     return $display_list;
207 }
208
209 sub delete_attribute_type {
210     my $template = shift;
211     my $code = shift;
212
213     my $attr_type = C4::Members::AttributeTypes->fetch($code);
214     if (defined($attr_type)) {
215         if ($attr_type->num_patrons() > 0) {
216             $template->param(ERROR_delete_in_use => $code);
217             $template->param(ERROR_num_patrons => $attr_type->num_patrons());
218         } else {
219             $attr_type->delete();
220             $template->param(deleted_attribute_type => $code);
221         }
222     } else {
223         $template->param(ERROR_delete_not_found => $code);
224     }
225 }
226
227 sub edit_attribute_type_form {
228     my $template = shift;
229     my $code = shift;
230
231     my $attr_type = C4::Members::AttributeTypes->fetch($code);
232
233     $template->param(code => $code);
234     $template->param(description => $attr_type->description());
235     $template->param(class => $attr_type->class());
236
237     if ($attr_type->repeatable()) {
238         $template->param(repeatable_checked => 1);
239     }
240     $template->param(repeatable_disabled => 1);
241     if ($attr_type->unique_id()) {
242         $template->param(unique_id_checked => 1);
243     }
244     $template->param(unique_id_disabled => 1);
245     if ($attr_type->password_allowed()) {
246         $template->param(password_allowed_checked => 1);
247     }
248     if ($attr_type->opac_display()) {
249         $template->param(opac_display_checked => 1);
250     }
251     if ($attr_type->staff_searchable()) {
252         $template->param(staff_searchable_checked => 1);
253     }
254     if ($attr_type->display_checkout()) {
255         $template->param(display_checkout_checked => 'checked="checked"');
256     }
257     authorised_value_category_list($template, $attr_type->authorised_value_category());
258     pa_classes( $template, $attr_type->class );
259
260
261     my $branches = GetBranches;
262     my @branches_loop;
263     my $selected_branches = $attr_type->branches;
264     foreach my $branch (sort keys %$branches) {
265         my $selected = ( grep {$$_{branchcode} eq $branch} @$selected_branches ) ? 1 : 0;
266         push @branches_loop, {
267             branchcode => $branches->{$branch}{branchcode},
268             branchname => $branches->{$branch}{branchname},
269             selected => $selected,
270         };
271     }
272     $template->param( branches_loop => \@branches_loop );
273
274     $template->param ( category_code => $attr_type->category_code );
275     $template->param ( category_description => $attr_type->category_description );
276
277     $template->param(
278         attribute_type_form => 1,
279         edit_attribute_type => 1,
280         confirm_op => 'edit_attribute_type_confirmed',
281         categories => GetBorrowercategoryList,
282     );
283
284 }
285
286 sub patron_attribute_type_list {
287     my $template = shift;
288
289     my @attr_types = C4::Members::AttributeTypes::GetAttributeTypes( 1, 1 );
290
291     my @classes = uniq( map { $_->{class} } @attr_types );
292     @classes = sort @classes;
293
294     my @attributes_loop;
295     for my $class (@classes) {
296         my ( @items, $branches );
297         for my $attr (@attr_types) {
298             next if $attr->{class} ne $class;
299             my $attr_type = C4::Members::AttributeTypes->fetch($attr->{code});
300             $attr->{branches} = $attr_type->branches;
301             push @items, $attr;
302         }
303         my $lib = GetAuthorisedValueByCode( 'PA_CLASS', $class ) || $class;
304         push @attributes_loop, {
305             class => $class,
306             items => \@items,
307             lib   => $lib,
308             branches => $branches,
309         };
310     }
311     $template->param(available_attribute_types => \@attributes_loop);
312     $template->param(display_list => 1);
313 }
314
315 sub authorised_value_category_list {
316     my $template = shift;
317     my $selected = @_ ? shift : '';
318
319     my $categories = GetAuthorisedValueCategories();
320     my @list = ();
321     foreach my $category (@$categories) {
322         my $entry = { category => $category };
323         $entry->{selected} = 1 if $category eq $selected;
324         push @list, $entry;
325     }
326     $template->param(authorised_value_categories => \@list);
327 }
328
329 sub pa_classes {
330     my $template = shift;
331     my $selected = @_ ? shift : '';
332
333     $template->param(classes_val_loop => GetAuthorisedValues( 'PA_CLASS', $selected ) );
334 }