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