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
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
28 sub AuthorizedValuesForCategory {
29 my ($searchstring,$type)=@_;
30 my $dbh = C4::Context->dbh;
31 $searchstring=~ s/\'/\\\'/g;
32 my @data=split(' ',$searchstring);
34 my $sth=$dbh->prepare( 'Select id, category, authorised_value, lib, imageurl
35 from authorised_values
37 order by category,authorised_value' );
38 $sth->execute("$data[0]");
41 while (my $data=$sth->fetchrow_hashref){
46 return ($cnt,\@results);
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",
62 flagsrequired => {parameters => 1},
66 my $op = $input->param('op');
69 $template->param(script_name => $script_name,
70 $op => 1); # we show only the TMPL_VAR names $op
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') {
80 my $dbh = C4::Context->dbh;
81 my $sth=$dbh->prepare("select id, category, authorised_value, lib, imageurl from authorised_values where id=?");
83 $data=$sth->fetchrow_hashref;
86 $data->{'category'} = $input->param('category');
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);
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'},
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 $imageurl = '' if $imageurl =~ /removeImage/;
114 my $duplicate_entry = 0;
116 if ( $id ) { # Update
117 my $sth = $dbh->prepare( "SELECT category, authorised_value FROM authorised_values WHERE id='$id' ");
119 my ($category, $authorised_value) = $sth->fetchrow_array();
121 if ( $authorised_value ne $new_authorised_value ) {
122 my $sth = $dbh->prepare_cached( "SELECT COUNT(*) FROM authorised_values " .
123 "WHERE category = '$new_category' AND authorised_value = '$new_authorised_value' and id<>$id");
125 ($duplicate_entry) = $sth->fetchrow_array();
126 warn "**** duplicate_entry = $duplicate_entry";
128 unless ( $duplicate_entry ) {
129 my $sth=$dbh->prepare( 'UPDATE authorised_values
131 authorised_value = ?,
135 my $lib = $input->param('lib');
136 undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
137 $sth->execute($new_category, $new_authorised_value, $lib, $imageurl, $id);
138 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$new_category."\"></html>";
143 my $sth = $dbh->prepare_cached( "SELECT COUNT(*) FROM authorised_values " .
144 "WHERE category = '$new_category' AND authorised_value = '$new_authorised_value' ");
146 ($duplicate_entry) = $sth->fetchrow_array();
148 unless ( $duplicate_entry ) {
149 my $sth=$dbh->prepare( 'INSERT INTO authorised_values
150 ( id, category, authorised_value, lib, imageurl )
151 values (?, ?, ?, ?, ?)' );
152 my $lib = $input->param('lib');
153 undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
154 $sth->execute($id, $new_category, $new_authorised_value, $lib, $imageurl );
156 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$input->param('category')."\"></html>";
160 if ( $duplicate_entry ) {
161 $template->param(duplicate_category => $new_category,
162 duplicate_value => $new_authorised_value,
167 ################## DELETE_CONFIRM ##################################
168 # called by default form, used to confirm deletion of data in DB
169 } elsif ($op eq 'delete_confirm') {
170 my $dbh = C4::Context->dbh;
171 my $sth=$dbh->prepare("select category,authorised_value,lib from authorised_values where id=?");
173 my $data=$sth->fetchrow_hashref;
175 $id = $input->param('id') unless $id;
176 $template->param(searchfield => $searchfield,
177 Tlib => $data->{'lib'},
178 Tvalue => $data->{'authorised_value'},
182 # END $OP eq DELETE_CONFIRM
183 ################## DELETE_CONFIRMED ##################################
184 # called by delete_confirm, used to effectively confirm deletion of data in DB
185 } elsif ($op eq 'delete_confirmed') {
186 my $dbh = C4::Context->dbh;
187 my $id = $input->param('id');
188 my $sth=$dbh->prepare("delete from authorised_values where id=?");
191 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=$searchfield\"></html>";
194 # END $OP eq DELETE_CONFIRMED
195 ################## DEFAULT ##################################
198 } #---- END $OP eq DEFAULT
199 output_html_with_http_headers $input, $cookie, $template->output;
204 # build categories list
205 my $sth = $dbh->prepare("select distinct category from authorised_values");
209 # a hash, to check that some hardcoded categories exist.
211 while ( my ($category) = $sth->fetchrow_array) {
212 push(@category_list,$category);
213 $categories{$category} = 1;
215 # push koha system categories
216 push @category_list, 'Asort1' unless $categories{'Asort1'};
217 push @category_list, 'Asort2' unless $categories{'Asort2'};
218 push @category_list, 'Bsort1' unless $categories{'Bsort1'};
219 push @category_list, 'Bsort2' unless $categories{'Bsort2'};
220 push @category_list, 'SUGGEST' unless $categories{'SUGGEST'};
221 push @category_list, 'DAMAGED' unless $categories{'DAMAGED'};
222 push @category_list, 'LOST' unless $categories{'LOST'};
224 @category_list = sort {$a cmp $b} @category_list;
225 my $tab_list = CGI::scrolling_list(-name=>'searchfield',
227 -values=> \@category_list,
234 $searchfield=$category_list[0];
236 my ($count,$results)=AuthorizedValuesForCategory($searchfield,'web');
240 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
246 my %row_data; # get a fresh hash for the row data
247 $row_data{category} = $results->[$i]{'category'};
248 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
249 $row_data{lib} = $results->[$i]{'lib'};
250 $row_data{imageurl} = getitemtypeimagelocation( 'intranet', $results->[$i]{'imageurl'} );
251 $row_data{edit} = "$script_name?op=add_form&id=".$results->[$i]{'id'};
252 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=$searchfield&id=".$results->[$i]{'id'};
253 push(@loop_data, \%row_data);
256 $template->param( loop => \@loop_data,
257 tab_list => $tab_list,
258 category => $searchfield );
261 my $prevpage = $offset-$pagesize;
262 $template->param(isprevpage => $offset,
263 prevpage=> $prevpage,
264 searchfield => $searchfield,
265 script_name => $script_name,
268 if ($offset+$pagesize<$count) {
269 my $nextpage =$offset+$pagesize;
270 $template->param(nextpage =>$nextpage,
271 searchfield => $searchfield,
272 script_name => $script_name,