Bug 15758: Koha::Libraries - Remove GetBranches
[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
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 #
21
22 use Modern::Perl;
23
24 use CGI qw ( -utf8 );
25 use List::MoreUtils qw/uniq/;
26
27 use C4::Auth;
28 use C4::Context;
29 use C4::Output;
30 use C4::Koha;
31 use C4::Members::AttributeTypes;
32
33 use Koha::Libraries;
34 use Koha::Patron::Categories;
35
36 my $script_name = "/cgi-bin/koha/admin/patron-attr-types.pl";
37
38 our $input = new CGI;
39 my $op = $input->param('op') || '';
40
41
42 our ($template, $loggedinuser, $cookie)
43     = get_template_and_user({template_name => "admin/patron-attr-types.tt",
44                  query => $input,
45                  type => "intranet",
46                  authnotrequired => 0,
47                  flagsrequired => {parameters => 'parameters_remaining_permissions'},
48                  debug => 1,
49                  });
50
51 $template->param(script_name => $script_name);
52
53 my $code = $input->param("code");
54
55 my $display_list = 0;
56 if ($op eq "edit_attribute_type") {
57     edit_attribute_type_form($template, $code);
58 } elsif ($op eq "edit_attribute_type_confirmed") {
59     $display_list = add_update_attribute_type('edit', $template, $code);
60 } elsif ($op eq "add_attribute_type") {
61     add_attribute_type_form($template);
62 } elsif ($op eq "add_attribute_type_confirmed") {
63     $display_list = add_update_attribute_type('add', $template, $code);
64 } elsif ($op eq "delete_attribute_type") {
65     $display_list = delete_attribute_type_form($template, $code);
66 } elsif ($op eq "delete_attribute_type_confirmed") {
67     delete_attribute_type($template, $code);
68     $display_list = 1;
69 } else {
70     $display_list = 1;
71 }
72
73 if ($display_list) {
74     unless (C4::Context->preference('ExtendedPatronAttributes')) {
75         $template->param(WARNING_extended_attributes_off => 1); 
76     }
77     patron_attribute_type_list($template);
78 }
79
80 output_html_with_http_headers $input, $cookie, $template->output;
81
82 exit 0;
83
84 sub add_attribute_type_form {
85     my $template = shift;
86
87     my $branches = Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed;
88     my @branches_loop;
89     foreach my $branch (sort keys %$branches) {
90         push @branches_loop, {
91             branchcode => $$branches{$branch}{branchcode},
92             branchname => $$branches{$branch}{branchname},
93         };
94     }
95
96     my $patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
97     $template->param(
98         attribute_type_form => 1,
99         confirm_op => 'add_attribute_type_confirmed',
100         categories => $patron_categories,
101         branches_loop => \@branches_loop,
102     );
103     authorised_value_category_list($template);
104     $template->param(classes_val_loop => GetAuthorisedValues( 'PA_CLASS'));
105 }
106
107 sub error_add_attribute_type_form {
108     my $template = shift;
109
110     $template->param(description => scalar $input->param('description'));
111
112     if ($input->param('repeatable')) {
113         $template->param(repeatable_checked => 1);
114     }
115     if ($input->param('unique_id')) {
116         $template->param(unique_id_checked => 1);
117     }
118     if ($input->param('opac_display')) {
119         $template->param(opac_display_checked => 1);
120     }
121     if ($input->param('staff_searchable')) {
122         $template->param(staff_searchable_checked => 1);
123     }
124     if ($input->param('display_checkout')) {
125         $template->param(display_checkout_checked => 'checked="checked"');
126     }
127
128     $template->param( category_code => scalar $input->param('category_code') );
129     $template->param( class => scalar $input->param('class') );
130
131     $template->param(
132         attribute_type_form => 1,
133         confirm_op => 'add_attribute_type_confirmed',
134     );
135     authorised_value_category_list($template, $input->param('authorised_value_category'));
136 }
137
138 sub add_update_attribute_type {
139     my $op = shift;
140     my $template = shift;
141     my $code = shift;
142
143     my $description = $input->param('description');
144
145     my $attr_type;
146     if ($op eq 'edit') {
147         $attr_type = C4::Members::AttributeTypes->fetch($code);
148         $attr_type->description($description);
149     } else {
150         my $existing = C4::Members::AttributeTypes->fetch($code);
151         if (defined($existing)) {
152             $template->param(duplicate_code_error => $code);
153             error_add_attribute_type_form($template);
154             return 0;
155         }
156         $attr_type = C4::Members::AttributeTypes->new($code, $description);
157         my $repeatable = $input->param('repeatable');
158         $attr_type->repeatable($repeatable);
159         my $unique_id = $input->param('unique_id');
160         $attr_type->unique_id($unique_id);
161     }
162
163     my $opac_display = $input->param('opac_display');
164     $attr_type->opac_display($opac_display);
165     my $staff_searchable = $input->param('staff_searchable');
166     $attr_type->staff_searchable($staff_searchable);
167     my $authorised_value_category = $input->param('authorised_value_category');
168     $attr_type->authorised_value_category($authorised_value_category);
169     my $display_checkout = $input->param('display_checkout');
170     $attr_type->display_checkout($display_checkout);
171     $attr_type->category_code(scalar $input->param('category_code'));
172     $attr_type->class(scalar $input->param('class'));
173     my @branches = $input->multi_param('branches');
174     $attr_type->branches( \@branches );
175
176     if ($op eq 'edit') {
177         $template->param(edited_attribute_type => $attr_type->code());
178     } else {
179         $template->param(added_attribute_type => $attr_type->code());
180     }
181     $attr_type->store();
182
183     return 1;
184 }
185
186 sub delete_attribute_type_form {
187     my $template = shift;
188     my $code = shift;
189
190     my $attr_type = C4::Members::AttributeTypes->fetch($code);
191     my $display_list = 0;
192     if (defined($attr_type)) {
193         $template->param(
194             delete_attribute_type_form => 1,
195             confirm_op => "delete_attribute_type_confirmed",
196             code => $code,
197             description => $attr_type->description(),
198         );
199     } else {
200         $template->param(ERROR_delete_not_found => $code);
201         $display_list = 1;
202     }
203     return $display_list;
204 }
205
206 sub delete_attribute_type {
207     my $template = shift;
208     my $code = shift;
209
210     my $attr_type = C4::Members::AttributeTypes->fetch($code);
211     if (defined($attr_type)) {
212         if ($attr_type->num_patrons() > 0) {
213             $template->param(ERROR_delete_in_use => $code);
214             $template->param(ERROR_num_patrons => $attr_type->num_patrons());
215         } else {
216             $attr_type->delete();
217             $template->param(deleted_attribute_type => $code);
218         }
219     } else {
220         $template->param(ERROR_delete_not_found => $code);
221     }
222 }
223
224 sub edit_attribute_type_form {
225     my $template = shift;
226     my $code = shift;
227
228     my $attr_type = C4::Members::AttributeTypes->fetch($code);
229
230     $template->param(code => $code);
231     $template->param(description => $attr_type->description());
232     $template->param(class => $attr_type->class());
233
234     if ($attr_type->repeatable()) {
235         $template->param(repeatable_checked => 1);
236     }
237     $template->param(repeatable_disabled => 1);
238     if ($attr_type->unique_id()) {
239         $template->param(unique_id_checked => 1);
240     }
241     $template->param(unique_id_disabled => 1);
242     if ($attr_type->opac_display()) {
243         $template->param(opac_display_checked => 1);
244     }
245     if ($attr_type->staff_searchable()) {
246         $template->param(staff_searchable_checked => 1);
247     }
248     if ($attr_type->display_checkout()) {
249         $template->param(display_checkout_checked => 'checked="checked"');
250     }
251     authorised_value_category_list($template, $attr_type->authorised_value_category());
252     $template->param(classes_val_loop => GetAuthorisedValues( 'PA_CLASS' ));
253
254
255     my $branches = Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed;
256     my @branches_loop;
257     my $selected_branches = $attr_type->branches;
258     foreach my $branch (@$branches) {
259         my $selected = ( grep {$_->{branchcode} eq $branch->{branchcode}} @$selected_branches ) ? 1 : 0;
260         push @branches_loop, {
261             branchcode => $branch->{branchcode},
262             branchname => $branch->{branchname},
263             selected => $selected,
264         };
265     }
266     $template->param( branches_loop => \@branches_loop );
267
268     $template->param(
269         category_code        => $attr_type->category_code,
270         category_class       => $attr_type->class,
271         category_description => $attr_type->category_description,
272     );
273
274     my $patron_categories = Koha::Patron::Categories->search({}, {order_by => ['description']});
275     $template->param(
276         attribute_type_form => 1,
277         edit_attribute_type => 1,
278         confirm_op => 'edit_attribute_type_confirmed',
279         categories => $patron_categories,
280     );
281
282 }
283
284 sub patron_attribute_type_list {
285     my $template = shift;
286
287     my @attr_types = C4::Members::AttributeTypes::GetAttributeTypes( 1, 1 );
288
289     my @classes = uniq( map { $_->{class} } @attr_types );
290     @classes = sort @classes;
291
292     my @attributes_loop;
293     for my $class (@classes) {
294         my ( @items, $branches );
295         for my $attr (@attr_types) {
296             next if $attr->{class} ne $class;
297             my $attr_type = C4::Members::AttributeTypes->fetch($attr->{code});
298             $attr->{branches} = $attr_type->branches;
299             push @items, $attr;
300         }
301         my $lib = GetAuthorisedValueByCode( 'PA_CLASS', $class ) || $class;
302         push @attributes_loop, {
303             class => $class,
304             items => \@items,
305             lib   => $lib,
306             branches => $branches,
307         };
308     }
309     $template->param(available_attribute_types => \@attributes_loop);
310     $template->param(display_list => 1);
311 }
312
313 sub authorised_value_category_list {
314     my $template = shift;
315     my $selected = @_ ? shift : '';
316
317     my $categories = GetAuthorisedValueCategories();
318     my @list = ();
319     foreach my $category (@$categories) {
320         my $entry = { category => $category };
321         $entry->{selected} = 1 if $category eq $selected;
322         push @list, $entry;
323     }
324     $template->param(authorised_value_categories => \@list);
325 }