Baker and Taylor OPAC integration, plus some logic for Tags
[koha.git] / opac / opac-search.pl
1 #!/usr/bin/perl
2 # Script to perform searching
3 # Mostly copied from search.pl, see POD there
4 use strict;            # always use
5
6 ## STEP 1. Load things that are used in both search page and
7 # results page and decide which template to load, operations 
8 # to perform, etc.
9 ## load Koha modules
10 use C4::Context;
11 use C4::Output;
12 use C4::Auth;
13 use C4::Search;
14 use C4::Biblio;  # GetBiblioData
15 use C4::Koha;
16 use C4::Tags qw(get_tags);
17 use POSIX qw(ceil floor);
18 use C4::Branch; # GetBranches
19
20 # create a new CGI object
21 # FIXME: no_undef_params needs to be tested
22 use CGI qw('-no_undef_params');
23 my $cgi = new CGI;
24
25 BEGIN {
26         if (C4::Context->preference('BakerTaylorEnabled')) {
27                 require C4::External::BakerTaylor;
28                 import C4::External::BakerTaylor qw(&image_url &link_url);
29         }
30 }
31
32 my ($template,$borrowernumber,$cookie);
33
34 # decide which template to use
35 my $template_name;
36 my $template_type;
37 my @params = $cgi->param("limit");
38
39 my $build_grouped_results = C4::Context->preference('OPACGroupResults');
40 if ($build_grouped_results) {
41     $template_name = 'opac-results-grouped.tmpl';
42
43 elsif ((@params>=1) || ($cgi->param("q")) || ($cgi->param('multibranchlimit')) || ($cgi->param('limit-yr')) ) {
44     $template_name = 'opac-results.tmpl';
45 }
46 else {
47     $template_name = 'opac-advsearch.tmpl';
48     $template_type = 'advsearch';
49 }
50 # load the template
51 ($template, $borrowernumber, $cookie) = get_template_and_user({
52     template_name => $template_name,
53     query => $cgi,
54     type => "opac",
55     authnotrequired => 1,
56     }
57 );
58 if (C4::Context->preference("marcflavour") eq "UNIMARC" ) {
59     $template->param('UNIMARC' => 1);
60 }
61
62 if (C4::Context->preference('BakerTaylorEnabled')) {
63         $template->param(
64                 BakerTaylorEnabled  => 1,
65                 BakerTaylorImageURL => &image_url(),
66                 BakerTaylorLinkURL  => &link_url(),
67                 BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
68         );
69 }
70 if (C4::Context->preference('TagsEnabled')) {
71         $template->param(TagsEnabled => 1);
72         foreach (qw(TagsShowOnList TagsInputOnList)) {
73                 C4::Context->preference($_) and $template->param($_ => 1);
74         }
75 }
76
77 ## URI Re-Writing
78 # Deprecated, but preserved because it's interesting :-)
79 # The same thing can be accomplished with mod_rewrite in
80 # a more elegant way
81 #                  
82 #my $rewrite_flag;
83 #my $uri = $cgi->url(-base => 1);
84 #my $relative_url = $cgi->url(-relative=>1);
85 #$uri.="/".$relative_url."?";
86 #warn "URI:$uri";
87 #my @cgi_params_list = $cgi->param();
88 #my $url_params = $cgi->Vars;
89 #
90 #for my $each_param_set (@cgi_params_list) {
91 #    $uri.= join "",  map "\&$each_param_set=".$_, split("\0",$url_params->{$each_param_set}) if $url_params->{$each_param_set};
92 #}
93 #warn "New URI:$uri";
94 # Only re-write a URI if there are params or if it already hasn't been re-written
95 #unless (($cgi->param('r')) || (!$cgi->param()) ) {
96 #    print $cgi->redirect(     -uri=>$uri."&r=1",
97 #                            -cookie => $cookie);
98 #    exit;
99 #}
100
101 # load the branches
102 my $branches = GetBranches();
103 my @branch_loop;
104
105 for my $branch_hash (sort keys %$branches) {
106     push @branch_loop, {value => "$branch_hash" , branchname => $branches->{$branch_hash}->{'branchname'}, };
107 }
108
109 my $categories = GetBranchCategories(undef,'searchdomain');
110
111 $template->param(branchloop => \@branch_loop, searchdomainloop => $categories);
112
113 # load the itemtypes
114 my $itemtypes = GetItemTypes;
115 my @itemtypesloop;
116 my $selected=1;
117 my $cnt;
118 my $imgdir = getitemtypeimagesrc('opac');
119
120 foreach my $thisitemtype ( sort {$itemtypes->{$a}->{'description'} cmp $itemtypes->{$b}->{'description'} } keys %$itemtypes ) {
121     my %row =(  number=>$cnt++,
122                 imageurl=> $itemtypes->{$thisitemtype}->{'imageurl'}?($imgdir."/".$itemtypes->{$thisitemtype}->{'imageurl'}):"",
123                 code => $thisitemtype,
124                 selected => $selected,
125                 description => $itemtypes->{$thisitemtype}->{'description'},
126                 count5 => $cnt % 4,
127             );
128     $selected = 0 if ($selected) ;
129     push @itemtypesloop, \%row;
130 }
131 $template->param(itemtypeloop => \@itemtypesloop);
132
133 # # load the itypes (Called item types in the template -- just authorized values for searching)
134 # my ($itypecount,@itype_loop) = GetCcodes();
135 # $template->param(itypeloop=>\@itype_loop,);
136
137 # The following should only be loaded if we're bringing up the advanced search template
138 if ( $template_type eq 'advsearch' ) {
139
140     # load the servers (used for searching -- to do federated searching, etc.)
141     my $primary_servers_loop;# = displayPrimaryServers();
142     $template->param(outer_servers_loop =>  $primary_servers_loop,);
143     
144     my $secondary_servers_loop;# = displaySecondaryServers();
145     $template->param(outer_sup_servers_loop => $secondary_servers_loop,);
146     
147     # determine what to display next to the search boxes (ie, boolean option
148     # shouldn't appear on the first one, scan indexes should, adding a new
149     # box should only appear on the last, etc.
150     my @search_boxes_array;
151     my $search_boxes_count = C4::Context->preference("OPACAdvSearchInputCount") | 3; # FIXME: should be a syspref
152     for (my $i=1;$i<=$search_boxes_count;$i++) {
153         # if it's the first one, don't display boolean option, but show scan indexes
154         if ($i==1) {
155             push @search_boxes_array,
156                 {
157                 scan_index => 1,
158                 };
159         
160         }
161         # if it's the last one, show the 'add field' box
162         elsif ($i==$search_boxes_count) {
163             push @search_boxes_array,
164                 {
165                 boolean => 1,
166                 add_field => 1,
167                 };
168         }
169         else {
170             push @search_boxes_array,
171                 {
172                 boolean => 1,
173                 };
174         }
175
176     }
177     $template->param(uc(C4::Context->preference("marcflavour")) => 1,
178                                           advsearch => 1,
179                       search_boxes_loop => \@search_boxes_array);
180
181 # use the global setting by default
182         if ( C4::Context->preference("expandedSearchOption") == 1) {
183                 $template->param( expanded_options => C4::Context->preference("expandedSearchOption") );
184         }
185         # but let the user override it
186         if ( ($cgi->param('expanded_options') == 0) || ($cgi->param('expanded_options') == 1 ) ) {
187         $template->param( expanded_options => $cgi->param('expanded_options'));
188         }
189
190     output_html_with_http_headers $cgi, $cookie, $template->output;
191     exit;
192 }
193
194 ### OK, if we're this far, we're performing an actual search
195
196 # Fetch the paramater list as a hash in scalar context:
197 #  * returns paramater list as tied hash ref
198 #  * we can edit the values by changing the key
199 #  * multivalued CGI paramaters are returned as a packaged string separated by "\0" (null)
200 my $params = $cgi->Vars;
201 my $tag;
202 $tag = $params->{tag} if $params->{tag};
203
204 # Params that can have more than one value
205 # sort by is used to sort the query
206 # in theory can have more than one but generally there's just one
207 my @sort_by;
208 my $default_sort_by = C4::Context->preference('OPACdefaultSortField')."_".C4::Context->preference('OPACdefaultSortOrder') 
209     if (C4::Context->preference('OPACdefaultSortField') && C4::Context->preference('OPACdefaultSortOrder'));
210
211 @sort_by = split("\0",$params->{'sort_by'}) if $params->{'sort_by'};
212 $sort_by[0] = $default_sort_by unless $sort_by[0];
213 foreach my $sort (@sort_by) {
214     $template->param($sort => 1);
215 }
216 $template->param('sort_by' => $sort_by[0]);
217
218 # Use the servers defined, or just search our local catalog(default)
219 my @servers;
220 @servers = split("\0",$params->{'server'}) if $params->{'server'};
221 unless (@servers) {
222     #FIXME: this should be handled using Context.pm
223     @servers = ("biblioserver");
224     # @servers = C4::Context->config("biblioserver");
225 }
226
227 # operators include boolean and proximity operators and are used
228 # to evaluate multiple operands
229 my @operators;
230 @operators = split("\0",$params->{'op'}) if $params->{'op'};
231
232 # indexes are query qualifiers, like 'title', 'author', etc. They
233 # can be single or multiple parameters separated by comma: kw,right-Truncation 
234 my @indexes = split("\0",$params->{'idx'});
235
236 # if a simple index (only one)  display the index used in the top search box
237 if ($indexes[0] && !$indexes[1]) {
238     $template->param("ms_".$indexes[0] => 1);
239 }
240 # an operand can be a single term, a phrase, or a complete ccl query
241 my @operands;
242 @operands = split("\0",$params->{'q'}) if $params->{'q'};
243
244 # if a simple search, display the value in the search box
245 if ($operands[0] && !$operands[1]) {
246     $template->param(ms_value => $operands[0]);
247 }
248
249 # limits are use to limit to results to a pre-defined category such as branch or language
250 my @limits;
251 @limits = split("\0",$params->{'limit'}) if $params->{'limit'};
252
253 if($params->{'multibranchlimit'}) {
254 push @limits, join(" or ", map { "branch: $_ "}  @{GetBranchesInCategory($params->{'multibranchlimit'})}) ;
255 }
256
257 my $available;
258 foreach my $limit(@limits) {
259     if ($limit =~/available/) {
260         $available = 1;
261     }
262 }
263 $template->param(available => $available);
264
265 # append year limits if they exist
266 if ($params->{'limit-yr'}) {
267     if ($params->{'limit-yr'} =~ /\d{4}-\d{4}/) {
268         my ($yr1,$yr2) = split(/-/, $params->{'limit-yr'});
269         push @limits, "yr,st-numeric,ge=$yr1 and yr,st-numeric,le=$yr2";
270     }
271     elsif ($params->{'limit-yr'} =~ /\d{4}/) {
272         push @limits, "yr,st-numeric=$params->{'limit-yr'}";
273     }
274     else {
275         #FIXME: Should return a error to the user, incorect date format specified
276     }
277 }
278
279 # Params that can only have one value
280 my $scan = $params->{'scan'};
281 my $count = C4::Context->preference('OPACnumSearchResults') || 20;
282 my $results_per_page = $params->{'count'} || $count;
283 my $offset = $params->{'offset'} || 0;
284 my $page = $cgi->param('page') || 1;
285 #my $offset = ($page-1)*$results_per_page;
286 my $hits;
287 my $expanded_facet = $params->{'expand'};
288
289 # Define some global variables
290 my ($error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$stopwords_removed,$query_type);
291
292 my @results;
293
294 ## I. BUILD THE QUERY
295 ( $error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$stopwords_removed,$query_type) = buildQuery(\@operators,\@operands,\@indexes,\@limits,\@sort_by);
296
297 sub _input_cgi_parse ($) { 
298     my @elements;
299     for my $this_cgi ( split('&',shift) ) {
300         next unless $this_cgi;
301         $this_cgi =~ /(.*)=(.*)/;
302         push @elements, { input_name => $1, input_value => $2 };
303     }
304     return @elements;
305 }
306
307 ## parse the query_cgi string and put it into a form suitable for <input>s
308 my @query_inputs = _input_cgi_parse($query_cgi);
309 $template->param ( QUERY_INPUTS => \@query_inputs );
310
311 ## parse the limit_cgi string and put it into a form suitable for <input>s
312 my @limit_inputs = _input_cgi_parse($limit_cgi);
313
314 # add OPAC 'hidelostitems'
315 if (C4::Context->preference('hidelostitems') == 1) {
316     # either lost ge 0 or no value in the lost register
317     $query ="($query) and ( (lost,st-numeric <= 0) or ( allrecords,AlwaysMatches='' not lost,AlwaysMatches='') )";
318 }
319
320 # add OPAC suppression - requires at least one item indexed with Suppress
321 if (C4::Context->preference('OpacSuppression')) {
322     $query = "($query) not Suppress=1";
323 }
324
325 $template->param ( LIMIT_INPUTS => \@limit_inputs );
326
327 ## II. DO THE SEARCH AND GET THE RESULTS
328 my $total; # the total results for the whole set
329 my $facets; # this object stores the faceted results that display on the left-hand of the results page
330 my @results_array;
331 my $results_hashref;
332
333 if ($tag) {
334         my $taglist = get_tags({term=>$tag});
335         $results_hashref->{biblioserver}->{hits} = scalar (@$taglist);
336         my @biblist  = (map {GetBiblioData($_->{biblionumber})} @$taglist);
337         my @marclist = (map {$_->{marc}} @biblist );
338         $DEBUG and printf STDERR "taglist (%s biblionumber)\nmarclist (%s records)\n", scalar(@$taglist), scalar(@marclist);
339         $results_hashref->{biblioserver}->{RECORDS} = \@marclist;
340         # FIXME: tag search and standard search should work together, not exclusively
341         # FIXME: No facets for tags search.
342 }
343 elsif (C4::Context->preference('NoZebra')) {
344     eval {
345         ($error, $results_hashref, $facets) = NZgetRecords($query,$simple_query,\@sort_by,\@servers,$results_per_page,$offset,$expanded_facet,$branches,$query_type,$scan);
346     };
347 } elsif ($build_grouped_results) {
348     eval {
349         ($error, $results_hashref, $facets) = C4::Search::pazGetRecords($query,$simple_query,\@sort_by,\@servers,$results_per_page,$offset,$expanded_facet,$branches,$query_type,$scan);
350     };
351 } else {
352     eval {
353         ($error, $results_hashref, $facets) = getRecords($query,$simple_query,\@sort_by,\@servers,$results_per_page,$offset,$expanded_facet,$branches,$query_type,$scan);
354     };
355 }
356 use Data::Dumper;
357 print STDERR "-" x 25, "\n", Dumper($results_hashref);
358 if ($@ || $error) {
359     $template->param(query_error => $error.$@);
360     output_html_with_http_headers $cgi, $cookie, $template->output;
361     exit;
362 }
363
364 # At this point, each server has given us a result set
365 # now we build that set for template display
366 my @sup_results_array;
367 for (my $i=0;$i<=@servers;$i++) {
368     my $server = $servers[$i];
369     if ($server =~/biblioserver/) { # this is the local bibliographic server
370         $hits = $results_hashref->{$server}->{"hits"};
371         my $page = $cgi->param('page') || 0;
372         my @newresults;
373         if ($build_grouped_results) {
374             foreach my $group (@{ $results_hashref->{$server}->{"GROUPS"} }) {
375                 # because pazGetRecords handles retieving only the records
376                 # we want as specified by $offset and $results_per_page,
377                 # we need to set the offset parameter of searchResults to 0
378                 my @group_results = searchResults( $query_desc, $group->{'group_count'},$results_per_page, 0,
379                                                    @{ $group->{"RECORDS"} });
380                 push @newresults, { group_label => $group->{'group_label'}, GROUP_RESULTS => \@group_results };
381             }
382         } else {
383             @newresults = searchResults( $query_desc,$hits,$results_per_page,$offset,@{$results_hashref->{$server}->{"RECORDS"}});
384         }
385                 my $tag_quantity;
386                 if (C4::Context->preference('TagsEnabled') and
387                         $tag_quantity = C4::Context->preference('TagsShowOnList')) {
388                         foreach (@newresults) {
389                                 my $bibnum = $_->{biblionumber} or next;
390                                 $_ ->{'TagLoop'} = get_tags({biblionumber=>$bibnum, 'sort'=>'-weight',
391                                                                                 limit=>$tag_quantity });
392                         }
393                 }
394                 foreach (@newresults) {
395                         my $clean = $_->{isbn} or next;
396                         unless (
397                                 $clean =~ /\b(\d{13})\b/ or
398                                 $clean =~ /\b(\d{10})\b/ or 
399                                 $clean =~ /\b(\d{9}X)\b/i
400                         ) {
401                                 next;
402                         }
403                         $_ ->{'clean_isbn'} = $1;
404                 }
405         $total = $total + $results_hashref->{$server}->{"hits"};
406         ## If there's just one result, redirect to the detail page
407         if ($total == 1) {         
408             my $biblionumber=@newresults[0]->{biblionumber};
409             print $cgi->redirect("/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber");
410             exit;
411         }
412         if ($hits) {
413             $template->param(total => $hits);
414             my $limit_cgi_not_availablity = $limit_cgi;
415             $limit_cgi_not_availablity =~ s/&limit=available//g;
416             $template->param(limit_cgi_not_availablity => $limit_cgi_not_availablity);
417             $template->param(limit_cgi => $limit_cgi);
418             $template->param(query_cgi => $query_cgi);
419             $template->param(query_desc => $query_desc);
420             $template->param(limit_desc => $limit_desc);
421             if ($query_desc || $limit_desc) {
422                 $template->param(searchdesc => 1);
423             }
424             $template->param(stopwords_removed => "@$stopwords_removed") if $stopwords_removed;
425             $template->param(results_per_page =>  $results_per_page);
426             $template->param(SEARCH_RESULTS => \@newresults,
427                                 OPACItemsResultsDisplay => (C4::Context->preference("OPACItemsResultsDisplay") eq "itemdetails"?1:0),
428                             );
429             ## Build the page numbers on the bottom of the page
430             my @page_numbers;
431             # total number of pages there will be
432             my $pages = ceil($hits / $results_per_page);
433             # default page number
434             my $current_page_number = 1;
435             $current_page_number = ($offset / $results_per_page + 1) if $offset;
436             my $previous_page_offset = $offset - $results_per_page unless ($offset - $results_per_page <0);
437             my $next_page_offset = $offset + $results_per_page;
438             # If we're within the first 10 pages, keep it simple
439             #warn "current page:".$current_page_number;
440             if ($current_page_number < 10) {
441                 # just show the first 10 pages
442                 # Loop through the pages
443                 my $pages_to_show = 10;
444                 $pages_to_show = $pages if $pages<10;
445                 for ($i=1; $i<=$pages_to_show;$i++) {
446                     # the offset for this page
447                     my $this_offset = (($i*$results_per_page)-$results_per_page);
448                     # the page number for this page
449                     my $this_page_number = $i;
450                     # it should only be highlighted if it's the current page
451                     my $highlight = 1 if ($this_page_number == $current_page_number);
452                     # put it in the array
453                     push @page_numbers, { offset => $this_offset, pg => $this_page_number, highlight => $highlight, sort_by => join " ",@sort_by };
454                                 
455                 }
456                         
457             }
458             # now, show twenty pages, with the current one smack in the middle
459             else {
460                 for ($i=$current_page_number; $i<=($current_page_number + 20 );$i++) {
461                     my $this_offset = ((($i-9)*$results_per_page)-$results_per_page);
462                     my $this_page_number = $i-9;
463                     my $highlight = 1 if ($this_page_number == $current_page_number);
464                     if ($this_page_number <= $pages) {
465                         push @page_numbers, { offset => $this_offset, pg => $this_page_number, highlight => $highlight, sort_by => join " ",@sort_by };
466                     }
467                 }
468                         
469             }
470             $template->param(   PAGE_NUMBERS => \@page_numbers,
471                                 previous_page_offset => $previous_page_offset) unless $pages < 2;
472             $template->param(next_page_offset => $next_page_offset) unless $pages eq $current_page_number;
473          }
474         # no hits
475         else {
476             $template->param(searchdesc => 1,query_desc => $query_desc,limit_desc => $limit_desc);
477         }
478     } # end of the if local
479     # asynchronously search the authority server
480     elsif ($server =~/authorityserver/) { # this is the local authority server
481         my @inner_sup_results_array;
482         for my $sup_record ( @{$results_hashref->{$server}->{"RECORDS"}} ) {
483             my $marc_record_object = MARC::Record->new_from_usmarc($sup_record);
484             my $title_field = $marc_record_object->field(100);
485              warn "Authority Found: ".$marc_record_object->as_formatted();
486             push @inner_sup_results_array, {
487                 'title' => $title_field->subfield('a'),
488                 'link' => "&amp;idx=an&amp;q=".$marc_record_object->field('001')->as_string(),
489             };
490         }
491         my $servername = $server;
492         push @sup_results_array, {  servername => $servername,
493                                     inner_sup_results_loop => \@inner_sup_results_array} if @inner_sup_results_array;
494     }
495     # FIXME: can add support for other targets as needed here
496     $template->param(           outer_sup_results_loop => \@sup_results_array);
497 } #/end of the for loop
498 #$template->param(FEDERATED_RESULTS => \@results_array);
499
500 $template->param(
501             #classlist => $classlist,
502             total => $total,
503             opacfacets => 1,
504             facets_loop => $facets,
505             scan => $scan,
506             search_error => $error,
507 );
508
509 if ($query_desc || $limit_desc) {
510     $template->param(searchdesc => 1);
511 }
512
513 ## Now let's find out if we have any supplemental data to show the user
514 #  and in the meantime, save the current query for statistical purposes, etc.
515 my $koha_spsuggest; # a flag to tell if we've got suggestions coming from Koha
516 my @koha_spsuggest; # place we store the suggestions to be returned to the template as LOOP
517 my $phrases = $query_desc;
518 my $ipaddress;
519
520 if ( C4::Context->preference("kohaspsuggest") ) {
521         my ($suggest_host, $suggest_dbname, $suggest_user, $suggest_pwd) = split(':', C4::Context->preference("kohaspsuggest"));
522         eval {
523             my $koha_spsuggest_dbh;
524             # FIXME: this needs to be moved to Context.pm
525             eval {
526                 $koha_spsuggest_dbh=DBI->connect("DBI:mysql:$suggest_dbname:$suggest_host","$suggest_user","$suggest_pwd");
527             };
528             if ($@) { 
529                 warn "can't connect to spsuggest db";
530             }
531             else {
532                 my $koha_spsuggest_insert = "INSERT INTO phrase_log(phr_phrase,phr_resultcount,phr_ip) VALUES(?,?,?)";
533                 my $koha_spsuggest_query = "SELECT display FROM distincts WHERE strcmp(soundex(suggestion), soundex(?)) = 0 order by soundex(suggestion) limit 0,5";
534                 my $koha_spsuggest_sth = $koha_spsuggest_dbh->prepare($koha_spsuggest_query);
535                 $koha_spsuggest_sth->execute($phrases);
536                 while (my $spsuggestion = $koha_spsuggest_sth->fetchrow_array) {
537                     $spsuggestion =~ s/(:|\/)//g;
538                     my %line;
539                     $line{spsuggestion} = $spsuggestion;
540                     push @koha_spsuggest,\%line;
541                     $koha_spsuggest = 1;
542                 }
543
544                 # Now save the current query
545                 $koha_spsuggest_sth=$koha_spsuggest_dbh->prepare($koha_spsuggest_insert);
546                 #$koha_spsuggest_sth->execute($phrases,$results_per_page,$ipaddress);
547                 $koha_spsuggest_sth->finish;
548
549                 $template->param( koha_spsuggest => $koha_spsuggest ) unless $hits;
550                 $template->param( SPELL_SUGGEST => \@koha_spsuggest,
551                 );
552             }
553     };
554     if ($@) {
555             warn "Kohaspsuggest failure:".$@;
556     }
557 }
558
559 # VI. BUILD THE TEMPLATE
560 output_html_with_http_headers $cgi, $cookie, $template->output;