fix "Add Tag" button in MARC framework editor
[koha.git] / admin / authorised_values.pl
blob0a672a66fda7cfef30b9ccb91635a15422c6fbdf
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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
20 use strict;
21 use CGI;
22 use C4::Auth;
23 use C4::Context;
24 use C4::Koha;
25 use C4::Output;
28 sub AuthorizedValuesForCategory {
29 my ($searchstring,$type)=@_;
30 my $dbh = C4::Context->dbh;
31 $searchstring=~ s/\'/\\\'/g;
32 my @data=split(' ',$searchstring);
33 my $count=@data;
34 my $sth=$dbh->prepare( 'Select id, category, authorised_value, lib, imageurl
35 from authorised_values
36 where (category = ?)
37 order by category,authorised_value' );
38 $sth->execute("$data[0]");
39 my @results;
40 my $cnt=0;
41 while (my $data=$sth->fetchrow_hashref){
42 push(@results,$data);
43 $cnt ++;
45 $sth->finish;
46 return ($cnt,\@results);
49 my $input = new CGI;
50 my $searchfield=$input->param('searchfield');
51 $searchfield=~ s/\,//g;
52 my $id = $input->param('id');
53 my $offset=$input->param('offset');
54 my $script_name="/cgi-bin/koha/admin/authorised_values.pl";
55 my $dbh = C4::Context->dbh;
57 my ($template, $borrowernumber, $cookie)
58 = get_template_and_user({template_name => "admin/authorised_values.tmpl",
59 query => $input,
60 type => "intranet",
61 authnotrequired => 0,
62 flagsrequired => {parameters => 1},
63 debug => 1,
64 });
65 my $pagesize=20;
66 my $op = $input->param('op');
68 if ($op) {
69 $template->param(script_name => $script_name,
70 $op => 1); # we show only the TMPL_VAR names $op
71 } else {
72 $template->param(script_name => $script_name,
73 else => 1); # we show only the TMPL_VAR names $op
75 ################## ADD_FORM ##################################
76 # called by default. Used to create form to add or modify a record
77 if ($op eq 'add_form') {
78 my $data;
79 if ($id) {
80 my $dbh = C4::Context->dbh;
81 my $sth=$dbh->prepare("select id, category, authorised_value, lib, imageurl from authorised_values where id=?");
82 $sth->execute($id);
83 $data=$sth->fetchrow_hashref;
84 $sth->finish;
85 } else {
86 $data->{'category'} = $input->param('category');
88 if ($id) {
89 $template->param(action_modify => 1);
90 $template->param('heading-modify-authorized-value-p' => 1);
91 } elsif ( ! $data->{'category'} ) {
92 $template->param(action_add_category => 1);
93 $template->param('heading-add-new-category-p' => 1);
94 } else {
95 $template->param(action_add_value => 1);
96 $template->param('heading-add-authorized-value-p' => 1);
98 $template->param('use-heading-flags-p' => 1);
99 $template->param( category => $data->{'category'},
100 authorised_value => $data->{'authorised_value'},
101 lib => $data->{'lib'},
102 id => $data->{'id'},
103 imagesets => C4::Koha::getImageSets( checked => $data->{'imageurl'} )
106 ################## ADD_VALIDATE ##################################
107 # called by add_form, used to insert/modify data in DB
108 } elsif ($op eq 'add_validate') {
109 my $dbh = C4::Context->dbh;
110 my $new_category = $input->param('category');
111 my $new_authorised_value = $input->param('authorised_value');
112 my $imageurl=$input->param( 'imageurl' )|'';
113 my $duplicate_entry = 0;
115 if ( $id ) { # Update
116 my $sth = $dbh->prepare( "SELECT category, authorised_value FROM authorised_values WHERE id='$id' ");
117 $sth->execute();
118 my ($category, $authorised_value) = $sth->fetchrow_array();
119 $sth->finish;
120 if ( $authorised_value ne $new_authorised_value ) {
121 my $sth = $dbh->prepare_cached( "SELECT COUNT(*) FROM authorised_values " .
122 "WHERE category = '$new_category' AND authorised_value = '$new_authorised_value' and id<>$id");
123 $sth->execute();
124 ($duplicate_entry) = $sth->fetchrow_array();
125 warn "**** duplicate_entry = $duplicate_entry";
127 unless ( $duplicate_entry ) {
128 my $sth=$dbh->prepare( 'UPDATE authorised_values
129 SET category = ?,
130 authorised_value = ?,
131 lib = ?,
132 imageurl = ?
133 WHERE id=?' );
134 my $lib = $input->param('lib');
135 undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
136 $sth->execute($new_category, $new_authorised_value, $lib, $imageurl, $id);
137 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$new_category."\"></html>";
138 exit;
141 else { # Insert
142 my $sth = $dbh->prepare_cached( "SELECT COUNT(*) FROM authorised_values " .
143 "WHERE category = '$new_category' AND authorised_value = '$new_authorised_value' ");
144 $sth->execute();
145 ($duplicate_entry) = $sth->fetchrow_array();
146 $sth->finish();
147 unless ( $duplicate_entry ) {
148 my $sth=$dbh->prepare( 'INSERT INTO authorised_values
149 ( id, category, authorised_value, lib, imageurl )
150 values (?, ?, ?, ?, ?)' );
151 my $lib = $input->param('lib');
152 undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
153 $sth->execute($id, $new_category, $new_authorised_value, $lib, $imageurl );
154 $sth->finish;
155 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$input->param('category')."\"></html>";
156 exit;
159 if ( $duplicate_entry ) {
160 $template->param(duplicate_category => $new_category,
161 duplicate_value => $new_authorised_value,
162 else => 1);
163 default_form();
166 ################## DELETE_CONFIRM ##################################
167 # called by default form, used to confirm deletion of data in DB
168 } elsif ($op eq 'delete_confirm') {
169 my $dbh = C4::Context->dbh;
170 my $sth=$dbh->prepare("select category,authorised_value,lib from authorised_values where id=?");
171 $sth->execute($id);
172 my $data=$sth->fetchrow_hashref;
173 $sth->finish;
174 $id = $input->param('id') unless $id;
175 $template->param(searchfield => $searchfield,
176 Tlib => $data->{'lib'},
177 Tvalue => $data->{'authorised_value'},
178 id =>$id,
181 # END $OP eq DELETE_CONFIRM
182 ################## DELETE_CONFIRMED ##################################
183 # called by delete_confirm, used to effectively confirm deletion of data in DB
184 } elsif ($op eq 'delete_confirmed') {
185 my $dbh = C4::Context->dbh;
186 my $id = $input->param('id');
187 my $sth=$dbh->prepare("delete from authorised_values where id=?");
188 $sth->execute($id);
189 $sth->finish;
190 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=$searchfield\"></html>";
191 exit;
193 # END $OP eq DELETE_CONFIRMED
194 ################## DEFAULT ##################################
195 } else { # DEFAULT
196 default_form();
197 } #---- END $OP eq DEFAULT
198 output_html_with_http_headers $input, $cookie, $template->output;
200 exit 0;
202 sub default_form {
203 # build categories list
204 my $sth = $dbh->prepare("select distinct category from authorised_values");
205 $sth->execute;
206 # the list
207 my @category_list;
208 # a hash, to check that some hardcoded categories exist.
209 my %categories;
210 while ( my ($category) = $sth->fetchrow_array) {
211 push(@category_list,$category);
212 $categories{$category} = 1;
214 # push koha system categories
215 push @category_list, 'Asort1' unless $categories{'Asort1'};
216 push @category_list, 'Asort2' unless $categories{'Asort2'};
217 push @category_list, 'Bsort1' unless $categories{'Bsort1'};
218 push @category_list, 'Bsort2' unless $categories{'Bsort2'};
219 push @category_list, 'SUGGEST' unless $categories{'SUGGEST'};
220 push @category_list, 'DAMAGED' unless $categories{'DAMAGED'};
221 push @category_list, 'LOST' unless $categories{'LOST'};
222 #reorder the list
223 @category_list = sort {$a cmp $b} @category_list;
224 my $tab_list = CGI::scrolling_list(-name=>'searchfield',
225 -id=>'searchfield',
226 -values=> \@category_list,
227 -default=>"",
228 -size=>1,
229 -tabindex=>'',
230 -multiple=>0,
232 if (!$searchfield) {
233 $searchfield=$category_list[0];
235 my ($count,$results)=AuthorizedValuesForCategory($searchfield,'web');
236 my $toggle=1;
237 my @loop_data = ();
238 # builds value list
239 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
240 if ($toggle eq 1){
241 $toggle=1;
242 } else {
243 $toggle=0;
245 my %row_data; # get a fresh hash for the row data
246 $row_data{category} = $results->[$i]{'category'};
247 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
248 $row_data{lib} = $results->[$i]{'lib'};
249 $row_data{imageurl} = getitemtypeimagesrc('intranet') . '/' . $results->[$i]{'imageurl'};
250 $row_data{edit} = "$script_name?op=add_form&amp;id=".$results->[$i]{'id'};
251 $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=$searchfield&amp;id=".$results->[$i]{'id'};
252 push(@loop_data, \%row_data);
255 $template->param( loop => \@loop_data,
256 tab_list => $tab_list,
257 category => $searchfield );
259 if ($offset>0) {
260 my $prevpage = $offset-$pagesize;
261 $template->param(isprevpage => $offset,
262 prevpage=> $prevpage,
263 searchfield => $searchfield,
264 script_name => $script_name,
267 if ($offset+$pagesize<$count) {
268 my $nextpage =$offset+$pagesize;
269 $template->param(nextpage =>$nextpage,
270 searchfield => $searchfield,
271 script_name => $script_name,