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