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
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.
29 use C4::Members::AttributeTypes;
31 my $script_name = "/cgi-bin/koha/admin/patron-attr-types.pl";
34 my $op = $input->param('op') || '';
37 my ($template, $loggedinuser, $cookie)
38 = get_template_and_user({template_name => "admin/patron-attr-types.tmpl",
42 flagsrequired => {parameters => 1},
46 $template->param(script_name => $script_name);
48 my $code = $input->param("code");
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);
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;
79 sub add_attribute_type_form {
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 {
92 $template->param(description => $input->param('description'));
94 if ($input->param('repeatable')) {
95 $template->param(repeatable_checked => 'checked="checked"');
97 if ($input->param('unique_id')) {
98 $template->param(unique_id_checked => 'checked="checked"');
100 if ($input->param('password_allowed')) {
101 $template->param(password_allowed_checked => 'checked="checked"');
103 if ($input->param('opac_display')) {
104 $template->param(opac_display_checked => 'checked="checked"');
106 if ($input->param('staff_searchable')) {
107 $template->param(staff_searchable_checked => 'checked="checked"');
111 attribute_type_form => 1,
112 confirm_op => 'add_attribute_type_confirmed',
114 authorised_value_category_list($template, $input->param('authorised_value_category'));
117 sub add_update_attribute_type {
119 my $template = shift;
122 my $description = $input->param('description');
126 $attr_type = C4::Members::AttributeTypes->fetch($code);
127 $attr_type->description($description);
129 my $existing = C4::Members::AttributeTypes->fetch($code);
130 if (defined($existing)) {
131 $template->param(duplicate_code_error => $code);
132 error_add_attribute_type_form($template);
135 $attr_type = C4::Members::AttributeTypes->new($code, $description);
136 my $repeatable = $input->param('repeatable');
137 $attr_type->repeatable($repeatable);
138 my $unique_id = $input->param('unique_id');
139 $attr_type->unique_id($unique_id);
142 my $opac_display = $input->param('opac_display');
143 $attr_type->opac_display($opac_display);
144 my $staff_searchable = $input->param('staff_searchable');
145 $attr_type->staff_searchable($staff_searchable);
146 my $authorised_value_category = $input->param('authorised_value_category');
147 $attr_type->authorised_value_category($authorised_value_category);
148 my $password_allowed = $input->param('password_allowed');
149 $attr_type->password_allowed($password_allowed);
152 $template->param(edited_attribute_type => $attr_type->code());
154 $template->param(added_attribute_type => $attr_type->code());
161 sub delete_attribute_type_form {
162 my $template = shift;
165 my $attr_type = C4::Members::AttributeTypes->fetch($code);
166 my $display_list = 0;
167 if (defined($attr_type)) {
169 delete_attribute_type_form => 1,
170 confirm_op => "delete_attribute_type_confirmed",
172 description => $attr_type->description(),
175 $template->param(ERROR_delete_not_found => $code);
178 return $display_list;
181 sub delete_attribute_type {
182 my $template = shift;
185 my $attr_type = C4::Members::AttributeTypes->fetch($code);
186 if (defined($attr_type)) {
187 if ($attr_type->num_patrons() > 0) {
188 $template->param(ERROR_delete_in_use => $code);
189 $template->param(ERROR_num_patrons => $attr_type->num_patrons());
191 $attr_type->delete();
192 $template->param(deleted_attribute_type => $code);
195 $template->param(ERROR_delete_not_found => $code);
199 sub edit_attribute_type_form {
200 my $template = shift;
203 my $attr_type = C4::Members::AttributeTypes->fetch($code);
205 $template->param(code => $code);
206 $template->param(description => $attr_type->description());
208 if ($attr_type->repeatable()) {
209 $template->param(repeatable_checked => 'checked="checked"');
211 $template->param(repeatable_disabled => 'disabled="disabled"');
212 if ($attr_type->unique_id()) {
213 $template->param(unique_id_checked => 'checked="checked"');
215 $template->param(unique_id_disabled => 'disabled="disabled"');
216 if ($attr_type->password_allowed()) {
217 $template->param(password_allowed_checked => 'checked="checked"');
219 if ($attr_type->opac_display()) {
220 $template->param(opac_display_checked => 'checked="checked"');
222 if ($attr_type->staff_searchable()) {
223 $template->param(staff_searchable_checked => 'checked="checked"');
226 authorised_value_category_list($template, $attr_type->authorised_value_category());
229 attribute_type_form => 1,
230 edit_attribute_type => 1,
231 confirm_op => 'edit_attribute_type_confirmed',
236 sub patron_attribute_type_list {
237 my $template = shift;
239 my @attr_types = C4::Members::AttributeTypes::GetAttributeTypes();
240 $template->param(available_attribute_types => \@attr_types);
241 $template->param(display_list => 1);
244 sub authorised_value_category_list {
245 my $template = shift;
246 my $selected = @_ ? shift : '';
248 my $categories = GetAuthorisedValueCategories();
250 foreach my $category (@$categories) {
251 my $entry = { category => $category };
252 $entry->{selected} = 1 if $category eq $selected;
255 $template->param(authorised_value_categories => \@list);