many bugfixes from Dombes Abbey tests
[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 use C4::Authorities;
28
29 my $input = new CGI;
30 my $search_category=$input->param('search_category');
31 $search_category=$input->param('category') unless $search_category;
32 #my $toponly = $input->param('toponly');
33 my $branch = $input->param('branch');
34 my $searchstring = $input->param('searchstring');
35 $searchstring=~ s/\,//g;
36 my $id = $input->param('id');
37 my $offset=$input->param('offset');
38 my $father=$input->param('father');
39
40 my $reqsel="select category,stdlib,freelib from bibliothesaurus where id='$id'";
41 my $reqdel="delete from bibliothesaurus where id='$id'";
42 my $script_name="/cgi-bin/koha/admin/thesaurus.pl";
43 my $dbh = C4::Context->dbh;
44 my $authoritysep = C4::Context->preference("authoritysep");
45
46 my $template = gettemplate("parameters/thesaurus.tmpl",0);
47 my $pagesize=20;
48
49 my $prevpage = $offset-$pagesize;
50 my $nextpage =$offset+$pagesize;
51
52 my $op = $input->param('op');
53
54 if ($op) {
55 $template->param(script_name => $script_name,
56                                                 $op              => 1); # we show only the TMPL_VAR names $op
57 } else {
58 $template->param(script_name => $script_name,
59                                                 else              => 1); # we show only the TMPL_VAR names $op
60 }
61 ################## ADD_FORM ##################################
62 # called by default. Used to create form to add or  modify a record
63 if ($op eq 'add_form') {
64         my $data;
65         if ($id) {
66                 my $dbh = C4::Context->dbh;
67                 my $sth=$dbh->prepare("select id,category,freelib,stdlib from bibliothesaurus where id='$id'");
68                 $sth->execute;
69                 $data=$sth->fetchrow_hashref;
70                 $sth->finish;
71         } else {
72                 $data->{'category'} = $input->param('category');
73                 $data->{'stdlib'} = $input->param('stdlib');
74         }
75         if ($search_category) {
76                 $template->param(action => "Modify thesaurus");
77         } else {
78                 $template->param(action => "Add thesaurus");
79         }
80         $template->param(category => $data->{'category'},
81                                                         stdlib => $data->{'stdlib'},
82                                                         freelib => $data->{'freelib'},
83                                                         id => $data->{'id'},
84                                                         branch => $branch,
85 #                                                       toponly => $toponly,
86                                                         search_category => $search_category,
87                                                         searchstring => $searchstring,
88                                                         offset => $offset,
89                                                         father => $father,
90                                                         );
91         if ($data->{'category'}) {
92                 $template->param(category => "<input type=\"hidden\" name=\"category\" value='$data->{'category'}'>$data->{'category'}");
93         } else {
94                 $template->param(category => "<input type=text name=\"category\" size=8 maxlength=8>");
95         }
96 ################## ADD_VALIDATE ##################################
97 # called by add_form, used to insert/modify data in DB
98 } elsif ($op eq 'add_validate') {
99         my $dbh = C4::Context->dbh;
100         my $freelib = $input->param('freelib');
101         $freelib = $input->param('stdlib') unless ($input->param('freelib'));
102         newauthority($dbh,$input->param('category'),$input->param('father')." ".$input->param('stdlib'), $freelib,'',1,'');
103         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=thesaurus.pl?branch=$branch&search_category=$search_category&searchstring=$searchstring&offset=$offset\"></html>";
104         exit;
105 ################## DELETE_CONFIRM ##################################
106 # called by default form, used to confirm deletion of data in DB
107 } elsif ($op eq 'delete_confirm') {
108         my $dbh = C4::Context->dbh;
109         my $sth=$dbh->prepare($reqsel);
110         $sth->execute;
111         my $data=$sth->fetchrow_hashref;
112         $sth->finish;
113         $template->param(search_category => $search_category,
114                                                         Tvalue => $data->{'stdlib'},
115                                                         id =>$id,
116                                                         );
117
118                                                                                                         # END $OP eq DELETE_CONFIRM
119 ################## DELETE_CONFIRMED ##################################
120 # called by delete_confirm, used to effectively confirm deletion of data in DB
121 } elsif ($op eq 'delete_confirmed') {
122         &delauthority($id);
123         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=thesaurus.pl?search_category=$search_category&searchstring=$searchstring\"></html>";
124         exit;
125                                                                                                         # END $OP eq DELETE_CONFIRMED
126 ################## DETAIL_FORM ##################################
127 } elsif ($op eq 'detail_form') {
128         # build categories list
129         my $sth = $dbh->prepare("select distinct category from bibliothesaurus");
130         $sth->execute;
131         my @category_list;
132         while ( my ($category) = $sth->fetchrow_array) {
133                 push(@category_list,$category);
134         }
135         my $tab_list = CGI::scrolling_list(-name=>'search_category',
136                         -values=> \@category_list,
137                         -default=>"$search_category",
138                         -size=>1,
139                         -multiple=>0,
140                         );
141         if (!$search_category) {
142                 $search_category=$category_list[0];
143         }
144         my $env;
145         my $sth = $dbh->prepare("select father,stdlib,category,hierarchy from bibliothesaurus where id=?");
146         $sth->execute($id);
147         my ($father,$stdlib,$category,$suphierarchy) = $sth->fetchrow_array;
148         $sth->finish;
149         $sth= $dbh->prepare("select id,freelib from bibliothesaurus where father=? and stdlib=?");
150         $sth->execute($father,$stdlib);
151         my $toggle="white";
152         # builds value list
153         my @loop_data;
154         while ( my ($id,$freelib) = $sth->fetchrow_array) {
155                 if ($toggle eq 'white'){
156                         $toggle="#ffffcc";
157                 } else {
158                         $toggle="white";
159                 }
160                 my %row_data;  # get a fresh hash for the row data
161                 $row_data{freelib} = $freelib;
162                 $row_data{edit} = "$script_name?op=add_form&id=$id";
163                 $row_data{delete} = "$script_name?op=delete_confirm&search_category=$search_category&id=$id";
164                 push(@loop_data, \%row_data);
165         }
166
167         $template->param(loop => \@loop_data,
168                                                         tab_list => $tab_list,
169                                                         category => $search_category,
170 #                                                       toponly => $toponly,
171                                                         searchstring => $searchstring,
172                                                         stdlib => $stdlib,
173                                                         category => $category);
174 ################## DEFAULT ##################################
175 } else { # DEFAULT
176         # build categories list
177         my $sth = $dbh->prepare("select distinct category from bibliothesaurus");
178         $sth->execute;
179         my @category_list;
180         while ( my ($category) = $sth->fetchrow_array) {
181                 push(@category_list,$category);
182         }
183         my $tab_list = CGI::scrolling_list(-name=>'search_category',
184                         -values=> \@category_list,
185                         -default=>"$search_category",
186                         -size=>1,
187                         -multiple=>0,
188                         );
189         if (!$search_category) {
190                 $search_category=$category_list[0];
191         }
192         my $env;
193         my ($count,$results)=searchauthority($env,$search_category,$branch,$searchstring,$offset,$pagesize);
194         my $toggle="white";
195         my @loop_data = ();
196         # builds value list
197         for (my $i=0; $i < $pagesize; $i++){
198                 if ($results->[$i]{'stdlib'}) {
199                         if ($toggle eq 'white'){
200                                 $toggle="#ffffcc";
201                         } else {
202                                 $toggle="white";
203                         }
204                         my %row_data;  # get a fresh hash for the row data
205                         $row_data{category} = $results->[$i]{'category'};
206 #                       $row_data{stdlib} = ("&nbsp;&nbsp;&nbsp;&nbsp;" x $results->[$i]{'level'}).$results->[$i]{'stdlib'};
207                         $row_data{stdlib} = $results->[$i]{'stdlib'};
208                         $row_data{freelib} = $results->[$i]{'freelib'};
209                         $row_data{freelib} =~ s/($searchstring)/<b>$1<\/b>/gi;
210                         $row_data{father} = $results->[$i]{'father'};
211                         $row_data{dig} ="<a href=thesaurus.pl?branch=$results->[$i]{'hierarchy'}$results->[$i]{'id'}|&search_category=$search_category>";
212                         $row_data{related} ="<a href=thesaurus.pl?id=$results->[$i]{'id'}&search_category=$search_category&op=detail_form>";
213                         $row_data{edit} = "$script_name?op=add_form&branch=$branch&search_category=$search_category&searchstring=$searchstring&offset=$offset&id=".$results->[$i]{'id'};
214                         $row_data{delete} = "$script_name?op=delete_confirm&search_category=$search_category&id=".$results->[$i]{'id'};
215                         push(@loop_data, \%row_data);
216                 }
217         }
218         # rebuild complete hierarchy
219         my  $sth = $dbh->prepare("select stdlib from bibliothesaurus where id=?");
220         my @hierarchy = split(/\|/,$branch);
221         my @hierarchy_loop;
222         my $x;
223         my $father;
224         for (my $xi=0;$xi<=$#hierarchy;$xi++) {
225                 my %link;
226                 $sth->execute($hierarchy[$xi]);
227                 my ($t) = $sth->fetchrow_array;
228                 $x.=$hierarchy[$xi]."|";
229                 $link{'string'}=$t;
230                 $link{'branch'}=$x;
231                 push (@hierarchy_loop, \%link);
232                 $father .= $t." $authoritysep ";
233         }
234         $template->param(loop => \@loop_data,
235                                                         tab_list => $tab_list,
236                                                         category => $search_category,
237 #                                                       toponly => $toponly,
238                                                         searchstring => $searchstring,
239                                                         hierarchy_loop => \@hierarchy_loop,
240                                                         branch => $branch,
241                                                         father => $father);
242         if ($offset>0) {
243                 $template->param(previous => "<a href=$script_name?branch=$branch&search_category=$search_category&searchstring=$searchstring&offset=$prevpage>&lt;&lt; Prev</a>");
244         }
245         if ($pagesize<$count) {
246                 $template->param(next => "<a href=$script_name?branch=$branch&search_category=$search_category&searchstring=$searchstring&offset=$nextpage>Next &gt;&gt;</a>");
247         }
248 } #---- END $OP eq DEFAULT
249
250 print "Content-Type: text/html\n\n", $template->output;