installer (part 1): started major changes
[koha_fer] / opac / opac-search.pl
index 55e6c6d..fb0e46d 100755 (executable)
@@ -335,8 +335,12 @@ my $params = $cgi->Vars;
 # Params that can have more than one value
 # sort by is used to sort the query
 # in theory can have more than one but generally there's just one
+my $default_sort_by = C4::Context->preference('OPACdefaultSortField')."_".C4::Context->preference('OPACdefaultSortOrder') 
+    if (C4::Context->preference('OPACdefaultSortField') && C4::Context->preference('OPACdefaultSortOrder'));
+
 my @sort_by;
 @sort_by = split("\0",$params->{'sort_by'}) if $params->{'sort_by'};
+$sort_by[0] = $default_sort_by unless $sort_by[0];
 foreach my $sort (@sort_by) {
        $template->param($sort => 1);
 }
@@ -358,20 +362,19 @@ my @operators;
 
 # indexes are query qualifiers, like 'title', 'author', etc. They
 # can be single or multiple parameters separated by comma: kw,right-Truncation 
-my @indexes;
-@indexes = split("\0",$params->{'idx'});
+my @indexes = split("\0",$params->{'idx'});
 
 # if a simple index (only one)  display the index used in the top search box
-if (@indexes[0] && !@indexes[1]) {
-       $template->param(@indexes[0] => 1);
+if ($indexes[0] && !$indexes[1]) {
+       $template->param("ms_".$indexes[0] => 1);
 }
 # an operand can be a single term, a phrase, or a complete ccl query
 my @operands;
 @operands = split("\0",$params->{'q'}) if $params->{'q'};
 
 # if a simple search, display the value in the search box
-if (@operands[0] && !@operands[1]) {
-    $template->param(ms_value => @operands[0]);
+if ($operands[0] && !$operands[1]) {
+    $template->param(ms_value => $operands[0]);
 }
 
 # limits are use to limit to results to a pre-defined category such as branch or language
@@ -395,7 +398,8 @@ push @limits, map "yr:".$_, split("\0",$params->{'limit-yr'}) if $params->{'limi
 
 # Params that can only have one value
 my $scan = $params->{'scan'};
-my $results_per_page = $params->{'count'} || 20;
+my $count = C4::Context->preference('OPACnumSearchResults') || 20;
+my $results_per_page = $params->{'count'} || $count;
 my $offset = $params->{'offset'} || 0;
 my $page = $cgi->param('page') || 1;
 #my $offset = ($page-1)*$results_per_page;
@@ -403,34 +407,41 @@ my $hits;
 my $expanded_facet = $params->{'expand'};
 
 # Define some global variables
-my ( $error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$stopwords_removed,$query_type);
+my ($error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$stopwords_removed,$query_type);
 
 my @results;
 
 ## I. BUILD THE QUERY
 ( $error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$stopwords_removed,$query_type) = buildQuery(\@operators,\@operands,\@indexes,\@limits,\@sort_by);
 
-## parse the query_cgi string and put it into a form suitable for <input>s
-my @query_inputs;
-for my $this_cgi ( split('&',$query_cgi) ) {
-       next unless $this_cgi;
-       $this_cgi =~ m/(.*=)(.*)/;
-       my $input_name = $1;
-       my $input_value = $2;
-       $input_name =~ s/=$//;
-       push @query_inputs, { input_name => $input_name, input_value => $input_value };
+sub _input_cgi_parse ($) { 
+       my @elements;
+       for my $this_cgi ( split('&',shift) ) {
+               next unless $this_cgi;
+               $this_cgi =~ /(.*)=(.*)/;
+               my $input_name = $1;
+               my $input_value = $2;
+               push @elements, { input_name => $input_name, input_value => $input_value };
+       }
+       return @elements;
 }
+
+## parse the query_cgi string and put it into a form suitable for <input>s
+my @query_inputs = _input_cgi_parse($query_cgi);
 $template->param ( QUERY_INPUTS => \@query_inputs );
 
 ## parse the limit_cgi string and put it into a form suitable for <input>s
-my @limit_inputs;
-for my $this_cgi ( split('&',$limit_cgi) ) {
-       next unless $this_cgi;
-    $this_cgi =~ m/(.*=)(.*)/;
-    my $input_name = $1;
-    my $input_value = $2;
-    $input_name =~ s/=$//;
-    push @limit_inputs, { input_name => $input_name, input_value => $input_value };
+my @limit_inputs = _input_cgi_parse($query_cgi);
+
+# add OPAC 'hidelostitems'
+# not items with 
+if (C4::Context->preference('hidelostitems') == 1) {
+       $query ="($query) not ((lost,st-numeric gt 0) or ( allrecords,AlwaysMatches='' not lost,AlwaysMatches=''))";
+       warn "Q".$query;
+}
+# add OPAC suppression - requires at least one item indexed with Suppress
+if (C4::Context->preference('OpacSuppression')) {
+    $query = "($query) not Suppress=1";
 }
 $template->param ( LIMIT_INPUTS => \@limit_inputs );
 
@@ -451,7 +462,6 @@ if (C4::Context->preference('NoZebra')) {
 }
 if ($@ || $error) {
     $template->param(query_error => $error.$@);
-
     output_html_with_http_headers $cgi, $cookie, $template->output;
     exit;
 }
@@ -550,6 +560,10 @@ for (my $i=0;$i<=@servers;$i++) {
                                                                previous_page_offset => $previous_page_offset) unless $pages < 2;
                        $template->param(next_page_offset => $next_page_offset) unless $pages eq $current_page_number;
          }
+         else {
+            # Here we must handle cases where no results are returned otherwise the template gives a malformed message. -fbcit
+            $template->param(query_desc => $query_desc);
+         }
     } # end of the if local
     else {
         # check if it's a z3950 or opensearch source