removing all useless %env / $env
[koha.git] / catalogue / dictionary.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 use C4::Output;
23 use C4::Interface::CGI::Output;
24 use C4::Auth;
25 use CGI;
26 use C4::Search;
27 use C4::AuthoritiesMarc;
28 use C4::Context;
29 use C4::Biblio;
30
31
32 =head1 NAME
33
34 dictionnary.pl : script to search in biblio & authority an existing value
35
36 =head1 SYNOPSIS
37
38 useful when the user want to search a term before running a query. For example, to see if "computer" is used in the database
39
40 The parameter "marclist" tells which field is searched (title, author, subject, but could be anything else)
41
42 This script searches in both biblios & authority
43 * in biblio, the script search in all marc fields related to what the user is looking for (for example, if the dictionnary is used on "author", the script searches in biblio.author, but also in additional authors & any MARC field related to author (through the "seealso" MARC constraint)
44 * in authority, the script search everywhere. Thus, the accepted & rejected forms are found.
45
46 The script shows all results & the user can choose what he want, that is copied into search form.
47
48 =cut
49
50 my $input = new CGI;
51 my $field =$input->param('marclist');
52 #warn "field :$field";
53 my ($tablename, $kohafield)=split /./,$field;
54 #my $tablename=$input->param('tablename');
55 $tablename="biblio" unless ($tablename);
56 #my $kohafield = $input->param('kohafield');
57 my @search = $input->param('search');
58 # warn " ".$search[0];
59 my $index = $input->param('index');
60 # warn " index: ".$index;
61 my $op=$input->param('op');
62 if (($search[0]) and not ($op eq 'do_search')){
63         $op='do_search';
64 }
65 my $script_name = 'catalogue/dictionary.pl';
66 my $query;
67 my $type=$input->param('type');
68 #warn " ".$type;
69
70 my $dbh = C4::Context->dbh;
71 my ($template, $loggedinuser, $cookie);
72
73 my $startfrom=$input->param('startfrom');
74 $startfrom=0 if(!defined $startfrom);
75 my $searchdesc;
76 my $resultsperpage;
77
78 #warn "Starting process";
79
80 if ($op eq "do_search") {
81         #
82         # searching in biblio
83         #
84         my $sth=$dbh->prepare("Select distinct tagfield,tagsubfield from marc_subfield_structure where kohafield = ?");
85         $sth->execute("$field");
86         my (@tags, @and_or, @operator, @excluding,@value);
87         
88         while ((my $tagfield,my $tagsubfield,my $liblibrarian) = $sth->fetchrow) {
89                 push @tags, $dbh->quote("$tagfield$tagsubfield");
90         }
91
92         $resultsperpage= $input->param('resultsperpage');
93         $resultsperpage = 19 if(!defined $resultsperpage);
94         my $orderby = $input->param('orderby');
95
96         findseealso($dbh,\@tags);
97
98         my @results, my $total;
99         my $strsth="select distinct subfieldvalue, count(marc_subfield_table.bibid) from marc_subfield_table,marc_word where marc_word.word like ? and marc_subfield_table.bibid=marc_word.bibid and marc_subfield_table.tagorder=marc_word.tagorder and marc_word.tagsubfield in ";
100         my $listtags="(";
101         foreach my $tag (@tags){
102                 $listtags .= $tag .",";
103         }
104         $listtags =~s/,$/)/;
105         $strsth .= $listtags." and marc_word.tagsubfield=concat(marc_subfield_table.tag,marc_subfield_table.subfieldcode) group by subfieldvalue ";
106 #       warn "search in biblio : ".$strsth;
107         my $value = uc($search[0]);
108         $value=~s/\*/%/g;
109         $value.= "%" if not($value=~m/%/);
110 #       warn " texte : ".$value;
111
112         $sth=$dbh->prepare($strsth);
113         $sth->execute($value);
114         my $total;
115         my @catresults;
116         my $javalue;
117         while (my ($value,$ctresults)=$sth->fetchrow) {
118                 # This $javalue is used for the javascript selectentry function (javalue for javascript value !)
119                 $javalue = $value;
120                 $javalue =~s/'/\\'/g;
121
122                 push @catresults,{value=> $value, 
123                                                   javalue=> $javalue,
124                                                   even=>($total-$startfrom*$resultsperpage)%2,
125                                                   count=>$ctresults
126                                                   } if (($total>=$startfrom*$resultsperpage) and ($total<($startfrom+1)*$resultsperpage));
127                 $total++;
128         }
129         
130
131         my $strsth="Select distinct authtypecode from marc_subfield_structure where (";
132         foreach my $listtags (@tags){
133                 my @taglist=split /,/,$listtags;
134                 foreach my $curtag (@taglist){
135                         $curtag =~s/\s+//;
136                         $strsth.="(tagfield='".substr($curtag,1,3)."' AND tagsubfield='".substr($curtag,4,1)."') OR";
137                 }
138         }
139         
140         $strsth=~s/ OR$/)/;
141         my $strsth = $strsth." and authtypecode is not NULL";
142 #       warn $strsth;
143         my $sth=$dbh->prepare($strsth);
144         $sth->execute;
145         
146         #
147         # searching in authorities
148         #
149         my @authresults;
150         my $authnbresults;
151         while ((my $authtypecode) = $sth->fetchrow) {
152                 my ($curauthresults,$nbresults) = authoritysearch($dbh,[''],[''],[''],['contains'],
153                                                                                                                 \@search,$startfrom*$resultsperpage, $resultsperpage,$authtypecode);
154                 if (defined(@$curauthresults)) {
155                         for (my $i = 0; $i < @$curauthresults ;$i++) {
156                                 @$curauthresults[$i]->{jamainentry} = @$curauthresults[$i]->{mainentry};
157                                 @$curauthresults[$i]->{jamainentry} =~ s/'/\\'/g;
158                         }
159                 }
160                 push @authresults, @$curauthresults;
161                 $authnbresults+=$nbresults;
162 #               warn "auth : $authtypecode nbauthresults : $nbresults";
163         }
164         
165         # 
166         # OK, filling the template with authorities & biblio entries found.
167         #
168         ($template, $loggedinuser, $cookie)
169                 = get_template_and_user({template_name => "catalogue/dictionary.tmpl",
170                                 query => $input,
171                                 type => $type,
172                                 authnotrequired => 0,
173                                 flagsrequired => {catalogue => 1},
174                                 debug => 1,
175                                 });
176
177         # multi page display gestion
178         my $displaynext=0;
179         my $displayprev=$startfrom;
180         if(($total - (($startfrom+1)*($resultsperpage))) > 0 ) {
181                 $displaynext = 1;
182         }
183
184         my @field_data = ();
185
186         for(my $i = 0 ; $i <= $#tags ; $i++) {
187                 push @field_data, { term => "marclist", val=>$tags[$i] };
188                 push @field_data, { term => "and_or", val=>$and_or[$i] };
189                 push @field_data, { term => "excluding", val=>$excluding[$i] };
190                 push @field_data, { term => "operator", val=>$operator[$i] };
191                 push @field_data, { term => "value", val=>$value[$i] };
192         }
193
194         my @numbers = ();
195
196         if ($total>$resultsperpage) {
197                 for (my $i=1; $i<$total/$resultsperpage+1; $i++) {
198                         if ($i<16) {
199                         my $highlight=0;
200                         ($startfrom==($i-1)) && ($highlight=1);
201                         push @numbers, { number => $i,
202                                         highlight => $highlight ,
203                                         searchdata=> \@field_data,
204                                         startfrom => ($i-1)};
205                         }
206         }
207         }
208
209         my $from = $startfrom*$resultsperpage+1;
210         my $to;
211
212         if($total < (($startfrom+1)*$resultsperpage))
213         {
214                 $to = $total;
215         } else {
216                 $to = (($startfrom+1)*$resultsperpage);
217         }
218         $template->param(anindex => $input->param('index'));
219         $template->param(result => \@results,
220                                          catresult=> \@catresults,
221                                                 search => $search[0],
222                                                 marclist =>$field,
223                                                 authresult => \@authresults,
224                                                 nbresults => $authnbresults,
225                                                 startfrom=> $startfrom,
226                                                 displaynext=> $displaynext,
227                                                 displayprev=> $displayprev,
228                                                 resultsperpage => $resultsperpage,
229                                                 startfromnext => $startfrom+1,
230                                                 startfromprev => $startfrom-1,
231                                                 searchdata=>\@field_data,
232                                                 total=>$total,
233                                                 from=>$from,
234                                                 to=>$to,
235                                                 numbers=>\@numbers,
236                                                 MARC_ON => C4::Context->preference("marc"),
237                                                 );
238
239  } else {
240         ($template, $loggedinuser, $cookie)
241                 = get_template_and_user({template_name => "catalogue/dictionary.tmpl",
242                                 query => $input,
243                                 type => $type,
244                                 authnotrequired => 0,
245                                 flagsrequired => {catalogue => 1},
246                                 debug => 1,
247                                 });
248 #warn "type : $type";
249  
250  }
251 $template->param(search => $search[0],
252                 marclist =>$field,
253                 type=>$type,
254                 anindex => $input->param('index'),
255                 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
256                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
257                 IntranetNav => C4::Context->preference("IntranetNav"),
258                 );
259
260 # Print the page
261 output_html_with_http_headers $input, $cookie, $template->output;
262
263 # Local Variables:
264 # tab-width: 4
265 # End: