Bug 17252 - Koha::AuthorisedValues - Remove GetAuthorisedValueByCode
[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 our ($template, $loggedinuser, $cookie)
44     = get_template_and_user({template_name => "admin/patron-attr-types.tt",
45                  query => $input,
46                  type => "intranet",
47                  authnotrequired => 0,
48                  flagsrequired => {parameters => 'parameters_remaining_permissions'},
49                  debug => 1,
50                  });
51
52 $template->param(script_name => $script_name);
53
54 my $code = $input->param("code");
55
56 my $display_list = 0;
57 if ($op eq "edit_attribute_type") {
58     edit_attribute_type_form($template, $code);
59 } elsif ($op eq "edit_attribute_type_confirmed") {
60     $display_list = add_update_attribute_type('edit', $template, $code);
61 } elsif ($op eq "add_attribute_type") {
62     add_attribute_type_form($template);
63 } elsif ($op eq "add_attribute_type_confirmed") {
64     $display_list = add_update_attribute_type('add', $template, $code);
65 } elsif ($op eq "delete_attribute_type") {
66     $display_list = delete_attribute_type_form($template, $code);
67 } elsif ($op eq "delete_attribute_type_confirmed") {
68     delete_attribute_type($template, $code);
69     $display_list = 1;
70 } else {
71     $display_list = 1;
72 }
73
74 if ($display_list) {
75     unless (C4::Context->preference('ExtendedPatronAttributes')) {
76         $template->param(WARNING_extended_attributes_off => 1); 
77     }
78     patron_attribute_type_list($template);
79 }
80
81 output_html_with_http_headers $input, $cookie, $template->output;
82
83 exit 0;
84
85 sub add_attribute_type_form {
86     my $template = shift;
87
88     my $branches = Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed;
89     my @branches_loop;
90     foreach my $branch (@$branches) {
91         push @branches_loop, {
92             branchcode => $branch->{branchcode},
93             branchname => $branch->{branchname},
94         };
95     }
96
97     my $patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
98     $template->param(
99         attribute_type_form => 1,
100         confirm_op => 'add_attribute_type_confirmed',
101         categories => $patron_categories,
102         branches_loop => \@branches_loop,
103     );
104     authorised_value_category_list($template);
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('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 => scalar $input->param('category_code') );
130     $template->param( class => scalar $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 $display_checkout = $input->param('display_checkout');
171     $attr_type->display_checkout($display_checkout);
172     $attr_type->category_code(scalar $input->param('category_code'));
173     $attr_type->class(scalar $input->param('class'));
174     my @branches = $input->multi_param('branches');
175     $attr_type->branches( \@branches );
176
177     if ($op eq 'edit') {
178         $template->param(edited_attribute_type => $attr_type->code());
179     } else {
180         $template->param(added_attribute_type => $attr_type->code());
181     }
182     $attr_type->store();
183
184     return 1;
185 }
186
187 sub delete_attribute_type_form {
188     my $template = shift;
189     my $code = shift;
190
191     my $attr_type = C4::Members::AttributeTypes->fetch($code);
192     my $display_list = 0;
193     if (defined($attr_type)) {
194         $template->param(
195             delete_attribute_type_form => 1,
196             confirm_op => "delete_attribute_type_confirmed",
197             code => $code,
198             description => $attr_type->description(),
199         );
200     } else {
201         $template->param(ERROR_delete_not_found => $code);
202         $display_list = 1;
203     }
204     return $display_list;
205 }
206
207 sub delete_attribute_type {
208     my $template = shift;
209     my $code = shift;
210
211     my $attr_type = C4::Members::AttributeTypes->fetch($code);
212     if (defined($attr_type)) {
213         if ($attr_type->num_patrons() > 0) {
214             $template->param(ERROR_delete_in_use => $code);
215             $template->param(ERROR_num_patrons => $attr_type->num_patrons());
216         } else {
217             $attr_type->delete();
218             $template->param(deleted_attribute_type => $code);
219         }
220     } else {
221         $template->param(ERROR_delete_not_found => $code);
222     }
223 }
224
225 sub edit_attribute_type_form {
226     my $template = shift;
227     my $code = shift;
228
229     my $attr_type = C4::Members::AttributeTypes->fetch($code);
230
231     $template->param(code => $code);
232     $template->param(description => $attr_type->description());
233     $template->param(class => $attr_type->class());
234
235     if ($attr_type->repeatable()) {
236         $template->param(repeatable_checked => 1);
237     }
238     $template->param(repeatable_disabled => 1);
239     if ($attr_type->unique_id()) {
240         $template->param(unique_id_checked => 1);
241     }
242     $template->param(unique_id_disabled => 1);
243     if ($attr_type->opac_display()) {
244         $template->param(opac_display_checked => 1);
245     }
246     if ($attr_type->staff_searchable()) {
247         $template->param(staff_searchable_checked => 1);
248     }
249     if ($attr_type->display_checkout()) {
250         $template->param(display_checkout_checked => 'checked="checked"');
251     }
252     authorised_value_category_list($template, $attr_type->authorised_value_category());
253     $template->param(classes_val_loop => GetAuthorisedValues( 'PA_CLASS' ));
254
255
256     my $branches = Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed;
257     my @branches_loop;
258     my $selected_branches = $attr_type->branches;
259     foreach my $branch (@$branches) {
260         my $selected = ( grep {$_->{branchcode} eq $branch->{branchcode}} @$selected_branches ) ? 1 : 0;
261         push @branches_loop, {
262             branchcode => $branch->{branchcode},
263             branchname => $branch->{branchname},
264             selected => $selected,
265         };
266     }
267     $template->param( branches_loop => \@branches_loop );
268
269     $template->param(
270         category_code        => $attr_type->category_code,
271         category_class       => $attr_type->class,
272         category_description => $attr_type->category_description,
273     );
274
275     my $patron_categories = Koha::Patron::Categories->search({}, {order_by => ['description']});
276     $template->param(
277         attribute_type_form => 1,
278         edit_attribute_type => 1,
279         confirm_op => 'edit_attribute_type_confirmed',
280         categories => $patron_categories,
281     );
282
283 }
284
285 sub patron_attribute_type_list {
286     my $template = shift;
287
288     my @attr_types = C4::Members::AttributeTypes::GetAttributeTypes( 1, 1 );
289
290     my @classes = uniq( map { $_->{class} } @attr_types );
291     @classes = sort @classes;
292
293     my @attributes_loop;
294     for my $class (@classes) {
295         my ( @items, $branches );
296         for my $attr (@attr_types) {
297             next if $attr->{class} ne $class;
298             my $attr_type = C4::Members::AttributeTypes->fetch($attr->{code});
299             $attr->{branches} = $attr_type->branches;
300             push @items, $attr;
301         }
302         my $av = Koha::AuthorisedValues->search({ category => 'PA_CLASS', authorised_value => $class });
303         my $lib = $av->count ? $av->next->lib : $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 }