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
29 sub AuthorizedValuesForCategory {
30 my ($searchstring,$type)=@_;
31 my $dbh = C4::Context->dbh;
32 $searchstring=~ s/\'/\\\'/g;
33 my @data=split(' ',$searchstring);
35 my $sth=$dbh->prepare("Select id,category,authorised_value,lib from authorised_values where (category = ?) order by category,authorised_value");
36 $sth->execute("$data[0]");
39 while (my $data=$sth->fetchrow_hashref){
44 return ($cnt,\@results);
48 my ($category, $authorised_value) = @_;
49 my $dbh = C4::Context->dbh;
50 my $sth = $dbh->prepare_cached("SELECT COUNT(*) FROM authorised_values
52 AND authorised_value = ?");
53 $sth->execute($category, $authorised_value);
54 my ($count) = $sth->fetchrow_array();
60 my $searchfield=$input->param('searchfield');
61 $searchfield=~ s/\,//g;
62 my $id = $input->param('id');
63 my $offset=$input->param('offset');
64 my $script_name="/cgi-bin/koha/admin/authorised_values.pl";
65 my $dbh = C4::Context->dbh;
67 my ($template, $borrowernumber, $cookie)
68 = get_template_and_user({template_name => "admin/authorised_values.tmpl",
72 flagsrequired => {parameters => 1},
76 my $op = $input->param('op');
79 $template->param(script_name => $script_name,
80 $op => 1); # we show only the TMPL_VAR names $op
82 $template->param(script_name => $script_name,
83 else => 1); # we show only the TMPL_VAR names $op
85 ################## ADD_FORM ##################################
86 # called by default. Used to create form to add or modify a record
87 if ($op eq 'add_form') {
90 my $dbh = C4::Context->dbh;
91 my $sth=$dbh->prepare("select id,category,authorised_value,lib from authorised_values where id=?");
93 $data=$sth->fetchrow_hashref;
96 $data->{'category'} = $input->param('category');
99 $template->param(action_modify => 1);
100 $template->param('heading-modify-authorized-value-p' => 1);
101 } elsif ( ! $data->{'category'} ) {
102 $template->param(action_add_category => 1);
103 $template->param('heading-add-new-category-p' => 1);
105 $template->param(action_add_value => 1);
106 $template->param('heading-add-authorized-value-p' => 1);
108 $template->param('use-heading-flags-p' => 1);
109 $template->param(category => $data->{'category'},
110 authorised_value => $data->{'authorised_value'},
111 lib => $data->{'lib'},
114 ################## ADD_VALIDATE ##################################
115 # called by add_form, used to insert/modify data in DB
116 } elsif ($op eq 'add_validate') {
117 my $dbh = C4::Context->dbh;
119 if (_already_exists($input->param('category'), $input->param('authorised_value'))) {
120 $template->param(duplicate_category => $input->param('category'),
121 duplicate_value => $input->param('authorised_value'),
125 my $sth=$dbh->prepare("replace authorised_values (id,category,authorised_value,lib) values (?,?,?,?)");
126 my $lib = $input->param('lib');
127 undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
129 $sth->execute($input->param('id'), $input->param('category'), $input->param('authorised_value'), $lib);
131 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$input->param('category')."\"></html>";
134 ################## DELETE_CONFIRM ##################################
135 # called by default form, used to confirm deletion of data in DB
136 } elsif ($op eq 'delete_confirm') {
137 my $dbh = C4::Context->dbh;
138 my $sth=$dbh->prepare("select category,authorised_value,lib from authorised_values where id=?");
140 my $data=$sth->fetchrow_hashref;
142 $id = $input->param('id') unless $id;
143 $template->param(searchfield => $searchfield,
144 Tlib => $data->{'lib'},
145 Tvalue => $data->{'authorised_value'},
149 # END $OP eq DELETE_CONFIRM
150 ################## DELETE_CONFIRMED ##################################
151 # called by delete_confirm, used to effectively confirm deletion of data in DB
152 } elsif ($op eq 'delete_confirmed') {
153 my $dbh = C4::Context->dbh;
154 my $id = $input->param('id');
155 my $sth=$dbh->prepare("delete from authorised_values where id=?");
158 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=$searchfield\"></html>";
161 # END $OP eq DELETE_CONFIRMED
162 ################## DEFAULT ##################################
165 } #---- END $OP eq DEFAULT
166 output_html_with_http_headers $input, $cookie, $template->output;
171 # build categories list
172 my $sth = $dbh->prepare("select distinct category from authorised_values");
176 # a hash, to check that some hardcoded categories exist.
178 while ( my ($category) = $sth->fetchrow_array) {
179 push(@category_list,$category);
180 $categories{$category} = 1;
182 # push koha system categories
183 push @category_list, 'Asort1' unless $categories{'Asort1'};
184 push @category_list, 'Asort2' unless $categories{'Asort2'};
185 push @category_list, 'Bsort1' unless $categories{'Bsort1'};
186 push @category_list, 'Bsort2' unless $categories{'Bsort2'};
187 push @category_list, 'SUGGEST' unless $categories{'SUGGEST'};
188 push @category_list, 'DAMAGED' unless $categories{'DAMAGED'};
189 push @category_list, 'LOST' unless $categories{'LOST'};
191 @category_list = sort {$a cmp $b} @category_list;
192 my $tab_list = CGI::scrolling_list(-name=>'searchfield',
194 -values=> \@category_list,
201 $searchfield=$category_list[0];
203 my ($count,$results)=AuthorizedValuesForCategory($searchfield,'web');
207 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
213 my %row_data; # get a fresh hash for the row data
214 $row_data{category} = $results->[$i]{'category'};
215 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
216 $row_data{lib} = $results->[$i]{'lib'};
217 $row_data{edit} = "$script_name?op=add_form&id=".$results->[$i]{'id'};
218 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=$searchfield&id=".$results->[$i]{'id'};
219 push(@loop_data, \%row_data);
222 $template->param(loop => \@loop_data,
223 tab_list => $tab_list,
224 category => $searchfield);
227 my $prevpage = $offset-$pagesize;
228 $template->param(isprevpage => $offset,
229 prevpage=> $prevpage,
230 searchfield => $searchfield,
231 script_name => $script_name,
234 if ($offset+$pagesize<$count) {
235 my $nextpage =$offset+$pagesize;
236 $template->param(nextpage =>$nextpage,
237 searchfield => $searchfield,
238 script_name => $script_name,