Bug 9916 - Use DataTables in the OPAC
[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 ParseSearchHistoryCookie);
24 use CGI;
25 use JSON qw/decode_json encode_json/;
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 # If the user is not logged in, we deal with the cookie
49 if (!$loggedinuser) {
50
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 => encode_json([]),
57             -expires => ''
58             );
59
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);
64
65     # Showing search history
66     } else {
67
68         my @recentSearches = ParseSearchHistoryCookie($cgi);
69             if (@recentSearches) {
70
71                 # As the dates are stored as unix timestamps, let's do some formatting
72                 foreach my $asearch (@recentSearches) {
73
74                     # We create an iso date from the unix timestamp
75                     my $isodate = strftime "%Y-%m-%d", localtime($asearch->{'time'});
76
77                     # So we can create a C4::Dates object, to get the date formatted according to the dateformat syspref
78                     my $date = C4::Dates->new($isodate, "iso");
79                     my $sysprefdate = $date->output("syspref");
80                     
81                     # We also get the time of the day from the unix timestamp
82                     my $time = strftime " %H:%M:%S", localtime($asearch->{'time'});
83
84                     # And we got our human-readable date : 
85                     $asearch->{'time'} = $sysprefdate . $time;
86                 }
87
88                 $template->param(recentSearches => \@recentSearches);
89             }
90     }
91 } else {
92 # And if the user is logged in, we deal with the database
93    
94     my $dbh = C4::Context->dbh;
95
96     # Deleting search history
97     if ($cgi->param('action') && $cgi->param('action') eq 'delete') {
98         my $query = "DELETE FROM search_history WHERE userid = ?";
99         my $sth   = $dbh->prepare($query);
100         $sth->execute($loggedinuser);
101
102         # Redirecting to this same url so the user won't see the search history link in the header
103         my $uri = $cgi->url();
104         print $cgi->redirect($uri);
105
106
107     # Showing search history
108     } else {
109
110         my $date = C4::Dates->new();
111         my $dateformat = $date->DHTMLcalendar() . " %H:%i:%S"; # Current syspref date format + standard time format
112
113         # Getting the data with date format work done by mysql
114     my $query = "SELECT userid, sessionid, query_desc, query_cgi, total, time FROM search_history WHERE userid = ? AND sessionid = ?";
115         my $sth   = $dbh->prepare($query);
116         $sth->execute($loggedinuser, $cgi->cookie("CGISESSID"));
117         my $searches = $sth->fetchall_arrayref({});
118         $template->param(recentSearches => $searches);
119         
120         # Getting searches from previous sessions
121         $query = "SELECT COUNT(*) FROM search_history WHERE userid = ? AND sessionid != ?";
122         $sth   = $dbh->prepare($query);
123         $sth->execute($loggedinuser, $cgi->cookie("CGISESSID"));
124
125         # If at least one search from previous sessions has been performed
126         if ($sth->fetchrow_array > 0) {
127         $query = "SELECT userid, sessionid, query_desc, query_cgi, total, time FROM search_history WHERE userid = ? AND sessionid != ?";
128             $sth   = $dbh->prepare($query);
129             $sth->execute($loggedinuser, $cgi->cookie("CGISESSID"));
130             my $previoussearches = $sth->fetchall_arrayref({});
131             $template->param(previousSearches => $previoussearches);
132         
133         }
134
135         $sth->finish;
136
137
138     }
139
140 }
141
142 $template->param(searchhistoryview => 1);
143
144 output_html_with_http_headers $cgi, $cookie, $template->output;
145
146