Bug 29407: Make the pickup locations dropdown JS reusable
[koha.git] / admin / authorised_values.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use CGI qw ( -utf8 );
23 use List::MoreUtils qw( any );
24
25 use C4::Auth qw( get_template_and_user );
26 use C4::Context;
27 use C4::Koha qw( getitemtypeimagelocation );
28 use C4::Output qw( output_html_with_http_headers );
29
30 use Koha::AuthorisedValues;
31 use Koha::AuthorisedValueCategories;
32 use Koha::Libraries;
33
34 my $input = CGI->new;
35 my $id          = $input->param('id');
36 my $op          = $input->param('op') || 'list';
37 my $searchfield = $input->param('searchfield');
38 $searchfield = '' unless defined $searchfield;
39 $searchfield =~ s/\,//g;
40 my @messages;
41
42 our ($template, $borrowernumber, $cookie)= get_template_and_user({
43     template_name => "admin/authorised_values.tt",
44     flagsrequired => {parameters => 'manage_auth_values'},
45     query => $input,
46     type => "intranet",
47 });
48
49 ################## ADD_FORM ##################################
50 # called by default. Used to create form to add or  modify a record
51 if ($op eq 'add_form') {
52     my ( @selected_branches, $category, $av );
53     if ($id) {
54         $av = Koha::AuthorisedValues->new->find( $id );
55         @selected_branches = $av->library_limits ? $av->library_limits->as_list : ();
56     } else {
57         $category = $input->param('category');
58     }
59
60     my $branches = Koha::Libraries->search( {}, { order_by => ['branchname'] } );
61     my @branches_loop;
62     while ( my $branch = $branches->next ) {
63         push @branches_loop, {
64             branchcode => $branch->branchcode,
65             branchname => $branch->branchname,
66             selected   => any {$_->branchcode eq $branch->branchcode} @selected_branches,
67         };
68     }
69
70         if ($id) {
71                 $template->param(action_modify => 1);
72    } elsif ( ! $category ) {
73                 $template->param(action_add_category => 1);
74         } else {
75                 $template->param(action_add_value => 1);
76         }
77
78     if ( $av ) {
79         $template->param(
80             category_name => $av->category,
81             authorised_value => $av->authorised_value,
82             lib              => $av->lib,
83             lib_opac         => $av->lib_opac,
84             id               => $av->id,
85             imagesets        => C4::Koha::getImageSets( checked => $av->imageurl ),
86         );
87     } else {
88         $template->param(
89             category_name  => $category,
90             imagesets => C4::Koha::getImageSets(),
91         );
92     }
93     $template->param(
94         branches_loop    => \@branches_loop,
95     );
96
97 } elsif ($op eq 'add') {
98     my $new_authorised_value = $input->param('authorised_value');
99     my $new_category = $input->param('category');
100     my $imageurl     = $input->param( 'imageurl' ) || '';
101     $imageurl = '' if $imageurl =~ /removeImage/;
102     my $duplicate_entry = 0;
103     my @branches = grep { $_ ne q{} } $input->multi_param('branches');
104
105     if ( $new_category eq 'branches' or $new_category eq 'itemtypes' or $new_category eq 'cn_source' ) {
106         push @messages, {type => 'error', code => 'invalid_category_name' };
107     }
108     elsif ( $id ) { # Update
109         my $av = Koha::AuthorisedValues->new->find( $id );
110
111         $av->lib( scalar $input->param('lib') || undef );
112         $av->lib_opac( scalar $input->param('lib_opac') || undef );
113         $av->category( $new_category );
114         $av->authorised_value( $new_authorised_value );
115         $av->imageurl( $imageurl );
116         eval{
117             $av->store;
118             $av->replace_library_limits( \@branches );
119         };
120         if ( $@ ) {
121             push @messages, {type => 'error', code => 'error_on_update' };
122         } else {
123             push @messages, { type => 'message', code => 'success_on_update' };
124         }
125     }
126     else { # Insert
127         eval {
128             my $av = Koha::AuthorisedValue->new(
129                 {
130                     category         => $new_category,
131                     authorised_value => $new_authorised_value,
132                     lib              => scalar $input->param('lib') || undef,
133                     lib_opac         => scalar $input->param('lib_opac') || undef,
134                     imageurl         => $imageurl,
135                 }
136             )->store;
137             $av->replace_library_limits( \@branches );
138             $av->store;
139         };
140
141         if ( $@ ) {
142             push @messages, {type => 'error', code => 'error_on_insert' };
143         } else {
144             push @messages, { type => 'message', code => 'success_on_insert' };
145         }
146     }
147
148     $op = 'list';
149     $searchfield = $new_category;
150 } elsif ($op eq 'add_category' ) {
151     my $new_category = $input->param('category');
152
153     my $already_exists = Koha::AuthorisedValueCategories->find(
154         {
155             category_name => $new_category,
156         }
157     );
158
159     if ( $already_exists ) {
160         if ( $new_category eq 'branches' or $new_category eq 'itemtypes' or $new_category eq 'cn_source' ) {
161             push @messages, {type => 'error', code => 'invalid_category_name' };
162         } else {
163             push @messages, {type => 'error', code => 'cat_already_exists' };
164         }
165     }
166     else { # Insert
167         my $av = Koha::AuthorisedValueCategory->new( {
168             category_name => $new_category,
169         } );
170
171         eval {
172             $av->store;
173         };
174
175         if ( $@ ) {
176             push @messages, {type => 'error', code => 'error_on_insert_cat' };
177         } else {
178             push @messages, { type => 'message', code => 'success_on_insert_cat' };
179             $searchfield = $new_category;
180         }
181     }
182
183     $op = 'list';
184 } elsif ($op eq 'delete') {
185     my $av = Koha::AuthorisedValues->new->find( $id );
186     my $deleted = eval {$av->delete};
187     if ( $@ or not $deleted ) {
188         push @messages, {type => 'error', code => 'error_on_delete' };
189     } else {
190         push @messages, { type => 'message', code => 'success_on_delete' };
191     }
192
193     $op = 'list';
194 } elsif ($op eq 'delete_category') {
195     my $category_name = $input->param('category_name');
196     my $avc = Koha::AuthorisedValueCategories->find( $category_name );
197     my $deleted = eval {$avc->delete};
198     if ( $@ or not $deleted ) {
199         push @messages, {type => 'error', code => 'error_on_delete_category' };
200     } else {
201         push @messages, { type => 'message', code => 'success_on_delete_category' };
202     }
203
204     $op = 'list';
205 }
206
207 $template->param(
208     op => $op,
209     searchfield => $searchfield,
210     messages => \@messages,
211 );
212
213 if ( $op eq 'list' ) {
214     # build categories list
215     my @categories = Koha::AuthorisedValueCategories->search({ category_name => { -not_in => ['', 'branches', 'itemtypes', 'cn_source']}}, { order_by => ['category_name'] } );
216     my @category_list;
217     for my $category ( @categories ) {
218         push( @category_list, $category->category_name );
219     }
220
221     $searchfield ||= $category_list[0];
222
223     my @avs_by_category = Koha::AuthorisedValues->new->search( { category => $searchfield } );
224     my @loop_data = ();
225     # builds value list
226     for my $av ( @avs_by_category ) {
227         my %row_data;  # get a fresh hash for the row data
228         $row_data{category}              = $av->category;
229         $row_data{authorised_value}      = $av->authorised_value;
230         $row_data{lib}                   = $av->lib;
231         $row_data{lib_opac}              = $av->lib_opac;
232         $row_data{imageurl}              = getitemtypeimagelocation( 'intranet', $av->imageurl );
233         $row_data{branches}              = $av->library_limits ? $av->library_limits->as_list : [];
234         $row_data{id}                    = $av->id;
235         push(@loop_data, \%row_data);
236     }
237
238     $template->param(
239         loop     => \@loop_data,
240         category => Koha::AuthorisedValueCategories->find($searchfield), # TODO Move this up and add a Koha::AVC->authorised_values method to replace call for avs_by_category
241         categories => \@category_list,
242     );
243
244 }
245 output_html_with_http_headers $input, $cookie, $template->output;