3 # Copyright 2013 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
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.
22 use C4::Auth qw(:DEFAULT get_session);
30 use C4::Search::History;
32 use POSIX qw(strftime);
37 # Getting the template and auth
38 my ($template, $loggedinuser, $cookie) = get_template_and_user(
40 template_name => "opac-search-history.tt",
44 flagsrequired => {borrowers => 1},
49 my $type = $cgi->param('type');
50 my $action = $cgi->param('action') || q{};
51 my $previous = $cgi->param('previous');
53 # If the user is not logged in, we deal with the session
54 unless ( $loggedinuser ) {
55 # Deleting search history
56 if ($cgi->param('action') && $cgi->param('action') eq 'delete') {
57 # Deleting session's search history
58 my $type = $cgi->param('type');
61 @searches = C4::Search::History::get_from_session({ cgi => $cgi });
62 @searches = map { $_->{type} ne $type ? $_ : () } @searches;
64 C4::Search::History::set_to_session({ cgi => $cgi, search_history => \@searches });
66 # Redirecting to this same url so the user won't see the search history link in the header
67 my $uri = $cgi->url();
68 print $cgi->redirect(-uri => $uri);
69 # Showing search history
71 # Getting the searches from session
72 my @current_searches = C4::Search::History::get_from_session({
76 my @current_biblio_searches = map {
77 $_->{type} eq 'biblio' ? $_ : ()
80 my @current_authority_searches = map {
81 $_->{type} eq 'authority' ? $_ : ()
85 current_biblio_searches => \@current_biblio_searches,
86 current_authority_searches => \@current_authority_searches,
90 # And if the user is logged in, we deal with the database
91 my $dbh = C4::Context->dbh;
93 # Deleting search history
94 if ( $action eq 'delete' ) {
95 my $sessionid = defined $previous
96 ? $cgi->cookie("CGISESSID")
98 C4::Search::History::delete(
100 userid => $loggedinuser,
101 sessionid => $sessionid,
103 previous => $previous
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);
110 # Showing search history
112 my $current_searches = C4::Search::History::get({
113 userid => $loggedinuser,
114 sessionid => $cgi->cookie("CGISESSID")
116 my @current_biblio_searches = map {
117 $_->{type} eq 'biblio' ? $_ : ()
118 } @$current_searches;
120 my @current_authority_searches = map {
121 $_->{type} eq 'authority' ? $_ : ()
122 } @$current_searches;
124 my $previous_searches = C4::Search::History::get({
125 userid => $loggedinuser,
126 sessionid => $cgi->cookie("CGISESSID"),
130 my @previous_biblio_searches = map {
131 $_->{type} eq 'biblio' ? $_ : ()
132 } @$previous_searches;
134 my @previous_authority_searches = map {
135 $_->{type} eq 'authority' ? $_ : ()
136 } @$previous_searches;
139 current_biblio_searches => \@current_biblio_searches,
140 current_authority_searches => \@current_authority_searches,
141 previous_biblio_searches => \@previous_biblio_searches,
142 previous_authority_searches => \@previous_authority_searches,
148 $template->param(searchhistoryview => 1);
150 output_html_with_http_headers $cgi, $cookie, $template->output;