Bug 36277: Avoid useless warnings
[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 qw( get_template_and_user );
28 use C4::Context;
29 use C4::Output qw( output_html_with_http_headers );
30 use Koha::Patron::Attribute::Types;
31
32 use Koha::AuthorisedValues;
33 use Koha::Libraries;
34 use Koha::Patron::Categories;
35
36 my $script_name = "/cgi-bin/koha/admin/patron-attr-types.pl";
37
38 our $input = CGI->new;
39 my $op = $input->param('op') || '';
40
41
42 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
43     {   template_name   => "admin/patron-attr-types.tt",
44         query           => $input,
45         type            => "intranet",
46         flagsrequired => { parameters => 'manage_patron_attributes' }
47     }
48 );
49
50
51 $template->param(script_name => $script_name);
52
53 my $code = $input->param("code");
54
55 my $display_list = 0;
56 if ($op eq "edit_attribute_type") {
57     edit_attribute_type_form($template, $code);
58 } elsif ($op eq "edit_attribute_type_confirmed") {
59     $display_list = add_update_attribute_type('edit', $template, $code);
60 } elsif ($op eq "add_attribute_type") {
61     add_attribute_type_form($template);
62 } elsif ($op eq "add_attribute_type_confirmed") {
63     $display_list = add_update_attribute_type('add', $template, $code);
64 } elsif ($op eq "delete_attribute_type") {
65     $display_list = delete_attribute_type_form($template, $code);
66 } elsif ($op eq "delete_attribute_type_confirmed") {
67     delete_attribute_type($template, $code);
68     $display_list = 1;
69 } else {
70     $display_list = 1;
71 }
72
73 if ($display_list) {
74     unless (C4::Context->preference('ExtendedPatronAttributes')) {
75         $template->param(WARNING_extended_attributes_off => 1); 
76     }
77     patron_attribute_type_list($template);
78 }
79
80 output_html_with_http_headers $input, $cookie, $template->output;
81
82 exit 0;
83
84 sub add_attribute_type_form {
85     my $template = shift;
86
87     my $patron_categories = Koha::Patron::Categories->search_with_library_limits({}, {order_by => ['description']});
88     $template->param(
89         attribute_type_form => 1,
90         confirm_op => 'add_attribute_type_confirmed',
91         categories => $patron_categories,
92     );
93 }
94
95 sub error_add_attribute_type_form {
96     my $template = shift;
97
98     $template->param(description => scalar $input->param('description'));
99     $template->param( category_code => scalar $input->param('category_code') );
100     $template->param( class => scalar $input->param('class') );
101
102     $template->param(
103         attribute_type_form => 1,
104         confirm_op => 'add_attribute_type_confirmed',
105         authorised_value_category => scalar $input->param('authorised_value_category'),
106     );
107 }
108
109 sub add_update_attribute_type {
110     my $op       = shift;
111     my $template = shift;
112     my $code     = shift;
113
114     my $description               = $input->param('description');
115     my $repeatable                = $input->param('repeatable') ? 1 : 0;
116     my $unique_id                 = $input->param('unique_id') ? 1 : 0;
117     my $opac_display              = $input->param('opac_display') ? 1 : 0;
118     my $opac_editable             = $input->param('opac_editable') ? 1 : 0;
119     my $staff_searchable          = $input->param('staff_searchable') ? 1 : 0;
120     my $searched_by_default       = $input->param('searched_by_default') ? 1 : 0;
121     my $keep_for_pseudonymization = $input->param('keep_for_pseudonymization') ? 1 : 0;
122     my $mandatory                 = $input->param('mandatory') ? 1 : 0;
123     my $authorised_value_category = $input->param('authorised_value_category');
124     my $display_checkout          = $input->param('display_checkout') ? 1 : 0;
125     my $category_code             = $input->param('category_code') || undef;
126     my $class                     = $input->param('class');
127
128     my $attr_type = Koha::Patron::Attribute::Types->find($code);
129     if ( $op eq 'edit' ) {
130         $attr_type->description($description);
131     }
132     else {
133         if ($attr_type) {    # Already exists
134             $template->param( duplicate_code_error => $code );
135
136             # FIXME Regression here
137             # Form will not be refilled with entered values on error
138             error_add_attribute_type_form($template);
139             return 0;
140         }
141         $attr_type = Koha::Patron::Attribute::Type->new(
142             {
143                 code        => $code,
144                 description => $description,
145             }
146         );
147     }
148
149     $attr_type->set(
150         {
151             repeatable                => $repeatable,
152             unique_id                 => $unique_id,
153             opac_display              => $opac_display,
154             opac_editable             => $opac_editable,
155             staff_searchable          => $staff_searchable,
156             searched_by_default       => $searched_by_default,
157             keep_for_pseudonymization => $keep_for_pseudonymization,
158             mandatory                 => $mandatory,
159             authorised_value_category => $authorised_value_category,
160             display_checkout          => $display_checkout,
161             category_code             => $category_code,
162             class                     => $class,
163         }
164     )->store;
165
166     my @branches = grep { ! /^\s*$/ } $input->multi_param('branches');
167     $attr_type->library_limits( \@branches );
168
169     if ( $op eq 'edit' ) {
170         $template->param( edited_attribute_type => $attr_type->code() );
171     }
172     else {
173         $template->param( added_attribute_type => $attr_type->code() );
174     }
175
176     return 1;
177 }
178
179 sub delete_attribute_type_form {
180     my $template = shift;
181     my $code = shift;
182
183     my $attr_type = Koha::Patron::Attribute::Types->find($code);
184     my $display_list = 0;
185     if (defined($attr_type)) {
186         $template->param(
187             delete_attribute_type_form => 1,
188             confirm_op => "delete_attribute_type_confirmed",
189             code => $code,
190             description => $attr_type->description(),
191         );
192     } else {
193         $template->param(ERROR_delete_not_found => $code);
194         $display_list = 1;
195     }
196     return $display_list;
197 }
198
199 sub delete_attribute_type {
200     my $template = shift;
201     my $code = shift;
202
203     my $attr_type = Koha::Patron::Attribute::Types->find($code);
204     if (defined($attr_type)) {
205         # TODO Check must be done for previous step as well
206         if ( my $num_patrons = Koha::Patrons->filter_by_attribute_type($code)->count ) {
207             $template->param(ERROR_delete_in_use => $code);
208             $template->param(ERROR_num_patrons => $num_patrons );
209         } else {
210             $attr_type->delete();
211             $template->param(deleted_attribute_type => $code);
212         }
213     } else {
214         # FIXME Really needed?
215         $template->param(ERROR_delete_not_found => $code);
216     }
217 }
218
219 sub edit_attribute_type_form {
220     my $template = shift;
221     my $code = shift;
222
223     my $attr_type = Koha::Patron::Attribute::Types->find($code);
224
225     my $patron_categories = Koha::Patron::Categories->search({}, {order_by => ['description']});
226
227     my $can_be_set_to_nonrepeatable = 1;
228     if ( $attr_type->repeatable == 1 ) {
229         $attr_type->repeatable(0);
230         eval {$attr_type->check_repeatables};
231         $can_be_set_to_nonrepeatable = 0 if $@;
232         $attr_type->repeatable(1);
233     }
234     my $can_be_set_to_unique = 1;
235     if ( $attr_type->unique_id == 0 ) {
236         $attr_type->unique_id(1);
237         eval {$attr_type->check_unique_ids};
238         $can_be_set_to_unique = 0 if $@;
239         $attr_type->unique_id(0);
240     }
241     $template->param(
242         attribute_type => $attr_type,
243         attribute_type_form => 1,
244         edit_attribute_type => 1,
245         can_be_set_to_nonrepeatable => $can_be_set_to_nonrepeatable,
246         can_be_set_to_unique => $can_be_set_to_unique,
247         confirm_op => 'edit_attribute_type_confirmed',
248         categories => $patron_categories,
249     );
250
251 }
252
253 sub patron_attribute_type_list {
254     my $template = shift;
255
256     my @attr_types = Koha::Patron::Attribute::Types->search->as_list;
257
258     my @classes = uniq( map { $_->class } @attr_types );
259     @classes = sort @classes;
260
261     my @attributes_loop;
262     # FIXME This is not efficient and should be improved
263     for my $class (@classes) {
264         my @items;
265         for my $attr (@attr_types) {
266             next if $attr->class ne $class;
267             push @items, $attr;
268         }
269         my $av = Koha::AuthorisedValues->search({ category => 'PA_CLASS', authorised_value => $class });
270         my $lib = $av->count ? $av->next->lib : $class;
271         push @attributes_loop, {
272             class => $class,
273             items => \@items,
274             lib   => $lib,
275         };
276     }
277     $template->param(available_attribute_types => \@attributes_loop);
278     $template->param(display_list => 1);
279 }