Bug 9857: Did you mean? uses wrong punctuation in search links
[koha.git] / admin / authorised_values.pl
blob272ed0012b6a22d68000ca99c5c4f58c5032a846
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::AuthorisedValueCategories;
30 use Koha::Libraries;
32 my $input = new CGI;
33 my $id = $input->param('id');
34 my $op = $input->param('op') || 'list';
35 my $searchfield = $input->param('searchfield');
36 $searchfield = '' unless defined $searchfield;
37 $searchfield =~ s/\,//g;
38 my @messages;
40 our ($template, $borrowernumber, $cookie)= get_template_and_user({
41 template_name => "admin/authorised_values.tt",
42 authnotrequired => 0,
43 flagsrequired => {parameters => 'parameters_remaining_permissions'},
44 query => $input,
45 type => "intranet",
46 debug => 1,
47 });
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->branch_limitations;
56 } else {
57 $category = $input->param('category');
60 my $branches = Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed;
61 my @branches_loop;
62 foreach my $branch ( @$branches ) {
63 my $selected = ( grep {$_ eq $branch->{branchcode}} @$selected_branches ) ? 1 : 0;
64 push @branches_loop, {
65 branchcode => $branch->{branchcode},
66 branchname => $branch->{branchname},
67 selected => $selected,
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);
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 ),
88 } else {
89 $template->param(
90 category => $category,
91 imagesets => C4::Koha::getImageSets(),
94 $template->param(
95 branches_loop => \@branches_loop,
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');
106 my $already_exists = Koha::AuthorisedValues->search(
108 category => $new_category,
109 authorised_value => $new_authorised_value,
111 )->next;
113 if ( $already_exists and ( not $id or $already_exists->id != $id ) ) {
114 push @messages, {type => 'error', code => 'already_exists' };
116 elsif ( $new_category eq 'branches' or $new_category eq 'itemtypes' or $new_category eq 'cn_source' ) {
117 push @messages, {type => 'error', code => 'invalid_category_name' };
119 elsif ( $id ) { # Update
120 my $av = Koha::AuthorisedValues->new->find( $id );
122 $av->lib( scalar $input->param('lib') || undef );
123 $av->lib_opac( scalar $input->param('lib_opac') || undef );
124 $av->category( $new_category );
125 $av->authorised_value( $new_authorised_value );
126 $av->imageurl( $imageurl );
127 eval{
128 $av->store;
129 $av->replace_branch_limitations( \@branches );
131 if ( $@ ) {
132 push @messages, {type => 'error', code => 'error_on_update' };
133 } else {
134 push @messages, { type => 'message', code => 'success_on_update' };
137 else { # Insert
138 my $av = Koha::AuthorisedValue->new( {
139 category => $new_category,
140 authorised_value => $new_authorised_value,
141 lib => scalar $input->param('lib') || undef,
142 lib_opac => scalar $input->param('lib_opac') || undef,
143 imageurl => $imageurl,
144 } );
146 eval {
147 $av->store;
148 $av->replace_branch_limitations( \@branches );
151 if ( $@ ) {
152 push @messages, {type => 'error', code => 'error_on_insert' };
153 } else {
154 push @messages, { type => 'message', code => 'success_on_insert' };
158 $op = 'list';
159 $searchfield = $new_category;
160 } elsif ($op eq 'add_category' ) {
161 my $new_category = $input->param('category');
163 my $already_exists = Koha::AuthorisedValueCategories->find(
165 category_name => $new_category,
169 if ( $already_exists ) {
170 if ( $new_category eq 'branches' or $new_category eq 'itemtypes' or $new_category eq 'cn_source' ) {
171 push @messages, {type => 'error', code => 'invalid_category_name' };
172 } else {
173 push @messages, {type => 'error', code => 'cat_already_exists' };
176 else { # Insert
177 my $av = Koha::AuthorisedValueCategory->new( {
178 category_name => $new_category,
179 } );
181 eval {
182 $av->store;
185 if ( $@ ) {
186 push @messages, {type => 'error', code => 'error_on_insert_cat' };
187 } else {
188 push @messages, { type => 'message', code => 'success_on_insert_cat' };
189 $searchfield = $new_category;
193 $op = 'list';
194 } elsif ($op eq 'delete') {
195 my $av = Koha::AuthorisedValues->new->find( $id );
196 my $deleted = eval {$av->delete};
197 if ( $@ or not $deleted ) {
198 push @messages, {type => 'error', code => 'error_on_delete' };
199 } else {
200 push @messages, { type => 'message', code => 'success_on_delete' };
203 $op = 'list';
204 $template->param( delete_success => 1 );
207 $template->param(
208 op => $op,
209 searchfield => $searchfield,
210 messages => \@messages,
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 );
221 $searchfield ||= $category_list[0];
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->branch_limitations;
234 $row_data{id} = $av->id;
235 push(@loop_data, \%row_data);
238 $template->param(
239 loop => \@loop_data,
240 category => $searchfield,
241 categories => \@category_list,
245 output_html_with_http_headers $input, $cookie, $template->output;