Bug 5466 On new orders get currency from vendor
[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 => 'checked="checked"');
96     }
97     if ($input->param('unique_id')) {
98         $template->param(unique_id_checked => 'checked="checked"');
99     }
100     if ($input->param('password_allowed')) {
101         $template->param(password_allowed_checked => 'checked="checked"');
102     }
103     if ($input->param('opac_display')) {
104         $template->param(opac_display_checked => 'checked="checked"');
105     }
106     if ($input->param('staff_searchable')) {
107         $template->param(staff_searchable_checked => 'checked="checked"');
108     }
109
110     $template->param(
111         attribute_type_form => 1,
112         confirm_op => 'add_attribute_type_confirmed',
113     );
114     authorised_value_category_list($template, $input->param('authorised_value_category'));
115 }
116
117 sub add_update_attribute_type {
118     my $op = shift;
119     my $template = shift;
120     my $code = shift;
121
122     my $description = $input->param('description');
123
124     my $attr_type;
125     if ($op eq 'edit') {
126         $attr_type = C4::Members::AttributeTypes->fetch($code);
127         $attr_type->description($description);
128     } else {
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);
133             return 0;
134         }
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);
140     }
141
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);
150
151     if ($op eq 'edit') {
152         $template->param(edited_attribute_type => $attr_type->code());
153     } else {
154         $template->param(added_attribute_type => $attr_type->code());
155     }
156     $attr_type->store();
157
158     return 1;
159 }
160
161 sub delete_attribute_type_form {
162     my $template = shift;
163     my $code = shift;
164
165     my $attr_type = C4::Members::AttributeTypes->fetch($code);
166     my $display_list = 0;
167     if (defined($attr_type)) {
168         $template->param(
169             delete_attribute_type_form => 1,
170             confirm_op => "delete_attribute_type_confirmed",
171             code => $code,
172             description => $attr_type->description(),
173         );
174     } else {
175         $template->param(ERROR_delete_not_found => $code);
176         $display_list = 1;
177     }
178     return $display_list;
179 }
180
181 sub delete_attribute_type {
182     my $template = shift;
183     my $code = shift;
184
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());
190         } else {
191             $attr_type->delete();
192             $template->param(deleted_attribute_type => $code);
193         }
194     } else {
195         $template->param(ERROR_delete_not_found => $code);
196     }
197 }
198
199 sub edit_attribute_type_form {
200     my $template = shift;
201     my $code = shift;
202
203     my $attr_type = C4::Members::AttributeTypes->fetch($code);
204
205     $template->param(code => $code);
206     $template->param(description => $attr_type->description());
207
208     if ($attr_type->repeatable()) {
209         $template->param(repeatable_checked => 'checked="checked"');
210     }
211     $template->param(repeatable_disabled => 'disabled="disabled"');
212     if ($attr_type->unique_id()) {
213         $template->param(unique_id_checked => 'checked="checked"');
214     }
215     $template->param(unique_id_disabled => 'disabled="disabled"');
216     if ($attr_type->password_allowed()) {
217         $template->param(password_allowed_checked => 'checked="checked"');
218     }
219     if ($attr_type->opac_display()) {
220         $template->param(opac_display_checked => 'checked="checked"');
221     }
222     if ($attr_type->staff_searchable()) {
223         $template->param(staff_searchable_checked => 'checked="checked"');
224     }
225
226     authorised_value_category_list($template, $attr_type->authorised_value_category());
227
228     $template->param(
229         attribute_type_form => 1,
230         edit_attribute_type => 1,
231         confirm_op => 'edit_attribute_type_confirmed',
232     );
233
234 }
235
236 sub patron_attribute_type_list {
237     my $template = shift;
238     
239     my @attr_types = C4::Members::AttributeTypes::GetAttributeTypes();
240     $template->param(available_attribute_types => \@attr_types);
241     $template->param(display_list => 1);
242 }
243
244 sub authorised_value_category_list {
245     my $template = shift;
246     my $selected = @_ ? shift : '';
247
248     my $categories = GetAuthorisedValueCategories();
249     my @list = ();
250     foreach my $category (@$categories) {
251         my $entry = { category => $category };
252         $entry->{selected} = 1 if $category eq $selected;
253         push @list, $entry;
254     }
255     $template->param(authorised_value_categories => \@list);
256 }