fix for bug 2348: Cannot change patron category
[koha.git] / admin / patron-attr-types.pl
blobbe7e89111ade7dbb2748f89fe918dd31fecabc03
1 #! /usr/bin/perl
3 # Copyright 2008 LibLime
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
21 use strict;
22 use CGI;
23 use C4::Auth;
24 use C4::Context;
25 use C4::Output;
26 use C4::Koha;
27 use C4::Members::AttributeTypes;
29 my $script_name = "/cgi-bin/koha/admin/patron-attr-types.pl";
31 my $input = new CGI;
32 my $op = $input->param('op');
35 my ($template, $loggedinuser, $cookie)
36 = get_template_and_user({template_name => "admin/patron-attr-types.tmpl",
37 query => $input,
38 type => "intranet",
39 authnotrequired => 0,
40 flagsrequired => {parameters => 1},
41 debug => 1,
42 });
44 $template->param(script_name => $script_name);
46 my $code = $input->param("code");
48 my $display_list = 0;
49 if ($op eq "edit_attribute_type") {
50 edit_attribute_type_form($template, $code);
51 } elsif ($op eq "edit_attribute_type_confirmed") {
52 $display_list = add_update_attribute_type('edit', $template, $code);
53 } elsif ($op eq "add_attribute_type") {
54 add_attribute_type_form($template);
55 } elsif ($op eq "add_attribute_type_confirmed") {
56 $display_list = add_update_attribute_type('add', $template, $code);
57 } elsif ($op eq "delete_attribute_type") {
58 $display_list = delete_attribute_type_form($template, $code);
59 } elsif ($op eq "delete_attribute_type_confirmed") {
60 delete_attribute_type($template, $code);
61 $display_list = 1;
62 } else {
63 $display_list = 1;
66 if ($display_list) {
67 unless (C4::Context->preference('ExtendedPatronAttributes')) {
68 $template->param(WARNING_extended_attributes_off => 1);
70 patron_attribute_type_list($template);
73 output_html_with_http_headers $input, $cookie, $template->output;
75 exit 0;
77 sub add_attribute_type_form {
78 my $template = shift;
80 $template->param(
81 attribute_type_form => 1,
82 confirm_op => 'add_attribute_type_confirmed',
84 authorised_value_category_list($template);
87 sub error_add_attribute_type_form {
88 my $template = shift;
90 $template->param(description => $input->param('description'));
92 if ($input->param('repeatable')) {
93 $template->param(repeatable_checked => 'checked="checked"');
95 if ($input->param('unique_id')) {
96 $template->param(unique_id_checked => 'checked="checked"');
98 if ($input->param('password_allowed')) {
99 $template->param(password_allowed_checked => 'checked="checked"');
101 if ($input->param('opac_display')) {
102 $template->param(opac_display_checked => 'checked="checked"');
104 if ($input->param('staff_searchable')) {
105 $template->param(staff_searchable_checked => 'checked="checked"');
108 $template->param(
109 attribute_type_form => 1,
110 confirm_op => 'add_attribute_type_confirmed',
112 authorised_value_category_list($template, $input->param('authorised_value_category'));
115 sub add_update_attribute_type {
116 my $op = shift;
117 my $template = shift;
118 my $code = shift;
120 my $description = $input->param('description');
122 my $attr_type;
123 if ($op eq 'edit') {
124 $attr_type = C4::Members::AttributeTypes->fetch($code);
125 $attr_type->description($description);
126 } else {
127 my $existing = C4::Members::AttributeTypes->fetch($code);
128 if (defined($existing)) {
129 $template->param(duplicate_code_error => $code);
130 error_add_attribute_type_form($template);
131 return 0;
133 $attr_type = C4::Members::AttributeTypes->new($code, $description);
134 my $repeatable = $input->param('repeatable');
135 $attr_type->repeatable($repeatable);
136 my $unique_id = $input->param('unique_id');
137 $attr_type->unique_id($unique_id);
140 my $opac_display = $input->param('opac_display');
141 $attr_type->opac_display($opac_display);
142 my $staff_searchable = $input->param('staff_searchable');
143 $attr_type->staff_searchable($staff_searchable);
144 my $authorised_value_category = $input->param('authorised_value_category');
145 $attr_type->authorised_value_category($authorised_value_category);
146 my $password_allowed = $input->param('password_allowed');
147 $attr_type->password_allowed($password_allowed);
149 if ($op eq 'edit') {
150 $template->param(edited_attribute_type => $attr_type->code());
151 } else {
152 $template->param(added_attribute_type => $attr_type->code());
154 $attr_type->store();
156 return 1;
159 sub delete_attribute_type_form {
160 my $template = shift;
161 my $code = shift;
163 my $attr_type = C4::Members::AttributeTypes->fetch($code);
164 my $display_list = 0;
165 if (defined($attr_type)) {
166 $template->param(
167 delete_attribute_type_form => 1,
168 confirm_op => "delete_attribute_type_confirmed",
169 code => $code,
170 description => $attr_type->description(),
172 } else {
173 $template->param(ERROR_delete_not_found => $code);
174 $display_list = 1;
176 return $display_list;
179 sub delete_attribute_type {
180 my $template = shift;
181 my $code = shift;
183 my $attr_type = C4::Members::AttributeTypes->fetch($code);
184 if (defined($attr_type)) {
185 if ($attr_type->num_patrons() > 0) {
186 $template->param(ERROR_delete_in_use => $code);
187 $template->param(ERROR_num_patrons => $attr_type->num_patrons());
188 } else {
189 $attr_type->delete();
190 $template->param(deleted_attribute_type => $code);
192 } else {
193 $template->param(ERROR_delete_not_found => $code);
197 sub edit_attribute_type_form {
198 my $template = shift;
199 my $code = shift;
201 my $attr_type = C4::Members::AttributeTypes->fetch($code);
203 $template->param(code => $code);
204 $template->param(description => $attr_type->description());
206 if ($attr_type->repeatable()) {
207 $template->param(repeatable_checked => 'checked="checked"');
209 $template->param(repeatable_disabled => 'disabled="disabled"');
210 if ($attr_type->unique_id()) {
211 $template->param(unique_id_checked => 'checked="checked"');
213 $template->param(unique_id_disabled => 'disabled="disabled"');
214 if ($attr_type->password_allowed()) {
215 $template->param(password_allowed_checked => 'checked="checked"');
217 if ($attr_type->opac_display()) {
218 $template->param(opac_display_checked => 'checked="checked"');
220 if ($attr_type->staff_searchable()) {
221 $template->param(staff_searchable_checked => 'checked="checked"');
224 authorised_value_category_list($template, $attr_type->authorised_value_category());
226 $template->param(
227 attribute_type_form => 1,
228 edit_attribute_type => 1,
229 confirm_op => 'edit_attribute_type_confirmed',
234 sub patron_attribute_type_list {
235 my $template = shift;
237 my @attr_types = C4::Members::AttributeTypes::GetAttributeTypes();
238 $template->param(available_attribute_types => \@attr_types);
239 $template->param(display_list => 1);
242 sub authorised_value_category_list {
243 my $template = shift;
244 my $selected = @_ ? shift : '';
246 my $categories = GetAuthorisedValueCategories();
247 my @list = ();
248 foreach my $category (@$categories) {
249 my $entry = { category => $category };
250 $entry->{selected} = 1 if $category eq $selected;
251 push @list, $entry;
253 $template->param(authorised_value_categories => \@list);