Bug 16385: Fix breadcrumbs when ordering from subscription
[koha.git] / admin / patron-attr-types.pl
blobceb1184ba34f900de2c72ea4944217be01a0a866
1 #! /usr/bin/perl
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>.
22 use Modern::Perl;
24 use CGI qw ( -utf8 );
25 use List::MoreUtils qw/uniq/;
27 use C4::Auth;
28 use C4::Branch;
29 use C4::Context;
30 use C4::Output;
31 use C4::Koha;
32 use C4::Members qw/GetBorrowercategoryList/;
33 use C4::Members::AttributeTypes;
35 my $script_name = "/cgi-bin/koha/admin/patron-attr-types.pl";
37 our $input = new CGI;
38 my $op = $input->param('op') || '';
41 our ($template, $loggedinuser, $cookie)
42 = get_template_and_user({template_name => "admin/patron-attr-types.tt",
43 query => $input,
44 type => "intranet",
45 authnotrequired => 0,
46 flagsrequired => {parameters => 'parameters_remaining_permissions'},
47 debug => 1,
48 });
50 $template->param(script_name => $script_name);
52 my $code = $input->param("code");
54 my $display_list = 0;
55 if ($op eq "edit_attribute_type") {
56 edit_attribute_type_form($template, $code);
57 } elsif ($op eq "edit_attribute_type_confirmed") {
58 $display_list = add_update_attribute_type('edit', $template, $code);
59 } elsif ($op eq "add_attribute_type") {
60 add_attribute_type_form($template);
61 } elsif ($op eq "add_attribute_type_confirmed") {
62 $display_list = add_update_attribute_type('add', $template, $code);
63 } elsif ($op eq "delete_attribute_type") {
64 $display_list = delete_attribute_type_form($template, $code);
65 } elsif ($op eq "delete_attribute_type_confirmed") {
66 delete_attribute_type($template, $code);
67 $display_list = 1;
68 } else {
69 $display_list = 1;
72 if ($display_list) {
73 unless (C4::Context->preference('ExtendedPatronAttributes')) {
74 $template->param(WARNING_extended_attributes_off => 1);
76 patron_attribute_type_list($template);
79 output_html_with_http_headers $input, $cookie, $template->output;
81 exit 0;
83 sub add_attribute_type_form {
84 my $template = shift;
86 my $branches = GetBranches;
87 my @branches_loop;
88 foreach my $branch (sort keys %$branches) {
89 push @branches_loop, {
90 branchcode => $$branches{$branch}{branchcode},
91 branchname => $$branches{$branch}{branchname},
95 $template->param(
96 attribute_type_form => 1,
97 confirm_op => 'add_attribute_type_confirmed',
98 categories => GetBorrowercategoryList,
99 branches_loop => \@branches_loop,
101 authorised_value_category_list($template);
102 $template->param(classes_val_loop => GetAuthorisedValues( 'PA_CLASS'));
105 sub error_add_attribute_type_form {
106 my $template = shift;
108 $template->param(description => scalar $input->param('description'));
110 if ($input->param('repeatable')) {
111 $template->param(repeatable_checked => 1);
113 if ($input->param('unique_id')) {
114 $template->param(unique_id_checked => 1);
116 if ($input->param('opac_display')) {
117 $template->param(opac_display_checked => 1);
119 if ($input->param('staff_searchable')) {
120 $template->param(staff_searchable_checked => 1);
122 if ($input->param('display_checkout')) {
123 $template->param(display_checkout_checked => 'checked="checked"');
126 $template->param( category_code => scalar $input->param('category_code') );
127 $template->param( class => scalar $input->param('class') );
129 $template->param(
130 attribute_type_form => 1,
131 confirm_op => 'add_attribute_type_confirmed',
133 authorised_value_category_list($template, $input->param('authorised_value_category'));
136 sub add_update_attribute_type {
137 my $op = shift;
138 my $template = shift;
139 my $code = shift;
141 my $description = $input->param('description');
143 my $attr_type;
144 if ($op eq 'edit') {
145 $attr_type = C4::Members::AttributeTypes->fetch($code);
146 $attr_type->description($description);
147 } else {
148 my $existing = C4::Members::AttributeTypes->fetch($code);
149 if (defined($existing)) {
150 $template->param(duplicate_code_error => $code);
151 error_add_attribute_type_form($template);
152 return 0;
154 $attr_type = C4::Members::AttributeTypes->new($code, $description);
155 my $repeatable = $input->param('repeatable');
156 $attr_type->repeatable($repeatable);
157 my $unique_id = $input->param('unique_id');
158 $attr_type->unique_id($unique_id);
161 my $opac_display = $input->param('opac_display');
162 $attr_type->opac_display($opac_display);
163 my $staff_searchable = $input->param('staff_searchable');
164 $attr_type->staff_searchable($staff_searchable);
165 my $authorised_value_category = $input->param('authorised_value_category');
166 $attr_type->authorised_value_category($authorised_value_category);
167 my $display_checkout = $input->param('display_checkout');
168 $attr_type->display_checkout($display_checkout);
169 $attr_type->category_code(scalar $input->param('category_code'));
170 $attr_type->class(scalar $input->param('class'));
171 my @branches = $input->multi_param('branches');
172 $attr_type->branches( \@branches );
174 if ($op eq 'edit') {
175 $template->param(edited_attribute_type => $attr_type->code());
176 } else {
177 $template->param(added_attribute_type => $attr_type->code());
179 $attr_type->store();
181 return 1;
184 sub delete_attribute_type_form {
185 my $template = shift;
186 my $code = shift;
188 my $attr_type = C4::Members::AttributeTypes->fetch($code);
189 my $display_list = 0;
190 if (defined($attr_type)) {
191 $template->param(
192 delete_attribute_type_form => 1,
193 confirm_op => "delete_attribute_type_confirmed",
194 code => $code,
195 description => $attr_type->description(),
197 } else {
198 $template->param(ERROR_delete_not_found => $code);
199 $display_list = 1;
201 return $display_list;
204 sub delete_attribute_type {
205 my $template = shift;
206 my $code = shift;
208 my $attr_type = C4::Members::AttributeTypes->fetch($code);
209 if (defined($attr_type)) {
210 if ($attr_type->num_patrons() > 0) {
211 $template->param(ERROR_delete_in_use => $code);
212 $template->param(ERROR_num_patrons => $attr_type->num_patrons());
213 } else {
214 $attr_type->delete();
215 $template->param(deleted_attribute_type => $code);
217 } else {
218 $template->param(ERROR_delete_not_found => $code);
222 sub edit_attribute_type_form {
223 my $template = shift;
224 my $code = shift;
226 my $attr_type = C4::Members::AttributeTypes->fetch($code);
228 $template->param(code => $code);
229 $template->param(description => $attr_type->description());
230 $template->param(class => $attr_type->class());
232 if ($attr_type->repeatable()) {
233 $template->param(repeatable_checked => 1);
235 $template->param(repeatable_disabled => 1);
236 if ($attr_type->unique_id()) {
237 $template->param(unique_id_checked => 1);
239 $template->param(unique_id_disabled => 1);
240 if ($attr_type->opac_display()) {
241 $template->param(opac_display_checked => 1);
243 if ($attr_type->staff_searchable()) {
244 $template->param(staff_searchable_checked => 1);
246 if ($attr_type->display_checkout()) {
247 $template->param(display_checkout_checked => 'checked="checked"');
249 authorised_value_category_list($template, $attr_type->authorised_value_category());
250 $template->param(classes_val_loop => GetAuthorisedValues( 'PA_CLASS' ));
253 my $branches = GetBranches;
254 my @branches_loop;
255 my $selected_branches = $attr_type->branches;
256 foreach my $branch (sort keys %$branches) {
257 my $selected = ( grep {$$_{branchcode} eq $branch} @$selected_branches ) ? 1 : 0;
258 push @branches_loop, {
259 branchcode => $branches->{$branch}{branchcode},
260 branchname => $branches->{$branch}{branchname},
261 selected => $selected,
264 $template->param( branches_loop => \@branches_loop );
266 $template->param(
267 category_code => $attr_type->category_code,
268 category_class => $attr_type->class,
269 category_description => $attr_type->category_description,
272 $template->param(
273 attribute_type_form => 1,
274 edit_attribute_type => 1,
275 confirm_op => 'edit_attribute_type_confirmed',
276 categories => GetBorrowercategoryList,
281 sub patron_attribute_type_list {
282 my $template = shift;
284 my @attr_types = C4::Members::AttributeTypes::GetAttributeTypes( 1, 1 );
286 my @classes = uniq( map { $_->{class} } @attr_types );
287 @classes = sort @classes;
289 my @attributes_loop;
290 for my $class (@classes) {
291 my ( @items, $branches );
292 for my $attr (@attr_types) {
293 next if $attr->{class} ne $class;
294 my $attr_type = C4::Members::AttributeTypes->fetch($attr->{code});
295 $attr->{branches} = $attr_type->branches;
296 push @items, $attr;
298 my $lib = GetAuthorisedValueByCode( 'PA_CLASS', $class ) || $class;
299 push @attributes_loop, {
300 class => $class,
301 items => \@items,
302 lib => $lib,
303 branches => $branches,
306 $template->param(available_attribute_types => \@attributes_loop);
307 $template->param(display_list => 1);
310 sub authorised_value_category_list {
311 my $template = shift;
312 my $selected = @_ ? shift : '';
314 my $categories = GetAuthorisedValueCategories();
315 my @list = ();
316 foreach my $category (@$categories) {
317 my $entry = { category => $category };
318 $entry->{selected} = 1 if $category eq $selected;
319 push @list, $entry;
321 $template->param(authorised_value_categories => \@list);