Bug 14385: Extend OpacHiddenItems to allow specifying exempt borrower categories
[koha.git] / catalogue / search.pl
index e4ca11a..d9961a0 100755 (executable)
@@ -132,8 +132,7 @@ Not yet completed...
 
 =cut
 
-use strict;            # always use
-#use warnings; FIXME - Bug 2505
+use Modern::Perl;
 
 ## STEP 1. Load things that are used in both search page and
 # results page and decide which template to load, operations 
@@ -151,7 +150,7 @@ use POSIX qw(ceil floor);
 use C4::Search::History;
 
 use Koha::ItemTypes;
-use Koha::LibraryCategories;
+use Koha::Library::Groups;
 use Koha::Patrons;
 use Koha::SearchEngine::Search;
 use Koha::SearchEngine::QueryBuilder;
@@ -171,6 +170,7 @@ my $template_type;
 my @params = $cgi->multi_param("limit");
 if ((@params>=1) || ($cgi->param("q")) || ($cgi->param('multibranchlimit')) || ($cgi->param('limit-yr')) ) {
     $template_name = 'catalogue/results.tt';
+    $template_type = 'results';
 }
 else {
     $template_name = 'catalogue/advsearch.tt';
@@ -191,9 +191,6 @@ my $lang = C4::Languages::getlanguage($cgi);
 if (C4::Context->preference("marcflavour") eq "UNIMARC" ) {
     $template->param('UNIMARC' => 1);
 }
-if (C4::Context->preference("IntranetNumbersPreferPhrase")) {
-    $template->param('numbersphr' => 1);
-}
 
 if($cgi->cookie("holdfor")){ 
     my $holdfor_patron = Koha::Patrons->find( $cgi->cookie("holdfor") );
@@ -213,12 +210,13 @@ if($cgi->cookie("intranet_bib_list")){
     @cart_list = split(/\//, $cart_list);
 }
 
-# load the branches
-my $categories = Koha::LibraryCategories->search( { categorytype => 'searchdomain' }, { order_by => [ 'categorytype', 'categorycode' ] } );
+my @search_groups =
+  Koha::Library::Groups->get_search_groups( { interface => 'staff' } );
+@search_groups = sort { $a->title cmp $b->title } @search_groups;
 
 $template->param(
     selected_branchcode => ( C4::Context->IsSuperLibrarian ? C4::Context->userenv : '' ),
-    searchdomainloop => $categories
+    search_groups    => \@search_groups,
 );
 
 # load the Type stuff
@@ -399,9 +397,9 @@ my %is_nolimit = map { $_ => 1 } @nolimits;
 @limits = grep { not $is_nolimit{$_} } @limits;
 
 if($params->{'multibranchlimit'}) {
-    my $library_category = Koha::LibraryCategories->find( $params->{multibranchlimit} );
-    my @libraries = $library_category->libraries;
-    my $multibranch = '('.join( " or ", map { 'branch: ' . $_->id } @libraries ) .')';
+    my $search_group = Koha::Library::Groups->find( $params->{multibranchlimit} );
+    my @libraries = $search_group->all_libraries;
+    my $multibranch = '('.join( " or ", map { 'branch: ' . $_->branchcode } @libraries ) .')';
     push @limits, $multibranch if ($multibranch ne  '()');
 }
 
@@ -449,9 +447,9 @@ my $scan = $params->{'scan'};
 my $count = C4::Context->preference('numSearchResults') || 20;
 my $results_per_page = $params->{'count'} || $count;
 my $offset = $params->{'offset'} || 0;
+$offset = 0 if $offset < 0;
 my $page = $cgi->param('page') || 1;
 #my $offset = ($page-1)*$results_per_page;
-my $hits;
 my $expanded_facet = $params->{'expand'};
 
 # Define some global variables
@@ -476,42 +474,47 @@ my @query_inputs;
 my $scan_index_to_use;
 my $scan_search_term_to_use;
 
-for my $this_cgi ( split('&',$query_cgi) ) {
-    next unless $this_cgi;
-    $this_cgi =~ m/(.*?)=(.*)/;
-    my $input_name = $1;
-    my $input_value = $2;
-    push @query_inputs, { input_name => $input_name, input_value => Encode::decode_utf8( uri_unescape( $input_value ) ) };
-    if ($input_name eq 'idx') {
-        $scan_index_to_use = $input_value; # unless $scan_index_to_use;
-    }
-    if ($input_name eq 'q') {
-        $scan_search_term_to_use = Encode::decode_utf8( uri_unescape( $input_value ));
+if ($query_cgi) {
+    for my $this_cgi ( split('&', $query_cgi) ) {
+        next unless $this_cgi;
+        $this_cgi =~ m/(.*?)=(.*)/;
+        my $input_name = $1;
+        my $input_value = $2;
+        push @query_inputs, { input_name => $input_name, input_value => Encode::decode_utf8( uri_unescape( $input_value ) ) };
+        if ($input_name eq 'idx') {
+            $scan_index_to_use = $input_value; # unless $scan_index_to_use;
+        }
+        if ($input_name eq 'q') {
+            $scan_search_term_to_use = Encode::decode_utf8( uri_unescape( $input_value ));
+        }
     }
 }
+
 $template->param ( QUERY_INPUTS => \@query_inputs,
                    scan_index_to_use => $scan_index_to_use,
                    scan_search_term_to_use => $scan_search_term_to_use );
 
 ## 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;
-    # handle special case limit-yr
-    if ($this_cgi =~ /yr,st-numeric/) {
-        push @limit_inputs, { input_name => 'limit-yr', input_value => $limit_yr_value };   
-        next;
+if ($limit_cgi) {
+    for my $this_cgi ( split('&', $limit_cgi) ) {
+        next unless $this_cgi;
+        # handle special case limit-yr
+        if ($this_cgi =~ /yr,st-numeric/) {
+            push @limit_inputs, { input_name => 'limit-yr', input_value => $limit_yr_value };
+            next;
+        }
+        $this_cgi =~ m/(.*=)(.*)/;
+        my $input_name = $1;
+        my $input_value = $2;
+        $input_name =~ s/=$//;
+        push @limit_inputs, { input_name => $input_name, input_value => Encode::decode_utf8( uri_unescape($input_value) ) };
     }
-    $this_cgi =~ m/(.*=)(.*)/;
-    my $input_name = $1;
-    my $input_value = $2;
-    $input_name =~ s/=$//;
-    push @limit_inputs, { input_name => $input_name, input_value => Encode::decode_utf8( uri_unescape($input_value) ) };
 }
 $template->param ( LIMIT_INPUTS => \@limit_inputs );
 
 ## II. DO THE SEARCH AND GET THE RESULTS
-my $total; # the total results for the whole set
+my $total = 0; # the total results for the whole set
 my $facets; # this object stores the faceted results that display on the left-hand of the results page
 my $results_hashref;
 
@@ -536,11 +539,11 @@ my @sup_results_array;
 for (my $i=0;$i<@servers;$i++) {
     my $server = $servers[$i];
     if ($server =~/biblioserver/) { # this is the local bibliographic server
-        $hits = $results_hashref->{$server}->{"hits"};
+        my $hits = $results_hashref->{$server}->{"hits"} // 0;
         my $page = $cgi->param('page') || 0;
-        my @newresults = searchResults('intranet', $query_desc, $hits, $results_per_page, $offset, $scan,
+        my @newresults = searchResults({ 'interface' => 'intranet' }, $query_desc, $hits, $results_per_page, $offset, $scan,
                                        $results_hashref->{$server}->{"RECORDS"});
-        $total = $total + $results_hashref->{$server}->{"hits"};
+        $total = $total + $hits;
 
         # Search history
         if (C4::Context->preference('EnableSearchHistory')) {
@@ -588,9 +591,11 @@ for (my $i=0;$i<@servers;$i++) {
 
         if ($hits) {
             $template->param(total => $hits);
-            my $limit_cgi_not_availablity = $limit_cgi;
-            $limit_cgi_not_availablity =~ s/&limit=available//g;
-            $template->param(limit_cgi_not_availablity => $limit_cgi_not_availablity);
+            if ($limit_cgi) {
+                my $limit_cgi_not_availablity = $limit_cgi;
+                $limit_cgi_not_availablity =~ s/&limit=available//g;
+                $template->param(limit_cgi_not_availablity => $limit_cgi_not_availablity);
+            }
             $template->param(limit_cgi => $limit_cgi);
             $template->param(query_cgi => $query_cgi);
             $template->param(query_desc => $query_desc);
@@ -614,8 +619,12 @@ for (my $i=0;$i<@servers;$i++) {
             ## FIXME: add a global function for this, it's better than the current global one
             ## Build the page numbers on the bottom of the page
             my @page_numbers;
+            my $max_result_window = $searcher->max_result_window;
+            my $hits_to_paginate = ($max_result_window && $max_result_window < $hits) ? $max_result_window : $hits;
+            $template->param( hits_to_paginate => $hits_to_paginate );
             # total number of pages there will be
-            my $pages = ceil($hits / $results_per_page);
+            my $pages = ceil($hits_to_paginate / $results_per_page);
+            my $last_page_offset = ( $pages -1 ) * $results_per_page;
             # default page number
             my $current_page_number = 1;
             $current_page_number = ($offset / $results_per_page + 1) if $offset;
@@ -666,6 +675,7 @@ for (my $i=0;$i<@servers;$i++) {
             }
             # FIXME: no previous_page_offset when pages < 2
             $template->param(   PAGE_NUMBERS => \@page_numbers,
+                                last_page_offset => $last_page_offset,
                                 previous_page_offset => $previous_page_offset) unless $pages < 2;
             $template->param(   next_page_offset => $next_page_offset) unless $pages eq $current_page_number;
         }
@@ -703,13 +713,13 @@ for (my $i=0;$i<@servers;$i++) {
 $template->{'VARS'}->{'searchid'} = $cgi->param('searchid');
 
 my $gotonumber = $cgi->param('gotoNumber');
-if ($gotonumber eq 'last' || $gotonumber eq 'first') {
+if ( $gotonumber && ( $gotonumber eq 'last' || $gotonumber eq 'first' ) ) {
     $template->{'VARS'}->{'gotoNumber'} = $gotonumber;
 }
 $template->{'VARS'}->{'gotoPage'}   = 'detail.pl';
 my $gotopage = $cgi->param('gotoPage');
 $template->{'VARS'}->{'gotoPage'} = $gotopage
-  if $gotopage =~ m/^(ISBD|labeledMARC|MARC|more)?detail.pl$/;
+  if $gotopage && $gotopage =~ m/^(ISBD|labeledMARC|MARC|more)?detail.pl$/;
 
 for my $facet ( @$facets ) {
     for my $entry ( @{ $facet->{facets} } ) {