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 C4::Members::AttributeTypes;
33 use Koha::AuthorisedValues;
35 use Koha::Patron::Categories;
37 my $script_name = "/cgi-bin/koha/admin/patron-attr-types.pl";
40 my $op = $input->param('op') || '';
43 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
44 { template_name => "admin/patron-attr-types.tt",
48 flagsrequired => { parameters => 'manage_patron_attributes' }
53 $template->param(script_name => $script_name);
55 my $code = $input->param("code");
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);
76 unless (C4::Context->preference('ExtendedPatronAttributes')) {
77 $template->param(WARNING_extended_attributes_off => 1);
79 patron_attribute_type_list($template);
82 output_html_with_http_headers $input, $cookie, $template->output;
86 sub add_attribute_type_form {
89 my $branches = Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed;
91 foreach my $branch (@$branches) {
92 push @branches_loop, {
93 branchcode => $branch->{branchcode},
94 branchname => $branch->{branchname},
98 my $patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
100 attribute_type_form => 1,
101 confirm_op => 'add_attribute_type_confirmed',
102 categories => $patron_categories,
103 branches_loop => \@branches_loop,
105 $template->param(classes_val_loop => GetAuthorisedValues( 'PA_CLASS'));
108 sub error_add_attribute_type_form {
109 my $template = shift;
111 $template->param(description => scalar $input->param('description'));
113 if ($input->param('repeatable')) {
114 $template->param(repeatable_checked => 1);
116 if ($input->param('unique_id')) {
117 $template->param(unique_id_checked => 1);
119 if ($input->param('opac_display')) {
120 $template->param(opac_display_checked => 1);
122 if ($input->param('opac_editable')) {
123 $template->param(opac_editable_checked => 1);
125 if ($input->param('staff_searchable')) {
126 $template->param(staff_searchable_checked => 1);
128 if ($input->param('display_checkout')) {
129 $template->param(display_checkout_checked => 'checked="checked"');
132 $template->param( category_code => scalar $input->param('category_code') );
133 $template->param( class => scalar $input->param('class') );
136 attribute_type_form => 1,
137 confirm_op => 'add_attribute_type_confirmed',
138 authorised_value_category => scalar $input->param('authorised_value_category'),
142 sub add_update_attribute_type {
144 my $template = shift;
147 my $description = $input->param('description');
148 my $repeatable = $input->param('repeatable') ? 1 : 0;
149 my $unique_id = $input->param('unique_id') ? 1 : 0;
150 my $opac_display = $input->param('opac_display') ? 1 : 0;
151 my $opac_editable = $input->param('opac_editable') ? 1 : 0;
152 my $staff_searchable = $input->param('staff_searchable') ? 1 : 0;
153 my $authorised_value_category = $input->param('authorised_value_category');
154 my $display_checkout = $input->param('display_checkout') ? 1 : 0;
155 my $category_code = $input->param('category_code') || undef;
156 my $class = $input->param('class');
158 my $description = $input->param('description');
162 $attr_type = C4::Members::AttributeTypes->fetch($code);
163 $attr_type->description($description);
165 my $existing = C4::Members::AttributeTypes->fetch($code);
166 if (defined($existing)) {
167 $template->param(duplicate_code_error => $code);
168 error_add_attribute_type_form($template);
171 $attr_type = C4::Members::AttributeTypes->new($code, $description);
172 my $repeatable = $input->param('repeatable');
173 $attr_type->repeatable($repeatable);
174 my $unique_id = $input->param('unique_id');
175 $attr_type->unique_id($unique_id);
178 my $opac_display = $input->param('opac_display');
179 $attr_type->opac_display($opac_display);
180 my $opac_editable = $input->param('opac_editable');
181 $attr_type->opac_editable($opac_editable);
182 my $staff_searchable = $input->param('staff_searchable');
183 $attr_type->staff_searchable($staff_searchable);
184 my $authorised_value_category = $input->param('authorised_value_category');
185 $attr_type->authorised_value_category($authorised_value_category);
186 my $display_checkout = $input->param('display_checkout');
187 $attr_type->display_checkout($display_checkout);
188 $attr_type->category_code(scalar $input->param('category_code'));
189 $attr_type->class(scalar $input->param('class'));
190 my @branches = $input->multi_param('branches');
191 $attr_type->branches( \@branches );
194 $template->param(edited_attribute_type => $attr_type->code());
196 $template->param(added_attribute_type => $attr_type->code());
203 sub delete_attribute_type_form {
204 my $template = shift;
207 my $attr_type = C4::Members::AttributeTypes->fetch($code);
208 my $display_list = 0;
209 if (defined($attr_type)) {
211 delete_attribute_type_form => 1,
212 confirm_op => "delete_attribute_type_confirmed",
214 description => $attr_type->description(),
217 $template->param(ERROR_delete_not_found => $code);
220 return $display_list;
223 sub delete_attribute_type {
224 my $template = shift;
227 my $attr_type = C4::Members::AttributeTypes->fetch($code);
228 if (defined($attr_type)) {
229 if ($attr_type->num_patrons() > 0) {
230 $template->param(ERROR_delete_in_use => $code);
231 $template->param(ERROR_num_patrons => $attr_type->num_patrons());
233 $attr_type->delete();
234 $template->param(deleted_attribute_type => $code);
237 $template->param(ERROR_delete_not_found => $code);
241 sub edit_attribute_type_form {
242 my $template = shift;
245 my $attr_type = C4::Members::AttributeTypes->fetch($code);
247 $template->param(code => $code);
248 $template->param(description => $attr_type->description());
249 $template->param(class => $attr_type->class());
251 if ($attr_type->repeatable()) {
252 $template->param(repeatable_checked => 1);
254 $template->param(repeatable_disabled => 1);
255 if ($attr_type->unique_id()) {
256 $template->param(unique_id_checked => 1);
258 $template->param(unique_id_disabled => 1);
259 if ($attr_type->opac_display()) {
260 $template->param(opac_display_checked => 1);
262 if ($attr_type->opac_editable()) {
263 $template->param(opac_editable_checked => 1);
265 if ($attr_type->staff_searchable()) {
266 $template->param(staff_searchable_checked => 1);
268 if ($attr_type->display_checkout()) {
269 $template->param(display_checkout_checked => 'checked="checked"');
271 $template->param( authorised_value_category => $attr_type->authorised_value_category() );
272 $template->param(classes_val_loop => GetAuthorisedValues( 'PA_CLASS' ));
274 my $branches = Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed;
276 my $selected_branches = $attr_type->branches;
277 foreach my $branch (@$branches) {
278 my $selected = ( grep {$_->{branchcode} eq $branch->{branchcode}} @$selected_branches ) ? 1 : 0;
279 push @branches_loop, {
280 branchcode => $branch->{branchcode},
281 branchname => $branch->{branchname},
282 selected => $selected,
285 $template->param( branches_loop => \@branches_loop );
288 category_code => $attr_type->category_code,
289 category_class => $attr_type->class,
290 category_description => $attr_type->category_description,
293 my $patron_categories = Koha::Patron::Categories->search({}, {order_by => ['description']});
295 attribute_type_form => 1,
296 edit_attribute_type => 1,
297 confirm_op => 'edit_attribute_type_confirmed',
298 categories => $patron_categories,
303 sub patron_attribute_type_list {
304 my $template = shift;
306 my @attr_types = C4::Members::AttributeTypes::GetAttributeTypes( 1, 1 );
308 my @classes = uniq( map { $_->{class} } @attr_types );
309 @classes = sort @classes;
312 for my $class (@classes) {
313 my ( @items, $branches );
314 for my $attr (@attr_types) {
315 next if $attr->{class} ne $class;
316 my $attr_type = C4::Members::AttributeTypes->fetch($attr->{code});
317 $attr->{branches} = $attr_type->branches;
320 my $av = Koha::AuthorisedValues->search({ category => 'PA_CLASS', authorised_value => $class });
321 my $lib = $av->count ? $av->next->lib : $class;
322 push @attributes_loop, {
326 branches => $branches,
329 $template->param(available_attribute_types => \@attributes_loop);
330 $template->param(display_list => 1);