3 # Copyright 2008 LibLime
4 # Parts copyright 2010 BibLibre
6 # This file is part of Koha.
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.
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.
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>.
25 use List::MoreUtils qw( uniq );
27 use C4::Auth qw( get_template_and_user );
29 use C4::Output qw( output_html_with_http_headers );
30 use Koha::Patron::Attribute::Types;
32 use Koha::AuthorisedValues;
34 use Koha::Patron::Categories;
36 my $script_name = "/cgi-bin/koha/admin/patron-attr-types.pl";
38 our $input = CGI->new;
39 my $op = $input->param('op') || '';
42 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
43 { template_name => "admin/patron-attr-types.tt",
46 flagsrequired => { parameters => 'manage_patron_attributes' }
51 $template->param(script_name => $script_name);
53 my $code = $input->param("code");
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);
74 unless (C4::Context->preference('ExtendedPatronAttributes')) {
75 $template->param(WARNING_extended_attributes_off => 1);
77 patron_attribute_type_list($template);
80 output_html_with_http_headers $input, $cookie, $template->output;
84 sub add_attribute_type_form {
87 my $patron_categories = Koha::Patron::Categories->search_with_library_limits({}, {order_by => ['description']});
89 attribute_type_form => 1,
90 confirm_op => 'add_attribute_type_confirmed',
91 categories => $patron_categories,
95 sub error_add_attribute_type_form {
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') );
103 attribute_type_form => 1,
104 confirm_op => 'add_attribute_type_confirmed',
105 authorised_value_category => scalar $input->param('authorised_value_category'),
109 sub add_update_attribute_type {
111 my $template = shift;
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 $keep_for_pseudonymization = $input->param('keep_for_pseudonymization') ? 1 : 0;
121 my $mandatory = $input->param('mandatory') ? 1 : 0;
122 my $authorised_value_category = $input->param('authorised_value_category');
123 my $display_checkout = $input->param('display_checkout') ? 1 : 0;
124 my $category_code = $input->param('category_code') || undef;
125 my $class = $input->param('class');
127 my $attr_type = Koha::Patron::Attribute::Types->find($code);
128 if ( $op eq 'edit' ) {
129 $attr_type->description($description);
132 if ($attr_type) { # Already exists
133 $template->param( duplicate_code_error => $code );
135 # FIXME Regression here
136 # Form will not be refilled with entered values on error
137 error_add_attribute_type_form($template);
140 $attr_type = Koha::Patron::Attribute::Type->new(
143 description => $description,
150 repeatable => $repeatable,
151 unique_id => $unique_id,
152 opac_display => $opac_display,
153 opac_editable => $opac_editable,
154 staff_searchable => $staff_searchable,
155 keep_for_pseudonymization => $keep_for_pseudonymization,
156 mandatory => $mandatory,
157 authorised_value_category => $authorised_value_category,
158 display_checkout => $display_checkout,
159 category_code => $category_code,
164 my @branches = grep { ! /^\s*$/ } $input->multi_param('branches');
165 $attr_type->library_limits( \@branches );
167 if ( $op eq 'edit' ) {
168 $template->param( edited_attribute_type => $attr_type->code() );
171 $template->param( added_attribute_type => $attr_type->code() );
177 sub delete_attribute_type_form {
178 my $template = shift;
181 my $attr_type = Koha::Patron::Attribute::Types->find($code);
182 my $display_list = 0;
183 if (defined($attr_type)) {
185 delete_attribute_type_form => 1,
186 confirm_op => "delete_attribute_type_confirmed",
188 description => $attr_type->description(),
191 $template->param(ERROR_delete_not_found => $code);
194 return $display_list;
197 sub delete_attribute_type {
198 my $template = shift;
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 );
208 $attr_type->delete();
209 $template->param(deleted_attribute_type => $code);
212 # FIXME Really needed?
213 $template->param(ERROR_delete_not_found => $code);
217 sub edit_attribute_type_form {
218 my $template = shift;
221 my $attr_type = Koha::Patron::Attribute::Types->find($code);
223 my $patron_categories = Koha::Patron::Categories->search({}, {order_by => ['description']});
225 my $can_be_set_to_nonrepeatable = 1;
226 if ( $attr_type->repeatable == 1 ) {
227 $attr_type->repeatable(0);
228 eval {$attr_type->check_repeatables};
229 $can_be_set_to_nonrepeatable = 0 if $@;
230 $attr_type->repeatable(1);
232 my $can_be_set_to_unique = 1;
233 if ( $attr_type->unique_id == 0 ) {
234 $attr_type->unique_id(1);
235 eval {$attr_type->check_unique_ids};
236 $can_be_set_to_unique = 0 if $@;
237 $attr_type->unique_id(0);
240 attribute_type => $attr_type,
241 attribute_type_form => 1,
242 edit_attribute_type => 1,
243 can_be_set_to_nonrepeatable => $can_be_set_to_nonrepeatable,
244 can_be_set_to_unique => $can_be_set_to_unique,
245 confirm_op => 'edit_attribute_type_confirmed',
246 categories => $patron_categories,
251 sub patron_attribute_type_list {
252 my $template = shift;
254 my @attr_types = Koha::Patron::Attribute::Types->search->as_list;
256 my @classes = uniq( map { $_->class } @attr_types );
257 @classes = sort @classes;
260 # FIXME This is not efficient and should be improved
261 for my $class (@classes) {
263 for my $attr (@attr_types) {
264 next if $attr->class ne $class;
267 my $av = Koha::AuthorisedValues->search({ category => 'PA_CLASS', authorised_value => $class });
268 my $lib = $av->count ? $av->next->lib : $class;
269 push @attributes_loop, {
275 $template->param(available_attribute_types => \@attributes_loop);
276 $template->param(display_list => 1);