6 use C4::Interface::CGI::Output;
16 my @spsuggest; # the array for holding suggestions
17 my $suggest; # a flag to be set (if there are suggestions it's 1)
18 my $firstbiblionumber; # needed for directly sending user to first item
30 my $dbh=C4::Context->dbh;
31 my $sth=$dbh->prepare("select description,itemtype from itemtypes order by description");
33 while (my ($description,$itemtype) = $sth->fetchrow) {
34 $itemtypelist.="<option value=\"$itemtype\">$description</option>\n";
36 my $sth=$dbh->prepare("select description,subcategorycode from subcategorytable order by description");
38 while (my ($description,$subcategorycode) = $sth->fetchrow) {
39 $subcategorylist.="<option value=\"$subcategorycode\">$description</option>\n";
41 my $sth=$dbh->prepare("select description,mediatypecode from mediatypetable order by description");
43 while (my ($description,$mediatypecode) = $sth->fetchrow) {
44 $mediatypelist.="<option value=\"$mediatypecode\">$description</option>\n";
46 my $sth=$dbh->prepare("select description,categorycode from categorytable order by description");
48 while (my ($description,$categorycode) = $sth->fetchrow) {
49 $categorylist .= '<input type="radio" name="categorylist" value="'.$categorycode.'">'.$description.'<br>';
51 my $sth=$dbh->prepare("select branchname,branchcode from branches order by branchname");
54 while (my ($branchname,$branchcode) = $sth->fetchrow) {
55 $brancheslist.="<option value=\"$branchcode\">$branchname</option>\n";
58 my $op = $query->param("op");
59 my $type=$query->param('type');
60 my $avail=$query->param('avail');
61 my $itemtypesstring=$query->param("itemtypesstring");
62 $itemtypesstring =~s/"//g;
63 my @itemtypes = split ( /\|/, $itemtypesstring);
64 my $branchesstring=$query->param("branchesstring");
65 $branchesstring =~s/"//g;
66 my @branches = split (/\|/, $branchesstring);
68 my $startfrom=$query->param('startfrom');
69 $startfrom=0 if(!defined $startfrom);
70 my ($template, $loggedinuser, $cookie);
74 if ($op eq "do_search") {
75 my @marclist = $query->param('marclist');
76 my @and_or = $query->param('and_or');
77 my @excluding = $query->param('excluding');
78 my @operator = $query->param('operator');
79 my @value = $query->param('value');
80 my $orderby = $query->param('orderby');
81 my $desc_or_asc = $query->param('desc_or_asc');
82 my $exactsearch = $query->param('exact');
83 for (my $i=0;$i<=$#marclist;$i++) {
84 if ($searchdesc) { # don't put the and_or on the 1st search term
85 $searchdesc .= $and_or[$i].$excluding[$i]." ".($marclist[$i]?$marclist[$i]:"*").$operator[$i].$value[$i] if ($value[$i]);
87 $searchdesc = $excluding[$i].($marclist[$i]?$marclist[$i]:"*").$operator[$i].$value[$i] if ($value[$i]);
88 if ($marclist[$i] eq "biblioitems.isbn") {
92 if ($itemtypesstring ne ''){
93 $searchdesc .= 'filtered by itemtypes ';
94 $searchdesc .= join(" ",@itemtypes)
97 if ($branchesstring ne ''){
98 $searchdesc .= ' in branches ';
99 $searchdesc .= join(" ",@branches)
102 $searchdesc .= '. Only available items shown.'
104 $resultsperpage= $query->param('resultsperpage');
105 $resultsperpage = 19 if(!defined $resultsperpage);
108 foreach (@operator) {
112 # builds tag and subfield arrays
115 foreach my $marc (@marclist) {
117 my ($tag,$subfield) = MARCfind_marc_from_kohafield($dbh,$marc,'');
119 push @tags,$dbh->quote("$tag$subfield");
121 push @tags, $dbh->quote(substr($marc,0,4));
127 findseealso($dbh,\@tags);
130 if ($itemtypesstring ne ''){
131 $sqlstring = 'and (biblioitems.itemtype IN (';
133 foreach my $itemtype (@itemtypes){
134 if ($itemtype ne ''){
135 if ($itemtypeloop != 0){
138 $sqlstring .= '"'.$itemtype.'"';
144 if ($branchesstring ne ''){
145 $sqlstring .= 'and biblio.biblionumber=items.biblionumber and (items.holdingbranch IN (';
147 $extratables = ',items';
148 foreach my $branch (@branches){
150 if ($branchesloop != 0){
153 $sqlstring .= '"'.$branch.'"';
160 $extratables .= ',items,issues,reserves';
161 $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) ";
163 my ($results,$total) = catalogsearch($dbh, \@tags,\@and_or,
164 \@excluding, \@operator, \@value,
165 $startfrom*$resultsperpage, $resultsperpage,$orderby,$desc_or_asc);
167 if (C4::Context->preference("BiblioDefaultView") eq "normal") {
168 print $query->redirect("/cgi-bin/koha/opac-detail.pl?bib=".@$results[0]->{biblionumber});
169 } elsif (C4::Context->preference("BiblioDefaultView") eq "marc") {
170 print $query->redirect("/cgi-bin/koha/opac-MARCdetail.pl?bib=".@$results[0]->{biblionumber});
172 print $query->redirect("/cgi-bin/koha/opac-ISBDdetail.pl?bib=".@$results[0]->{biblionumber});
176 ($template, $loggedinuser, $cookie)
177 = get_template_and_user({template_name => "opac-searchresults.tmpl",
180 authnotrequired => 1,
184 # multi page display gestion
186 my $displayprev=$startfrom;
187 if(($total - (($startfrom+1)*($resultsperpage))) > 0 ){
194 ## This next does a number of things:
195 # 1. It allows you to track all the searches made for stats, etc.
196 # 2. It fixes the 'searchdesc' variable problem by introducing
197 # a. 'searchterms' which comes out as 'Keyword: neal stephenson'
198 # b. 'phraseorterm' which comes out as 'neal stephenson'
199 # both of these are useful for differen purposes ... I use searchterms
200 # for display purposes and phraseorterm for passing the search terms
201 # to an external source through a url (like a database search)
202 # 3. It provides the variables necessary for the spellchecking (look below for
206 $totalresults = $total;
208 ## This formats the 'search results' string and populates
209 ## the 'OPLIN' variable as well as the 'spellcheck' variable
210 ## with appropriate values based on the user's search input
212 my $searchterms; #returned in place of searchdesc for 'results for search'
213 # as a string (can format if need be)
217 my %searchtypehash = ( # used only for the searchterms string formation
218 # and for spellcheck string
227 my @searchterm = $query->param('value');
229 for (my $i=0; $i <= $#searchterm; $i++) {
230 my $searchtype = $searchtypehash{$i};
231 push @spphrases, $searchterm[$i];
232 if ($searchterms) { #don't put and in again
233 if ($searchterm[$i]) {
234 $phraseorterm.=$searchterm[$i];
235 $searchterms.=" AND ".$searchtype." : \'".$searchterm[$i]."\'";
238 if ($searchterm[$i]) {
239 $phraseorterm.=$searchterm[$i];
240 $searchterms.=$searchtype.": \'".$searchterm[$i]."\'";
245 # Spellchecck stuff ... needs to use above scheme but must change
247 my $phrases = $query->param('value');
248 #my $searchterms = $query->param('value');
249 # warn "here is searchterms:".$searchterms;
251 # FIXME: should be obvious ;-)
252 #foreach my $phrases (@spphrases) {
253 $phrases =~ s/(\.|\?|\:|\!|\'|,|\-|\"|\(|\)|\[|\]|\{|\})/ /g;
254 $phrases =~ s/(\Athe |\Aa |\Aan |)//g;
255 my $spchkphraseorterm = $phraseorterm;
256 $spchkphraseorterm =~ tr/A-Z/a-z/;
257 $spchkphraseorterm =~ s/(\.|\?|\:|\!|\'|,|\-|\"|\(|\)|\[|\]|\{|\})/ /g;
258 $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;
259 $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;
261 $spchkphraseorterm =~s/ / /g;
262 my $resultcount = $total;
263 my $ipaddress = $query->remote_host();
267 #need to create a table to record the search info
268 #...FIXME: add the script name that creates the table
270 my $dbhpop=DBI->connect("DBI:mysql:demosuggest:localhost","auth","YourPass")) {
272 # insert the search info query
273 my $insertpop = "INSERT INTO phrase_log(phr_phrase,phr_resultcount,phr_ip) VALUES(?,?,?)";
275 # grab spelling suggestions query
276 my $getsugg = "SELECT display FROM spellcheck WHERE strcmp(soundex(suggestion), soundex(?)) = 0 order by soundex(suggestion) limit 0,5";
278 #get spelling suggestions when there are no results
279 if ($resultcount eq 0) {
280 my $sthgetsugg=$dbhpop->prepare($getsugg);
281 $sthgetsugg->execute($spchkphraseorterm);
282 while (my ($spsuggestion)=$sthgetsugg->fetchrow_array) {
283 # warn "==>$spsuggestion";
284 #push @spsuggest, +{ spsuggestion => $spsuggestion };
286 $line{spsuggestion} = $spsuggestion;
287 push @spsuggest,\%line;
290 # warn "==>".$#spsuggest;
293 # end of spelling suggestions
295 my $sthpop=$dbhpop->prepare($insertpop);
297 #$sthpop->execute($phrases,$resultcount,$ipaddress);
301 ### end of tracking stuff -- jmf at kados dot org
303 $template->param(suggest => $suggest );
304 $template->param( SPELL_SUGGEST => \@spsuggest );
305 $template->param( searchterms => $searchterms );
306 $template->param( phraseorterm => $phraseorterm );
307 #warn "here's the search terms: ".$searchterms;
309 ### end of spelling suggestions
312 for(my $i = 0 ; $i <= $#marclist ; $i++)
314 push @field_data, { term => "marclist", val=>$marclist[$i] };
315 push @field_data, { term => "and_or", val=>$and_or[$i] };
316 push @field_data, { term => "excluding", val=>$excluding[$i] };
317 push @field_data, { term => "operator", val=>$operator[$i] };
318 push @field_data, { term => "value", val=>$value[$i] };
320 push @field_data, {term => "desc_or_asc", val => $desc_or_asc} if $desc_or_asc;
321 push @field_data, {term => "orderby", val => $orderby} if $orderby;
324 if ($total>$resultsperpage)
326 for (my $i=1; $i<$total/$resultsperpage+1; $i++)
331 ($startfrom==($i-1)) && ($highlight=1);
332 push @numbers, { number => $i,
333 highlight => $highlight ,
334 searchdata=> \@field_data,
335 startfrom => ($i-1)};
340 my $from = $startfrom*$resultsperpage+1;
343 if($total < (($startfrom+1)*$resultsperpage))
347 $to = (($startfrom+1)*$resultsperpage);
349 my $defaultview = 'BiblioDefaultView'.C4::Context->preference('BiblioDefaultView');
350 $template->param(results => $results,
351 startfrom=> $startfrom,
352 displaynext=> $displaynext,
353 displayprev=> $displayprev,
354 resultsperpage => $resultsperpage,
356 startfromnext => $startfrom+1,
357 startfromprev => $startfrom-1,
358 searchdata=>\@field_data,
363 searchdesc=> $searchdesc,
365 suggestion => C4::Context->preference("suggestion"),
366 virtualshelves => C4::Context->preference("virtualshelves"),
370 ($template, $loggedinuser, $cookie)
371 = get_template_and_user({template_name => "opac-search.tmpl",
374 authnotrequired => 1,
378 my $query="Select itemtype,description from itemtypes order by description";
379 my $sth=$dbh->prepare($query);
383 while (my ($value,$lib) = $sth->fetchrow_array) {
384 my %row =( value => $value,
387 push @itemtypeloop, \%row;
392 my @oldselect_branch;
393 my %oldselect_branches;
394 my ($oldcount2,@oldbranches)=branches();
395 push @oldselect_branch, "";
396 $oldselect_branches{''} = "";
397 for (my $i=0;$i<$oldcount2;$i++){
398 push @oldselect_branch, $oldbranches[$i]->{'branchcode'};#
399 $oldselect_branches{$oldbranches[$i]->{'branchcode'}} = $oldbranches[$i]->{'branchname'};
401 my $CGIbranch=CGI::scrolling_list( -name => 'value',
402 -values => \@oldselect_branch,
403 -labels => \%oldselect_branches,
409 my $branches=getbranches();
410 push @select_branch, "";
411 $select_branches{''} = "";
412 foreach my $branch ( keys %$branches ){
413 push @select_branch, $branches->{$branch}->{'branchcode'};
414 $select_branches{$branches->{$branch}->{'branchcode'}} = $branches->{$branch}->{'branchname'};
416 my $CGIbranch=CGI::scrolling_list( -name => 'value',
417 -values => \@select_branch,
418 -labels => \%select_branches,
423 $template->param('Disable_Dictionary'=>C4::Context->preference("Disable_Dictionary")) if (C4::Context->preference("Disable_Dictionary"));
426 # CHRIS : Whats this?
427 # classlist => $classlist,
428 CGIitemtype => $CGIitemtype,
429 CGIbranch => $CGIbranch,
430 suggestion => C4::Context->preference("suggestion"),
431 virtualshelves => C4::Context->preference("virtualshelves"),
432 LibraryName => C4::Context->preference("LibraryName"),
433 OpacNav => C4::Context->preference("OpacNav"),
434 opaccredits => C4::Context->preference("opaccredits"),
435 AmazonContent => C4::Context->preference("AmazonContent"),
436 opacsmallimage => C4::Context->preference("opacsmallimage"),
437 opaclayoutstylesheet => C4::Context->preference("opaclayoutstylesheet"),
438 opaccolorstylesheet => C4::Context->preference("opaccolorstylesheet"),
442 if ($totalresults == 1){
443 # if its a barcode search by definition we will only have one result.
444 # And if we have a result
445 # lets jump straight to the detail.pl page
446 print $query->redirect("/cgi-bin/koha/opac-detail.pl?bib=$firstbiblionumber");
449 output_html_with_http_headers $query, $cookie, $template->output;