Bug 25301: removed useless URI parameter "categorycode" from template
[koha.git] / admin / authorised_values.pl
blobe8cffdd697f963f78be0b123a8dace7defa74f74
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
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.
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.
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>.
20 use Modern::Perl;
22 use CGI qw ( -utf8 );
23 use List::MoreUtils qw(any);
25 use C4::Auth;
26 use C4::Context;
27 use C4::Koha;
28 use C4::Output;
30 use Koha::AuthorisedValues;
31 use Koha::AuthorisedValueCategories;
32 use Koha::Libraries;
34 my $input = new CGI;
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;
42 our ($template, $borrowernumber, $cookie)= get_template_and_user({
43 template_name => "admin/authorised_values.tt",
44 authnotrequired => 0,
45 flagsrequired => {parameters => 'manage_auth_values'},
46 query => $input,
47 type => "intranet",
48 debug => 1,
49 });
51 ################## ADD_FORM ##################################
52 # called by default. Used to create form to add or modify a record
53 if ($op eq 'add_form') {
54 my ( @selected_branches, $category, $av );
55 if ($id) {
56 $av = Koha::AuthorisedValues->new->find( $id );
57 @selected_branches = $av->library_limits ? $av->library_limits->as_list : ();
58 } else {
59 $category = $input->param('category');
62 my $branches = Koha::Libraries->search( {}, { order_by => ['branchname'] } );
63 my @branches_loop;
64 while ( my $branch = $branches->next ) {
65 push @branches_loop, {
66 branchcode => $branch->branchcode,
67 branchname => $branch->branchname,
68 selected => any {$_->branchcode eq $branch->branchcode} @selected_branches,
72 if ($id) {
73 $template->param(action_modify => 1);
74 } elsif ( ! $category ) {
75 $template->param(action_add_category => 1);
76 } else {
77 $template->param(action_add_value => 1);
80 if ( $av ) {
81 $template->param(
82 category => $av->category,
83 authorised_value => $av->authorised_value,
84 lib => $av->lib,
85 lib_opac => $av->lib_opac,
86 id => $av->id,
87 imagesets => C4::Koha::getImageSets( checked => $av->imageurl ),
89 } else {
90 $template->param(
91 category_name => $category,
92 imagesets => C4::Koha::getImageSets(),
95 $template->param(
96 branches_loop => \@branches_loop,
99 } elsif ($op eq 'add') {
100 my $new_authorised_value = $input->param('authorised_value');
101 my $new_category = $input->param('category');
102 my $imageurl = $input->param( 'imageurl' ) || '';
103 $imageurl = '' if $imageurl =~ /removeImage/;
104 my $duplicate_entry = 0;
105 my @branches = grep { $_ ne q{} } $input->multi_param('branches');
107 if ( $new_category eq 'branches' or $new_category eq 'itemtypes' or $new_category eq 'cn_source' ) {
108 push @messages, {type => 'error', code => 'invalid_category_name' };
110 elsif ( $id ) { # Update
111 my $av = Koha::AuthorisedValues->new->find( $id );
113 $av->lib( scalar $input->param('lib') || undef );
114 $av->lib_opac( scalar $input->param('lib_opac') || undef );
115 $av->category( $new_category );
116 $av->authorised_value( $new_authorised_value );
117 $av->imageurl( $imageurl );
118 eval{
119 $av->store;
120 $av->replace_library_limits( \@branches );
122 if ( $@ ) {
123 push @messages, {type => 'error', code => 'error_on_update' };
124 } else {
125 push @messages, { type => 'message', code => 'success_on_update' };
128 else { # Insert
129 eval {
130 my $av = Koha::AuthorisedValue->new(
132 category => $new_category,
133 authorised_value => $new_authorised_value,
134 lib => scalar $input->param('lib') || undef,
135 lib_opac => scalar $input->param('lib_opac') || undef,
136 imageurl => $imageurl,
138 )->store;
139 $av->replace_library_limits( \@branches );
140 $av->store;
143 if ( $@ ) {
144 push @messages, {type => 'error', code => 'error_on_insert' };
145 } else {
146 push @messages, { type => 'message', code => 'success_on_insert' };
150 $op = 'list';
151 $searchfield = $new_category;
152 } elsif ($op eq 'add_category' ) {
153 my $new_category = $input->param('category');
155 my $already_exists = Koha::AuthorisedValueCategories->find(
157 category_name => $new_category,
161 if ( $already_exists ) {
162 if ( $new_category eq 'branches' or $new_category eq 'itemtypes' or $new_category eq 'cn_source' ) {
163 push @messages, {type => 'error', code => 'invalid_category_name' };
164 } else {
165 push @messages, {type => 'error', code => 'cat_already_exists' };
168 else { # Insert
169 my $av = Koha::AuthorisedValueCategory->new( {
170 category_name => $new_category,
171 } );
173 eval {
174 $av->store;
177 if ( $@ ) {
178 push @messages, {type => 'error', code => 'error_on_insert_cat' };
179 } else {
180 push @messages, { type => 'message', code => 'success_on_insert_cat' };
181 $searchfield = $new_category;
185 $op = 'list';
186 } elsif ($op eq 'delete') {
187 my $av = Koha::AuthorisedValues->new->find( $id );
188 my $deleted = eval {$av->delete};
189 if ( $@ or not $deleted ) {
190 push @messages, {type => 'error', code => 'error_on_delete' };
191 } else {
192 push @messages, { type => 'message', code => 'success_on_delete' };
195 $op = 'list';
196 } elsif ($op eq 'delete_category') {
197 my $category_name = $input->param('category_name');
198 my $avc = Koha::AuthorisedValueCategories->find( $category_name );
199 my $deleted = eval {$avc->delete};
200 if ( $@ or not $deleted ) {
201 push @messages, {type => 'error', code => 'error_on_delete_category' };
202 } else {
203 push @messages, { type => 'message', code => 'success_on_delete_category' };
206 $op = 'list';
209 $template->param(
210 op => $op,
211 searchfield => $searchfield,
212 messages => \@messages,
215 if ( $op eq 'list' ) {
216 # build categories list
217 my @categories = Koha::AuthorisedValueCategories->search({ category_name => { -not_in => ['', 'branches', 'itemtypes', 'cn_source']}}, { order_by => ['category_name'] } );
218 my @category_list;
219 for my $category ( @categories ) {
220 push( @category_list, $category->category_name );
223 $searchfield ||= $category_list[0];
225 my @avs_by_category = Koha::AuthorisedValues->new->search( { category => $searchfield } );
226 my @loop_data = ();
227 # builds value list
228 for my $av ( @avs_by_category ) {
229 my %row_data; # get a fresh hash for the row data
230 $row_data{category} = $av->category;
231 $row_data{authorised_value} = $av->authorised_value;
232 $row_data{lib} = $av->lib;
233 $row_data{lib_opac} = $av->lib_opac;
234 $row_data{imageurl} = getitemtypeimagelocation( 'intranet', $av->imageurl );
235 $row_data{branches} = $av->library_limits ? $av->library_limits->as_list : [];
236 $row_data{id} = $av->id;
237 push(@loop_data, \%row_data);
240 $template->param(
241 loop => \@loop_data,
242 category => Koha::AuthorisedValueCategories->find($searchfield), # TODO Move this up and add a Koha::AVC->authorised_values method to replace call for avs_by_category
243 categories => \@category_list,
247 output_html_with_http_headers $input, $cookie, $template->output;