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