Bug 9552 - BIB1 Relation "Greater Than" Attribute Not Mapped Properly in CCL.Properties
[koha.git] / opac / opac-search-history.pl
blob1b76c558945e5c87e8e1f3c393d2a7c1a60f2ea9
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 # If the user is not logged in, we deal with the cookie
49 if (!$loggedinuser) {
51 # Deleting search history
52 if ($cgi->param('action') && $cgi->param('action') eq 'delete') {
53 # Deleting cookie's content
54 my $recentSearchesCookie = $cgi->cookie(
55 -name => 'KohaOpacRecentSearches',
56 -value => freeze([]),
57 -expires => ''
60 # Redirecting to this same url with the cookie in the headers so it's deleted immediately
61 my $uri = $cgi->url();
62 print $cgi->redirect(-uri => $uri,
63 -cookie => $recentSearchesCookie);
65 # Showing search history
66 } else {
68 # Getting the cookie
69 my $searchcookie = $cgi->cookie('KohaOpacRecentSearches');
70 if ($searchcookie && thaw(uri_unescape($searchcookie))) {
71 my @recentSearches = @{thaw(uri_unescape($searchcookie))};
72 if (@recentSearches) {
74 # As the dates are stored as unix timestamps, let's do some formatting
75 foreach my $asearch (@recentSearches) {
77 # We create an iso date from the unix timestamp
78 my $isodate = strftime "%Y-%m-%d", localtime($asearch->{'time'});
80 # So we can create a C4::Dates object, to get the date formatted according to the dateformat syspref
81 my $date = C4::Dates->new($isodate, "iso");
82 my $sysprefdate = $date->output("syspref");
84 # We also get the time of the day from the unix timestamp
85 my $time = strftime " %H:%M:%S", localtime($asearch->{'time'});
87 # And we got our human-readable date :
88 $asearch->{'time'} = $sysprefdate . $time;
91 $template->param(recentSearches => \@recentSearches);
95 } else {
96 # And if the user is logged in, we deal with the database
98 my $dbh = C4::Context->dbh;
100 # Deleting search history
101 if ($cgi->param('action') && $cgi->param('action') eq 'delete') {
102 my $query = "DELETE FROM search_history WHERE userid = ?";
103 my $sth = $dbh->prepare($query);
104 $sth->execute($loggedinuser);
106 # Redirecting to this same url so the user won't see the search history link in the header
107 my $uri = $cgi->url();
108 print $cgi->redirect($uri);
111 # Showing search history
112 } else {
114 my $date = C4::Dates->new();
115 my $dateformat = $date->DHTMLcalendar() . " %H:%i:%S"; # Current syspref date format + standard time format
117 # Getting the data with date format work done by mysql
118 my $query = "SELECT userid, sessionid, query_desc, query_cgi, total, DATE_FORMAT(time, \"$dateformat\") as time FROM search_history WHERE userid = ? AND sessionid = ?";
119 my $sth = $dbh->prepare($query);
120 $sth->execute($loggedinuser, $cgi->cookie("CGISESSID"));
121 my $searches = $sth->fetchall_arrayref({});
122 $template->param(recentSearches => $searches);
124 # Getting searches from previous sessions
125 $query = "SELECT COUNT(*) FROM search_history WHERE userid = ? AND sessionid != ?";
126 $sth = $dbh->prepare($query);
127 $sth->execute($loggedinuser, $cgi->cookie("CGISESSID"));
129 # If at least one search from previous sessions has been performed
130 if ($sth->fetchrow_array > 0) {
131 $query = "SELECT userid, sessionid, query_desc, query_cgi, total, DATE_FORMAT(time, \"$dateformat\") as time FROM search_history WHERE userid = ? AND sessionid != ?";
132 $sth = $dbh->prepare($query);
133 $sth->execute($loggedinuser, $cgi->cookie("CGISESSID"));
134 my $previoussearches = $sth->fetchall_arrayref({});
135 $template->param(previousSearches => $previoussearches);
139 $sth->finish;
146 $template->param(searchhistoryview => 1);
148 output_html_with_http_headers $cgi, $cookie, $template->output;