Bug 13205: Last/first page options for result list paging
[koha.git] / catalogue / search.pl
index dd73277..7b8e5e2 100755 (executable)
@@ -146,16 +146,16 @@ use C4::Auth qw(:DEFAULT get_session);
 use C4::Search;
 use C4::Languages qw(getLanguages);
 use C4::Koha;
-use C4::Members qw(GetMember);
 use URI::Escape;
 use POSIX qw(ceil floor);
-use C4::Branch; # GetBranches
 use C4::Search::History;
 
+use Koha::ItemTypes;
 use Koha::LibraryCategories;
-use Koha::Virtualshelves;
+use Koha::Patrons;
 use Koha::SearchEngine::Search;
 use Koha::SearchEngine::QueryBuilder;
+use Koha::Virtualshelves;
 
 use URI::Escape;
 
@@ -196,38 +196,33 @@ if (C4::Context->preference("IntranetNumbersPreferPhrase")) {
 }
 
 if($cgi->cookie("holdfor")){ 
-    my $holdfor_patron = GetMember('borrowernumber' => $cgi->cookie("holdfor"));
+    my $holdfor_patron = Koha::Patrons->find( $cgi->cookie("holdfor") );
     $template->param(
         holdfor => $cgi->cookie("holdfor"),
-        holdfor_surname => $holdfor_patron->{'surname'},
-        holdfor_firstname => $holdfor_patron->{'firstname'},
-        holdfor_cardnumber => $holdfor_patron->{'cardnumber'},
+        holdfor_surname => $holdfor_patron->surname,
+        holdfor_firstname => $holdfor_patron->firstname,
+        holdfor_cardnumber => $holdfor_patron->cardnumber,
     );
 }
 
-# load the branches
-my $branches = GetBranches();
-
-# Populate branch_loop with all branches sorted by their name.  If
-# IndependentBranches is activated, set the default branch to the borrower
-# branch, except for superlibrarian who need to search all libraries.
-my $user = C4::Context->userenv;
-my @branch_loop = map {
-     {
-        value      => $_,
-        branchname => $branches->{$_}->{branchname},
-        selected   => $user->{branch} eq $_ && C4::Branch::onlymine(),
-     }
-} sort {
-    $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname}
-} keys %$branches;
+# get biblionumbers stored in the cart
+my @cart_list;
 
+if($cgi->cookie("intranet_bib_list")){
+    my $cart_list = $cgi->cookie("intranet_bib_list");
+    @cart_list = split(/\//, $cart_list);
+}
+
+# load the branches
 my $categories = Koha::LibraryCategories->search( { categorytype => 'searchdomain' }, { order_by => [ 'categorytype', 'categorycode' ] } );
 
-$template->param(branchloop => \@branch_loop, searchdomainloop => $categories);
+$template->param(
+    selected_branchcode => ( C4::Context->IsSuperLibrarian ? C4::Context->userenv : '' ),
+    searchdomainloop => $categories
+);
 
 # load the Type stuff
-my $itemtypes = GetItemTypes;
+my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
 # the index parameter is different for item-level itemtypes
 my $itype_or_itemtype = (C4::Context->preference("item-level_itypes"))?'itype':'itemtype';
 my @advancedsearchesloop;
@@ -422,12 +417,7 @@ $template->param(available => $available);
 my $limit_yr;
 my $limit_yr_value;
 if ($params->{'limit-yr'}) {
-    if ($params->{'limit-yr'} =~ /\d{4}-\d{4}/) {
-        my ($yr1,$yr2) = split(/-/, $params->{'limit-yr'});
-        $limit_yr = "yr,st-numeric,ge=$yr1 and yr,st-numeric,le=$yr2";
-        $limit_yr_value = "$yr1-$yr2";
-    }
-    elsif ($params->{'limit-yr'} =~ /\d{4}/) {
+    if ($params->{'limit-yr'} =~ /\d{4}/) {
         $limit_yr = "yr,st-numeric=$params->{'limit-yr'}";
         $limit_yr_value = $params->{'limit-yr'};
     }
@@ -459,6 +449,7 @@ 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;
@@ -472,8 +463,6 @@ my $builder = Koha::SearchEngine::QueryBuilder->new(
 my $searcher = Koha::SearchEngine::Search->new(
     { index => $Koha::SearchEngine::BIBLIOS_INDEX } );
 
-my @results;
-
 ## I. BUILD THE QUERY
 (
     $error,             $query, $simple_query, $query_cgi,
@@ -486,6 +475,7 @@ my @results;
 ## parse the query_cgi string and put it into a form suitable for <input>s
 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;
@@ -496,9 +486,13 @@ for my $this_cgi ( split('&',$query_cgi) ) {
     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_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;
@@ -523,10 +517,10 @@ my $facets; # this object stores the faceted results that display on the left-ha
 my $results_hashref;
 
 eval {
-    my $itemtypes = GetItemTypes;
+    my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
     ( $error, $results_hashref, $facets ) = $searcher->search_compat(
         $query,            $simple_query, \@sort_by,       \@servers,
-        $results_per_page, $offset,       $expanded_facet, $branches,
+        $results_per_page, $offset,       $expanded_facet, undef,
         $itemtypes,        $query_type,   $scan
     );
 };
@@ -612,6 +606,10 @@ for (my $i=0;$i<@servers;$i++) {
             # in order to avoid problems generated by the default size value in TT
             foreach my $line (@newresults) {
                 if ( not exists $line->{'size'} ) { $line->{'size'} = "" }
+                # while we're checking each line, see if item is in the cart
+                if ( grep {$_ eq $line->{'biblionumber'}} @cart_list) {
+                    $line->{'incart'} = 1;
+                }
             }
             $template->param(SEARCH_RESULTS => \@newresults);
             ## FIXME: add a global function for this, it's better than the current global one
@@ -619,6 +617,7 @@ for (my $i=0;$i<@servers;$i++) {
             my @page_numbers;
             # total number of pages there will be
             my $pages = ceil($hits / $results_per_page);
+            my $last_page = ($pages * $results_per_page) - $results_per_page;
             # default page number
             my $current_page_number = 1;
             $current_page_number = ($offset / $results_per_page + 1) if $offset;
@@ -669,6 +668,7 @@ for (my $i=0;$i<@servers;$i++) {
             }
             # FIXME: no previous_page_offset when pages < 2
             $template->param(   PAGE_NUMBERS => \@page_numbers,
+                                last_page => $last_page,
                                 previous_page_offset => $previous_page_offset) unless $pages < 2;
             $template->param(   next_page_offset => $next_page_offset) unless $pages eq $current_page_number;
         }
@@ -784,7 +784,7 @@ sub prepare_adv_search_types {
     # the index parameter is different for item-level itemtypes
     my $itype_or_itemtype =
       ( C4::Context->preference("item-level_itypes") ) ? 'itype' : 'itemtype';
-    my $itemtypes = GetItemTypes;
+    my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
 
     my ( $cnt, @result );
     foreach my $advanced_srch_type (@advanced_search_types) {
@@ -796,8 +796,8 @@ sub prepare_adv_search_types {
             my @itypesloop;
             foreach my $thisitemtype (
                 sort {
-                    $itemtypes->{$a}->{'description'}
-                      cmp $itemtypes->{$b}->{'description'}
+                    $itemtypes->{$a}->{'translated_description'}
+                      cmp $itemtypes->{$b}->{'translated_description'}
                 } keys %$itemtypes
               )
             {
@@ -805,7 +805,7 @@ sub prepare_adv_search_types {
                     number      => $cnt++,
                     ccl         => "$itype_or_itemtype,phr",
                     code        => $thisitemtype,
-                    description => $itemtypes->{$thisitemtype}->{'description'},
+                    description => $itemtypes->{$thisitemtype}->{'translated_description'},
                     imageurl    => getitemtypeimagelocation(
                         'intranet', $itemtypes->{$thisitemtype}->{'imageurl'}
                     ),