MT 1587 : CSV export for cart and shelves, with the ability to define different expor...
[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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use C4::Auth qw(:DEFAULT get_session);
22 use CGI;
23 use Storable qw(freeze thaw);
24 use C4::Context;
25 use C4::Output;
26 use C4::Log;
27 use C4::Items;
28 use C4::Debug;
29 use C4::Dates;
30 use URI::Escape;
31 use POSIX qw(strftime);
32
33
34 my $cgi = new CGI;
35
36 # Getting the template and auth
37 my ($template, $loggedinuser, $cookie)
38 = get_template_and_user({template_name => "opac-search-history.tmpl",
39                                 query => $cgi,
40                                 type => "opac",
41                                 authnotrequired => 1,
42                                 flagsrequired => {borrowers => 1},
43                                 debug => 1,
44                                 });
45
46 $template->param(dateformat => C4::Context->preference("dateformat"));
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 => freeze([]),
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         # 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) {
73
74                 # As the dates are stored as unix timestamps, let's do some formatting
75                 foreach my $asearch (@recentSearches) {
76
77                     # We create an iso date from the unix timestamp
78                     my $isodate = strftime "%Y-%m-%d", localtime($asearch->{'time'});
79
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");
83                     
84                     # We also get the time of the day from the unix timestamp
85                     my $time = strftime " %H:%M:%S", localtime($asearch->{'time'});
86
87                     # And we got our human-readable date : 
88                     $asearch->{'time'} = $sysprefdate . $time;
89                 }
90
91                 $template->param(recentSearches => \@recentSearches);
92             }
93         }
94     }
95 } else {
96 # And if the user is logged in, we deal with the database
97    
98     my $dbh = C4::Context->dbh;
99
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);
105
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);
109
110
111     # Showing search history
112     } else {
113
114         my $date = C4::Dates->new();
115         my $dateformat = $date->DHTMLcalendar() . " %H:%i:%S"; # Current syspref date format + standard time format
116
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);
123         
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"));
128
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);
136         
137         }
138
139         $sth->finish;
140
141
142     }
143
144 }
145 output_html_with_http_headers $cgi, $cookie, $template->output;
146
147