Bug 3212 Force leader 9 position to 'a' for new biblios
[koha.git] / opac / opac-search-history.pl
blob65141b9f7a60013a638c84c4b74b9774cd32d755
1 #!/usr/bin/perl
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
10 # version.
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.
20 use strict;
21 use warnings;
23 use C4::Auth qw(:DEFAULT get_session);
24 use CGI;
25 use Storable qw(freeze thaw);
26 use C4::Context;
27 use C4::Output;
28 use C4::Log;
29 use C4::Items;
30 use C4::Debug;
31 use C4::Dates;
32 use URI::Escape;
33 use POSIX qw(strftime);
36 my $cgi = new CGI;
38 # Getting the template and auth
39 my ($template, $loggedinuser, $cookie)
40 = get_template_and_user({template_name => "opac-search-history.tmpl",
41 query => $cgi,
42 type => "opac",
43 authnotrequired => 1,
44 flagsrequired => {borrowers => 1},
45 debug => 1,
46 });
48 $template->param(dateformat => C4::Context->preference("dateformat"));
50 # If the user is not logged in, we deal with the cookie
51 if (!$loggedinuser) {
53 # Deleting search history
54 if ($cgi->param('action') && $cgi->param('action') eq 'delete') {
55 # Deleting cookie's content
56 my $recentSearchesCookie = $cgi->cookie(
57 -name => 'KohaOpacRecentSearches',
58 -value => freeze([]),
59 -expires => ''
62 # Redirecting to this same url with the cookie in the headers so it's deleted immediately
63 my $uri = $cgi->url();
64 print $cgi->redirect(-uri => $uri,
65 -cookie => $recentSearchesCookie);
67 # Showing search history
68 } else {
70 # Getting the cookie
71 my $searchcookie = $cgi->cookie('KohaOpacRecentSearches');
72 if ($searchcookie && thaw(uri_unescape($searchcookie))) {
73 my @recentSearches = @{thaw(uri_unescape($searchcookie))};
74 if (@recentSearches) {
76 # As the dates are stored as unix timestamps, let's do some formatting
77 foreach my $asearch (@recentSearches) {
79 # We create an iso date from the unix timestamp
80 my $isodate = strftime "%Y-%m-%d", localtime($asearch->{'time'});
82 # So we can create a C4::Dates object, to get the date formatted according to the dateformat syspref
83 my $date = C4::Dates->new($isodate, "iso");
84 my $sysprefdate = $date->output("syspref");
86 # We also get the time of the day from the unix timestamp
87 my $time = strftime " %H:%M:%S", localtime($asearch->{'time'});
89 # And we got our human-readable date :
90 $asearch->{'time'} = $sysprefdate . $time;
93 $template->param(recentSearches => \@recentSearches);
97 } else {
98 # And if the user is logged in, we deal with the database
100 my $dbh = C4::Context->dbh;
102 # Deleting search history
103 if ($cgi->param('action') && $cgi->param('action') eq 'delete') {
104 my $query = "DELETE FROM search_history WHERE userid = ?";
105 my $sth = $dbh->prepare($query);
106 $sth->execute($loggedinuser);
108 # Redirecting to this same url so the user won't see the search history link in the header
109 my $uri = $cgi->url();
110 print $cgi->redirect($uri);
113 # Showing search history
114 } else {
116 my $date = C4::Dates->new();
117 my $dateformat = $date->DHTMLcalendar() . " %H:%i:%S"; # Current syspref date format + standard time format
119 # Getting the data with date format work done by mysql
120 my $query = "SELECT userid, sessionid, query_desc, query_cgi, total, DATE_FORMAT(time, \"$dateformat\") as time FROM search_history WHERE userid = ? AND sessionid = ?";
121 my $sth = $dbh->prepare($query);
122 $sth->execute($loggedinuser, $cgi->cookie("CGISESSID"));
123 my $searches = $sth->fetchall_arrayref({});
124 $template->param(recentSearches => $searches);
126 # Getting searches from previous sessions
127 $query = "SELECT COUNT(*) FROM search_history WHERE userid = ? AND sessionid != ?";
128 $sth = $dbh->prepare($query);
129 $sth->execute($loggedinuser, $cgi->cookie("CGISESSID"));
131 # If at least one search from previous sessions has been performed
132 if ($sth->fetchrow_array > 0) {
133 $query = "SELECT userid, sessionid, query_desc, query_cgi, total, DATE_FORMAT(time, \"$dateformat\") as time FROM search_history WHERE userid = ? AND sessionid != ?";
134 $sth = $dbh->prepare($query);
135 $sth->execute($loggedinuser, $cgi->cookie("CGISESSID"));
136 my $previoussearches = $sth->fetchall_arrayref({});
137 $template->param(previousSearches => $previoussearches);
141 $sth->finish;
148 $template->param(searchhistoryview => 1);
150 output_html_with_http_headers $cgi, $cookie, $template->output;