Bumping database version to 3.05.00.015
[koha.git] / opac / opac-search-history.pl
1 #!/usr/bin/perl
2
3 # Copyright 2009 BibLibre SARL
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
15 #
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.
19
20 use strict;
21 use warnings;
22
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);
34
35
36 my $cgi = new CGI;
37
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                                 });
47
48 $template->param(dateformat => C4::Context->preference("dateformat"));
49
50 # If the user is not logged in, we deal with the cookie
51 if (!$loggedinuser) {
52
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 => ''
60             );
61
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);
66
67     # Showing search history
68     } else {
69
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) {
75
76                 # As the dates are stored as unix timestamps, let's do some formatting
77                 foreach my $asearch (@recentSearches) {
78
79                     # We create an iso date from the unix timestamp
80                     my $isodate = strftime "%Y-%m-%d", localtime($asearch->{'time'});
81
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");
85                     
86                     # We also get the time of the day from the unix timestamp
87                     my $time = strftime " %H:%M:%S", localtime($asearch->{'time'});
88
89                     # And we got our human-readable date : 
90                     $asearch->{'time'} = $sysprefdate . $time;
91                 }
92
93                 $template->param(recentSearches => \@recentSearches);
94             }
95         }
96     }
97 } else {
98 # And if the user is logged in, we deal with the database
99    
100     my $dbh = C4::Context->dbh;
101
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);
107
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);
111
112
113     # Showing search history
114     } else {
115
116         my $date = C4::Dates->new();
117         my $dateformat = $date->DHTMLcalendar() . " %H:%i:%S"; # Current syspref date format + standard time format
118
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);
125         
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"));
130
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);
138         
139         }
140
141         $sth->finish;
142
143
144     }
145
146 }
147
148 $template->param(searchhistoryview => 1);
149
150 output_html_with_http_headers $cgi, $cookie, $template->output;
151
152