Merge remote-tracking branch 'kc/new/bug_6205' into kcmaster
[koha.git] / admin / patron-attr-types.pl
blob29a0d901ad3bf5da61fbd8701e0c39819a829641
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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 use strict;
23 use warnings;
24 use CGI;
25 use C4::Auth;
26 use C4::Context;
27 use C4::Output;
28 use C4::Koha;
29 use C4::Members::AttributeTypes;
31 my $script_name = "/cgi-bin/koha/admin/patron-attr-types.pl";
33 my $input = new CGI;
34 my $op = $input->param('op') || '';
37 my ($template, $loggedinuser, $cookie)
38 = get_template_and_user({template_name => "admin/patron-attr-types.tmpl",
39 query => $input,
40 type => "intranet",
41 authnotrequired => 0,
42 flagsrequired => {parameters => 1},
43 debug => 1,
44 });
46 $template->param(script_name => $script_name);
48 my $code = $input->param("code");
50 my $display_list = 0;
51 if ($op eq "edit_attribute_type") {
52 edit_attribute_type_form($template, $code);
53 } elsif ($op eq "edit_attribute_type_confirmed") {
54 $display_list = add_update_attribute_type('edit', $template, $code);
55 } elsif ($op eq "add_attribute_type") {
56 add_attribute_type_form($template);
57 } elsif ($op eq "add_attribute_type_confirmed") {
58 $display_list = add_update_attribute_type('add', $template, $code);
59 } elsif ($op eq "delete_attribute_type") {
60 $display_list = delete_attribute_type_form($template, $code);
61 } elsif ($op eq "delete_attribute_type_confirmed") {
62 delete_attribute_type($template, $code);
63 $display_list = 1;
64 } else {
65 $display_list = 1;
68 if ($display_list) {
69 unless (C4::Context->preference('ExtendedPatronAttributes')) {
70 $template->param(WARNING_extended_attributes_off => 1);
72 patron_attribute_type_list($template);
75 output_html_with_http_headers $input, $cookie, $template->output;
77 exit 0;
79 sub add_attribute_type_form {
80 my $template = shift;
82 $template->param(
83 attribute_type_form => 1,
84 confirm_op => 'add_attribute_type_confirmed',
86 authorised_value_category_list($template);
89 sub error_add_attribute_type_form {
90 my $template = shift;
92 $template->param(description => $input->param('description'));
94 if ($input->param('repeatable')) {
95 $template->param(repeatable_checked => 1);
97 if ($input->param('unique_id')) {
98 $template->param(unique_id_checked => 1);
100 if ($input->param('password_allowed')) {
101 $template->param(password_allowed_checked => 1);
103 if ($input->param('opac_display')) {
104 $template->param(opac_display_checked => 1);
106 if ($input->param('staff_searchable')) {
107 $template->param(staff_searchable_checked => 1);
109 if ($input->param('display_checkout')) {
110 $template->param(display_checkout_checked => 'checked="checked"');
113 $template->param(
114 attribute_type_form => 1,
115 confirm_op => 'add_attribute_type_confirmed',
117 authorised_value_category_list($template, $input->param('authorised_value_category'));
120 sub add_update_attribute_type {
121 my $op = shift;
122 my $template = shift;
123 my $code = shift;
125 my $description = $input->param('description');
127 my $attr_type;
128 if ($op eq 'edit') {
129 $attr_type = C4::Members::AttributeTypes->fetch($code);
130 $attr_type->description($description);
131 } else {
132 my $existing = C4::Members::AttributeTypes->fetch($code);
133 if (defined($existing)) {
134 $template->param(duplicate_code_error => $code);
135 error_add_attribute_type_form($template);
136 return 0;
138 $attr_type = C4::Members::AttributeTypes->new($code, $description);
139 my $repeatable = $input->param('repeatable');
140 $attr_type->repeatable($repeatable);
141 my $unique_id = $input->param('unique_id');
142 $attr_type->unique_id($unique_id);
145 my $opac_display = $input->param('opac_display');
146 $attr_type->opac_display($opac_display);
147 my $staff_searchable = $input->param('staff_searchable');
148 $attr_type->staff_searchable($staff_searchable);
149 my $authorised_value_category = $input->param('authorised_value_category');
150 $attr_type->authorised_value_category($authorised_value_category);
151 my $password_allowed = $input->param('password_allowed');
152 $attr_type->password_allowed($password_allowed);
153 my $display_checkout = $input->param('display_checkout');
154 $attr_type->display_checkout($display_checkout);
156 if ($op eq 'edit') {
157 $template->param(edited_attribute_type => $attr_type->code());
158 } else {
159 $template->param(added_attribute_type => $attr_type->code());
161 $attr_type->store();
163 return 1;
166 sub delete_attribute_type_form {
167 my $template = shift;
168 my $code = shift;
170 my $attr_type = C4::Members::AttributeTypes->fetch($code);
171 my $display_list = 0;
172 if (defined($attr_type)) {
173 $template->param(
174 delete_attribute_type_form => 1,
175 confirm_op => "delete_attribute_type_confirmed",
176 code => $code,
177 description => $attr_type->description(),
179 } else {
180 $template->param(ERROR_delete_not_found => $code);
181 $display_list = 1;
183 return $display_list;
186 sub delete_attribute_type {
187 my $template = shift;
188 my $code = shift;
190 my $attr_type = C4::Members::AttributeTypes->fetch($code);
191 if (defined($attr_type)) {
192 if ($attr_type->num_patrons() > 0) {
193 $template->param(ERROR_delete_in_use => $code);
194 $template->param(ERROR_num_patrons => $attr_type->num_patrons());
195 } else {
196 $attr_type->delete();
197 $template->param(deleted_attribute_type => $code);
199 } else {
200 $template->param(ERROR_delete_not_found => $code);
204 sub edit_attribute_type_form {
205 my $template = shift;
206 my $code = shift;
208 my $attr_type = C4::Members::AttributeTypes->fetch($code);
210 $template->param(code => $code);
211 $template->param(description => $attr_type->description());
213 if ($attr_type->repeatable()) {
214 $template->param(repeatable_checked => 1);
216 $template->param(repeatable_disabled => 1);
217 if ($attr_type->unique_id()) {
218 $template->param(unique_id_checked => 1);
220 $template->param(unique_id_disabled => 1);
221 if ($attr_type->password_allowed()) {
222 $template->param(password_allowed_checked => 1);
224 if ($attr_type->opac_display()) {
225 $template->param(opac_display_checked => 1);
227 if ($attr_type->staff_searchable()) {
228 $template->param(staff_searchable_checked => 1);
230 if ($attr_type->display_checkout()) {
231 $template->param(display_checkout_checked => 'checked="checked"');
233 authorised_value_category_list($template, $attr_type->authorised_value_category());
235 $template->param(
236 attribute_type_form => 1,
237 edit_attribute_type => 1,
238 confirm_op => 'edit_attribute_type_confirmed',
243 sub patron_attribute_type_list {
244 my $template = shift;
246 my @attr_types = C4::Members::AttributeTypes::GetAttributeTypes();
247 $template->param(available_attribute_types => \@attr_types);
248 $template->param(display_list => 1);
251 sub authorised_value_category_list {
252 my $template = shift;
253 my $selected = @_ ? shift : '';
255 my $categories = GetAuthorisedValueCategories();
256 my @list = ();
257 foreach my $category (@$categories) {
258 my $entry = { category => $category };
259 $entry->{selected} = 1 if $category eq $selected;
260 push @list, $entry;
262 $template->param(authorised_value_categories => \@list);