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 my ($env,$category,$toponly,$branch,$searchstring,$type)=@_;
31 my $dbh = C4::Context->dbh;
32 $searchstring=~ s/\'/\\\'/g;
33 my $query="Select distinct stdlib,id,hierarchy,level from bibliothesaurus where (category like \"$category%\")";
34 $query .= " and hierarchy='$branch'" if ($branch && $toponly);
35 $query .= " and hierarchy like \"$branch%\"" if ($branch && !$toponly);
36 $query .= " and hierarchy=''" if (!$branch & $toponly);
37 $query .= " and stdlib like \"$searchstring%\"" if ($searchstring);
38 $query .= " order by category,stdlib";
39 my $sth=$dbh->prepare($query);
44 while (my $data=$sth->fetchrow_hashref){
45 if ($old_stdlib ne $data->{'stdlib'}) {
49 $old_stdlib = $data->{'stdlib'};
52 return ($cnt,\@results);
56 my $search_category=$input->param('search_category');
57 my $toponly = $input->param('toponly');
58 my $branch = $input->param('branch');
59 my $searchstring = $input->param('searchstring');
60 $searchstring=~ s/\,//g;
61 my $id = $input->param('id');
62 my $reqsel="select category,stdlib,freelib from authorised_values where id='$id'";
63 my $reqdel="delete from authorised_values where id='$id'";
64 my $offset=$input->param('offset');
65 my $script_name="/cgi-bin/koha/admin/thesaurus.pl";
66 my $dbh = C4::Context->dbh;
68 my $template = gettemplate("parameters/thesaurus.tmpl",0);
70 my $op = $input->param('op');
73 $template->param(script_name => $script_name,
74 $op => 1); # we show only the TMPL_VAR names $op
76 $template->param(script_name => $script_name,
77 else => 1); # we show only the TMPL_VAR names $op
79 ################## ADD_FORM ##################################
80 # called by default. Used to create form to add or modify a record
81 if ($op eq 'add_form') {
84 my $dbh = C4::Context->dbh;
85 my $sth=$dbh->prepare("select id,category,freelib,stdlib from bibliothesaurus where id='$id'");
87 $data=$sth->fetchrow_hashref;
90 $data->{'category'} = $input->param('category');
91 $data->{'stdlib'} = $input->param('stdlib');
93 if ($search_category) {
94 $template->param(action => "Modify authorised value");
96 $template->param(action => "Add authorised value");
98 $template->param(category => $data->{'category'},
99 stdlib => $data->{'stdlib'},
100 freelib => $data->{'freelib'},
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 bibliothesaurus (id,category,stdlib,freelib) values (?,?,?,?)");
113 $sth->execute($input->param('id'), $input->param('category'), $input->param('stdlib'), $input->param('freelib'));
115 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=thesaurus.pl?search_category=".$input->param('category')."\"></html>";
117 ################## DELETE_CONFIRM ##################################
118 # called by default form, used to confirm deletion of data in DB
119 } elsif ($op eq 'delete_confirm') {
120 my $dbh = C4::Context->dbh;
121 my $sth=$dbh->prepare($reqsel);
123 my $data=$sth->fetchrow_hashref;
125 $template->param(search_category => $search_category,
126 Tvalue => $data->{'stdlib'},
130 # END $OP eq DELETE_CONFIRM
131 ################## DELETE_CONFIRMED ##################################
132 # called by delete_confirm, used to effectively confirm deletion of data in DB
133 } elsif ($op eq 'delete_confirmed') {
134 my $dbh = C4::Context->dbh;
135 my $sth=$dbh->prepare($reqdel);
138 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=thesaurus.pl?search_category=$search_category\"></html>";
140 # END $OP eq DELETE_CONFIRMED
141 ################## DETAIL_FORM ##################################
142 } elsif ($op eq 'detail_form') {
143 # build categories list
144 my $sth = $dbh->prepare("select distinct category from bibliothesaurus");
147 while ( my ($category) = $sth->fetchrow_array) {
148 push(@category_list,$category);
150 my $tab_list = CGI::scrolling_list(-name=>'search_category',
151 -values=> \@category_list,
156 if (!$search_category) {
157 $search_category=$category_list[0];
160 my $sth = $dbh->prepare("select stdlib,category from bibliothesaurus where id=?");
162 my ($stdlib,$category) = $sth->fetchrow_array;
163 warn "ID : $id / $stdlib / $category";
165 $sth= $dbh->prepare("select id,freelib from bibliothesaurus where stdlib=?");
166 $sth->execute($stdlib);
170 while ( my ($id,$freelib) = $sth->fetchrow_array) {
171 if ($toggle eq 'white'){
176 my %row_data; # get a fresh hash for the row data
177 $row_data{freelib} = $freelib;
178 $row_data{edit} = "$script_name?op=add_form&id=$id";
179 $row_data{delete} = "$script_name?op=delete_confirm&search_category=$search_category&id=$id";
180 push(@loop_data, \%row_data);
183 $template->param(loop => \@loop_data,
184 tab_list => $tab_list,
185 category => $search_category,
187 searchstring => $searchstring,
189 category => $category);
190 ################## DEFAULT ##################################
192 # build categories list
193 my $sth = $dbh->prepare("select distinct category from bibliothesaurus");
196 while ( my ($category) = $sth->fetchrow_array) {
197 push(@category_list,$category);
199 my $tab_list = CGI::scrolling_list(-name=>'search_category',
200 -values=> \@category_list,
205 if (!$search_category) {
206 $search_category=$category_list[0];
209 my ($count,$results)=StringSearch($env,$search_category,$toponly,$branch,$searchstring,'web');
213 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
214 if ($toggle eq 'white'){
219 my %row_data; # get a fresh hash for the row data
220 $row_data{category} = $results->[$i]{'category'};
221 $row_data{stdlib} = (" " x $results->[$i]{'level'}).$results->[$i]{'stdlib'};
222 $row_data{stdlib} = $results->[$i]{'stdlib'};
223 $row_data{dig} ="<a href=thesaurus.pl?branch=$results->[$i]{'hierarchy'}".($results->[$i]{'hierarchy'}?"|":"")."$results->[$i]{'id'}&toponly=$toponly>";
224 $row_data{related} ="<a href=thesaurus.pl?id=$results->[$i]{'id'}&op=detail_form>";
225 $row_data{edit} = "$script_name?op=add_form&id=".$results->[$i]{'id'};
226 $row_data{delete} = "$script_name?op=delete_confirm&search_category=$search_category&id=".$results->[$i]{'id'};
227 push(@loop_data, \%row_data);
229 # rebuild coplete hierarchy
230 my $sth = $dbh->prepare("select stdlib from bibliothesaurus where id=?");
231 my @hierarchy = split(/\|/,$branch);
233 warn "total : $#hierarchy";
235 for (my $xi=0;$xi<=$#hierarchy;$xi++) {
237 $sth->execute($hierarchy[$xi]);
238 my ($t) = $sth->fetchrow_array;
242 push (@hierarchy_loop, \%link);
245 $template->param(loop => \@loop_data,
246 tab_list => $tab_list,
247 category => $search_category,
249 searchstring => $searchstring,
250 hierarchy_loop => \@hierarchy_loop);
252 my $prevpage = $offset-$pagesize;
253 $template->param(previous => "<a href=$script_name?branch=$branch&toponly=$toponly&category=$search_category&searchstring=$searchstring&offset=$prevpage><< Prev</a>");
255 if ($offset+$pagesize<$count) {
256 my $nextpage =$offset+$pagesize;
257 $template->param(next => "<a href=$script_name?branch=$branch&toponly=$toponly&category=$search_category&searchstring=$searchstring&offset=$nextpage>Next >></a>");
259 } #---- END $OP eq DEFAULT
261 print "Content-Type: text/html\n\n", $template->output;