6 use C4::Interface::CGI::Output;
14 my @spsuggest; # the array for holding suggestions
15 my $suggest; # a flag to be set (if there are suggestions it's 1)
16 my $firstbiblionumber; # needed for directly sending user to first item
25 my $dbh=C4::Context->dbh;
26 my $sth=$dbh->prepare("select description,itemtype from itemtypes order by description");
28 while (my ($description,$itemtype) = $sth->fetchrow) {
29 $itemtypelist.="<option value=\"$itemtype\">$description</option>\n";
31 my $sth=$dbh->prepare("select description,subcategorycode from subcategorytable order by description");
33 while (my ($description,$subcategorycode) = $sth->fetchrow) {
34 $subcategorylist.="<option value=\"$subcategorycode\">$description</option>\n";
36 my $sth=$dbh->prepare("select description,mediatypecode from mediatypetable order by description");
38 while (my ($description,$mediatypecode) = $sth->fetchrow) {
39 $mediatypelist.="<option value=\"$mediatypecode\">$description</option>\n";
41 my $sth=$dbh->prepare("select description,categorycode from categorytable order by description");
43 while (my ($description,$categorycode) = $sth->fetchrow) {
44 $categorylist .= '<input type="radio" name="categorylist" value="'.$categorycode.'">'.$description.'<br>';
46 my $sth=$dbh->prepare("select branchname,branchcode from branches order by branchname");
49 while (my ($branchname,$branchcode) = $sth->fetchrow) {
50 $brancheslist.="<option value=\"$branchcode\">$branchname</option>\n";
53 my $op = $query->param("op");
54 my $type=$query->param('type');
56 my $itemtypesstring=$query->param("itemtypesstring");
57 $itemtypesstring =~s/"//g;
58 my @itemtypes = split ( /\|/, $itemtypesstring);
59 my $branchesstring=$query->param("branchesstring");
60 $branchesstring =~s/"//g;
61 my @branches = split (/\|/, $branchesstring);
63 my $startfrom=$query->param('startfrom');
64 $startfrom=0 if(!defined $startfrom);
65 my ($template, $loggedinuser, $cookie);
69 if ($op eq "do_search") {
70 my @marclist = $query->param('marclist');
71 my @and_or = $query->param('and_or');
72 my @excluding = $query->param('excluding');
73 my @operator = $query->param('operator');
74 my @value = $query->param('value');
76 for (my $i=0;$i<=$#marclist;$i++) {
77 if ($searchdesc) { # don't put the and_or on the 1st search term
78 $searchdesc .= $and_or[$i]." ".$excluding[$i]." ".($marclist[$i]?$marclist[$i]:"*")." ".$operator[$i]." ".$value[$i]." " if ($value[$i]);
80 $searchdesc = $excluding[$i]." ".($marclist[$i]?$marclist[$i]:"*")." ".$operator[$i]." ".$value[$i]." " if ($value[$i]);
83 if ($itemtypesstring ne ''){
84 $searchdesc .= 'filtered by itemtypes ';
85 $searchdesc .= join(" ",@itemtypes)
87 if ($branchesstring ne ''){
88 $searchdesc .= ' in branches ';
89 $searchdesc .= join(" ",@branches)
91 $resultsperpage= $query->param('resultsperpage');
92 $resultsperpage = 19 if(!defined $resultsperpage);
93 my $orderby = $query->param('orderby');
94 my $desc_or_asc = $query->param('desc_or_asc');
95 # builds tag and subfield arrays
98 foreach my $marc (@marclist) {
100 my ($tag,$subfield) = MARCfind_marc_from_kohafield($dbh,$marc,'');
102 push @tags,$dbh->quote("$tag$subfield");
104 push @tags, $dbh->quote(substr($marc,0,4));
110 findseealso($dbh,\@tags);
112 if ($itemtypesstring ne ''){
113 $sqlstring = 'and (biblioitems.itemtype IN (';
115 foreach my $itemtype (@itemtypes){
116 if ($itemtype ne ''){
117 if ($itemtypeloop != 0){
120 $sqlstring .= '"'.$itemtype.'"';
126 if ($branchesstring ne ''){
127 $sqlstring .= 'and biblio.biblionumber=items.biblionumber and (items.holdingbranch IN (';
129 foreach my $branch (@branches){
131 if ($branchesloop != 0){
134 $sqlstring .= '"'.$branch.'"';
141 my ($results,$total) = catalogsearch($dbh, \@tags,\@and_or,
142 \@excluding, \@operator, \@value,
143 $startfrom*$resultsperpage, $resultsperpage,$orderby,$desc_or_asc,$sqlstring);
145 if (C4::Context->preference("BiblioDefaultView") eq "normal") {
146 print $query->redirect("/cgi-bin/koha/opac-detail.pl?bib=".@$results[0]->{biblionumber});
147 } elsif (C4::Context->preference("BiblioDefaultView") eq "MARC") {
148 print $query->redirect("/cgi-bin/koha/MARCdetail.pl?bib=".@$results[0]->{biblionumber});
150 print $query->redirect("/cgi-bin/koha/ISBDdetail.pl?bib=".@$results[0]->{biblionumber});
154 ($template, $loggedinuser, $cookie)
155 = get_template_and_user({template_name => "opac-searchresults.tmpl",
158 authnotrequired => 1,
162 # multi page display gestion
164 my $displayprev=$startfrom;
165 if(($total - (($startfrom+1)*($resultsperpage))) > 0 ){
172 ## This next does a number of things:
173 # 1. It allows you to track all the searches made for stats, etc.
174 # 2. It fixes the 'searchdesc' variable problem by introducing
175 # a. 'searchterms' which comes out as 'Keyword: neal stephenson'
176 # b. 'phraseorterm' which comes out as 'neal stephenson'
177 # both of these are useful for differen purposes ... I use searchterms
178 # for display purposes and phraseorterm for passing the search terms
179 # to an external source through a url (like a database search)
180 # 3. It provides the variables necessary for the spellchecking (look below for
184 $totalresults = $total;
185 ## This formats the 'search results' string and populates
186 ## the 'OPLIN' variable as well as the 'spellcheck' variable
187 ## with appropriate values based on the user's search input
189 my $searchterms; #returned in place of searchdesc for 'results for search'
190 # as a string (can format if need be)
194 my %searchtypehash = ( # used only for the searchterms string formation
195 # and for spellcheck string
204 my @searchterm = $query->param('value');
206 for (my $i=0; $i <= $#searchterm; $i++) {
207 my $searchtype = $searchtypehash{$i};
208 push @spphrases, $searchterm[$i];
209 if ($searchterms) { #don't put and in again
210 if ($searchterm[$i]) {
211 $phraseorterm.=$searchterm[$i];
212 $searchterms.=" AND ".$searchtype." : \'".$searchterm[$i]."\'";
215 if ($searchterm[$i]) {
216 $phraseorterm.=$searchterm[$i];
217 $searchterms.=$searchtype.": \'".$searchterm[$i]."\'";
222 # Spellchecck stuff ... needs to use above scheme but must change
224 my $phrases = $query->param('value');
225 #my $searchterms = $query->param('value');
226 # warn "here is searchterms:".$searchterms;
228 # FIXME: should be obvious ;-)
229 #foreach my $phrases (@spphrases) {
230 $phrases =~ s/(\.|\?|\:|\!|\'|,|\-|\"|\(|\)|\[|\]|\{|\})/ /g;
231 $phrases =~ s/(\Athe |\Aa |\Aan |)//g;
232 my $spchkphraseorterm = $phraseorterm;
233 $spchkphraseorterm =~ tr/A-Z/a-z/;
234 $spchkphraseorterm =~ s/(\.|\?|\:|\!|\'|,|\-|\"|\(|\)|\[|\]|\{|\})/ /g;
235 $spchkphraseorterm =~s/(\Aand-or |\Aand\/or |\Aanon |\Aan |\Aa |\Abut |\Aby |\Ade |\Ader |\Adr |\Adu|et |\Afor |\Afrom |\Ain |\Ainto |\Ait |\Amy |\Anot |\Aon |\Aor |\Aper |\Apt |\Aspp |\Ato |\Avs |\Awith |\Athe )/ /g;
236 $spchkphraseorterm =~s/( and-or | and\/or | anon | an | a | but | by | de | der | dr | du|et | for | from | in | into | it | my | not | on | or | per | pt | spp | to | vs | with | the )/ /g;
238 $spchkphraseorterm =~s/ / /g;
239 my $resultcount = $total;
240 my $ipaddress = $query->remote_host();
244 #need to create a table to record the search info
245 #...FIXME: add the script name that creates the table
247 my $dbhpop=DBI->connect("DBI:mysql:demosuggest:localhost","auth","YourPass")) {
249 # insert the search info query
250 my $insertpop = "INSERT INTO phrase_log(phr_phrase,phr_resultcount,phr_ip) VALUES(?,?,?)";
252 # grab spelling suggestions query
253 my $getsugg = "SELECT display FROM spellcheck WHERE strcmp(soundex(suggestion), soundex(?)) = 0 order by soundex(suggestion) limit 0,5";
255 #get spelling suggestions when there are no results
256 if ($resultcount eq 0) {
257 my $sthgetsugg=$dbhpop->prepare($getsugg);
258 $sthgetsugg->execute($spchkphraseorterm);
259 while (my ($spsuggestion)=$sthgetsugg->fetchrow_array) {
260 # warn "==>$spsuggestion";
261 #push @spsuggest, +{ spsuggestion => $spsuggestion };
263 $line{spsuggestion} = $spsuggestion;
264 push @spsuggest,\%line;
267 # warn "==>".$#spsuggest;
270 # end of spelling suggestions
272 my $sthpop=$dbhpop->prepare($insertpop);
274 #$sthpop->execute($phrases,$resultcount,$ipaddress);
278 ### end of tracking stuff -- jmf at kados dot org
280 $template->param(suggest => $suggest );
281 $template->param( SPELL_SUGGEST => \@spsuggest );
282 $template->param( searchterms => $searchterms );
283 $template->param( phraseorterm => $phraseorterm );
284 #warn "here's the search terms: ".$searchterms;
286 ### end of spelling suggestions
289 for(my $i = 0 ; $i <= $#marclist ; $i++)
291 push @field_data, { term => "marclist", val=>$marclist[$i] };
292 push @field_data, { term => "and_or", val=>$and_or[$i] };
293 push @field_data, { term => "excluding", val=>$excluding[$i] };
294 push @field_data, { term => "operator", val=>$operator[$i] };
295 push @field_data, { term => "value", val=>$value[$i] };
300 if ($total>$resultsperpage)
302 for (my $i=1; $i<$total/$resultsperpage+1; $i++)
307 ($startfrom==($i-1)) && ($highlight=1);
308 push @numbers, { number => $i,
309 highlight => $highlight ,
310 searchdata=> \@field_data,
311 startfrom => ($i-1)};
316 my $from = $startfrom*$resultsperpage+1;
319 if($total < (($startfrom+1)*$resultsperpage))
323 $to = (($startfrom+1)*$resultsperpage);
325 my $defaultview = 'BiblioDefaultView'.C4::Context->preference('BiblioDefaultView');
326 $template->param(results => $results,
327 startfrom=> $startfrom,
328 displaynext=> $displaynext,
329 displayprev=> $displayprev,
330 resultsperpage => $resultsperpage,
332 startfromnext => $startfrom+1,
333 startfromprev => $startfrom-1,
334 searchdata=>\@field_data,
339 searchdesc=> $searchdesc,
341 suggestion => C4::Context->preference("suggestion"),
342 virtualshelves => C4::Context->preference("virtualshelves"),
343 itemtypelist => $itemtypelist,
344 subcategorylist => $subcategorylist,
345 brancheslist => $brancheslist,
346 categorylist => $categorylist,
347 mediatypelist => $mediatypelist,
348 itemtypesstring => $itemtypesstring,
352 ($template, $loggedinuser, $cookie)
353 = get_template_and_user({template_name => "opac-search.tmpl",
356 authnotrequired => 1,
360 $sth=$dbh->prepare("Select itemtype,description from itemtypes order by description");
366 while (my ($value,$lib) = $sth->fetchrow_array) {
367 push @itemtype, $value;
368 $itemtypes{$value}=$lib;
371 my $CGIitemtype=CGI::scrolling_list( -name => 'value',
372 -values => \@itemtype,
373 -labels => \%itemtypes,
381 my ($count2,@branches)=branches();
382 push @select_branch, "";
383 $select_branches{''} = "";
384 for (my $i=0;$i<$count2;$i++){
385 push @select_branch, $branches[$i]->{'branchcode'};#
386 $select_branches{$branches[$i]->{'branchcode'}} = $branches[$i]->{'branchname'};
388 my $CGIbranch=CGI::scrolling_list( -name => 'value',
389 -values => \@select_branch,
390 -labels => \%select_branches,
395 $template->param(itemtypelist => $itemtypelist,
396 CGIitemtype => $CGIitemtype,
397 CGIbranch => $CGIbranch,
398 suggestion => C4::Context->preference("suggestion"),
399 virtualshelves => C4::Context->preference("virtualshelves"),
403 if ($totalresults == 1){
404 # if its a barcode search by definition we will only have one result.
405 # And if we have a result
406 # lets jump straight to the detail.pl page
407 print $query->redirect("/cgi-bin/koha/opac-detail.pl?bib=$firstbiblionumber");
410 output_html_with_http_headers $query, $cookie, $template->output;