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