Removed useless line
[koha.git] / search.marc / 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::SearchMarc;
28 use C4::AuthoritiesMarc;
29 use C4::Context;
30 use C4::Biblio;
31 use HTML::Template;
32
33 =head1 NAME
34
35 dictionnary.pl : script to search in biblio & authority an existing value
36
37 =head1 SYNOPSIS
38
39 useful when the user want to search a term before running a query. For example, to see if "computer" is used in the database
40
41 The parameter "marclist" tells which field is searched (title, author, subject, but could be anything else)
42
43 This script searches in both biblios & authority
44 * 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)
45 * in authority, the script search everywhere. Thus, the accepted & rejected forms are found.
46
47 The script shows all results & the user can choose what he want, that is copied into search form.
48
49 =cut
50
51 my $input = new CGI;
52 my $field =$input->param('marclist');
53 #warn "field :$field";
54 my ($tablename, $kohafield)=split /./,$field;
55 #my $tablename=$input->param('tablename');
56 $tablename="biblio" unless ($tablename);
57 #my $kohafield = $input->param('kohafield');
58 my @search = $input->param('search');
59 # warn " ".$search[0];
60 my $index = $input->param('index');
61 # warn " index: ".$index;
62 my $op=$input->param('op');
63 if (($search[0]) and not ($op eq 'do_search')){
64         $op='do_search';
65 }
66 my $script_name = 'search.marc/dictionary.pl';
67 my $query;
68 my $type=$input->param('type');
69 #warn " ".$type;
70
71 my $dbh = C4::Context->dbh;
72 my ($template, $loggedinuser, $cookie);
73
74 my $env;
75
76 my $startfrom=$input->param('startfrom');
77 $startfrom=0 if(!defined $startfrom);
78 my $searchdesc;
79 my $resultsperpage;
80
81 #warn "Starting process";
82
83 if ($op eq "do_search") {
84         #
85         # searching in biblio
86         #
87         my $sth=$dbh->prepare("Select distinct tagfield,tagsubfield from marc_subfield_structure where kohafield = ?");
88         $sth->execute("$field");
89         my (@tags, @and_or, @operator, @excluding,@value);
90         
91         while ((my $tagfield,my $tagsubfield,my $liblibrarian) = $sth->fetchrow) {
92                 push @tags, $dbh->quote("$tagfield$tagsubfield");
93         }
94
95         $resultsperpage= $input->param('resultsperpage');
96         $resultsperpage = 19 if(!defined $resultsperpage);
97         my $orderby = $input->param('orderby');
98
99         findseealso($dbh,\@tags);
100
101         my @results, my $total;
102         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 ";
103         my $listtags="(";
104         foreach my $tag (@tags){
105                 $listtags .= $tag .",";
106         }
107         $listtags =~s/,$/)/;
108         $strsth .= $listtags." and marc_word.tagsubfield=concat(marc_subfield_table.tag,marc_subfield_table.subfieldcode) group by subfieldvalue ";
109 #       warn "search in biblio : ".$strsth;
110         my $value = uc($search[0]);
111         $value=~s/\*/%/g;
112         $value.= "%" if not($value=~m/%/);
113 #       warn " texte : ".$value;
114
115         $sth=$dbh->prepare($strsth);
116         $sth->execute($value);
117         my $total;
118         my @catresults;
119         my $javalue;
120
121         while (my ($value,$ctresults)=$sth->fetchrow) {
122 #               warn "countresults : ".$ctresults;
123
124                 # This $javalue is used for the javascript selectentry function (javalue for javascript value !)
125                 $javalue = $value;
126                 $javalue =~s/'/\\'/g;
127
128                 push @catresults,{value=> $value, 
129                                                         javalue=> $javalue,
130                                                   even=>($total-$startfrom*$resultsperpage)%2,
131                                                   count=>$ctresults
132                                                   } if (($total>=$startfrom*$resultsperpage) and ($total<($startfrom+1)*$resultsperpage));
133                 $total++;
134         }
135         
136
137         my $strsth="Select distinct authtypecode from marc_subfield_structure where (";
138         foreach my $listtags (@tags){
139                 my @taglist=split /,/,$listtags;
140                 foreach my $curtag (@taglist){
141                         # regexp used to prevent errors if user puts spaces in "search also" of the framework description.
142
143                         $curtag =~s/\s+//;
144                         $strsth.="(tagfield='".substr($curtag,1,3)."' AND tagsubfield='".substr($curtag,4,1)."') OR";
145                 }
146         }
147         
148         $strsth=~s/ OR$/)/;
149         my $strsth = $strsth." and authtypecode is not NULL";
150 #       warn $strsth;
151         my $sth=$dbh->prepare($strsth);
152         $sth->execute;
153         
154         #
155         # searching in authorities
156         #
157         my @authresults;
158         my $authnbresults;
159         while ((my $authtypecode) = $sth->fetchrow) {
160                 my ($curauthresults,$nbresults) = authoritysearch($dbh,[''],[''],[''],['contains'],
161                                                                                                                 \@search,$startfrom*$resultsperpage, $resultsperpage,$authtypecode);
162
163
164                 if (defined(@$curauthresults)) {
165                         for (my $i = 0; $i < @$curauthresults ;$i++) {
166                                 @$curauthresults[$i]->{jamainentry} = @$curauthresults[$i]->{mainentry};
167                                 @$curauthresults[$i]->{jamainentry} =~ s/'/\\'/g;
168                         }
169                 }
170
171                 push @authresults, @$curauthresults;
172                 $authnbresults+=$nbresults;
173 #               warn "auth : $authtypecode nbauthresults : $nbresults";
174         }
175         
176         # 
177         # OK, filling the template with authorities & biblio entries found.
178         #
179         ($template, $loggedinuser, $cookie)
180                 = get_template_and_user({template_name => "search.marc/dictionary.tmpl",
181                                 query => $input,
182                                 type => $type,
183                                 authnotrequired => 0,
184                                 flagsrequired => {catalogue => 1},
185                                 debug => 1,
186                                 });
187
188         # multi page display gestion
189         my $displaynext=0;
190         my $displayprev=$startfrom;
191         if(($total - (($startfrom+1)*($resultsperpage))) > 0 ) {
192                 $displaynext = 1;
193         }
194
195         my @field_data = ();
196
197         for(my $i = 0 ; $i <= $#tags ; $i++) {
198                 push @field_data, { term => "marclist", val=>$tags[$i] };
199                 push @field_data, { term => "and_or", val=>$and_or[$i] };
200                 push @field_data, { term => "excluding", val=>$excluding[$i] };
201                 push @field_data, { term => "operator", val=>$operator[$i] };
202                 push @field_data, { term => "value", val=>$value[$i] };
203         }
204
205         my @numbers = ();
206
207         if ($total>$resultsperpage) {
208                 for (my $i=1; $i<$total/$resultsperpage+1; $i++) {
209                         if ($i<16) {
210                         my $highlight=0;
211                         ($startfrom==($i-1)) && ($highlight=1);
212                         push @numbers, { number => $i,
213                                         highlight => $highlight ,
214                                         searchdata=> \@field_data,
215                                         startfrom => ($i-1)};
216                         }
217         }
218         }
219
220         my $from = $startfrom*$resultsperpage+1;
221         my $to;
222
223         if($total < (($startfrom+1)*$resultsperpage))
224         {
225                 $to = $total;
226         } else {
227                 $to = (($startfrom+1)*$resultsperpage);
228         }
229         $template->param(anindex => $input->param('index'));
230         $template->param(result => \@results,
231                                          catresult=> \@catresults,
232                                                 search => $search[0],
233                                                 marclist =>$field,
234                                                 authresult => \@authresults,
235                                                 nbresults => $authnbresults,
236                                                 startfrom=> $startfrom,
237                                                 displaynext=> $displaynext,
238                                                 displayprev=> $displayprev,
239                                                 resultsperpage => $resultsperpage,
240                                                 startfromnext => $startfrom+1,
241                                                 startfromprev => $startfrom-1,
242                                                 searchdata=>\@field_data,
243                                                 total=>$total,
244                                                 from=>$from,
245                                                 to=>$to,
246                                                 numbers=>\@numbers,
247                                                 MARC_ON => C4::Context->preference("marc"),
248                                                 );
249
250  } else {
251         ($template, $loggedinuser, $cookie)
252                 = get_template_and_user({template_name => "search.marc/dictionary.tmpl",
253                                 query => $input,
254                                 type => $type,
255                                 authnotrequired => 0,
256                                 flagsrequired => {catalogue => 1},
257                                 debug => 1,
258                                 });
259 #warn "type : $type";
260  
261  }
262 $template->param(search => $search[0],
263                                         marclist =>$field,
264                                         type=>$type,
265                                         anindex => $input->param('index'));
266
267 # Print the page
268 output_html_with_http_headers $input, $cookie, $template->output;
269
270 # Local Variables:
271 # tab-width: 4
272 # End: