Bug 7450: missing placeholders in authorised_values.pl
[koha.git] / admin / patron-attr-types.pl
1 #! /usr/bin/perl
2 #
3 # Copyright 2008 LibLime
4 # Parts copyright 2010 BibLibre
5 #
6 # This file is part of Koha.
7 #
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.
12 #
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.
16 #
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.
20 #
21
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;
30
31 my $script_name = "/cgi-bin/koha/admin/patron-attr-types.pl";
32
33 my $input = new CGI;
34 my $op = $input->param('op') || '';
35
36
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                  });
45
46 $template->param(script_name => $script_name);
47
48 my $code = $input->param("code");
49
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;
66 }
67
68 if ($display_list) {
69     unless (C4::Context->preference('ExtendedPatronAttributes')) {
70         $template->param(WARNING_extended_attributes_off => 1); 
71     }
72     patron_attribute_type_list($template);
73 }
74
75 output_html_with_http_headers $input, $cookie, $template->output;
76
77 exit 0;
78
79 sub add_attribute_type_form {
80     my $template = shift;
81
82     $template->param(
83         attribute_type_form => 1,
84         confirm_op => 'add_attribute_type_confirmed',
85     );
86     authorised_value_category_list($template);
87 }
88
89 sub error_add_attribute_type_form {
90     my $template = shift;
91
92     $template->param(description => $input->param('description'));
93
94     if ($input->param('repeatable')) {
95         $template->param(repeatable_checked => 1);
96     }
97     if ($input->param('unique_id')) {
98         $template->param(unique_id_checked => 1);
99     }
100     if ($input->param('password_allowed')) {
101         $template->param(password_allowed_checked => 1);
102     }
103     if ($input->param('opac_display')) {
104         $template->param(opac_display_checked => 1);
105     }
106     if ($input->param('staff_searchable')) {
107         $template->param(staff_searchable_checked => 1);
108     }
109     if ($input->param('display_checkout')) {
110         $template->param(display_checkout_checked => 'checked="checked"');
111     }
112
113     $template->param(
114         attribute_type_form => 1,
115         confirm_op => 'add_attribute_type_confirmed',
116     );
117     authorised_value_category_list($template, $input->param('authorised_value_category'));
118 }
119
120 sub add_update_attribute_type {
121     my $op = shift;
122     my $template = shift;
123     my $code = shift;
124
125     my $description = $input->param('description');
126
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;
137         }
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);
143     }
144
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);
155
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());
160     }
161     $attr_type->store();
162
163     return 1;
164 }
165
166 sub delete_attribute_type_form {
167     my $template = shift;
168     my $code = shift;
169
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(),
178         );
179     } else {
180         $template->param(ERROR_delete_not_found => $code);
181         $display_list = 1;
182     }
183     return $display_list;
184 }
185
186 sub delete_attribute_type {
187     my $template = shift;
188     my $code = shift;
189
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);
198         }
199     } else {
200         $template->param(ERROR_delete_not_found => $code);
201     }
202 }
203
204 sub edit_attribute_type_form {
205     my $template = shift;
206     my $code = shift;
207
208     my $attr_type = C4::Members::AttributeTypes->fetch($code);
209
210     $template->param(code => $code);
211     $template->param(description => $attr_type->description());
212
213     if ($attr_type->repeatable()) {
214         $template->param(repeatable_checked => 1);
215     }
216     $template->param(repeatable_disabled => 1);
217     if ($attr_type->unique_id()) {
218         $template->param(unique_id_checked => 1);
219     }
220     $template->param(unique_id_disabled => 1);
221     if ($attr_type->password_allowed()) {
222         $template->param(password_allowed_checked => 1);
223     }
224     if ($attr_type->opac_display()) {
225         $template->param(opac_display_checked => 1);
226     }
227     if ($attr_type->staff_searchable()) {
228         $template->param(staff_searchable_checked => 1);
229     }
230     if ($attr_type->display_checkout()) {
231         $template->param(display_checkout_checked => 'checked="checked"');
232     }
233     authorised_value_category_list($template, $attr_type->authorised_value_category());
234
235     $template->param(
236         attribute_type_form => 1,
237         edit_attribute_type => 1,
238         confirm_op => 'edit_attribute_type_confirmed',
239     );
240
241 }
242
243 sub patron_attribute_type_list {
244     my $template = shift;
245     
246     my @attr_types = C4::Members::AttributeTypes::GetAttributeTypes();
247     $template->param(available_attribute_types => \@attr_types);
248     $template->param(display_list => 1);
249 }
250
251 sub authorised_value_category_list {
252     my $template = shift;
253     my $selected = @_ ? shift : '';
254
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;
261     }
262     $template->param(authorised_value_categories => \@list);
263 }