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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use C4::Auth qw(:DEFAULT get_session);
28 use C4::Search::History;
31 use POSIX qw(strftime);
36 # Getting the template and auth
37 my ($template, $loggedinuser, $cookie) = get_template_and_user(
39 template_name => "opac-search-history.tt",
42 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
46 unless ( C4::Context->preference("EnableOpacSearchHistory") ) {
47 print $cgi->redirect("/cgi-bin/koha/errors/404.pl"); # escape early
51 my $type = $cgi->param('type');
52 my $action = $cgi->param('action') || q{};
53 my $previous = $cgi->param('previous');
55 # If the user is not logged in, we deal with the session
56 unless ( $loggedinuser ) {
57 # Deleting search history
58 if ( $action eq 'delete') {
59 # Deleting session's search history
60 my @id = $cgi->multi_param('id');
61 my $all = not scalar( @id );
63 my $type = $cgi->param('type');
66 @searches = C4::Search::History::get_from_session({ cgi => $cgi });
68 @searches = map { $_->{type} ne $type ? $_ : () } @searches;
71 @searches = map { my $search = $_; ( grep { $_ eq $search->{id} } @id ) ? () : $_ } @searches;
74 C4::Search::History::set_to_session({ cgi => $cgi, search_history => \@searches });
76 # Redirecting to this same url so the user won't see the search history link in the header
77 print $cgi->redirect(-uri => '/cgi-bin/koha/opac-search-history.pl');
78 # Showing search history
80 # Getting the searches from session
81 my @current_searches = C4::Search::History::get_from_session({
85 my @current_biblio_searches = map {
86 $_->{type} eq 'biblio' ? $_ : ()
89 my @current_authority_searches = map {
90 $_->{type} eq 'authority' ? $_ : ()
94 current_biblio_searches => \@current_biblio_searches,
95 current_authority_searches => \@current_authority_searches,
99 # And if the user is logged in, we deal with the database
100 my $dbh = C4::Context->dbh;
102 # Deleting search history
103 if ( $action eq 'delete' ) {
104 my @id = $cgi->multi_param('id');
106 C4::Search::History::delete(
108 userid => $loggedinuser,
109 id => [ $cgi->param('id') ],
113 C4::Search::History::delete(
115 userid => $loggedinuser,
119 # Redirecting to this same url so the user won't see the search history link in the header
120 print $cgi->redirect(-uri => '/cgi-bin/koha/opac-search-history.pl');
122 # Showing search history
124 my $current_searches = C4::Search::History::get({
125 userid => $loggedinuser,
126 sessionid => $cgi->cookie("CGISESSID")
128 my @current_biblio_searches = map {
129 $_->{type} eq 'biblio' ? $_ : ()
130 } @$current_searches;
132 my @current_authority_searches = map {
133 $_->{type} eq 'authority' ? $_ : ()
134 } @$current_searches;
136 my $previous_searches = C4::Search::History::get({
137 userid => $loggedinuser,
138 sessionid => $cgi->cookie("CGISESSID"),
142 my @previous_biblio_searches = map {
143 $_->{type} eq 'biblio' ? $_ : ()
144 } @$previous_searches;
146 my @previous_authority_searches = map {
147 $_->{type} eq 'authority' ? $_ : ()
148 } @$previous_searches;
151 current_biblio_searches => \@current_biblio_searches,
152 current_authority_searches => \@current_authority_searches,
153 previous_biblio_searches => \@previous_biblio_searches,
154 previous_authority_searches => \@previous_authority_searches,
160 $template->param(searchhistoryview => 1);
162 output_html_with_http_headers $cgi, $cookie, $template->output, undef, { force_no_caching => 1 };