supplier management changes
[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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19 #
20
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;
28
29 my $script_name = "/cgi-bin/koha/admin/patron-attr-types.pl";
30
31 my $input = new CGI;
32 my $op = $input->param('op');
33
34
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                  });
43
44 $template->param(script_name => $script_name);
45
46 my $code = $input->param("code");
47
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;
64 }
65
66 if ($display_list) {
67     unless (C4::Context->preference('ExtendedPatronAttributes')) {
68         $template->param(WARNING_extended_attributes_off => 1); 
69     }
70     patron_attribute_type_list($template);
71 }
72
73 output_html_with_http_headers $input, $cookie, $template->output;
74
75 exit 0;
76
77 sub add_attribute_type_form {
78     my $template = shift;
79
80     $template->param(
81         attribute_type_form => 1,
82         confirm_op => 'add_attribute_type_confirmed',
83     );
84     authorised_value_category_list($template);
85 }
86
87 sub error_add_attribute_type_form {
88     my $template = shift;
89
90     $template->param(description => $input->param('description'));
91
92     if ($input->param('repeatable')) {
93         $template->param(repeatable_checked => 'checked="checked"');
94     }
95     if ($input->param('unique_id')) {
96         $template->param(unique_id_checked => 'checked="checked"');
97     }
98     if ($input->param('password_allowed')) {
99         $template->param(password_allowed_checked => 'checked="checked"');
100     }
101     if ($input->param('opac_display')) {
102         $template->param(opac_display_checked => 'checked="checked"');
103     }
104     if ($input->param('staff_searchable')) {
105         $template->param(staff_searchable_checked => 'checked="checked"');
106     }
107
108     $template->param(
109         attribute_type_form => 1,
110         confirm_op => 'add_attribute_type_confirmed',
111     );
112     authorised_value_category_list($template, $input->param('authorised_value_category'));
113 }
114
115 sub add_update_attribute_type {
116     my $op = shift;
117     my $template = shift;
118     my $code = shift;
119
120     my $description = $input->param('description');
121
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;
132         }
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);
138     }
139
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);
148
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());
153     }
154     $attr_type->store();
155
156     return 1;
157 }
158
159 sub delete_attribute_type_form {
160     my $template = shift;
161     my $code = shift;
162
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(),
171         );
172     } else {
173         $template->param(ERROR_delete_not_found => $code);
174         $display_list = 1;
175     }
176     return $display_list;
177 }
178
179 sub delete_attribute_type {
180     my $template = shift;
181     my $code = shift;
182
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);
191         }
192     } else {
193         $template->param(ERROR_delete_not_found => $code);
194     }
195 }
196
197 sub edit_attribute_type_form {
198     my $template = shift;
199     my $code = shift;
200
201     my $attr_type = C4::Members::AttributeTypes->fetch($code);
202
203     $template->param(code => $code);
204     $template->param(description => $attr_type->description());
205
206     if ($attr_type->repeatable()) {
207         $template->param(repeatable_checked => 'checked="checked"');
208     }
209     $template->param(repeatable_disabled => 'disabled="disabled"');
210     if ($attr_type->unique_id()) {
211         $template->param(unique_id_checked => 'checked="checked"');
212     }
213     $template->param(unique_id_disabled => 'disabled="disabled"');
214     if ($attr_type->password_allowed()) {
215         $template->param(password_allowed_checked => 'checked="checked"');
216     }
217     if ($attr_type->opac_display()) {
218         $template->param(opac_display_checked => 'checked="checked"');
219     }
220     if ($attr_type->staff_searchable()) {
221         $template->param(staff_searchable_checked => 'checked="checked"');
222     }
223
224     authorised_value_category_list($template, $attr_type->authorised_value_category());
225
226     $template->param(
227         attribute_type_form => 1,
228         edit_attribute_type => 1,
229         confirm_op => 'edit_attribute_type_confirmed',
230     );
231
232 }
233
234 sub patron_attribute_type_list {
235     my $template = shift;
236     
237     my @attr_types = C4::Members::AttributeTypes::GetAttributeTypes();
238     $template->param(available_attribute_types => \@attr_types);
239     $template->param(display_list => 1);
240 }
241
242 sub authorised_value_category_list {
243     my $template = shift;
244     my $selected = @_ ? shift : '';
245
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;
252     }
253     $template->param(authorised_value_categories => \@list);
254 }