Bug 766: Create a plugin routine to build dropdown list
[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 ParseSearchHistorySession SetSearchHistorySession);
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)= get_template_and_user({template_name => "opac-search-history.tmpl",
40                                 query => $cgi,
41                                 type => "opac",
42                                 authnotrequired => 1,
43                                 flagsrequired => {borrowers => 1},
44                                 debug => 1,
45                                 });
46
47 # If the user is not logged in, we deal with the session
48 if (!$loggedinuser) {
49
50     # Deleting search history
51     if ($cgi->param('action') && $cgi->param('action') eq 'delete') {
52         # Deleting session's search history
53         SetSearchHistorySession($cgi, []);
54
55         # Redirecting to this same url so the user won't see the search history link in the header
56         my $uri = $cgi->url();
57         print $cgi->redirect(-uri => $uri);
58     # Showing search history
59     } else {
60
61         my @recentSearches = ParseSearchHistorySession($cgi);
62             if (@recentSearches) {
63
64                 # As the dates are stored as unix timestamps, let's do some formatting
65                 foreach my $asearch (@recentSearches) {
66
67                     # We create an iso date from the unix timestamp
68                     my $isodate = strftime "%Y-%m-%d", localtime($asearch->{'time'});
69
70                     # We also get the time of the day from the unix timestamp
71                     my $time = strftime " %H:%M:%S", localtime($asearch->{'time'});
72
73                     # And we got our human-readable date : 
74             $asearch->{'time'} = $isodate . $time;
75                 }
76
77                 $template->param(recentSearches => \@recentSearches);
78             }
79     }
80 } else {
81 # And if the user is logged in, we deal with the database
82    
83     my $dbh = C4::Context->dbh;
84
85     # Deleting search history
86     if ($cgi->param('action') && $cgi->param('action') eq 'delete') {
87         my $query = "DELETE FROM search_history WHERE userid = ?";
88         my $sth   = $dbh->prepare($query);
89         $sth->execute($loggedinuser);
90
91         # Redirecting to this same url so the user won't see the search history link in the header
92         my $uri = $cgi->url();
93         print $cgi->redirect($uri);
94
95
96     # Showing search history
97     } else {
98
99         my $date = C4::Dates->new();
100         my $dateformat = $date->DHTMLcalendar() . " %H:%i:%S"; # Current syspref date format + standard time format
101
102         # Getting the data with date format work done by mysql
103     my $query = "SELECT userid, sessionid, query_desc, query_cgi, total, time FROM search_history WHERE userid = ? AND sessionid = ?";
104         my $sth   = $dbh->prepare($query);
105         $sth->execute($loggedinuser, $cgi->cookie("CGISESSID"));
106         my $searches = $sth->fetchall_arrayref({});
107         $template->param(recentSearches => $searches);
108         
109         # Getting searches from previous sessions
110         $query = "SELECT COUNT(*) FROM search_history WHERE userid = ? AND sessionid != ?";
111         $sth   = $dbh->prepare($query);
112         $sth->execute($loggedinuser, $cgi->cookie("CGISESSID"));
113
114         # If at least one search from previous sessions has been performed
115         if ($sth->fetchrow_array > 0) {
116         $query = "SELECT userid, sessionid, query_desc, query_cgi, total, time FROM search_history WHERE userid = ? AND sessionid != ?";
117             $sth   = $dbh->prepare($query);
118             $sth->execute($loggedinuser, $cgi->cookie("CGISESSID"));
119             my $previoussearches = $sth->fetchall_arrayref({});
120             $template->param(previousSearches => $previoussearches);
121         
122         }
123
124         $sth->finish;
125
126
127     }
128
129 }
130
131 $template->param(searchhistoryview => 1);
132
133 output_html_with_http_headers $cgi, $cookie, $template->output;
134
135