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