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