Bug 27204: Add unit tests for fine_items
[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 Koha::Patron::Attribute::Types;
32
33 use Koha::AuthorisedValues;
34 use Koha::Libraries;
35 use Koha::Patron::Categories;
36
37 my $script_name = "/cgi-bin/koha/admin/patron-attr-types.pl";
38
39 our $input = new CGI;
40 my $op = $input->param('op') || '';
41
42
43 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
44     {   template_name   => "admin/patron-attr-types.tt",
45         query           => $input,
46         type            => "intranet",
47         flagsrequired => { parameters => 'manage_patron_attributes' }
48     }
49 );
50
51
52 $template->param(script_name => $script_name);
53
54 my $code = $input->param("code");
55
56 my $display_list = 0;
57 if ($op eq "edit_attribute_type") {
58     edit_attribute_type_form($template, $code);
59 } elsif ($op eq "edit_attribute_type_confirmed") {
60     $display_list = add_update_attribute_type('edit', $template, $code);
61 } elsif ($op eq "add_attribute_type") {
62     add_attribute_type_form($template);
63 } elsif ($op eq "add_attribute_type_confirmed") {
64     $display_list = add_update_attribute_type('add', $template, $code);
65 } elsif ($op eq "delete_attribute_type") {
66     $display_list = delete_attribute_type_form($template, $code);
67 } elsif ($op eq "delete_attribute_type_confirmed") {
68     delete_attribute_type($template, $code);
69     $display_list = 1;
70 } else {
71     $display_list = 1;
72 }
73
74 if ($display_list) {
75     unless (C4::Context->preference('ExtendedPatronAttributes')) {
76         $template->param(WARNING_extended_attributes_off => 1); 
77     }
78     patron_attribute_type_list($template);
79 }
80
81 output_html_with_http_headers $input, $cookie, $template->output;
82
83 exit 0;
84
85 sub add_attribute_type_form {
86     my $template = shift;
87
88     my $patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
89     $template->param(
90         attribute_type_form => 1,
91         confirm_op => 'add_attribute_type_confirmed',
92         categories => $patron_categories,
93     );
94 }
95
96 sub error_add_attribute_type_form {
97     my $template = shift;
98
99     $template->param(description => scalar $input->param('description'));
100
101     $template->param( category_code => scalar $input->param('category_code') );
102     $template->param( class => scalar $input->param('class') );
103
104     $template->param(
105         attribute_type_form => 1,
106         confirm_op => 'add_attribute_type_confirmed',
107         authorised_value_category => scalar $input->param('authorised_value_category'),
108     );
109 }
110
111 sub add_update_attribute_type {
112     my $op       = shift;
113     my $template = shift;
114     my $code     = shift;
115
116     my $description               = $input->param('description');
117     my $repeatable                = $input->param('repeatable') ? 1 : 0;
118     my $unique_id                 = $input->param('unique_id') ? 1 : 0;
119     my $opac_display              = $input->param('opac_display') ? 1 : 0;
120     my $opac_editable             = $input->param('opac_editable') ? 1 : 0;
121     my $staff_searchable          = $input->param('staff_searchable') ? 1 : 0;
122     my $authorised_value_category = $input->param('authorised_value_category');
123     my $display_checkout          = $input->param('display_checkout') ? 1 : 0;
124     my $category_code             = $input->param('category_code') || undef;
125     my $class                     = $input->param('class');
126
127     my $attr_type = Koha::Patron::Attribute::Types->find($code);
128     if ( $op eq 'edit' ) {
129         $attr_type->description($description);
130     }
131     else {
132         if ($attr_type) {    # Already exists
133             $template->param( duplicate_code_error => $code );
134
135             # FIXME Regression here
136             # Form will not be refilled with entered values on error
137             error_add_attribute_type_form($template);
138             return 0;
139         }
140         $attr_type = Koha::Patron::Attribute::Type->new(
141             {
142                 code        => $code,
143                 description => $description,
144                 repeatable  => $repeatable,
145                 unique_id   => $unique_id,
146             }
147         );
148     }
149     $attr_type->set(
150         {
151             opac_display              => $opac_display,
152             opac_editable             => $opac_editable,
153             staff_searchable          => $staff_searchable,
154             authorised_value_category => $authorised_value_category,
155             display_checkout          => $display_checkout,
156             category_code             => $category_code,
157             class                     => $class,
158         }
159     )->store;
160
161     my @branches = grep { ! /^\s*$/ } $input->multi_param('branches');
162     $attr_type->library_limits( \@branches );
163
164     if ( $op eq 'edit' ) {
165         $template->param( edited_attribute_type => $attr_type->code() );
166     }
167     else {
168         $template->param( added_attribute_type => $attr_type->code() );
169     }
170
171     return 1;
172 }
173
174 sub delete_attribute_type_form {
175     my $template = shift;
176     my $code = shift;
177
178     my $attr_type = Koha::Patron::Attribute::Types->find($code);
179     my $display_list = 0;
180     if (defined($attr_type)) {
181         $template->param(
182             delete_attribute_type_form => 1,
183             confirm_op => "delete_attribute_type_confirmed",
184             code => $code,
185             description => $attr_type->description(),
186         );
187     } else {
188         $template->param(ERROR_delete_not_found => $code);
189         $display_list = 1;
190     }
191     return $display_list;
192 }
193
194 sub delete_attribute_type {
195     my $template = shift;
196     my $code = shift;
197
198     my $attr_type = Koha::Patron::Attribute::Types->find($code);
199     if (defined($attr_type)) {
200         # TODO Check must be done for previous step as well
201         if ( my $num_patrons = Koha::Patrons->filter_by_attribute_type($code)->count ) {
202             $template->param(ERROR_delete_in_use => $code);
203             $template->param(ERROR_num_patrons => $num_patrons );
204         } else {
205             $attr_type->delete();
206             $template->param(deleted_attribute_type => $code);
207         }
208     } else {
209         # FIXME Really needed?
210         $template->param(ERROR_delete_not_found => $code);
211     }
212 }
213
214 sub edit_attribute_type_form {
215     my $template = shift;
216     my $code = shift;
217
218     my $attr_type = Koha::Patron::Attribute::Types->find($code);
219     $template->param(attribute_type => $attr_type);
220
221     my $patron_categories = Koha::Patron::Categories->search({}, {order_by => ['description']});
222     $template->param(
223         attribute_type_form => 1,
224         edit_attribute_type => 1,
225         confirm_op => 'edit_attribute_type_confirmed',
226         categories => $patron_categories,
227     );
228
229 }
230
231 sub patron_attribute_type_list {
232     my $template = shift;
233
234     my @attr_types = Koha::Patron::Attribute::Types->search->as_list;
235
236     my @classes = uniq( map { $_->class } @attr_types );
237     @classes = sort @classes;
238
239     my @attributes_loop;
240     # FIXME This is not efficient and should be improved
241     for my $class (@classes) {
242         my @items;
243         for my $attr (@attr_types) {
244             next if $attr->class ne $class;
245             push @items, $attr;
246         }
247         my $av = Koha::AuthorisedValues->search({ category => 'PA_CLASS', authorised_value => $class });
248         my $lib = $av->count ? $av->next->lib : $class;
249         push @attributes_loop, {
250             class => $class,
251             items => \@items,
252             lib   => $lib,
253         };
254     }
255     $template->param(available_attribute_types => \@attributes_loop);
256     $template->param(display_list => 1);
257 }