Fix for Bug 5689 - System preference notifications are not translatable
[koha.git] / admin / patron-attr-types.pl
1 #! /usr/bin/perl
2 #
3 # Copyright 2008 LibLime
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #
20
21 use strict;
22 use warnings;
23 use CGI;
24 use C4::Auth;
25 use C4::Context;
26 use C4::Output;
27 use C4::Koha;
28 use C4::Members::AttributeTypes;
29
30 my $script_name = "/cgi-bin/koha/admin/patron-attr-types.pl";
31
32 my $input = new CGI;
33 my $op = $input->param('op') || '';
34
35
36 my ($template, $loggedinuser, $cookie)
37     = get_template_and_user({template_name => "admin/patron-attr-types.tmpl",
38                  query => $input,
39                  type => "intranet",
40                  authnotrequired => 0,
41                  flagsrequired => {parameters => 1},
42                  debug => 1,
43                  });
44
45 $template->param(script_name => $script_name);
46
47 my $code = $input->param("code");
48
49 my $display_list = 0;
50 if ($op eq "edit_attribute_type") {
51     edit_attribute_type_form($template, $code);
52 } elsif ($op eq "edit_attribute_type_confirmed") {
53     $display_list = add_update_attribute_type('edit', $template, $code);
54 } elsif ($op eq "add_attribute_type") {
55     add_attribute_type_form($template);
56 } elsif ($op eq "add_attribute_type_confirmed") {
57     $display_list = add_update_attribute_type('add', $template, $code);
58 } elsif ($op eq "delete_attribute_type") {
59     $display_list = delete_attribute_type_form($template, $code);
60 } elsif ($op eq "delete_attribute_type_confirmed") {
61     delete_attribute_type($template, $code);
62     $display_list = 1;
63 } else {
64     $display_list = 1;
65 }
66
67 if ($display_list) {
68     unless (C4::Context->preference('ExtendedPatronAttributes')) {
69         $template->param(WARNING_extended_attributes_off => 1); 
70     }
71     patron_attribute_type_list($template);
72 }
73
74 output_html_with_http_headers $input, $cookie, $template->output;
75
76 exit 0;
77
78 sub add_attribute_type_form {
79     my $template = shift;
80
81     $template->param(
82         attribute_type_form => 1,
83         confirm_op => 'add_attribute_type_confirmed',
84     );
85     authorised_value_category_list($template);
86 }
87
88 sub error_add_attribute_type_form {
89     my $template = shift;
90
91     $template->param(description => $input->param('description'));
92
93     if ($input->param('repeatable')) {
94         $template->param(repeatable_checked => 'checked="checked"');
95     }
96     if ($input->param('unique_id')) {
97         $template->param(unique_id_checked => 'checked="checked"');
98     }
99     if ($input->param('password_allowed')) {
100         $template->param(password_allowed_checked => 'checked="checked"');
101     }
102     if ($input->param('opac_display')) {
103         $template->param(opac_display_checked => 'checked="checked"');
104     }
105     if ($input->param('staff_searchable')) {
106         $template->param(staff_searchable_checked => 'checked="checked"');
107     }
108
109     $template->param(
110         attribute_type_form => 1,
111         confirm_op => 'add_attribute_type_confirmed',
112     );
113     authorised_value_category_list($template, $input->param('authorised_value_category'));
114 }
115
116 sub add_update_attribute_type {
117     my $op = shift;
118     my $template = shift;
119     my $code = shift;
120
121     my $description = $input->param('description');
122
123     my $attr_type;
124     if ($op eq 'edit') {
125         $attr_type = C4::Members::AttributeTypes->fetch($code);
126         $attr_type->description($description);
127     } else {
128         my $existing = C4::Members::AttributeTypes->fetch($code);
129         if (defined($existing)) {
130             $template->param(duplicate_code_error => $code);
131             error_add_attribute_type_form($template);
132             return 0;
133         }
134         $attr_type = C4::Members::AttributeTypes->new($code, $description);
135         my $repeatable = $input->param('repeatable');
136         $attr_type->repeatable($repeatable);
137         my $unique_id = $input->param('unique_id');
138         $attr_type->unique_id($unique_id);
139     }
140
141     my $opac_display = $input->param('opac_display');
142     $attr_type->opac_display($opac_display);
143     my $staff_searchable = $input->param('staff_searchable');
144     $attr_type->staff_searchable($staff_searchable);
145     my $authorised_value_category = $input->param('authorised_value_category');
146     $attr_type->authorised_value_category($authorised_value_category);
147     my $password_allowed = $input->param('password_allowed');
148     $attr_type->password_allowed($password_allowed);
149
150     if ($op eq 'edit') {
151         $template->param(edited_attribute_type => $attr_type->code());
152     } else {
153         $template->param(added_attribute_type => $attr_type->code());
154     }
155     $attr_type->store();
156
157     return 1;
158 }
159
160 sub delete_attribute_type_form {
161     my $template = shift;
162     my $code = shift;
163
164     my $attr_type = C4::Members::AttributeTypes->fetch($code);
165     my $display_list = 0;
166     if (defined($attr_type)) {
167         $template->param(
168             delete_attribute_type_form => 1,
169             confirm_op => "delete_attribute_type_confirmed",
170             code => $code,
171             description => $attr_type->description(),
172         );
173     } else {
174         $template->param(ERROR_delete_not_found => $code);
175         $display_list = 1;
176     }
177     return $display_list;
178 }
179
180 sub delete_attribute_type {
181     my $template = shift;
182     my $code = shift;
183
184     my $attr_type = C4::Members::AttributeTypes->fetch($code);
185     if (defined($attr_type)) {
186         if ($attr_type->num_patrons() > 0) {
187             $template->param(ERROR_delete_in_use => $code);
188             $template->param(ERROR_num_patrons => $attr_type->num_patrons());
189         } else {
190             $attr_type->delete();
191             $template->param(deleted_attribute_type => $code);
192         }
193     } else {
194         $template->param(ERROR_delete_not_found => $code);
195     }
196 }
197
198 sub edit_attribute_type_form {
199     my $template = shift;
200     my $code = shift;
201
202     my $attr_type = C4::Members::AttributeTypes->fetch($code);
203
204     $template->param(code => $code);
205     $template->param(description => $attr_type->description());
206
207     if ($attr_type->repeatable()) {
208         $template->param(repeatable_checked => 'checked="checked"');
209     }
210     $template->param(repeatable_disabled => 'disabled="disabled"');
211     if ($attr_type->unique_id()) {
212         $template->param(unique_id_checked => 'checked="checked"');
213     }
214     $template->param(unique_id_disabled => 'disabled="disabled"');
215     if ($attr_type->password_allowed()) {
216         $template->param(password_allowed_checked => 'checked="checked"');
217     }
218     if ($attr_type->opac_display()) {
219         $template->param(opac_display_checked => 'checked="checked"');
220     }
221     if ($attr_type->staff_searchable()) {
222         $template->param(staff_searchable_checked => 'checked="checked"');
223     }
224
225     authorised_value_category_list($template, $attr_type->authorised_value_category());
226
227     $template->param(
228         attribute_type_form => 1,
229         edit_attribute_type => 1,
230         confirm_op => 'edit_attribute_type_confirmed',
231     );
232
233 }
234
235 sub patron_attribute_type_list {
236     my $template = shift;
237     
238     my @attr_types = C4::Members::AttributeTypes::GetAttributeTypes();
239     $template->param(available_attribute_types => \@attr_types);
240     $template->param(display_list => 1);
241 }
242
243 sub authorised_value_category_list {
244     my $template = shift;
245     my $selected = @_ ? shift : '';
246
247     my $categories = GetAuthorisedValueCategories();
248     my @list = ();
249     foreach my $category (@$categories) {
250         my $entry = { category => $category };
251         $entry->{selected} = 1 if $category eq $selected;
252         push @list, $entry;
253     }
254     $template->param(authorised_value_categories => \@list);
255 }