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