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
25 use C4::Interface::CGI::Output;
32 my ($env,$searchstring,$type)=@_;
33 my $dbh = C4::Context->dbh;
34 $searchstring=~ s/\'/\\\'/g;
35 my @data=split(' ',$searchstring);
37 my $query="Select id,category,authorised_value,lib from authorised_values where (category like \"$data[0]%\") order by category,authorised_value";
38 my $sth=$dbh->prepare($query);
42 while (my $data=$sth->fetchrow_hashref){
47 return ($cnt,\@results);
51 my $searchfield=$input->param('searchfield');
52 $searchfield=~ s/\,//g;
53 my $id = $input->param('id');
54 my $reqsel="select category,authorised_value,lib from authorised_values where id='$id'";
55 my $reqdel="delete from authorised_values where id='$id'";
56 my $offset=$input->param('offset');
57 my $script_name="/cgi-bin/koha/admin/authorised_values.pl";
58 my $dbh = C4::Context->dbh;
60 my ($template, $borrowernumber, $cookie)
61 = get_template_and_user({template_name => "parameters/authorised_values.tmpl",
65 flagsrequired => {parameters => 1},
69 my $op = $input->param('op');
72 $template->param(script_name => $script_name,
73 $op => 1); # we show only the TMPL_VAR names $op
75 $template->param(script_name => $script_name,
76 else => 1); # we show only the TMPL_VAR names $op
78 ################## ADD_FORM ##################################
79 # called by default. Used to create form to add or modify a record
80 if ($op eq 'add_form') {
83 my $dbh = C4::Context->dbh;
84 my $sth=$dbh->prepare("select id,category,authorised_value,lib from authorised_values where id='$id'");
86 $data=$sth->fetchrow_hashref;
89 $data->{'category'} = $input->param('category');
92 $template->param(action => "Modify authorised value");
93 } elsif ( ! $data->{'category'} ) {
94 $template->param(action => "Add new category");
96 $template->param(action => "Add authorised value");
98 $template->param(category => $data->{'category'},
99 authorised_value => $data->{'authorised_value'},
100 lib => $data->{'lib'},
103 if ($data->{'category'}) {
104 $template->param(category => "<input type=\"hidden\" name=\"category\" value='$data->{'category'}'>$data->{'category'}");
106 $template->param(category => "<input type=text name=\"category\" size=8 maxlength=8>");
108 ################## ADD_VALIDATE ##################################
109 # called by add_form, used to insert/modify data in DB
110 } elsif ($op eq 'add_validate') {
111 my $dbh = C4::Context->dbh;
112 my $sth=$dbh->prepare("replace authorised_values (id,category,authorised_value,lib) values (?,?,?,?)");
113 my $lib = $input->param('lib');
114 undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
116 $sth->execute($input->param('id'), $input->param('category'), $input->param('authorised_value'), $lib);
118 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$input->param('category')."\"></html>";
120 ################## DELETE_CONFIRM ##################################
121 # called by default form, used to confirm deletion of data in DB
122 } elsif ($op eq 'delete_confirm') {
123 my $dbh = C4::Context->dbh;
124 my $sth=$dbh->prepare($reqsel);
126 my $data=$sth->fetchrow_hashref;
128 $template->param(searchfield => $searchfield,
129 Tvalue => $data->{'authorised_value'},
133 # END $OP eq DELETE_CONFIRM
134 ################## DELETE_CONFIRMED ##################################
135 # called by delete_confirm, used to effectively confirm deletion of data in DB
136 } elsif ($op eq 'delete_confirmed') {
137 my $dbh = C4::Context->dbh;
138 my $sth=$dbh->prepare($reqdel);
141 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=$searchfield\"></html>";
144 # END $OP eq DELETE_CONFIRMED
145 ################## DEFAULT ##################################
147 # build categories list
148 my $sth = $dbh->prepare("select distinct category from authorised_values");
151 while ( my ($category) = $sth->fetchrow_array) {
152 push(@category_list,$category);
154 # push koha system categories
155 my $tab_list = CGI::scrolling_list(-name=>'searchfield',
156 -values=> \@category_list,
162 $searchfield=$category_list[0];
165 my ($count,$results)=StringSearch($env,$searchfield,'web');
169 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
170 if ($toggle eq 'white'){
175 my %row_data; # get a fresh hash for the row data
176 $row_data{category} = $results->[$i]{'category'};
177 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
178 $row_data{lib} = $results->[$i]{'lib'};
179 $row_data{edit} = "$script_name?op=add_form&id=".$results->[$i]{'id'};
180 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=$searchfield&id=".$results->[$i]{'id'};
181 push(@loop_data, \%row_data);
184 $template->param(loop => \@loop_data,
185 tab_list => $tab_list,
186 category => $searchfield);
189 my $prevpage = $offset-$pagesize;
190 $template->param(previous => "<a href=\"$script_name?offset=".$prevpage.'">');
192 if ($offset+$pagesize<$count) {
193 my $nextpage =$offset+$pagesize;
194 $template->param(next => "<a href=\"$script_name?offset=".$nextpage.'">');
198 } #---- END $OP eq DEFAULT
200 output_html_with_http_headers $input, $cookie, $template->output;