+ # Logged-in opac search history
+ # If the requested template is an opac one and opac search history is enabled
+ if ($in->{type} eq 'opac' && C4::Context->preference('EnableOpacSearchHistory')) {
+ my $dbh = C4::Context->dbh;
+ my $query = "SELECT COUNT(*) FROM search_history WHERE userid=?";
+ my $sth = $dbh->prepare($query);
+ $sth->execute($borrowernumber);
+
+ # If at least one search has already been performed
+ if ($sth->fetchrow_array > 0) {
+ # We show the link in opac
+ $template->param(ShowOpacRecentSearchLink => 1);
+ }
+
+ # And if there's a cookie with searches performed when the user was not logged in,
+ # we add them to the logged-in search history
+ my $searchcookie = $in->{'query'}->cookie('KohaOpacRecentSearches');
+ if ($searchcookie){
+ $searchcookie = uri_unescape($searchcookie);
+ my @recentSearches = @{thaw($searchcookie) || []};
+ if (@recentSearches) {
+ my $sth = $dbh->prepare($SEARCH_HISTORY_INSERT_SQL);
+ $sth->execute( $borrowernumber,
+ $in->{'query'}->cookie("CGISESSID"),
+ $_->{'query_desc'},
+ $_->{'query_cgi'},
+ $_->{'total'},
+ $_->{'time'},
+ ) foreach @recentSearches;
+
+ # And then, delete the cookie's content
+ my $newsearchcookie = $in->{'query'}->cookie(
+ -name => 'KohaOpacRecentSearches',
+ -value => freeze([]),
+ -expires => ''
+ );
+ $cookie = [$cookie, $newsearchcookie];
+ }
+ }
+ }