Bug 12114: (QA Followup) Use JSON instead of JSON::XS
[koha_fer] / opac / opac-search.pl
index 6349e3c..83f16d8 100755 (executable)
@@ -42,8 +42,8 @@ for ( $searchengine ) {
 }
 
 use C4::Output;
-use C4::Auth qw(:DEFAULT get_session);
-use C4::Languages qw(getAllLanguages);
+use C4::Auth qw(:DEFAULT get_session ParseSearchHistorySession SetSearchHistorySession);
+use C4::Languages qw(getLanguages);
 use C4::Search;
 use C4::Biblio;  # GetBiblioData
 use C4::Koha;
@@ -51,10 +51,11 @@ use C4::Tags qw(get_tags);
 use C4::Branch; # GetBranches
 use C4::SocialData;
 use C4::Ratings;
+use C4::External::OverDrive;
 
 use POSIX qw(ceil floor strftime);
 use URI::Escape;
-use Storable qw(thaw freeze);
+use JSON qw/decode_json encode_json/;
 use Business::ISBN;
 
 my $DisplayMultiPlaceHold = C4::Context->preference("DisplayMultiPlaceHold");
@@ -197,7 +198,7 @@ $template->param(
 );
 
 # load the language limits (for search)
-my $languages_limit_loop = getAllLanguages($lang);
+my $languages_limit_loop = getLanguages($lang, 1);
 $template->param(search_languages_loop => $languages_limit_loop,);
 
 # load the Type stuff
@@ -246,10 +247,6 @@ foreach my $advanced_srch_type (@advanced_search_types) {
 }
 $template->param(advancedsearchesloop => \@advancedsearchesloop);
 
-# # load the itypes (Called item types in the template -- just authorized values for searching)
-# my ($itypecount,@itype_loop) = GetCcodes();
-# $template->param(itypeloop=>\@itype_loop,);
-
 # The following should only be loaded if we're bringing up the advanced search template
 if ( $template_type && $template_type eq 'advsearch' ) {
     # load the servers (used for searching -- to do federated searching, etc.)
@@ -559,6 +556,7 @@ for (my $i=0;$i<@servers;$i++) {
             @newresults = searchResults('opac', $query_desc, $hits, $results_per_page, $offset, $scan,
                                         $results_hashref->{$server}->{"RECORDS"});
         }
+        $hits = 0 unless @newresults;
 
         foreach my $res (@newresults) {
 
@@ -613,36 +611,26 @@ for (my $i=0;$i<@servers;$i++) {
         }
 
         if ($results_hashref->{$server}->{"hits"}){
-            $total = $total + $results_hashref->{$server}->{"hits"};
+            $total = $total + $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)};
-                }
-            }
+            my @recentSearches = ParseSearchHistorySession($cgi);
 
             # Adding the new search if needed
             my $path_info = $cgi->url(-path_info=>1);
             my $query_cgi_history = $cgi->url(-query=>1);
             $query_cgi_history =~ s/^$path_info\?//;
             $query_cgi_history =~ s/;/&/g;
-            my $query_desc_history = "$query_desc, $limit_desc";
+            my $query_desc_history = join ", ", grep { defined $_ } $query_desc, $limit_desc;
 
             if (!$borrowernumber || $borrowernumber eq '') {
-                # To a cookie (the user is not logged in)
+                # To the session (the user is not logged in)
                 if (!$offset) {
                     push @recentSearches, {
-                                "query_desc" => $query_desc_history || "unknown",
-                                "query_cgi"  => $query_cgi_history  || "unknown",
+                                "query_desc" => Encode::decode_utf8($query_desc_history) || "unknown",
+                                "query_cgi"  => Encode::decode_utf8($query_cgi_history)  || "unknown",
                                 "time"       => time(),
                                 "total"      => $total
                               };
@@ -650,17 +638,10 @@ for (my $i=0;$i<@servers;$i++) {
                 }
 
                 shift @recentSearches if (@recentSearches > 15);
-                # 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];
+                SetSearchHistorySession($cgi, \@recentSearches);
             }
             else {
-                # To the session (the user is logged in)
+                # To the database (the user is logged in)
                 if (!$offset) {
                     AddSearchHistory($borrowernumber, $cgi->cookie("CGISESSID"), $query_desc_history, $query_cgi_history, $total);
                     $template->param(ShowOpacRecentSearchLink => 1);
@@ -899,5 +880,10 @@ $template->{VARS}->{IDreamBooksReviews} = C4::Context->preference('IDreamBooksRe
 $template->{VARS}->{IDreamBooksReadometer} = C4::Context->preference('IDreamBooksReadometer');
 $template->{VARS}->{IDreamBooksResults} = C4::Context->preference('IDreamBooksResults');
 
+if ($offset == 0 && IsOverDriveEnabled()) {
+    $template->param(OverDriveEnabled => 1);
+    $template->param(OverDriveLibraryID => C4::Context->preference('OverDriveLibraryID'));
+}
+
     $template->param( borrowernumber    => $borrowernumber);
 output_with_http_headers $cgi, $cookie, $template->output, $content_type;