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