3 # Copyright 2009 BibLibre SARL
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 use C4::Auth qw(:DEFAULT get_session ParseSearchHistorySession SetSearchHistorySession);
25 use JSON qw/decode_json encode_json/;
33 use POSIX qw(strftime);
38 # Getting the template and auth
39 my ($template, $loggedinuser, $cookie)= get_template_and_user({template_name => "opac-search-history.tmpl",
43 flagsrequired => {borrowers => 1},
47 # If the user is not logged in, we deal with the cookie
50 # Deleting search history
51 if ($cgi->param('action') && $cgi->param('action') eq 'delete') {
52 # Deleting cookie's content
53 SetSearchHistorySession($cgi, []);
55 # Redirecting to this same url with the cookie in the headers so it's deleted immediately
56 my $uri = $cgi->url();
57 print $cgi->redirect(-uri => $uri);
59 # Showing search history
62 my @recentSearches = ParseSearchHistorySession($cgi);
63 if (@recentSearches) {
65 # As the dates are stored as unix timestamps, let's do some formatting
66 foreach my $asearch (@recentSearches) {
68 # We create an iso date from the unix timestamp
69 my $isodate = strftime "%Y-%m-%d", localtime($asearch->{'time'});
71 # We also get the time of the day from the unix timestamp
72 my $time = strftime " %H:%M:%S", localtime($asearch->{'time'});
74 # And we got our human-readable date :
75 $asearch->{'time'} = $isodate . $time;
78 $template->param(recentSearches => \@recentSearches);
82 # And if the user is logged in, we deal with the database
84 my $dbh = C4::Context->dbh;
86 # Deleting search history
87 if ($cgi->param('action') && $cgi->param('action') eq 'delete') {
88 my $query = "DELETE FROM search_history WHERE userid = ?";
89 my $sth = $dbh->prepare($query);
90 $sth->execute($loggedinuser);
92 # Redirecting to this same url so the user won't see the search history link in the header
93 my $uri = $cgi->url();
94 print $cgi->redirect($uri);
97 # Showing search history
100 my $date = C4::Dates->new();
101 my $dateformat = $date->DHTMLcalendar() . " %H:%i:%S"; # Current syspref date format + standard time format
103 # Getting the data with date format work done by mysql
104 my $query = "SELECT userid, sessionid, query_desc, query_cgi, total, time FROM search_history WHERE userid = ? AND sessionid = ?";
105 my $sth = $dbh->prepare($query);
106 $sth->execute($loggedinuser, $cgi->cookie("CGISESSID"));
107 my $searches = $sth->fetchall_arrayref({});
108 $template->param(recentSearches => $searches);
110 # Getting searches from previous sessions
111 $query = "SELECT COUNT(*) FROM search_history WHERE userid = ? AND sessionid != ?";
112 $sth = $dbh->prepare($query);
113 $sth->execute($loggedinuser, $cgi->cookie("CGISESSID"));
115 # If at least one search from previous sessions has been performed
116 if ($sth->fetchrow_array > 0) {
117 $query = "SELECT userid, sessionid, query_desc, query_cgi, total, time FROM search_history WHERE userid = ? AND sessionid != ?";
118 $sth = $dbh->prepare($query);
119 $sth->execute($loggedinuser, $cgi->cookie("CGISESSID"));
120 my $previoussearches = $sth->fetchall_arrayref({});
121 $template->param(previousSearches => $previoussearches);
132 $template->param(searchhistoryview => 1);
134 output_html_with_http_headers $cgi, $cookie, $template->output;