+ my $page = $cgi->param('page') || 0;
+ my @newresults;
+ if ($build_grouped_results) {
+ foreach my $group (@{ $results_hashref->{$server}->{"GROUPS"} }) {
+ # because pazGetRecords handles retieving only the records
+ # we want as specified by $offset and $results_per_page,
+ # we need to set the offset parameter of searchResults to 0
+ my @group_results = searchResults( 'opac', $query_desc, $group->{'group_count'},$results_per_page, 0, $scan,
+ @{ $group->{"RECORDS"} }, C4::Context->preference('hidelostitems'));
+ push @newresults, { group_label => $group->{'group_label'}, GROUP_RESULTS => \@group_results };
+ }
+ } else {
+ @newresults = searchResults('opac', $query_desc, $hits, $results_per_page, $offset, $scan,
+ @{$results_hashref->{$server}->{"RECORDS"}},, C4::Context->preference('hidelostitems'));
+ }
+ my $tag_quantity;
+ if (C4::Context->preference('TagsEnabled') and
+ $tag_quantity = C4::Context->preference('TagsShowOnList')) {
+ foreach (@newresults) {
+ my $bibnum = $_->{biblionumber} or next;
+ $_->{itemsissued} = CountItemsIssued( $bibnum );
+ $_ ->{'TagLoop'} = get_tags({biblionumber=>$bibnum, approved=>1, 'sort'=>'-weight',
+ limit=>$tag_quantity });
+ }
+ }
+ foreach (@newresults) {
+ $_->{coins} = GetCOinSBiblio($_->{'biblionumber'});
+ }
+
+ if ($results_hashref->{$server}->{"hits"}){
+ $total = $total + $results_hashref->{$server}->{"hits"};
+ }
+ # Opac search history
+ my $newsearchcookie;
+ if (C4::Context->preference('EnableOpacSearchHistory')) {
+ my @recentSearches;
+
+ # Getting the (maybe) already sent cookie
+ my $searchcookie = $cgi->cookie('KohaOpacRecentSearches');
+ if ($searchcookie){
+ $searchcookie = uri_unescape($searchcookie);
+ if (thaw($searchcookie)) {
+ @recentSearches = @{thaw($searchcookie)};
+ }
+ }
+
+ # Adding the new search if needed
+ if (!$borrowernumber || $borrowernumber eq '') {
+ # To a cookie (the user is not logged in)
+
+ if (($params->{'offset'}||'') eq '') {
+
+ push @recentSearches, {
+ "query_desc" => $query_desc || "unknown",
+ "query_cgi" => $query_cgi || "unknown",
+ "time" => time(),
+ "total" => $total
+ };
+ $template->param(ShowOpacRecentSearchLink => 1);
+ }
+
+ # Pushing the cookie back
+ $newsearchcookie = $cgi->cookie(
+ -name => 'KohaOpacRecentSearches',
+ # We uri_escape the whole freezed structure so we're sure we won't have any encoding problems
+ -value => uri_escape(freeze(\@recentSearches)),
+ -expires => ''
+ );
+ $cookie = [$cookie, $newsearchcookie];
+ }
+ else {
+ # To the session (the user is logged in)
+ if (($params->{'offset'}||'') eq '') {
+ AddSearchHistory($borrowernumber, $cgi->cookie("CGISESSID"), $query_desc, $query_cgi, $total);
+ $template->param(ShowOpacRecentSearchLink => 1);
+ }
+ }
+ }
+ ## If there's just one result, redirect to the detail page
+ if ($total == 1 && $format ne 'rss2'
+ && $format ne 'opensearchdescription' && $format ne 'atom') {
+ my $biblionumber=$newresults[0]->{biblionumber};
+ if (C4::Context->preference('BiblioDefaultView') eq 'isbd') {
+ print $cgi->redirect("/cgi-bin/koha/opac-ISBDdetail.pl?biblionumber=$biblionumber");
+ } elsif (C4::Context->preference('BiblioDefaultView') eq 'marc') {
+ print $cgi->redirect("/cgi-bin/koha/opac-MARCdetail.pl?biblionumber=$biblionumber");
+ } else {
+ print $cgi->redirect("/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber");
+ }
+ exit;
+ }