- 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([]),
- -HttpOnly => 1,
- -expires => ''
- );
- $cookie = [$cookie, $newsearchcookie];
- }
+ my @recentSearches = C4::Search::History::get_from_session({ cgi => $in->{'query'} });
+ if (@recentSearches) {
+ my $dbh = C4::Context->dbh;
+ my $query = q{
+ INSERT INTO search_history(userid, sessionid, query_desc, query_cgi, type, total, time )
+ VALUES (?, ?, ?, ?, ?, ?, ?)
+ };
+
+ my $sth = $dbh->prepare($query);
+ $sth->execute( $borrowernumber,
+ $in->{query}->cookie("CGISESSID"),
+ $_->{query_desc},
+ $_->{query_cgi},
+ $_->{type} || 'biblio',
+ $_->{total},
+ $_->{time},
+ ) foreach @recentSearches;
+
+ # clear out the search history from the session now that
+ # we've saved it to the database
+ C4::Search::History::set_to_session({ cgi => $in->{'query'}, search_history => [] });