rel_3_0 moved to HEAD
[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 $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                         -tabindex=>'',
205                         -multiple=>0,
206                         );
207         if (!$search_category) {
208                 $search_category=$category_list[0];
209         }
210         my $env;
211         my ($count,$results)=searchauthority($env,$search_category,$branch,$searchstring,$offset,$pagesize);
212         my $toggle="white";
213         my @loop_data = ();
214         # builds value list
215         for (my $i=0; $i < $pagesize; $i++){
216                 if ($results->[$i]{'stdlib'}) {
217                         if ($toggle eq 'white'){
218                                 $toggle="#ffffcc";
219                         } else {
220                                 $toggle="white";
221                         }
222                         my %row_data;  # get a fresh hash for the row data
223                         $row_data{category} = $results->[$i]{'category'};
224 #                       $row_data{stdlib} = ("&nbsp;&nbsp;&nbsp;&nbsp;" x $results->[$i]{'level'}).$results->[$i]{'stdlib'};
225                         $row_data{stdlib} = $results->[$i]{'stdlib'};
226                         $row_data{freelib} = $results->[$i]{'freelib'};
227                         $row_data{freelib} =~ s/($searchstring)/<b>$1<\/b>/gi;
228                         $row_data{father} = $results->[$i]{'father'};
229                         $row_data{dig} ="<a href=thesaurus.pl?branch=$results->[$i]{'hierarchy'}$results->[$i]{'id'}|&search_category=$search_category>";
230                         $row_data{related} ="<a href=thesaurus.pl?id=$results->[$i]{'id'}&search_category=$search_category&op=detail_form>";
231                         $row_data{edit} = "$script_name?op=add_form&branch=$branch&search_category=$search_category&searchstring=$searchstring&offset=$offset&id=".$results->[$i]{'id'};
232                         $row_data{delete} = "$script_name?op=delete_confirm&search_category=$search_category&id=".$results->[$i]{'id'};
233                         push(@loop_data, \%row_data);
234                 }
235         }
236         # rebuild complete hierarchy
237         my  $sth = $dbh->prepare("select stdlib from bibliothesaurus where id=?");
238         my @hierarchy = split(/\|/,$branch);
239         my @hierarchy_loop;
240         my $x;
241         my $father;
242         for (my $xi=0;$xi<=$#hierarchy;$xi++) {
243                 my %link;
244                 $sth->execute($hierarchy[$xi]);
245                 my ($t) = $sth->fetchrow_array;
246                 $x.=$hierarchy[$xi]."|";
247                 $link{'string'}=$t;
248                 $link{'branch'}=$x;
249                 push (@hierarchy_loop, \%link);
250                 $father .= $t." $authoritysep ";
251         }
252         $template->param(loop => \@loop_data,
253                                                         tab_list => $tab_list,
254                                                         category => $search_category,
255 #                                                       toponly => $toponly,
256                                                         searchstring => $searchstring,
257                                                         hierarchy_loop => \@hierarchy_loop,
258                                                         branch => $branch,
259                                                         father => $father);
260         if ($offset>0) {
261                 $template->param(previous => "$script_name?branch=$branch&search_category=$search_category&searchstring=$searchstring&offset=$prevpage");
262         }
263         if ($pagesize<$count) {
264                 $template->param(next => "$script_name?branch=$branch&search_category=$search_category&searchstring=$searchstring&offset=$nextpage");
265         }
266 } #---- END $OP eq DEFAULT
267 $template->param(intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
268                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
269                 IntranetNav => C4::Context->preference("IntranetNav"),
270                 );
271 output_html_with_http_headers $input, $cookie, $template->output;