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
30 sub AuthorizedValuesForCategory
($) {
31 my ($searchstring) = shift or return;
32 my $dbh = C4
::Context
->dbh;
33 $searchstring=~ s/\'/\\\'/g;
34 my @data=split(' ',$searchstring);
35 my $sth=$dbh->prepare('
36 SELECT id, category, authorised_value, lib, imageurl
37 FROM authorised_values
39 ORDER BY category, authorised_value
41 $sth->execute("$data[0]");
42 return $sth->fetchall_arrayref({});
46 my $id = $input->param('id');
47 my $offset = $input->param('offset') || 0;
48 my $searchfield = $input->param('searchfield');
49 $searchfield = '' unless defined $searchfield;
50 $searchfield=~ s/\,//g;
51 my $script_name = "/cgi-bin/koha/admin/authorised_values.pl";
52 my $dbh = C4
::Context
->dbh;
54 my ($template, $borrowernumber, $cookie)= get_template_and_user
({
55 template_name
=> "admin/authorised_values.tmpl",
57 flagsrequired
=> {parameters
=> 1},
63 my $op = $input->param('op') || '';
65 $template->param( script_name
=> $script_name,
67 ################## ADD_FORM ##################################
68 # called by default. Used to create form to add or modify a record
69 if ($op eq 'add_form') {
72 my $sth=$dbh->prepare("select id, category, authorised_value, lib, imageurl from authorised_values where id=?");
74 $data=$sth->fetchrow_hashref;
76 $data->{'category'} = $input->param('category');
79 $template->param(action_modify
=> 1);
80 $template->param('heading-modify-authorized-value-p' => 1);
81 } elsif ( ! $data->{'category'} ) {
82 $template->param(action_add_category
=> 1);
83 $template->param('heading-add-new-category-p' => 1);
85 $template->param(action_add_value
=> 1);
86 $template->param('heading-add-authorized-value-p' => 1);
88 $template->param('use-heading-flags-p' => 1);
89 $template->param( category
=> $data->{'category'},
90 authorised_value
=> $data->{'authorised_value'},
91 lib
=> $data->{'lib'},
93 imagesets
=> C4
::Koha
::getImageSets
( checked
=> $data->{'imageurl'} )
96 ################## ADD_VALIDATE ##################################
97 # called by add_form, used to insert/modify data in DB
98 } elsif ($op eq 'add_validate') {
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;
105 if ( $id ) { # Update
106 my $sth = $dbh->prepare( "SELECT category, authorised_value FROM authorised_values WHERE id='$id' ");
108 my ($category, $authorised_value) = $sth->fetchrow_array();
109 if ( $authorised_value ne $new_authorised_value ) {
110 my $sth = $dbh->prepare_cached( "SELECT COUNT(*) FROM authorised_values " .
111 "WHERE category = '$new_category' AND authorised_value = '$new_authorised_value' and id<>$id");
113 ($duplicate_entry) = $sth->fetchrow_array();
114 warn "**** duplicate_entry = $duplicate_entry";
116 unless ( $duplicate_entry ) {
117 my $sth=$dbh->prepare( 'UPDATE authorised_values
119 authorised_value = ?,
123 my $lib = $input->param('lib');
124 undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
125 $sth->execute($new_category, $new_authorised_value, $lib, $imageurl, $id);
126 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$new_category."\"></html>";
131 my $sth = $dbh->prepare_cached( "SELECT COUNT(*) FROM authorised_values " .
132 "WHERE category = '$new_category' AND authorised_value = '$new_authorised_value' ");
134 ($duplicate_entry) = $sth->fetchrow_array();
135 unless ( $duplicate_entry ) {
136 my $sth=$dbh->prepare( 'INSERT INTO authorised_values
137 ( id, category, authorised_value, lib, imageurl )
138 values (?, ?, ?, ?, ?)' );
139 my $lib = $input->param('lib');
140 undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
141 $sth->execute($id, $new_category, $new_authorised_value, $lib, $imageurl );
142 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$input->param('category')."\"></html>";
146 if ( $duplicate_entry ) {
147 $template->param(duplicate_category
=> $new_category,
148 duplicate_value
=> $new_authorised_value,
153 ################## DELETE_CONFIRM ##################################
154 # called by default form, used to confirm deletion of data in DB
155 } elsif ($op eq 'delete_confirm') {
156 my $sth=$dbh->prepare("select category,authorised_value,lib from authorised_values where id=?");
158 my $data=$sth->fetchrow_hashref;
159 $id = $input->param('id') unless $id;
160 $template->param(searchfield
=> $searchfield,
161 Tlib
=> $data->{'lib'},
162 Tvalue
=> $data->{'authorised_value'},
166 # END $OP eq DELETE_CONFIRM
167 ################## DELETE_CONFIRMED ##################################
168 # called by delete_confirm, used to effectively confirm deletion of data in DB
169 } elsif ($op eq 'delete_confirmed') {
170 my $id = $input->param('id');
171 my $sth=$dbh->prepare("delete from authorised_values where id=?");
173 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=$searchfield\"></html>";
175 # END $OP eq DELETE_CONFIRMED
176 ################## DEFAULT ##################################
179 } #---- END $OP eq DEFAULT
180 output_html_with_http_headers
$input, $cookie, $template->output;
185 # build categories list
186 my $sth = $dbh->prepare("select distinct category from authorised_values");
189 my %categories; # a hash, to check that some hardcoded categories exist.
190 while ( my ($category) = $sth->fetchrow_array) {
191 push(@category_list,$category);
192 $categories{$category} = 1;
194 # push koha system categories
195 foreach (qw(Asort1 Asort2 Bsort1 Bsort2 SUGGEST DAMAGED LOST)) {
196 push @category_list, $_ unless $categories{$_};
200 @category_list = sort {$a cmp $b} @category_list;
201 my $tab_list = CGI
::scrolling_list
(-name
=>'searchfield',
203 -values=> \
@category_list,
209 $searchfield=$category_list[0];
211 my ($results) = AuthorizedValuesForCategory
($searchfield);
212 my $count = scalar(@
$results);
215 for (my $i=$offset; $i < ($offset+$pagesize<$count?
$offset+$pagesize:$count); $i++){
216 my %row_data; # get a fresh hash for the row data
217 $row_data{category
} = $results->[$i]{'category'};
218 $row_data{authorised_value
} = $results->[$i]{'authorised_value'};
219 $row_data{lib
} = $results->[$i]{'lib'};
220 $row_data{imageurl
} = getitemtypeimagelocation
( 'intranet', $results->[$i]{'imageurl'} );
221 $row_data{edit
} = "$script_name?op=add_form&id=".$results->[$i]{'id'};
222 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=$searchfield&id=".$results->[$i]{'id'};
223 push(@loop_data, \
%row_data);
226 $template->param( loop => \
@loop_data,
227 tab_list
=> $tab_list,
228 category
=> $searchfield );
231 my $prevpage = $offset-$pagesize;
232 $template->param(isprevpage
=> $offset,
233 prevpage
=> $prevpage,
234 searchfield
=> $searchfield,
235 script_name
=> $script_name,
238 if ($offset+$pagesize<$count) {
239 my $nextpage =$offset+$pagesize;
240 $template->param(nextpage
=>$nextpage,
241 searchfield
=> $searchfield,
242 script_name
=> $script_name,