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
21 # use warnings; #FIXME
29 sub AuthorizedValuesForCategory ($) {
30 my ($searchstring) = shift or return;
31 my $dbh = C4::Context->dbh;
32 $searchstring=~ s/\'/\\\'/g;
33 my @data=split(' ',$searchstring);
34 my $sth=$dbh->prepare('
35 SELECT id, category, authorised_value, lib, imageurl
36 FROM authorised_values
38 ORDER BY category, authorised_value
40 $sth->execute("$data[0]");
41 return $sth->fetchall_arrayref({});
45 my $id = $input->param('id');
46 my $offset = $input->param('offset');
47 my $searchfield = $input->param('searchfield');
48 $searchfield=~ s/\,//g;
49 my $script_name = "/cgi-bin/koha/admin/authorised_values.pl";
50 my $dbh = C4::Context->dbh;
52 my ($template, $borrowernumber, $cookie)= get_template_and_user({
53 template_name => "admin/authorised_values.tmpl",
55 flagsrequired => {parameters => 1},
61 my $op = $input->param('op');
63 $template->param( script_name => $script_name,
65 ################## ADD_FORM ##################################
66 # called by default. Used to create form to add or modify a record
67 if ($op eq 'add_form') {
70 my $sth=$dbh->prepare("select id, category, authorised_value, lib, imageurl from authorised_values where id=?");
72 $data=$sth->fetchrow_hashref;
74 $data->{'category'} = $input->param('category');
77 $template->param(action_modify => 1);
78 $template->param('heading-modify-authorized-value-p' => 1);
79 } elsif ( ! $data->{'category'} ) {
80 $template->param(action_add_category => 1);
81 $template->param('heading-add-new-category-p' => 1);
83 $template->param(action_add_value => 1);
84 $template->param('heading-add-authorized-value-p' => 1);
86 $template->param('use-heading-flags-p' => 1);
87 $template->param( category => $data->{'category'},
88 authorised_value => $data->{'authorised_value'},
89 lib => $data->{'lib'},
91 imagesets => C4::Koha::getImageSets( checked => $data->{'imageurl'} )
94 ################## ADD_VALIDATE ##################################
95 # called by add_form, used to insert/modify data in DB
96 } elsif ($op eq 'add_validate') {
97 my $new_authorised_value = $input->param('authorised_value');
98 my $new_category = $input->param('category');
99 my $imageurl = $input->param( 'imageurl' ) || '';
100 $imageurl = '' if $imageurl =~ /removeImage/;
101 my $duplicate_entry = 0;
103 if ( $id ) { # Update
104 my $sth = $dbh->prepare( "SELECT category, authorised_value FROM authorised_values WHERE id='$id' ");
106 my ($category, $authorised_value) = $sth->fetchrow_array();
107 if ( $authorised_value ne $new_authorised_value ) {
108 my $sth = $dbh->prepare_cached( "SELECT COUNT(*) FROM authorised_values " .
109 "WHERE category = '$new_category' AND authorised_value = '$new_authorised_value' and id<>$id");
111 ($duplicate_entry) = $sth->fetchrow_array();
112 warn "**** duplicate_entry = $duplicate_entry";
114 unless ( $duplicate_entry ) {
115 my $sth=$dbh->prepare( 'UPDATE authorised_values
117 authorised_value = ?,
121 my $lib = $input->param('lib');
122 undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
123 $sth->execute($new_category, $new_authorised_value, $lib, $imageurl, $id);
124 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$new_category."\"></html>";
129 my $sth = $dbh->prepare_cached( "SELECT COUNT(*) FROM authorised_values " .
130 "WHERE category = '$new_category' AND authorised_value = '$new_authorised_value' ");
132 ($duplicate_entry) = $sth->fetchrow_array();
133 unless ( $duplicate_entry ) {
134 my $sth=$dbh->prepare( 'INSERT INTO authorised_values
135 ( id, category, authorised_value, lib, imageurl )
136 values (?, ?, ?, ?, ?)' );
137 my $lib = $input->param('lib');
138 undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
139 $sth->execute($id, $new_category, $new_authorised_value, $lib, $imageurl );
140 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$input->param('category')."\"></html>";
144 if ( $duplicate_entry ) {
145 $template->param(duplicate_category => $new_category,
146 duplicate_value => $new_authorised_value,
151 ################## DELETE_CONFIRM ##################################
152 # called by default form, used to confirm deletion of data in DB
153 } elsif ($op eq 'delete_confirm') {
154 my $sth=$dbh->prepare("select category,authorised_value,lib from authorised_values where id=?");
156 my $data=$sth->fetchrow_hashref;
157 $id = $input->param('id') unless $id;
158 $template->param(searchfield => $searchfield,
159 Tlib => $data->{'lib'},
160 Tvalue => $data->{'authorised_value'},
164 # END $OP eq DELETE_CONFIRM
165 ################## DELETE_CONFIRMED ##################################
166 # called by delete_confirm, used to effectively confirm deletion of data in DB
167 } elsif ($op eq 'delete_confirmed') {
168 my $id = $input->param('id');
169 my $sth=$dbh->prepare("delete from authorised_values where id=?");
171 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=$searchfield\"></html>";
173 # END $OP eq DELETE_CONFIRMED
174 ################## DEFAULT ##################################
177 } #---- END $OP eq DEFAULT
178 output_html_with_http_headers $input, $cookie, $template->output;
183 # build categories list
184 my $sth = $dbh->prepare("select distinct category from authorised_values");
187 my %categories; # a hash, to check that some hardcoded categories exist.
188 while ( my ($category) = $sth->fetchrow_array) {
189 push(@category_list,$category);
190 $categories{$category} = 1;
192 # push koha system categories
193 foreach (qw(Asort1 Asort2 Bsort1 Bsort2 SUGGEST DAMAGED LOST)) {
194 push @category_list, $_ unless $categories{$_};
198 @category_list = sort {$a cmp $b} @category_list;
199 my $tab_list = CGI::scrolling_list(-name=>'searchfield',
201 -values=> \@category_list,
207 $searchfield=$category_list[0];
209 my ($results) = AuthorizedValuesForCategory($searchfield);
210 my $count = scalar(@$results);
213 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
214 my %row_data; # get a fresh hash for the row data
215 $row_data{category} = $results->[$i]{'category'};
216 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
217 $row_data{lib} = $results->[$i]{'lib'};
218 $row_data{imageurl} = getitemtypeimagelocation( 'intranet', $results->[$i]{'imageurl'} );
219 $row_data{edit} = "$script_name?op=add_form&id=".$results->[$i]{'id'};
220 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=$searchfield&id=".$results->[$i]{'id'};
221 push(@loop_data, \%row_data);
224 $template->param( loop => \@loop_data,
225 tab_list => $tab_list,
226 category => $searchfield );
229 my $prevpage = $offset-$pagesize;
230 $template->param(isprevpage => $offset,
231 prevpage=> $prevpage,
232 searchfield => $searchfield,
233 script_name => $script_name,
236 if ($offset+$pagesize<$count) {
237 my $nextpage =$offset+$pagesize;
238 $template->param(nextpage =>$nextpage,
239 searchfield => $searchfield,
240 script_name => $script_name,