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
28 my $dbh=C4::Context->dbh;
29 my $sth=$dbh->prepare("select description,itemtype from itemtypes order by description");
31 while (my ($description,$itemtype) = $sth->fetchrow) {
32 $itemtypelist.="<option value=\"$itemtype\">$description</option>\n";
34 my $sth=$dbh->prepare("select description,subcategorycode from subcategorytable order by description");
36 while (my ($description,$subcategorycode) = $sth->fetchrow) {
37 $subcategorylist.="<option value=\"$subcategorycode\">$description</option>\n";
39 my $sth=$dbh->prepare("select description,mediatypecode from mediatypetable order by description");
41 while (my ($description,$mediatypecode) = $sth->fetchrow) {
42 $mediatypelist.="<option value=\"$mediatypecode\">$description</option>\n";
44 my $sth=$dbh->prepare("select description,categorycode from categorytable order by description");
46 while (my ($description,$categorycode) = $sth->fetchrow) {
47 $categorylist .= '<input type="radio" name="categorylist" value="'.$categorycode.'">'.$description.'<br>';
49 my $sth=$dbh->prepare("select branchname,branchcode from branches order by branchname");
52 while (my ($branchname,$branchcode) = $sth->fetchrow) {
53 $brancheslist.="<option value=\"$branchcode\">$branchname</option>\n";
56 my $op = $query->param("op");
57 my $type=$query->param('type');
58 my $avail=$query->param('avail');
59 my $itemtypesstring=$query->param("itemtypesstring");
60 $itemtypesstring =~s/"//g;
61 my @itemtypes = split ( /\|/, $itemtypesstring);
62 my $branchesstring=$query->param("branchesstring");
63 $branchesstring =~s/"//g;
64 my @branches = split (/\|/, $branchesstring);
66 my $startfrom=$query->param('startfrom');
67 $startfrom=0 if(!defined $startfrom);
68 my ($template, $loggedinuser, $cookie);
72 if ($op eq "do_search") {
73 my @marclist = $query->param('marclist');
74 my @and_or = $query->param('and_or');
75 my @excluding = $query->param('excluding');
76 my @operator = $query->param('operator');
77 my @value = $query->param('value');
79 for (my $i=0;$i<=$#marclist;$i++) {
80 if ($searchdesc) { # don't put the and_or on the 1st search term
81 $searchdesc .= $and_or[$i]." ".$excluding[$i]." ".($marclist[$i]?$marclist[$i]:"*")." ".$operator[$i]." ".$value[$i]." " if ($value[$i]);
83 $searchdesc = $excluding[$i]." ".($marclist[$i]?$marclist[$i]:"*")." ".$operator[$i]." ".$value[$i]." " if ($value[$i]);
86 if ($itemtypesstring ne ''){
87 $searchdesc .= 'filtered by itemtypes ';
88 $searchdesc .= join(" ",@itemtypes)
91 if ($branchesstring ne ''){
92 $searchdesc .= ' in branches ';
93 $searchdesc .= join(" ",@branches)
96 $searchdesc .= '. Only available items shown.'
98 $resultsperpage= $query->param('resultsperpage');
99 $resultsperpage = 19 if(!defined $resultsperpage);
100 my $orderby = $query->param('orderby');
101 my $desc_or_asc = $query->param('desc_or_asc');
102 # builds tag and subfield arrays
105 foreach my $marc (@marclist) {
107 my ($tag,$subfield) = MARCfind_marc_from_kohafield($dbh,$marc,'');
109 push @tags,$dbh->quote("$tag$subfield");
111 push @tags, $dbh->quote(substr($marc,0,4));
117 findseealso($dbh,\@tags);
120 if ($itemtypesstring ne ''){
121 $sqlstring = 'and (biblioitems.itemtype IN (';
123 foreach my $itemtype (@itemtypes){
124 if ($itemtype ne ''){
125 if ($itemtypeloop != 0){
128 $sqlstring .= '"'.$itemtype.'"';
134 if ($branchesstring ne ''){
135 $sqlstring .= 'and biblio.biblionumber=items.biblionumber and (items.holdingbranch IN (';
137 $extratables = ',items';
138 foreach my $branch (@branches){
140 if ($branchesloop != 0){
143 $sqlstring .= '"'.$branch.'"';
150 $extratables .= ',items,issues,reserves';
151 $sqlstring .= "and biblioitems.biblioitemnumber=items.biblioitemnumber and items.itemnumber !=issues.itemnumber and biblio.biblionumber !=reserves.biblionumber and (items.itemlost IS NULL or items.itemlost = 0) and (items.notforloan IS NULL or items.notforloan =0) and (items.wthdrawn IS NULL or items.wthdrawn =0) ";
153 my ($results,$total) = catalogsearch($dbh, \@tags,\@and_or,
154 \@excluding, \@operator, \@value,
155 $startfrom*$resultsperpage, $resultsperpage,$orderby,$desc_or_asc,$sqlstring, $extratables);
157 if (C4::Context->preference("BiblioDefaultView") eq "normal") {
158 print $query->redirect("/cgi-bin/koha/opac-detail.pl?bib=".@$results[0]->{biblionumber});
159 } elsif (C4::Context->preference("BiblioDefaultView") eq "MARC") {
160 print $query->redirect("/cgi-bin/koha/MARCdetail.pl?bib=".@$results[0]->{biblionumber});
162 print $query->redirect("/cgi-bin/koha/ISBDdetail.pl?bib=".@$results[0]->{biblionumber});
166 ($template, $loggedinuser, $cookie)
167 = get_template_and_user({template_name => "opac-searchresults.tmpl",
170 authnotrequired => 1,
174 # multi page display gestion
176 my $displayprev=$startfrom;
177 if(($total - (($startfrom+1)*($resultsperpage))) > 0 ){
184 ## This next does a number of things:
185 # 1. It allows you to track all the searches made for stats, etc.
186 # 2. It fixes the 'searchdesc' variable problem by introducing
187 # a. 'searchterms' which comes out as 'Keyword: neal stephenson'
188 # b. 'phraseorterm' which comes out as 'neal stephenson'
189 # both of these are useful for differen purposes ... I use searchterms
190 # for display purposes and phraseorterm for passing the search terms
191 # to an external source through a url (like a database search)
192 # 3. It provides the variables necessary for the spellchecking (look below for
196 $totalresults = $total;
198 ## This formats the 'search results' string and populates
199 ## the 'OPLIN' variable as well as the 'spellcheck' variable
200 ## with appropriate values based on the user's search input
202 my $searchterms; #returned in place of searchdesc for 'results for search'
203 # as a string (can format if need be)
207 my %searchtypehash = ( # used only for the searchterms string formation
208 # and for spellcheck string
217 my @searchterm = $query->param('value');
219 for (my $i=0; $i <= $#searchterm; $i++) {
220 my $searchtype = $searchtypehash{$i};
221 push @spphrases, $searchterm[$i];
222 if ($searchterms) { #don't put and in again
223 if ($searchterm[$i]) {
224 $phraseorterm.=$searchterm[$i];
225 $searchterms.=" AND ".$searchtype." : \'".$searchterm[$i]."\'";
228 if ($searchterm[$i]) {
229 $phraseorterm.=$searchterm[$i];
230 $searchterms.=$searchtype.": \'".$searchterm[$i]."\'";
235 # Spellchecck stuff ... needs to use above scheme but must change
237 my $phrases = $query->param('value');
238 #my $searchterms = $query->param('value');
239 # warn "here is searchterms:".$searchterms;
241 # FIXME: should be obvious ;-)
242 #foreach my $phrases (@spphrases) {
243 $phrases =~ s/(\.|\?|\:|\!|\'|,|\-|\"|\(|\)|\[|\]|\{|\})/ /g;
244 $phrases =~ s/(\Athe |\Aa |\Aan |)//g;
245 my $spchkphraseorterm = $phraseorterm;
246 $spchkphraseorterm =~ tr/A-Z/a-z/;
247 $spchkphraseorterm =~ s/(\.|\?|\:|\!|\'|,|\-|\"|\(|\)|\[|\]|\{|\})/ /g;
248 $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;
249 $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;
251 $spchkphraseorterm =~s/ / /g;
252 my $resultcount = $total;
253 my $ipaddress = $query->remote_host();
257 #need to create a table to record the search info
258 #...FIXME: add the script name that creates the table
260 my $dbhpop=DBI->connect("DBI:mysql:demosuggest:localhost","auth","YourPass")) {
262 # insert the search info query
263 my $insertpop = "INSERT INTO phrase_log(phr_phrase,phr_resultcount,phr_ip) VALUES(?,?,?)";
265 # grab spelling suggestions query
266 my $getsugg = "SELECT display FROM spellcheck WHERE strcmp(soundex(suggestion), soundex(?)) = 0 order by soundex(suggestion) limit 0,5";
268 #get spelling suggestions when there are no results
269 if ($resultcount eq 0) {
270 my $sthgetsugg=$dbhpop->prepare($getsugg);
271 $sthgetsugg->execute($spchkphraseorterm);
272 while (my ($spsuggestion)=$sthgetsugg->fetchrow_array) {
273 # warn "==>$spsuggestion";
274 #push @spsuggest, +{ spsuggestion => $spsuggestion };
276 $line{spsuggestion} = $spsuggestion;
277 push @spsuggest,\%line;
280 # warn "==>".$#spsuggest;
283 # end of spelling suggestions
285 my $sthpop=$dbhpop->prepare($insertpop);
287 #$sthpop->execute($phrases,$resultcount,$ipaddress);
291 ### end of tracking stuff -- jmf at kados dot org
293 $template->param(suggest => $suggest );
294 $template->param( SPELL_SUGGEST => \@spsuggest );
295 $template->param( searchterms => $searchterms );
296 $template->param( phraseorterm => $phraseorterm );
297 #warn "here's the search terms: ".$searchterms;
299 ### end of spelling suggestions
302 for(my $i = 0 ; $i <= $#marclist ; $i++)
304 push @field_data, { term => "marclist", val=>$marclist[$i] };
305 push @field_data, { term => "and_or", val=>$and_or[$i] };
306 push @field_data, { term => "excluding", val=>$excluding[$i] };
307 push @field_data, { term => "operator", val=>$operator[$i] };
308 push @field_data, { term => "value", val=>$value[$i] };
313 if ($total>$resultsperpage)
315 for (my $i=1; $i<$total/$resultsperpage+1; $i++)
320 ($startfrom==($i-1)) && ($highlight=1);
321 push @numbers, { number => $i,
322 highlight => $highlight ,
323 searchdata=> \@field_data,
324 startfrom => ($i-1)};
329 my $from = $startfrom*$resultsperpage+1;
332 if($total < (($startfrom+1)*$resultsperpage))
336 $to = (($startfrom+1)*$resultsperpage);
338 my $defaultview = 'BiblioDefaultView'.C4::Context->preference('BiblioDefaultView');
339 $template->param(results => $results,
340 startfrom=> $startfrom,
341 displaynext=> $displaynext,
342 displayprev=> $displayprev,
343 resultsperpage => $resultsperpage,
345 startfromnext => $startfrom+1,
346 startfromprev => $startfrom-1,
347 searchdata=>\@field_data,
352 searchdesc=> $searchdesc,
354 suggestion => C4::Context->preference("suggestion"),
355 virtualshelves => C4::Context->preference("virtualshelves"),
356 itemtypelist => $itemtypelist,
357 subcategorylist => $subcategorylist,
358 brancheslist => $brancheslist,
359 categorylist => $categorylist,
360 mediatypelist => $mediatypelist,
361 itemtypesstring => $itemtypesstring,
366 ($template, $loggedinuser, $cookie)
367 = get_template_and_user({template_name => "opac-search.tmpl",
370 authnotrequired => 1,
374 $sth=$dbh->prepare("Select itemtype,description from itemtypes order by description");
380 while (my ($value,$lib) = $sth->fetchrow_array) {
381 push @itemtype, $value;
382 $itemtypes{$value}=$lib;
385 my $CGIitemtype=CGI::scrolling_list( -name => 'value',
386 -values => \@itemtype,
387 -labels => \%itemtypes,
395 my ($count2,@branches)=branches();
396 push @select_branch, "";
397 $select_branches{''} = "";
398 for (my $i=0;$i<$count2;$i++){
399 push @select_branch, $branches[$i]->{'branchcode'};#
400 $select_branches{$branches[$i]->{'branchcode'}} = $branches[$i]->{'branchname'};
402 my $CGIbranch=CGI::scrolling_list( -name => 'value',
403 -values => \@select_branch,
404 -labels => \%select_branches,
409 $template->param(itemtypelist => $itemtypelist,
410 CGIitemtype => $CGIitemtype,
411 CGIbranch => $CGIbranch,
412 suggestion => C4::Context->preference("suggestion"),
413 virtualshelves => C4::Context->preference("virtualshelves"),
417 if ($totalresults == 1){
418 # if its a barcode search by definition we will only have one result.
419 # And if we have a result
420 # lets jump straight to the detail.pl page
421 print $query->redirect("/cgi-bin/koha/opac-detail.pl?bib=$firstbiblionumber");
424 output_html_with_http_headers $query, $cookie, $template->output;