Bug 12267: Remove borrower_attributes.password
[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::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.tt",
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     $template->param(classes_val_loop => GetAuthorisedValues( 'PA_CLASS'));
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('opac_display')) {
117         $template->param(opac_display_checked => 1);
118     }
119     if ($input->param('staff_searchable')) {
120         $template->param(staff_searchable_checked => 1);
121     }
122     if ($input->param('display_checkout')) {
123         $template->param(display_checkout_checked => 'checked="checked"');
124     }
125
126     $template->param( category_code => $input->param('category_code') );
127     $template->param( class => $input->param('class') );
128
129     $template->param(
130         attribute_type_form => 1,
131         confirm_op => 'add_attribute_type_confirmed',
132     );
133     authorised_value_category_list($template, $input->param('authorised_value_category'));
134 }
135
136 sub add_update_attribute_type {
137     my $op = shift;
138     my $template = shift;
139     my $code = shift;
140
141     my $description = $input->param('description');
142
143     my $attr_type;
144     if ($op eq 'edit') {
145         $attr_type = C4::Members::AttributeTypes->fetch($code);
146         $attr_type->description($description);
147     } else {
148         my $existing = C4::Members::AttributeTypes->fetch($code);
149         if (defined($existing)) {
150             $template->param(duplicate_code_error => $code);
151             error_add_attribute_type_form($template);
152             return 0;
153         }
154         $attr_type = C4::Members::AttributeTypes->new($code, $description);
155         my $repeatable = $input->param('repeatable');
156         $attr_type->repeatable($repeatable);
157         my $unique_id = $input->param('unique_id');
158         $attr_type->unique_id($unique_id);
159     }
160
161     my $opac_display = $input->param('opac_display');
162     $attr_type->opac_display($opac_display);
163     my $staff_searchable = $input->param('staff_searchable');
164     $attr_type->staff_searchable($staff_searchable);
165     my $authorised_value_category = $input->param('authorised_value_category');
166     $attr_type->authorised_value_category($authorised_value_category);
167     my $display_checkout = $input->param('display_checkout');
168     $attr_type->display_checkout($display_checkout);
169     $attr_type->category_code($input->param('category_code'));
170     $attr_type->class($input->param('class'));
171     my @branches = $input->param('branches');
172     $attr_type->branches( \@branches );
173
174     if ($op eq 'edit') {
175         $template->param(edited_attribute_type => $attr_type->code());
176     } else {
177         $template->param(added_attribute_type => $attr_type->code());
178     }
179     $attr_type->store();
180
181     return 1;
182 }
183
184 sub delete_attribute_type_form {
185     my $template = shift;
186     my $code = shift;
187
188     my $attr_type = C4::Members::AttributeTypes->fetch($code);
189     my $display_list = 0;
190     if (defined($attr_type)) {
191         $template->param(
192             delete_attribute_type_form => 1,
193             confirm_op => "delete_attribute_type_confirmed",
194             code => $code,
195             description => $attr_type->description(),
196         );
197     } else {
198         $template->param(ERROR_delete_not_found => $code);
199         $display_list = 1;
200     }
201     return $display_list;
202 }
203
204 sub delete_attribute_type {
205     my $template = shift;
206     my $code = shift;
207
208     my $attr_type = C4::Members::AttributeTypes->fetch($code);
209     if (defined($attr_type)) {
210         if ($attr_type->num_patrons() > 0) {
211             $template->param(ERROR_delete_in_use => $code);
212             $template->param(ERROR_num_patrons => $attr_type->num_patrons());
213         } else {
214             $attr_type->delete();
215             $template->param(deleted_attribute_type => $code);
216         }
217     } else {
218         $template->param(ERROR_delete_not_found => $code);
219     }
220 }
221
222 sub edit_attribute_type_form {
223     my $template = shift;
224     my $code = shift;
225
226     my $attr_type = C4::Members::AttributeTypes->fetch($code);
227
228     $template->param(code => $code);
229     $template->param(description => $attr_type->description());
230     $template->param(class => $attr_type->class());
231
232     if ($attr_type->repeatable()) {
233         $template->param(repeatable_checked => 1);
234     }
235     $template->param(repeatable_disabled => 1);
236     if ($attr_type->unique_id()) {
237         $template->param(unique_id_checked => 1);
238     }
239     $template->param(unique_id_disabled => 1);
240     if ($attr_type->opac_display()) {
241         $template->param(opac_display_checked => 1);
242     }
243     if ($attr_type->staff_searchable()) {
244         $template->param(staff_searchable_checked => 1);
245     }
246     if ($attr_type->display_checkout()) {
247         $template->param(display_checkout_checked => 'checked="checked"');
248     }
249     authorised_value_category_list($template, $attr_type->authorised_value_category());
250     $template->param(classes_val_loop => GetAuthorisedValues( 'PA_CLASS' ));
251
252
253     my $branches = GetBranches;
254     my @branches_loop;
255     my $selected_branches = $attr_type->branches;
256     foreach my $branch (sort keys %$branches) {
257         my $selected = ( grep {$$_{branchcode} eq $branch} @$selected_branches ) ? 1 : 0;
258         push @branches_loop, {
259             branchcode => $branches->{$branch}{branchcode},
260             branchname => $branches->{$branch}{branchname},
261             selected => $selected,
262         };
263     }
264     $template->param( branches_loop => \@branches_loop );
265
266     $template->param(
267         category_code        => $attr_type->category_code,
268         category_class       => $attr_type->class,
269         category_description => $attr_type->category_description,
270     );
271
272     $template->param(
273         attribute_type_form => 1,
274         edit_attribute_type => 1,
275         confirm_op => 'edit_attribute_type_confirmed',
276         categories => GetBorrowercategoryList,
277     );
278
279 }
280
281 sub patron_attribute_type_list {
282     my $template = shift;
283
284     my @attr_types = C4::Members::AttributeTypes::GetAttributeTypes( 1, 1 );
285
286     my @classes = uniq( map { $_->{class} } @attr_types );
287     @classes = sort @classes;
288
289     my @attributes_loop;
290     for my $class (@classes) {
291         my ( @items, $branches );
292         for my $attr (@attr_types) {
293             next if $attr->{class} ne $class;
294             my $attr_type = C4::Members::AttributeTypes->fetch($attr->{code});
295             $attr->{branches} = $attr_type->branches;
296             push @items, $attr;
297         }
298         my $lib = GetAuthorisedValueByCode( 'PA_CLASS', $class ) || $class;
299         push @attributes_loop, {
300             class => $class,
301             items => \@items,
302             lib   => $lib,
303             branches => $branches,
304         };
305     }
306     $template->param(available_attribute_types => \@attributes_loop);
307     $template->param(display_list => 1);
308 }
309
310 sub authorised_value_category_list {
311     my $template = shift;
312     my $selected = @_ ? shift : '';
313
314     my $categories = GetAuthorisedValueCategories();
315     my @list = ();
316     foreach my $category (@$categories) {
317         my $entry = { category => $category };
318         $entry->{selected} = 1 if $category eq $selected;
319         push @list, $entry;
320     }
321     $template->param(authorised_value_categories => \@list);
322 }