Bug 15128 - (QA Followup) Fix use of 'my' variable causing loss of data
[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 C4::Auth;
24 use C4::Branch;
25 use C4::Context;
26 use C4::Koha;
27 use C4::Output;
28
29 use Koha::AuthorisedValues;
30
31 my $input = new CGI;
32 my $id          = $input->param('id');
33 my $op          = $input->param('op') || 'list';
34 my $searchfield = $input->param('searchfield');
35 $searchfield = '' unless defined $searchfield;
36 $searchfield =~ s/\,//g;
37 my @messages;
38
39 our ($template, $borrowernumber, $cookie)= get_template_and_user({
40     template_name => "admin/authorised_values.tt",
41     authnotrequired => 0,
42     flagsrequired => {parameters => 'parameters_remaining_permissions'},
43     query => $input,
44     type => "intranet",
45     debug => 1,
46 });
47
48 ################## ADD_FORM ##################################
49 # called by default. Used to create form to add or  modify a record
50 if ($op eq 'add_form') {
51     my ( $selected_branches, $category, $av );
52     if ($id) {
53         $av = Koha::AuthorisedValues->new->find( $id );
54         $selected_branches = $av->branch_limitations;
55     } else {
56         $category = $input->param('category');
57     }
58
59     my $branches = GetBranches;
60     my @branches_loop;
61
62     foreach my $branchcode ( sort { uc($branches->{$a}->{branchname}) cmp uc($branches->{$b}->{branchname}) } keys %$branches ) {
63         my $selected = ( grep {$_ eq $branchcode} @$selected_branches ) ? 1 : 0;
64         push @branches_loop, {
65             branchcode => $branchcode,
66             branchname => $branches->{$branchcode}->{branchname},
67             selected => $selected,
68         };
69     }
70
71         if ($id) {
72                 $template->param(action_modify => 1);
73    } elsif ( ! $category ) {
74                 $template->param(action_add_category => 1);
75         } else {
76                 $template->param(action_add_value => 1);
77         }
78
79     if ( $av ) {
80         $template->param(
81             category => $av->category,
82             authorised_value => $av->authorised_value,
83             lib              => $av->lib,
84             lib_opac         => $av->lib_opac,
85             id               => $av->id,
86             imagesets        => C4::Koha::getImageSets( checked => $av->imageurl ),
87         );
88     } else {
89         $template->param(
90             category  => $category,
91             imagesets => C4::Koha::getImageSets(),
92         );
93     }
94     $template->param(
95         branches_loop    => \@branches_loop,
96     );
97
98 } elsif ($op eq 'add') {
99     my $new_authorised_value = $input->param('authorised_value');
100     my $new_category = $input->param('category');
101     my $imageurl     = $input->param( 'imageurl' ) || '';
102     $imageurl = '' if $imageurl =~ /removeImage/;
103     my $duplicate_entry = 0;
104     my @branches = grep { $_ ne q{} } $input->multi_param('branches');
105
106     my $already_exists = Koha::AuthorisedValues->search(
107         {
108             category => $new_category,
109             authorised_value => $new_authorised_value,
110         }
111     )->next;
112
113     if ( $already_exists and ( not $id or $already_exists->id != $id ) ) {
114         push @messages, {type => 'error', code => 'already_exists' };
115     }
116     elsif ( $id ) { # Update
117         my $av = Koha::AuthorisedValues->new->find( $id );
118
119         $av->lib( $input->param('lib') || undef );
120         $av->lib_opac( $input->param('lib_opac') || undef );
121         $av->category( $new_category );
122         $av->authorised_value( $new_authorised_value );
123         $av->imageurl( $imageurl );
124         eval{
125             $av->store;
126             $av->replace_branch_limitations( \@branches );
127         };
128         if ( $@ ) {
129             push @messages, {type => 'error', code => 'error_on_update' };
130         } else {
131             push @messages, { type => 'message', code => 'success_on_update' };
132         }
133     }
134     else { # Insert
135         my $av = Koha::AuthorisedValue->new( {
136             category => $new_category,
137             authorised_value => $new_authorised_value,
138             lib => scalar $input->param('lib') || undef,
139             lib_opac => scalar $input->param('lib_opac') || undef,
140             imageurl => $imageurl,
141         } );
142
143         eval {
144             $av->store;
145             $av->replace_branch_limitations( \@branches );
146         };
147
148         if ( $@ ) {
149             push @messages, {type => 'error', code => 'error_on_insert' };
150         } else {
151             push @messages, { type => 'message', code => 'success_on_insert' };
152         }
153     }
154
155     $op = 'list';
156     $searchfield = $new_category;
157 } elsif ($op eq 'delete') {
158     my $av = Koha::AuthorisedValues->new->find( $input->param('id') );
159     my $deleted = eval {$av->delete};
160     if ( $@ or not $deleted ) {
161         push @messages, {type => 'error', code => 'error_on_delete' };
162     } else {
163         push @messages, { type => 'message', code => 'success_on_delete' };
164     }
165
166     $op = 'list';
167     $template->param( delete_success => 1 );
168 }
169
170 $template->param(
171     op => $op,
172     searchfield => $searchfield,
173     messages => \@messages,
174 );
175
176 if ( $op eq 'list' ) {
177     # build categories list
178     my @categories = Koha::AuthorisedValues->new->categories;
179     my @category_list;
180     my %categories;    # a hash, to check that some hardcoded categories exist.
181     for my $category ( @categories ) {
182         push( @category_list, $category );
183         $categories{$category} = 1;
184     }
185
186     # push koha system categories
187     foreach (qw(Asort1 Asort2 Bsort1 Bsort2 SUGGEST DAMAGED LOST REPORT_GROUP REPORT_SUBGROUP DEPARTMENT TERM SUGGEST_STATUS ITEMTYPECAT)) {
188         push @category_list, $_ unless $categories{$_};
189     }
190
191     #reorder the list
192     @category_list = sort {$a cmp $b} @category_list;
193
194     $searchfield ||= $category_list[0];
195
196     my @avs_by_category = Koha::AuthorisedValues->new->search( { category => $searchfield } );
197     my @loop_data = ();
198     # builds value list
199     for my $av ( @avs_by_category ) {
200         my %row_data;  # get a fresh hash for the row data
201         $row_data{category}              = $av->category;
202         $row_data{authorised_value}      = $av->authorised_value;
203         $row_data{lib}                   = $av->lib;
204         $row_data{lib_opac}              = $av->lib_opac;
205         $row_data{imageurl}              = getitemtypeimagelocation( 'intranet', $av->imageurl );
206         $row_data{branches}              = $av->branch_limitations;
207         $row_data{id}                    = $av->id;
208         push(@loop_data, \%row_data);
209     }
210
211     $template->param(
212         loop     => \@loop_data,
213         category => $searchfield,
214         categories => \@category_list,
215     );
216
217 }
218 output_html_with_http_headers $input, $cookie, $template->output;