Translation updates for Koha 19.05.14
[koha.git] / admin / patron-attr-types.pl
blob79dc17adb36c9e832675052475f28cecab322f42
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::Context;
29 use C4::Output;
30 use C4::Koha;
31 use C4::Members::AttributeTypes;
33 use Koha::AuthorisedValues;
34 use Koha::Libraries;
35 use Koha::Patron::Categories;
37 my $script_name = "/cgi-bin/koha/admin/patron-attr-types.pl";
39 our $input = new CGI;
40 my $op = $input->param('op') || '';
43 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
44 { template_name => "admin/patron-attr-types.tt",
45 query => $input,
46 type => "intranet",
47 flagsrequired => { parameters => 'manage_patron_attributes' }
52 $template->param(script_name => $script_name);
54 my $code = $input->param("code");
56 my $display_list = 0;
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);
69 $display_list = 1;
70 } else {
71 $display_list = 1;
74 if ($display_list) {
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;
83 exit 0;
85 sub add_attribute_type_form {
86 my $template = shift;
88 my $branches = Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed;
89 my @branches_loop;
90 foreach my $branch (@$branches) {
91 push @branches_loop, {
92 branchcode => $branch->{branchcode},
93 branchname => $branch->{branchname},
97 my $patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
98 $template->param(
99 attribute_type_form => 1,
100 confirm_op => 'add_attribute_type_confirmed',
101 categories => $patron_categories,
102 branches_loop => \@branches_loop,
104 $template->param(classes_val_loop => GetAuthorisedValues( 'PA_CLASS'));
107 sub error_add_attribute_type_form {
108 my $template = shift;
110 $template->param(description => scalar $input->param('description'));
112 if ($input->param('repeatable')) {
113 $template->param(repeatable_checked => 1);
115 if ($input->param('unique_id')) {
116 $template->param(unique_id_checked => 1);
118 if ($input->param('opac_display')) {
119 $template->param(opac_display_checked => 1);
121 if ($input->param('opac_editable')) {
122 $template->param(opac_editable_checked => 1);
124 if ($input->param('staff_searchable')) {
125 $template->param(staff_searchable_checked => 1);
127 if ($input->param('display_checkout')) {
128 $template->param(display_checkout_checked => 'checked="checked"');
131 $template->param( category_code => scalar $input->param('category_code') );
132 $template->param( class => scalar $input->param('class') );
134 $template->param(
135 attribute_type_form => 1,
136 confirm_op => 'add_attribute_type_confirmed',
137 authorised_value_category => scalar $input->param('authorised_value_category'),
141 sub add_update_attribute_type {
142 my $op = shift;
143 my $template = shift;
144 my $code = shift;
146 my $description = $input->param('description');
148 my $attr_type;
149 if ($op eq 'edit') {
150 $attr_type = C4::Members::AttributeTypes->fetch($code);
151 $attr_type->description($description);
152 } else {
153 my $existing = C4::Members::AttributeTypes->fetch($code);
154 if (defined($existing)) {
155 $template->param(duplicate_code_error => $code);
156 error_add_attribute_type_form($template);
157 return 0;
159 $attr_type = C4::Members::AttributeTypes->new($code, $description);
160 my $repeatable = $input->param('repeatable');
161 $attr_type->repeatable($repeatable);
162 my $unique_id = $input->param('unique_id');
163 $attr_type->unique_id($unique_id);
166 my $opac_display = $input->param('opac_display');
167 $attr_type->opac_display($opac_display);
168 my $opac_editable = $input->param('opac_editable');
169 $attr_type->opac_editable($opac_editable);
170 my $staff_searchable = $input->param('staff_searchable');
171 $attr_type->staff_searchable($staff_searchable);
172 my $authorised_value_category = $input->param('authorised_value_category');
173 $attr_type->authorised_value_category($authorised_value_category);
174 my $display_checkout = $input->param('display_checkout');
175 $attr_type->display_checkout($display_checkout);
176 $attr_type->category_code(scalar $input->param('category_code'));
177 $attr_type->class(scalar $input->param('class'));
178 my @branches = $input->multi_param('branches');
179 $attr_type->branches( \@branches );
181 if ($op eq 'edit') {
182 $template->param(edited_attribute_type => $attr_type->code());
183 } else {
184 $template->param(added_attribute_type => $attr_type->code());
186 $attr_type->store();
188 return 1;
191 sub delete_attribute_type_form {
192 my $template = shift;
193 my $code = shift;
195 my $attr_type = C4::Members::AttributeTypes->fetch($code);
196 my $display_list = 0;
197 if (defined($attr_type)) {
198 $template->param(
199 delete_attribute_type_form => 1,
200 confirm_op => "delete_attribute_type_confirmed",
201 code => $code,
202 description => $attr_type->description(),
204 } else {
205 $template->param(ERROR_delete_not_found => $code);
206 $display_list = 1;
208 return $display_list;
211 sub delete_attribute_type {
212 my $template = shift;
213 my $code = shift;
215 my $attr_type = C4::Members::AttributeTypes->fetch($code);
216 if (defined($attr_type)) {
217 if ($attr_type->num_patrons() > 0) {
218 $template->param(ERROR_delete_in_use => $code);
219 $template->param(ERROR_num_patrons => $attr_type->num_patrons());
220 } else {
221 $attr_type->delete();
222 $template->param(deleted_attribute_type => $code);
224 } else {
225 $template->param(ERROR_delete_not_found => $code);
229 sub edit_attribute_type_form {
230 my $template = shift;
231 my $code = shift;
233 my $attr_type = C4::Members::AttributeTypes->fetch($code);
235 $template->param(code => $code);
236 $template->param(description => $attr_type->description());
237 $template->param(class => $attr_type->class());
239 if ($attr_type->repeatable()) {
240 $template->param(repeatable_checked => 1);
242 $template->param(repeatable_disabled => 1);
243 if ($attr_type->unique_id()) {
244 $template->param(unique_id_checked => 1);
246 $template->param(unique_id_disabled => 1);
247 if ($attr_type->opac_display()) {
248 $template->param(opac_display_checked => 1);
250 if ($attr_type->opac_editable()) {
251 $template->param(opac_editable_checked => 1);
253 if ($attr_type->staff_searchable()) {
254 $template->param(staff_searchable_checked => 1);
256 if ($attr_type->display_checkout()) {
257 $template->param(display_checkout_checked => 'checked="checked"');
259 $template->param( authorised_value_category => $attr_type->authorised_value_category() );
260 $template->param(classes_val_loop => GetAuthorisedValues( 'PA_CLASS' ));
262 my $branches = Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed;
263 my @branches_loop;
264 my $selected_branches = $attr_type->branches;
265 foreach my $branch (@$branches) {
266 my $selected = ( grep {$_->{branchcode} eq $branch->{branchcode}} @$selected_branches ) ? 1 : 0;
267 push @branches_loop, {
268 branchcode => $branch->{branchcode},
269 branchname => $branch->{branchname},
270 selected => $selected,
273 $template->param( branches_loop => \@branches_loop );
275 $template->param(
276 category_code => $attr_type->category_code,
277 category_class => $attr_type->class,
278 category_description => $attr_type->category_description,
281 my $patron_categories = Koha::Patron::Categories->search({}, {order_by => ['description']});
282 $template->param(
283 attribute_type_form => 1,
284 edit_attribute_type => 1,
285 confirm_op => 'edit_attribute_type_confirmed',
286 categories => $patron_categories,
291 sub patron_attribute_type_list {
292 my $template = shift;
294 my @attr_types = C4::Members::AttributeTypes::GetAttributeTypes( 1, 1 );
296 my @classes = uniq( map { $_->{class} } @attr_types );
297 @classes = sort @classes;
299 my @attributes_loop;
300 for my $class (@classes) {
301 my ( @items, $branches );
302 for my $attr (@attr_types) {
303 next if $attr->{class} ne $class;
304 my $attr_type = C4::Members::AttributeTypes->fetch($attr->{code});
305 $attr->{branches} = $attr_type->branches;
306 push @items, $attr;
308 my $av = Koha::AuthorisedValues->search({ category => 'PA_CLASS', authorised_value => $class });
309 my $lib = $av->count ? $av->next->lib : $class;
310 push @attributes_loop, {
311 class => $class,
312 items => \@items,
313 lib => $lib,
314 branches => $branches,
317 $template->param(available_attribute_types => \@attributes_loop);
318 $template->param(display_list => 1);