bugfixes and improvments
[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 authorised value");
77         } else {
78                 $template->param(action => "Add authorised value");
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         newauthority($dbh,$input->param('category'),$input->param('stdlib'), $input->param('freelib'),'',1,'');
101         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>";
102         exit;
103 ################## DELETE_CONFIRM ##################################
104 # called by default form, used to confirm deletion of data in DB
105 } elsif ($op eq 'delete_confirm') {
106         my $dbh = C4::Context->dbh;
107         my $sth=$dbh->prepare($reqsel);
108         $sth->execute;
109         my $data=$sth->fetchrow_hashref;
110         $sth->finish;
111         $template->param(search_category => $search_category,
112                                                         Tvalue => $data->{'stdlib'},
113                                                         id =>$id,
114                                                         );
115
116                                                                                                         # END $OP eq DELETE_CONFIRM
117 ################## DELETE_CONFIRMED ##################################
118 # called by delete_confirm, used to effectively confirm deletion of data in DB
119 } elsif ($op eq 'delete_confirmed') {
120 #       my $dbh = C4::Context->dbh;
121 #       my $sth=$dbh->prepare($reqdel);
122 #       $sth->execute;
123 #       $sth->finish;
124         &delauthority($id);
125         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=thesaurus.pl?search_category=$search_category&searchstring=$searchstring\"></html>";
126         exit;
127                                                                                                         # END $OP eq DELETE_CONFIRMED
128 ################## DETAIL_FORM ##################################
129 } elsif ($op eq 'detail_form') {
130         # build categories list
131         my $sth = $dbh->prepare("select distinct category from bibliothesaurus");
132         $sth->execute;
133         my @category_list;
134         while ( my ($category) = $sth->fetchrow_array) {
135                 push(@category_list,$category);
136         }
137         my $tab_list = CGI::scrolling_list(-name=>'search_category',
138                         -values=> \@category_list,
139                         -default=>"$search_category",
140                         -size=>1,
141                         -multiple=>0,
142                         );
143         if (!$search_category) {
144                 $search_category=$category_list[0];
145         }
146         my $env;
147         my $sth = $dbh->prepare("select father,stdlib,category,hierarchy from bibliothesaurus where id=?");
148         $sth->execute($id);
149         my ($father,$stdlib,$category,$suphierarchy) = $sth->fetchrow_array;
150         $sth->finish;
151         $sth= $dbh->prepare("select id,freelib from bibliothesaurus where father=? and stdlib=?");
152         $sth->execute($father,$stdlib);
153         my $toggle="white";
154         # builds value list
155         my @loop_data;
156         while ( my ($id,$freelib) = $sth->fetchrow_array) {
157                 if ($toggle eq 'white'){
158                         $toggle="#ffffcc";
159                 } else {
160                         $toggle="white";
161                 }
162                 my %row_data;  # get a fresh hash for the row data
163                 $row_data{freelib} = $freelib;
164                 $row_data{edit} = "$script_name?op=add_form&id=$id";
165                 $row_data{delete} = "$script_name?op=delete_confirm&search_category=$search_category&id=$id";
166                 push(@loop_data, \%row_data);
167         }
168
169         $template->param(loop => \@loop_data,
170                                                         tab_list => $tab_list,
171                                                         category => $search_category,
172 #                                                       toponly => $toponly,
173                                                         searchstring => $searchstring,
174                                                         stdlib => $stdlib,
175                                                         category => $category);
176 ################## DEFAULT ##################################
177 } else { # DEFAULT
178         # build categories list
179         my $sth = $dbh->prepare("select distinct category from bibliothesaurus");
180         $sth->execute;
181         my @category_list;
182         while ( my ($category) = $sth->fetchrow_array) {
183                 push(@category_list,$category);
184         }
185         my $tab_list = CGI::scrolling_list(-name=>'search_category',
186                         -values=> \@category_list,
187                         -default=>"$search_category",
188                         -size=>1,
189                         -multiple=>0,
190                         );
191         if (!$search_category) {
192                 $search_category=$category_list[0];
193         }
194         my $env;
195         my ($count,$results)=searchauthority($env,$search_category,$branch,$searchstring,$offset,$pagesize);
196         my $toggle="white";
197         my @loop_data = ();
198         # builds value list
199         for (my $i=0; $i < $pagesize; $i++){
200                 if ($results->[$i]{'stdlib'}) {
201                         if ($toggle eq 'white'){
202                                 $toggle="#ffffcc";
203                         } else {
204                                 $toggle="white";
205                         }
206                         my %row_data;  # get a fresh hash for the row data
207                         $row_data{category} = $results->[$i]{'category'};
208 #                       $row_data{stdlib} = ("&nbsp;&nbsp;&nbsp;&nbsp;" x $results->[$i]{'level'}).$results->[$i]{'stdlib'};
209                         $row_data{stdlib} = $results->[$i]{'stdlib'};
210                         $row_data{freelib} = $results->[$i]{'freelib'};
211                         $row_data{freelib} =~ s/($searchstring)/<b>$1<\/b>/gi;
212                         $row_data{father} = $results->[$i]{'father'};
213                         $row_data{dig} ="<a href=thesaurus.pl?branch=$results->[$i]{'hierarchy'}$results->[$i]{'id'}|&search_category=$search_category>";
214                         $row_data{related} ="<a href=thesaurus.pl?id=$results->[$i]{'id'}&search_category=$search_category&op=detail_form>";
215                         $row_data{edit} = "$script_name?op=add_form&branch=$branch&search_category=$search_category&searchstring=$searchstring&offset=$offset&id=".$results->[$i]{'id'};
216                         $row_data{delete} = "$script_name?op=delete_confirm&search_category=$search_category&id=".$results->[$i]{'id'};
217                         push(@loop_data, \%row_data);
218                 }
219         }
220         # rebuild complete hierarchy
221         my  $sth = $dbh->prepare("select stdlib from bibliothesaurus where id=?");
222         my @hierarchy = split(/\|/,$branch);
223         my @hierarchy_loop;
224         my $x;
225         my $father;
226         for (my $xi=0;$xi<=$#hierarchy;$xi++) {
227                 my %link;
228                 $sth->execute($hierarchy[$xi]);
229                 my ($t) = $sth->fetchrow_array;
230                 $x.=$hierarchy[$xi]."|";
231                 $link{'string'}=$t;
232                 $link{'branch'}=$x;
233                 push (@hierarchy_loop, \%link);
234                 $father .= $t." $authoritysep ";
235         }
236         $template->param(loop => \@loop_data,
237                                                         tab_list => $tab_list,
238                                                         category => $search_category,
239 #                                                       toponly => $toponly,
240                                                         searchstring => $searchstring,
241                                                         hierarchy_loop => \@hierarchy_loop,
242                                                         branch => $branch,
243                                                         father => $father);
244         if ($offset>0) {
245                 $template->param(previous => "<a href=$script_name?branch=$branch&search_category=$search_category&searchstring=$searchstring&offset=$prevpage>&lt;&lt; Prev</a>");
246         }
247         if ($pagesize<$count) {
248                 $template->param(next => "<a href=$script_name?branch=$branch&search_category=$search_category&searchstring=$searchstring&offset=$nextpage>Next &gt;&gt;</a>");
249         }
250 } #---- END $OP eq DEFAULT
251
252 print "Content-Type: text/html\n\n", $template->output;