fix problem that only one branches was on the screenbr
[koha.git] / admin / thesaurus.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
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
10 # version.
11 #
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.
15 #
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
19
20 use strict;
21 use CGI;
22 use C4::Context;
23 use C4::Output;
24 use C4::Search;
25 use HTML::Template;
26 use C4::Context;
27
28
29 sub StringSearch  {
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);
40         $sth->execute;
41         my @results;
42         my $cnt=0;
43         my $old_stdlib="";
44         while (my $data=$sth->fetchrow_hashref){
45         if ($old_stdlib ne $data->{'stdlib'}) {
46                 $cnt ++;
47                 push(@results,$data);
48         }
49         $old_stdlib = $data->{'stdlib'};
50         }
51         $sth->finish;
52         return ($cnt,\@results);
53 }
54
55 my $input = new CGI;
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;
67
68 my $template = gettemplate("parameters/thesaurus.tmpl",0);
69 my $pagesize=20;
70 my $op = $input->param('op');
71
72 if ($op) {
73 $template->param(script_name => $script_name,
74                                                 $op              => 1); # we show only the TMPL_VAR names $op
75 } else {
76 $template->param(script_name => $script_name,
77                                                 else              => 1); # we show only the TMPL_VAR names $op
78 }
79 ################## ADD_FORM ##################################
80 # called by default. Used to create form to add or  modify a record
81 if ($op eq 'add_form') {
82         my $data;
83         if ($id) {
84                 my $dbh = C4::Context->dbh;
85                 my $sth=$dbh->prepare("select id,category,freelib,stdlib from bibliothesaurus where id='$id'");
86                 $sth->execute;
87                 $data=$sth->fetchrow_hashref;
88                 $sth->finish;
89         } else {
90                 $data->{'category'} = $input->param('category');
91                 $data->{'stdlib'} = $input->param('stdlib');
92         }
93         if ($search_category) {
94                 $template->param(action => "Modify authorised value");
95         } else {
96                 $template->param(action => "Add authorised value");
97         }
98         $template->param(category => $data->{'category'},
99                                                         stdlib => $data->{'stdlib'},
100                                                         freelib => $data->{'freelib'},
101                                                         id => $data->{'id'}
102                                                         );
103         if ($data->{'category'}) {
104                 $template->param(category => "<input type=\"hidden\" name=\"category\" value='$data->{'category'}'>$data->{'category'}");
105         } else {
106                 $template->param(category => "<input type=text name=\"category\" size=8 maxlength=8>");
107         }
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'));
114         $sth->finish;
115         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=thesaurus.pl?search_category=".$input->param('category')."\"></html>";
116         exit;
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);
122         $sth->execute;
123         my $data=$sth->fetchrow_hashref;
124         $sth->finish;
125         $template->param(search_category => $search_category,
126                                                         Tvalue => $data->{'stdlib'},
127                                                         id =>$id,
128                                                         );
129
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);
136         $sth->execute;
137         $sth->finish;
138         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=thesaurus.pl?search_category=$search_category\"></html>";
139         exit;
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");
145         $sth->execute;
146         my @category_list;
147         while ( my ($category) = $sth->fetchrow_array) {
148                 push(@category_list,$category);
149         }
150         my $tab_list = CGI::scrolling_list(-name=>'search_category',
151                         -values=> \@category_list,
152                         -default=>"",
153                         -size=>1,
154                         -multiple=>0,
155                         );
156         if (!$search_category) {
157                 $search_category=$category_list[0];
158         }
159         my $env;
160         my $sth = $dbh->prepare("select stdlib,category from bibliothesaurus where id=?");
161         $sth->execute($id);
162         my ($stdlib,$category) = $sth->fetchrow_array;
163         warn "ID : $id / $stdlib / $category";
164         $sth->finish;
165         $sth= $dbh->prepare("select id,freelib from bibliothesaurus where stdlib=?");
166         $sth->execute($stdlib);
167         my $toggle="white";
168         # builds value list
169         my @loop_data;
170         while ( my ($id,$freelib) = $sth->fetchrow_array) {
171                 if ($toggle eq 'white'){
172                         $toggle="#ffffcc";
173                 } else {
174                         $toggle="white";
175                 }
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);
181         }
182
183         $template->param(loop => \@loop_data,
184                                                         tab_list => $tab_list,
185                                                         category => $search_category,
186                                                         toponly => $toponly,
187                                                         searchstring => $searchstring,
188                                                         stdlib => $stdlib,
189                                                         category => $category);
190 ################## DEFAULT ##################################
191 } else { # DEFAULT
192         # build categories list
193         my $sth = $dbh->prepare("select distinct category from bibliothesaurus");
194         $sth->execute;
195         my @category_list;
196         while ( my ($category) = $sth->fetchrow_array) {
197                 push(@category_list,$category);
198         }
199         my $tab_list = CGI::scrolling_list(-name=>'search_category',
200                         -values=> \@category_list,
201                         -default=>"",
202                         -size=>1,
203                         -multiple=>0,
204                         );
205         if (!$search_category) {
206                 $search_category=$category_list[0];
207         }
208         my $env;
209         my ($count,$results)=StringSearch($env,$search_category,$toponly,$branch,$searchstring,'web');
210         my $toggle="white";
211         my @loop_data = ();
212         # builds value list
213         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
214                 if ($toggle eq 'white'){
215                         $toggle="#ffffcc";
216                 } else {
217                         $toggle="white";
218                 }
219                 my %row_data;  # get a fresh hash for the row data
220                 $row_data{category} = $results->[$i]{'category'};
221                 $row_data{stdlib} = ("&nbsp;&nbsp;&nbsp;&nbsp;" 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);
228         }
229         # rebuild coplete hierarchy
230         my  $sth = $dbh->prepare("select stdlib from bibliothesaurus where id=?");
231         my @hierarchy = split(/\|/,$branch);
232         my @hierarchy_loop;
233         warn "total : $#hierarchy";
234         my $x;
235         for (my $xi=0;$xi<=$#hierarchy;$xi++) {
236                 my %link;
237                 $sth->execute($hierarchy[$xi]);
238                 my ($t) = $sth->fetchrow_array;
239                 $x.=$hierarchy[$xi];
240                 $link{'string'}=$t;
241                 $link{'branch'}=$x;
242                 push (@hierarchy_loop, \%link);
243                 $x.='|';
244         }
245         $template->param(loop => \@loop_data,
246                                                         tab_list => $tab_list,
247                                                         category => $search_category,
248                                                         toponly => $toponly,
249                                                         searchstring => $searchstring,
250                                                         hierarchy_loop => \@hierarchy_loop);
251         if ($offset>0) {
252                 my $prevpage = $offset-$pagesize;
253                 $template->param(previous => "<a href=$script_name?branch=$branch&toponly=$toponly&category=$search_category&searchstring=$searchstring&offset=$prevpage>&lt;&lt; Prev</a>");
254         }
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 &gt;&gt;</a>");
258         }
259 } #---- END $OP eq DEFAULT
260
261 print "Content-Type: text/html\n\n", $template->output;