Bug 16576 [Revised] Remove the use of "onclick" from label templates
[koha.git] / admin / authorised_values.pl
blob49fb661825c5eb7790e8f303e6d09efa8c59b64c
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 C4::Auth;
24 use C4::Context;
25 use C4::Koha;
26 use C4::Output;
28 use Koha::AuthorisedValues;
29 use Koha::Libraries;
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;
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 });
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');
59 my $branches = Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed;
60 my @branches_loop;
61 foreach my $branch ( @$branches ) {
62 my $selected = ( grep {$_ eq $branch->{branchcode}} @$selected_branches ) ? 1 : 0;
63 push @branches_loop, {
64 branchcode => $branch->{branchcode},
65 branchname => $branch->{branchname},
66 selected => $selected,
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);
78 if ( $av ) {
79 $template->param(
80 category => $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 ),
87 } else {
88 $template->param(
89 category => $category,
90 imagesets => C4::Koha::getImageSets(),
93 $template->param(
94 branches_loop => \@branches_loop,
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');
105 my $already_exists = Koha::AuthorisedValues->search(
107 category => $new_category,
108 authorised_value => $new_authorised_value,
110 )->next;
112 if ( $already_exists and ( not $id or $already_exists->id != $id ) ) {
113 push @messages, {type => 'error', code => 'already_exists' };
115 elsif ( $id ) { # Update
116 my $av = Koha::AuthorisedValues->new->find( $id );
118 $av->lib( $input->param('lib') || undef );
119 $av->lib_opac( $input->param('lib_opac') || undef );
120 $av->category( $new_category );
121 $av->authorised_value( $new_authorised_value );
122 $av->imageurl( $imageurl );
123 eval{
124 $av->store;
125 $av->replace_branch_limitations( \@branches );
127 if ( $@ ) {
128 push @messages, {type => 'error', code => 'error_on_update' };
129 } else {
130 push @messages, { type => 'message', code => 'success_on_update' };
133 else { # Insert
134 my $av = Koha::AuthorisedValue->new( {
135 category => $new_category,
136 authorised_value => $new_authorised_value,
137 lib => scalar $input->param('lib') || undef,
138 lib_opac => scalar $input->param('lib_opac') || undef,
139 imageurl => $imageurl,
140 } );
142 eval {
143 $av->store;
144 $av->replace_branch_limitations( \@branches );
147 if ( $@ ) {
148 push @messages, {type => 'error', code => 'error_on_insert' };
149 } else {
150 push @messages, { type => 'message', code => 'success_on_insert' };
154 $op = 'list';
155 $searchfield = $new_category;
156 } elsif ($op eq 'delete') {
157 my $av = Koha::AuthorisedValues->new->find( $input->param('id') );
158 my $deleted = eval {$av->delete};
159 if ( $@ or not $deleted ) {
160 push @messages, {type => 'error', code => 'error_on_delete' };
161 } else {
162 push @messages, { type => 'message', code => 'success_on_delete' };
165 $op = 'list';
166 $template->param( delete_success => 1 );
169 $template->param(
170 op => $op,
171 searchfield => $searchfield,
172 messages => \@messages,
175 if ( $op eq 'list' ) {
176 # build categories list
177 my @categories = Koha::AuthorisedValues->new->categories;
178 my @category_list;
179 my %categories; # a hash, to check that some hardcoded categories exist.
180 for my $category ( @categories ) {
181 push( @category_list, $category );
182 $categories{$category} = 1;
185 # push koha system categories
186 foreach (qw(Asort1 Asort2 Bsort1 Bsort2 SUGGEST DAMAGED LOST REPORT_GROUP REPORT_SUBGROUP DEPARTMENT TERM SUGGEST_STATUS ITEMTYPECAT)) {
187 push @category_list, $_ unless $categories{$_};
190 #reorder the list
191 @category_list = sort {$a cmp $b} @category_list;
193 $searchfield ||= $category_list[0];
195 my @avs_by_category = Koha::AuthorisedValues->new->search( { category => $searchfield } );
196 my @loop_data = ();
197 # builds value list
198 for my $av ( @avs_by_category ) {
199 my %row_data; # get a fresh hash for the row data
200 $row_data{category} = $av->category;
201 $row_data{authorised_value} = $av->authorised_value;
202 $row_data{lib} = $av->lib;
203 $row_data{lib_opac} = $av->lib_opac;
204 $row_data{imageurl} = getitemtypeimagelocation( 'intranet', $av->imageurl );
205 $row_data{branches} = $av->branch_limitations;
206 $row_data{id} = $av->id;
207 push(@loop_data, \%row_data);
210 $template->param(
211 loop => \@loop_data,
212 category => $searchfield,
213 categories => \@category_list,
217 output_html_with_http_headers $input, $cookie, $template->output;