fix limit-yr compatibility with sort_by option
[koha.git] / catalogue / search.pl
1 #!/usr/bin/perl
2 # Script to perform searching
3 # For documentation try 'perldoc /path/to/search'
4 #
5 # $Header$
6 #
7 # Copyright 2006 LibLime
8 #
9 # This file is part of Koha
10 #
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA  02111-1307 USA
23
24 =head1 NAME
25
26 search - a search script for finding records in a Koha system (Version 3.0)
27
28 =head1 OVERVIEW
29
30 This script contains a new search API for Koha 3.0. It is designed to be 
31 simple to use and configure, yet capable of performing feats like stemming,
32 field weighting, relevance ranking, support for multiple  query language
33 formats (CCL, CQL, PQF), full or nearly full support for the
34 bib1 attribute set, extended attribute sets defined in Zebra profiles, access
35 to the full range of Z39.50 query options, federated searches on Z39.50
36 targets, etc.
37
38 I believe the API as represented in this script is mostly sound, even if the
39 individual functions in Search.pm and Koha.pm need to be cleaned up. Of course,
40 you are free to disagree :-)
41
42 I will attempt to describe what is happening at each part of this script.
43 -- JF
44
45 =head2 INTRO
46
47 This script performs two functions:
48
49 =over 
50
51 =item 1. interacts with Koha to retrieve and display the results of a search
52
53 =item 2. loads the advanced search page
54
55 =back
56
57 These two functions share many of the same variables and modules, so the first
58 task is to load what they have in common and determine which template to use.
59 Once determined, proceed to only load the variables and procedures necessary
60 for that function.
61
62 =head2 THE ADVANCED SEARCH PAGE
63
64 If we're loading the advanced search page this script will call a number of
65 display* routines which populate objects that are sent to the template for 
66 display of things like search indexes, languages, search limits, branches,
67 etc. These are not stored in the template for two reasons:
68
69 =over
70
71 =item 1. Efficiency - we have more control over objects inside the script, 
72 and it's possible to not duplicate things like indexes (if the search indexes 
73 were stored in the template they would need to be repeated)
74
75 =item 2. Customization - if these elements were moved to the sql database it 
76 would allow a simple librarian to determine which fields to display on the page 
77 without editing any html (also how the fields should behave when being searched).
78
79 =back
80
81 However, they create one problem : the strings aren't translated. I have an idea
82 for how to do this that I will purusue soon.
83
84 =head2 PERFORMING A SEARCH
85
86 If we're performing a search, this script  performs three primary
87 operations:
88
89 =over 
90
91 =item 1. builds query strings (yes, plural)
92
93 =item 2. perform the search and return the results array
94
95 =item 3. build the HTML for output to the template
96
97 =back
98
99 There are several additional secondary functions performed that I will
100 not cover in detail.
101
102 =head3 1. Building Query Strings
103     
104 There are several types of queries needed in the process of search and retrieve:
105
106 =over
107
108 =item 1 Koha query - the query that is passed to Zebra
109
110 This is the most complex query that needs to be built. The original design goal 
111 was to use a custom CCL2PQF query parser to translate an incoming CCL query into
112 a multi-leaf query to pass to Zebra. It needs to be multi-leaf to allow field 
113 weighting, koha-specific relevance ranking, and stemming. When I have a chance 
114 I'll try to flesh out this section to better explain.
115
116 This query incorporates query profiles that aren't compatible with non-Zebra 
117 Z39.50 targets to acomplish the field weighting and relevance ranking.
118
119 =item 2 Federated query - the query that is passed to other Z39.50 targets
120
121 This query is just the user's query expressed in CCL CQL, or PQF for passing to a 
122 non-zebra Z39.50 target (one that doesn't support the extended profile that Zebra does).
123
124 =item 3 Search description - passed to the template / saved for future refinements of 
125 the query (by user)
126
127 This is a simple string that completely expresses the query in a way that can be parsed 
128 by Koha for future refinements of the query or as a part of a history feature. It differs
129 from the human search description:
130
131 1. it does not contain commas or = signs
132
133 =item 4 Human search description - what the user sees in the search_desc area
134
135 This is a simple string nearly identical to the Search description, but more human 
136 readable. It will contain = signs or commas, etc.
137
138 =back
139
140 =head3 2. Perform the Search
141
142 This section takes the query strings and performs searches on the named servers, including
143 the Koha Zebra server, stores the results in a deeply nested object, builds 'faceted results',
144 and returns these objects.
145
146 =head3 3. Build HTML
147
148 The final major section of this script takes the objects collected thusfar and builds the
149 HTML for output to the template and user.
150
151 =head3 Additional Notes
152
153 Not yet completed...
154
155 =cut
156
157 use strict;            # always use
158
159 ## STEP 1. Load things that are used in both search page and
160 # results page and decide which template to load, operations 
161 # to perform, etc.
162 ## load Koha modules
163 use C4::Context;
164 use C4::Output;
165 use C4::Auth;
166 use C4::Search;
167 use C4::Languages; # getAllLanguages
168 use C4::Koha;
169 use POSIX qw(ceil floor);
170 use C4::Branch; # GetBranches
171 # create a new CGI object
172 # not sure undef_params option is working, need to test
173 use CGI qw('-no_undef_params');
174 my $cgi = new CGI;
175
176 my ($template,$borrowernumber,$cookie);
177
178 # decide which template to use
179 my $template_name;
180 my $template_type;
181 my @params = $cgi->param("limit");
182 if ((@params>=1) || ($cgi->param("q")) || ($cgi->param('multibranchlimit')) || ($cgi->param('limit-yr')) ) {
183     $template_name = 'catalogue/results.tmpl';
184 }
185 else {
186     $template_name = 'catalogue/advsearch.tmpl';
187         $template_type = 'advsearch';
188 }
189 # load the template
190 ($template, $borrowernumber, $cookie) = get_template_and_user({
191     template_name => $template_name,
192     query => $cgi,
193     type => "intranet",
194     authnotrequired => 0,
195     flagsrequired   => { catalogue => 1 },
196     }
197 );
198 if (C4::Context->preference("marcflavour") eq "UNIMARC" ) {
199         $template->param('UNIMARC' => 1);
200 }
201
202 =head1 BUGS and FIXMEs
203
204 There are many, most are documented in the code. The one that
205 isn't fully documented, but referred to is the need for a full
206 query parser.
207
208 =cut
209
210 ## URI Re-Writing
211 # Deprecated, but preserved because it's interesting :-)
212 #my $rewrite_flag;
213 #my $uri = $cgi->url(-base => 1);
214 #my $relative_url = $cgi->url(-relative=>1);
215 #$uri.="/".$relative_url."?";
216 #warn "URI:$uri";
217 #my @cgi_params_list = $cgi->param();
218 #my $url_params = $cgi->Vars;
219 #
220 #for my $each_param_set (@cgi_params_list) {
221 #    $uri.= join "",  map "\&$each_param_set=".$_, split("\0",$url_params->{$each_param_set}) if $url_params->{$each_param_set};
222 #}
223 #warn "New URI:$uri";
224 # Only re-write a URI if there are params or if it already hasn't been re-written
225 #unless (($cgi->param('r')) || (!$cgi->param()) ) {
226 #    print $cgi->redirect(     -uri=>$uri."&r=1",
227 #                            -cookie => $cookie);
228 #    exit;
229 #}
230
231 # load the branches
232 my $branches = GetBranches();
233 my @branch_loop;
234 #push @branch_loop, {value => "", branchname => "All Branches", };
235 for my $branch_hash (sort keys %$branches) {
236     push @branch_loop, {value => "$branch_hash" , branchname => $branches->{$branch_hash}->{'branchname'}, };
237 }
238
239 my $categories = GetBranchCategories(undef,'searchdomain');
240
241 $template->param(branchloop => \@branch_loop, searchdomainloop => $categories);
242
243 # load the itemtypes
244 my $itemtypes = GetItemTypes;
245 my @itemtypesloop;
246 my $selected=1;
247 my $cnt;
248 my $imgdir = getitemtypeimagesrc();
249 foreach my $thisitemtype ( sort {$itemtypes->{$a}->{'description'} cmp $itemtypes->{$b}->{'description'} } keys %$itemtypes ) {
250     my %row =(  number=>$cnt++,
251                 imageurl=> $itemtypes->{$thisitemtype}->{'imageurl'}?($imgdir."/".$itemtypes->{$thisitemtype}->{'imageurl'}):"",
252                 code => $thisitemtype,
253                 selected => $selected,
254                 description => $itemtypes->{$thisitemtype}->{'description'},
255                 count5 => $cnt % 4,
256             );
257     $selected = 0 if ($selected) ;
258     push @itemtypesloop, \%row;
259 }
260 $template->param(itemtypeloop => \@itemtypesloop);
261
262 # # load the itypes (Called item types in the template -- just authorized values for searching)
263 # my ($itypecount,@itype_loop) = GetCcodes();
264 # $template->param(itypeloop=>\@itype_loop,);
265
266 # load the languages ( for switching from one template to another )
267 $template->param(languages_loop => getTranslatedLanguages('intranet','prog'));
268
269 # The following should only be loaded if we're bringing up the advanced search template
270 if ( $template_type eq 'advsearch' ) {
271     # load the servers (used for searching -- to do federated searching, etc.)
272     my $primary_servers_loop;# = displayPrimaryServers();
273     $template->param(outer_servers_loop =>  $primary_servers_loop,);
274     
275     my $secondary_servers_loop;# = displaySecondaryServers();
276     $template->param(outer_sup_servers_loop => $secondary_servers_loop,);
277     
278     # determine what to display next to the search boxes (ie, boolean option
279     # shouldn't appear on the first one, scan indexes should, adding a new
280     # box should only appear on the last, etc.
281     my @search_boxes_array;
282     my $search_boxes_count = C4::Context->preference("OPACAdvSearchInputCount") | 3; # FIXME: should be a syspref
283     for (my $i=1;$i<=$search_boxes_count;$i++) {
284                 # if it's the first one, don't display boolean option, but show scan indexes
285         if ($i==1) {
286             push @search_boxes_array,
287                 {
288                 scan_index => 1,
289                 };
290         
291         }
292                 # if it's the last one, show the 'add field' box
293         elsif ($i==$search_boxes_count) {
294             push @search_boxes_array,
295                 {
296                                 boolean => 1,
297                 add_field => 1,
298                                 };
299         }
300                 else {
301                         push @search_boxes_array,
302                                 {
303                                 boolean => 1,
304                                 };
305                 }
306
307     }
308     $template->param(uc(C4::Context->preference("marcflavour")) => 1,
309                       search_boxes_loop => \@search_boxes_array);
310
311     # load the language limits (for search)
312     my $languages_limit_loop = getAllLanguages();
313     $template->param(search_languages_loop => $languages_limit_loop,);
314
315         # use the global setting by default
316         if ( C4::Context->preference("expandedSearchOption") == 1) {
317                 $template->param( expanded_options => C4::Context->preference("expandedSearchOption") );
318         }
319         # but let the user override it
320         if ( ($cgi->param('expanded_options') == 0) || ($cgi->param('expanded_options') == 1 ) ) {
321         $template->param( expanded_options => $cgi->param('expanded_options'));
322         }
323
324     output_html_with_http_headers $cgi, $cookie, $template->output;
325     exit;
326 }
327
328 ### OK, if we're this far, we're performing an actual search
329
330 # Fetch the paramater list as a hash in scalar context:
331 #  * returns paramater list as tied hash ref
332 #  * we can edit the values by changing the key
333 #  * multivalued CGI paramaters are returned as a packaged string separated by "\0" (null)
334 my $params = $cgi->Vars;
335
336 # Params that can have more than one value
337 # sort by is used to sort the query
338 # in theory can have more than one but generally there's just one
339 my @sort_by;
340 my $default_sort_by = C4::Context->preference('defaultSortField')."_".C4::Context->preference('defaultSortOrder') 
341         if (C4::Context->preference('defaultSortField') && C4::Context->preference('defaultSortOrder'));
342
343 @sort_by = split("\0",$params->{'sort_by'}) if $params->{'sort_by'};
344 $sort_by[0] = $default_sort_by unless $sort_by[0];
345 foreach my $sort (@sort_by) {
346         $template->param($sort => 1);
347 }
348 $template->param('sort_by' => $sort_by[0]);
349
350 # Use the servers defined, or just search our local catalog(default)
351 my @servers;
352 @servers = split("\0",$params->{'server'}) if $params->{'server'};
353 unless (@servers) {
354     #FIXME: this should be handled using Context.pm
355     @servers = ("biblioserver");
356     # @servers = C4::Context->config("biblioserver");
357 }
358
359 # operators include boolean and proximity operators and are used
360 # to evaluate multiple operands
361 my @operators;
362 @operators = split("\0",$params->{'op'}) if $params->{'op'};
363
364 # indexes are query qualifiers, like 'title', 'author', etc. They
365 # can be single or multiple parameters separated by comma: kw,right-Truncation 
366 my @indexes;
367 @indexes = split("\0",$params->{'idx'});
368
369 # an operand can be a single term, a phrase, or a complete ccl query
370 my @operands;
371 @operands = split("\0",$params->{'q'}) if $params->{'q'};
372
373 # limits are use to limit to results to a pre-defined category such as branch or language
374 my @limits;
375 @limits = split("\0",$params->{'limit'}) if $params->{'limit'};
376
377 if($params->{'multibranchlimit'}) {
378 push @limits, join(" or ", map { "branch: $_ "}  @{GetBranchesInCategory($params->{'multibranchlimit'})}) ;
379 }
380
381 my $available;
382 foreach my $limit(@limits) {
383     if ($limit =~/available/) {
384         $available = 1;
385     }
386 }
387 $template->param(available => $available);
388
389 # append year limits if they exist
390 my $limit_yr;
391 my $limit_yr_value;
392 if ($params->{'limit-yr'}) {
393         if ($params->{'limit-yr'} =~ /\d{4}-\d{4}/) {
394                 my ($yr1,$yr2) = split(/-/, $params->{'limit-yr'});
395                 $limit_yr = "yr,st-numeric,ge=$yr1 and yr,st-numeric,le=$yr2";
396                 $limit_yr_value = "$yr1-$yr2";
397         }
398         elsif ($params->{'limit-yr'} =~ /\d{4}/) {
399                 $limit_yr = "yr,st-numeric=$params->{'limit-yr'}";
400                 $limit_yr_value = $params->{'limit-yr'};
401         }
402         push @limits,$limit_yr;
403         #FIXME: Should return a error to the user, incorect date format specified
404 }
405
406 # Params that can only have one value
407 my $scan = $params->{'scan'};
408 my $count = C4::Context->preference('numSearchResults') || 20;
409 my $results_per_page = $params->{'count'} || $count;
410 my $offset = $params->{'offset'} || 0;
411 my $page = $cgi->param('page') || 1;
412 #my $offset = ($page-1)*$results_per_page;
413 my $hits;
414 my $expanded_facet = $params->{'expand'};
415
416 # Define some global variables
417 my ( $error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$stopwords_removed,$query_type);
418
419 my @results;
420
421 ## I. BUILD THE QUERY
422 ( $error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$stopwords_removed,$query_type) = buildQuery(\@operators,\@operands,\@indexes,\@limits,\@sort_by,$scan);
423
424 ## parse the query_cgi string and put it into a form suitable for <input>s
425 my @query_inputs;
426 for my $this_cgi ( split('&',$query_cgi) ) {
427         next unless $this_cgi;
428         $this_cgi =~ m/(.*=)(.*)/;
429         my $input_name = $1;
430         my $input_value = $2;
431         $input_name =~ s/=$//;
432         push @query_inputs, { input_name => $input_name, input_value => $input_value };
433 }
434 $template->param ( QUERY_INPUTS => \@query_inputs );
435
436 ## parse the limit_cgi string and put it into a form suitable for <input>s
437 my @limit_inputs;
438 for my $this_cgi ( split('&',$limit_cgi) ) {
439         next unless $this_cgi;
440         # handle special case limit-yr
441         if ($this_cgi =~ /yr,st-numeric/) {
442                 push @limit_inputs, { input_name => 'limit-yr', input_value => $limit_yr_value };       
443                 next;
444         }
445     $this_cgi =~ m/(.*=)(.*)/;
446     my $input_name = $1;
447     my $input_value = $2;
448     $input_name =~ s/=$//;
449     push @limit_inputs, { input_name => $input_name, input_value => $input_value };
450 }
451 $template->param ( LIMIT_INPUTS => \@limit_inputs );
452
453 ## II. DO THE SEARCH AND GET THE RESULTS
454 my $total; # the total results for the whole set
455 my $facets; # this object stores the faceted results that display on the left-hand of the results page
456 my @results_array;
457 my $results_hashref;
458
459 if (C4::Context->preference('NoZebra')) {
460     $query=~s/yr(:|=)\s*([\d]{1,4})-([\d]{1,4})/(yr>=$2 and yr<=$3)/g;
461     $simple_query=~s/yr\s*(:|=)([\d]{1,4})-([\d]{1,4})/(yr>=$2 and yr<=$3)/g;
462     warn $query; 
463     eval {
464         ($error, $results_hashref, $facets) = NZgetRecords($query,$simple_query,\@sort_by,\@servers,$results_per_page,$offset,$expanded_facet,$branches,$query_type,$scan);
465     };
466 } else {
467     eval {
468         ($error, $results_hashref, $facets) = getRecords($query,$simple_query,\@sort_by,\@servers,$results_per_page,$offset,$expanded_facet,$branches,$query_type,$scan);
469     };
470 }
471 if ($@ || $error) {
472     $template->param(query_error => $error.$@);
473
474     output_html_with_http_headers $cgi, $cookie, $template->output;
475     exit;
476 }
477
478 # FIXME: This belongs in tools/ not in the primary search results page
479 my $op=$cgi->param("operation");
480 if ($op eq "bulkedit"){
481         my ($countchanged,$listunchanged)=
482         ModBiblios($results_hashref->{'biblioserver'}->{"RECORDS"},
483                       $params->{"tagsubfield"},
484                       $params->{"inputvalue"},
485                       $params->{"targetvalue"},
486                       $params->{"test"}
487                       );
488         $template->param(bulkeditresults=>1,
489                       tagsubfield=>$params->{"tagsubfield"},
490                       inputvalue=>$params->{"inputvalue"},
491                       targetvalue=>$params->{"targetvalue"},
492                       countchanged=>$countchanged,
493                       countunchanged=>scalar(@$listunchanged),
494                       listunchanged=>$listunchanged);
495
496         if (C4::Context->userenv->{'flags'}==1 ||(C4::Context->userenv->{'flags'} & ( 2**9 ) )){
497         #Edit Catalogue Permissions
498                 $template->param(bulkedit => 1);
499                 $template->param(tagsubfields=>GetManagedTagSubfields());
500         }
501 }
502 # At this point, each server has given us a result set
503 # now we build that set for template display
504 my @sup_results_array;
505 for (my $i=0;$i<=@servers;$i++) {
506     my $server = $servers[$i];
507     if ($server =~/biblioserver/) { # this is the local bibliographic server
508         $hits = $results_hashref->{$server}->{"hits"};
509         my $page = $cgi->param('page') || 0;
510         my @newresults = searchResults( $query_desc,$hits,$results_per_page,$offset,@{$results_hashref->{$server}->{"RECORDS"}});
511         $total = $total + $results_hashref->{$server}->{"hits"};
512         if ($hits) {
513             $template->param(total => $hits);
514                         $template->param(limit_cgi => $limit_cgi);
515                         $template->param(query_cgi => $query_cgi);
516                         $template->param(query_desc => $query_desc);
517                         $template->param(limit_desc => $limit_desc);
518                         if ($query_desc || $limit_desc) {
519                 $template->param(searchdesc => 1);
520                         }
521                         $template->param(stopwords_removed => "@$stopwords_removed") if $stopwords_removed;
522             $template->param(results_per_page =>  $results_per_page);
523             $template->param(SEARCH_RESULTS => \@newresults);
524                         ## Build the page numbers on the bottom of the page
525                         my @page_numbers;
526                         # total number of pages there will be
527                         my $pages = ceil($hits / $results_per_page);
528                         # default page number
529                         my $current_page_number = 1;
530                         $current_page_number = ($offset / $results_per_page + 1) if $offset;
531                         my $previous_page_offset = $offset - $results_per_page unless ($offset - $results_per_page <0);
532                         my $next_page_offset = $offset + $results_per_page;
533                         # If we're within the first 10 pages, keep it simple
534                         #warn "current page:".$current_page_number;
535                         if ($current_page_number < 10) {
536                                 # just show the first 10 pages
537                                 # Loop through the pages
538                                 my $pages_to_show = 10;
539                                 $pages_to_show = $pages if $pages<10;
540                                 for ($i=1; $i<=$pages_to_show;$i++) {
541                                         # the offset for this page
542                                         my $this_offset = (($i*$results_per_page)-$results_per_page);
543                                         # the page number for this page
544                                         my $this_page_number = $i;
545                                         # it should only be highlighted if it's the current page
546                                         my $highlight = 1 if ($this_page_number == $current_page_number);
547                                         # put it in the array
548                                         push @page_numbers, { offset => $this_offset, pg => $this_page_number, highlight => $highlight, sort_by => join " ",@sort_by };
549                                 
550                                 }
551                         
552                         }
553                         # now, show twenty pages, with the current one smack in the middle
554                         else {
555                                 for ($i=$current_page_number; $i<=($current_page_number + 20 );$i++) {
556                     my $this_offset = ((($i-9)*$results_per_page)-$results_per_page);
557                     my $this_page_number = $i-9;
558                     my $highlight = 1 if ($this_page_number == $current_page_number);
559                                         if ($this_page_number <= $pages) {
560                         push @page_numbers, { offset => $this_offset, pg => $this_page_number, highlight => $highlight, sort_by => join " ",@sort_by };
561                                         }
562                 }
563                         
564                         }
565                         $template->param(       PAGE_NUMBERS => \@page_numbers,
566                                                                 previous_page_offset => $previous_page_offset) unless $pages < 2;
567                         $template->param(next_page_offset => $next_page_offset) unless $pages eq $current_page_number;
568                 }
569                 # no hits
570                 else {
571                         $template->param(searchdesc => 1,query_desc => $query_desc,limit_desc => $limit_desc);
572                 }
573     } # end of the if local
574     else {
575         # check if it's a z3950 or opensearch source
576         my $zed3950 = 0;  # FIXME :: Hardcoded value.
577         if ($zed3950) {
578             my @inner_sup_results_array;
579             for my $sup_record ( @{$results_hashref->{$server}->{"RECORDS"}} ) {
580                 my $marc_record_object = MARC::Record->new_from_usmarc($sup_record);
581                 my $control_number = $marc_record_object->field('010')->subfield('a') if $marc_record_object->field('010');
582                 $control_number =~ s/^ //g;
583                 my $link = "http://catalog.loc.gov/cgi-bin/Pwebrecon.cgi?SAB1=".$control_number."&BOOL1=all+of+these&FLD1=LC+Control+Number+LCCN+%28K010%29+%28K010%29&GRP1=AND+with+next+set&SAB2=&BOOL2=all+of+these&FLD2=Keyword+Anywhere+%28GKEY%29+%28GKEY%29&PID=6211&SEQ=20060816121838&CNT=25&HIST=1";
584                 my $title = $marc_record_object->title();
585                 push @inner_sup_results_array, {
586                     'title' => $title,
587                     'link' => $link,
588                 };
589             }
590             my $servername = $server;
591             push @sup_results_array, { servername => $servername, inner_sup_results_loop => \@inner_sup_results_array};
592             $template->param(outer_sup_results_loop => \@sup_results_array);
593         }
594     }
595
596 } #/end of the for loop
597 #$template->param(FEDERATED_RESULTS => \@results_array);
598
599
600 $template->param(
601             #classlist => $classlist,
602             total => $total,
603             opacfacets => 1,
604             facets_loop => $facets,
605             scan => $scan,
606             search_error => $error,
607 );
608
609 if ($query_desc || $limit_desc) {
610         $template->param(searchdesc => 1);
611 }
612
613 ## Now let's find out if we have any supplemental data to show the user
614 #  and in the meantime, save the current query for statistical purposes, etc.
615 my $koha_spsuggest; # a flag to tell if we've got suggestions coming from Koha
616 my @koha_spsuggest; # place we store the suggestions to be returned to the template as LOOP
617 my $phrases = $query_desc;
618 my $ipaddress;
619
620 if ( C4::Context->preference("kohaspsuggest") ) {
621                 my ($suggest_host, $suggest_dbname, $suggest_user, $suggest_pwd) = split(':', C4::Context->preference("kohaspsuggest"));
622         eval {
623             my $koha_spsuggest_dbh;
624             # FIXME: this needs to be moved to Context.pm
625             eval {
626                 $koha_spsuggest_dbh=DBI->connect("DBI:mysql:$suggest_dbname:$suggest_host","$suggest_user","$suggest_pwd");
627             };
628             if ($@) { 
629                 warn "can't connect to spsuggest db";
630             }
631             else {
632                 my $koha_spsuggest_insert = "INSERT INTO phrase_log(phr_phrase,phr_resultcount,phr_ip) VALUES(?,?,?)";
633                 my $koha_spsuggest_query = "SELECT display FROM distincts WHERE strcmp(soundex(suggestion), soundex(?)) = 0 order by soundex(suggestion) limit 0,5";
634                 my $koha_spsuggest_sth = $koha_spsuggest_dbh->prepare($koha_spsuggest_query);
635                 $koha_spsuggest_sth->execute($phrases);
636                 while (my $spsuggestion = $koha_spsuggest_sth->fetchrow_array) {
637                     $spsuggestion =~ s/(:|\/)//g;
638                     my %line;
639                     $line{spsuggestion} = $spsuggestion;
640                     push @koha_spsuggest,\%line;
641                     $koha_spsuggest = 1;
642                 }
643
644                 # Now save the current query
645                 $koha_spsuggest_sth=$koha_spsuggest_dbh->prepare($koha_spsuggest_insert);
646                 #$koha_spsuggest_sth->execute($phrases,$results_per_page,$ipaddress);
647                 $koha_spsuggest_sth->finish;
648
649                 $template->param( koha_spsuggest => $koha_spsuggest ) unless $hits;
650                 $template->param( SPELL_SUGGEST => \@koha_spsuggest,
651                 );
652             }
653     };
654     if ($@) {
655             warn "Kohaspsuggest failure:".$@;
656     }
657 }
658
659 # VI. BUILD THE TEMPLATE
660 output_html_with_http_headers $cgi, $cookie, $template->output;