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/;
31 use Koha::Patron::Attribute::Types;
33 use Koha::AuthorisedValues;
35 use Koha::Patron::Categories;
37 my $script_name = "/cgi-bin/koha/admin/patron-attr-types.pl";
39 our $input = CGI->new;
40 my $op = $input->param('op') || '';
43 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
44 { template_name => "admin/patron-attr-types.tt",
47 flagsrequired => { parameters => 'manage_patron_attributes' }
52 $template->param(script_name => $script_name);
54 my $code = $input->param("code");
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);
75 unless (C4::Context->preference('ExtendedPatronAttributes')) {
76 $template->param(WARNING_extended_attributes_off => 1);
78 patron_attribute_type_list($template);
81 output_html_with_http_headers $input, $cookie, $template->output;
85 sub add_attribute_type_form {
88 my $patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
90 attribute_type_form => 1,
91 confirm_op => 'add_attribute_type_confirmed',
92 categories => $patron_categories,
96 sub error_add_attribute_type_form {
99 $template->param(description => scalar $input->param('description'));
100 $template->param( category_code => scalar $input->param('category_code') );
101 $template->param( class => scalar $input->param('class') );
104 attribute_type_form => 1,
105 confirm_op => 'add_attribute_type_confirmed',
106 authorised_value_category => scalar $input->param('authorised_value_category'),
110 sub add_update_attribute_type {
112 my $template = shift;
115 my $description = $input->param('description');
116 my $repeatable = $input->param('repeatable') ? 1 : 0;
117 my $unique_id = $input->param('unique_id') ? 1 : 0;
118 my $opac_display = $input->param('opac_display') ? 1 : 0;
119 my $opac_editable = $input->param('opac_editable') ? 1 : 0;
120 my $staff_searchable = $input->param('staff_searchable') ? 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');
128 my $attr_type = Koha::Patron::Attribute::Types->find($code);
129 if ( $op eq 'edit' ) {
130 $attr_type->description($description);
133 if ($attr_type) { # Already exists
134 $template->param( duplicate_code_error => $code );
136 # FIXME Regression here
137 # Form will not be refilled with entered values on error
138 error_add_attribute_type_form($template);
141 $attr_type = Koha::Patron::Attribute::Type->new(
144 description => $description,
151 repeatable => $repeatable,
152 unique_id => $unique_id,
153 opac_display => $opac_display,
154 opac_editable => $opac_editable,
155 staff_searchable => $staff_searchable,
156 keep_for_pseudonymization => $keep_for_pseudonymization,
157 mandatory => $mandatory,
158 authorised_value_category => $authorised_value_category,
159 display_checkout => $display_checkout,
160 category_code => $category_code,
165 my @branches = grep { ! /^\s*$/ } $input->multi_param('branches');
166 $attr_type->library_limits( \@branches );
168 if ( $op eq 'edit' ) {
169 $template->param( edited_attribute_type => $attr_type->code() );
172 $template->param( added_attribute_type => $attr_type->code() );
178 sub delete_attribute_type_form {
179 my $template = shift;
182 my $attr_type = Koha::Patron::Attribute::Types->find($code);
183 my $display_list = 0;
184 if (defined($attr_type)) {
186 delete_attribute_type_form => 1,
187 confirm_op => "delete_attribute_type_confirmed",
189 description => $attr_type->description(),
192 $template->param(ERROR_delete_not_found => $code);
195 return $display_list;
198 sub delete_attribute_type {
199 my $template = shift;
202 my $attr_type = Koha::Patron::Attribute::Types->find($code);
203 if (defined($attr_type)) {
204 # TODO Check must be done for previous step as well
205 if ( my $num_patrons = Koha::Patrons->filter_by_attribute_type($code)->count ) {
206 $template->param(ERROR_delete_in_use => $code);
207 $template->param(ERROR_num_patrons => $num_patrons );
209 $attr_type->delete();
210 $template->param(deleted_attribute_type => $code);
213 # FIXME Really needed?
214 $template->param(ERROR_delete_not_found => $code);
218 sub edit_attribute_type_form {
219 my $template = shift;
222 my $attr_type = Koha::Patron::Attribute::Types->find($code);
224 my $patron_categories = Koha::Patron::Categories->search({}, {order_by => ['description']});
226 my $can_be_set_to_nonrepeatable = 1;
227 if ( $attr_type->repeatable == 1 ) {
228 $attr_type->repeatable(0);
229 eval {$attr_type->check_repeatables};
230 $can_be_set_to_nonrepeatable = 0 if $@;
231 $attr_type->repeatable(1);
233 my $can_be_set_to_unique = 1;
234 if ( $attr_type->unique_id == 0 ) {
235 $attr_type->unique_id(1);
236 eval {$attr_type->check_unique_ids};
237 $can_be_set_to_unique = 0 if $@;
238 $attr_type->unique_id(0);
241 attribute_type => $attr_type,
242 attribute_type_form => 1,
243 edit_attribute_type => 1,
244 can_be_set_to_nonrepeatable => $can_be_set_to_nonrepeatable,
245 can_be_set_to_unique => $can_be_set_to_unique,
246 confirm_op => 'edit_attribute_type_confirmed',
247 categories => $patron_categories,
252 sub patron_attribute_type_list {
253 my $template = shift;
255 my @attr_types = Koha::Patron::Attribute::Types->search->as_list;
257 my @classes = uniq( map { $_->class } @attr_types );
258 @classes = sort @classes;
261 # FIXME This is not efficient and should be improved
262 for my $class (@classes) {
264 for my $attr (@attr_types) {
265 next if $attr->class ne $class;
268 my $av = Koha::AuthorisedValues->search({ category => 'PA_CLASS', authorised_value => $class });
269 my $lib = $av->count ? $av->next->lib : $class;
270 push @attributes_loop, {
276 $template->param(available_attribute_types => \@attributes_loop);
277 $template->param(display_list => 1);