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( get_template_and_user );
25 use C4::Output qw( output_html_with_http_headers );
26 use C4::Search::History;
32 # Getting the template and auth
33 my ($template, $loggedinuser, $cookie) = get_template_and_user(
35 template_name => "opac-search-history.tt",
38 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
42 unless ( C4::Context->preference("EnableOpacSearchHistory") ) {
43 print $cgi->redirect("/cgi-bin/koha/errors/404.pl"); # escape early
47 my $type = $cgi->param('type');
48 my $action = $cgi->param('action') || q{};
49 my $previous = $cgi->param('previous');
51 # If the user is not logged in, we deal with the session
52 unless ( $loggedinuser ) {
53 # Deleting search history
54 if ( $action eq 'delete') {
55 # Deleting session's search history
56 my @id = $cgi->multi_param('id');
57 my $all = not scalar( @id );
59 my $type = $cgi->param('type');
62 @searches = C4::Search::History::get_from_session({ cgi => $cgi });
64 @searches = map { $_->{type} ne $type ? $_ : () } @searches;
67 @searches = map { my $search = $_; ( grep { $_ eq $search->{id} } @id ) ? () : $_ } @searches;
70 C4::Search::History::set_to_session({ cgi => $cgi, search_history => \@searches });
72 # Redirecting to this same url so the user won't see the search history link in the header
73 print $cgi->redirect(-uri => '/cgi-bin/koha/opac-search-history.pl');
74 # Showing search history
76 # Getting the searches from session
77 my @current_searches = C4::Search::History::get_from_session({
81 my @current_biblio_searches = map {
82 $_->{type} eq 'biblio' ? $_ : ()
85 my @current_authority_searches = map {
86 $_->{type} eq 'authority' ? $_ : ()
90 current_biblio_searches => \@current_biblio_searches,
91 current_authority_searches => \@current_authority_searches,
95 # And if the user is logged in, we deal with the database
96 my $dbh = C4::Context->dbh;
98 # Deleting search history
99 if ( $action eq 'delete' ) {
100 my @id = $cgi->multi_param('id');
102 C4::Search::History::delete(
104 userid => $loggedinuser,
105 id => [ $cgi->param('id') ],
109 C4::Search::History::delete(
111 userid => $loggedinuser,
115 # Redirecting to this same url so the user won't see the search history link in the header
116 print $cgi->redirect(-uri => '/cgi-bin/koha/opac-search-history.pl');
118 # Showing search history
120 my $current_searches = C4::Search::History::get({
121 userid => $loggedinuser,
122 sessionid => $cgi->cookie("CGISESSID")
124 my @current_biblio_searches = map {
125 $_->{type} eq 'biblio' ? $_ : ()
126 } @$current_searches;
128 my @current_authority_searches = map {
129 $_->{type} eq 'authority' ? $_ : ()
130 } @$current_searches;
132 my $previous_searches = C4::Search::History::get({
133 userid => $loggedinuser,
134 sessionid => $cgi->cookie("CGISESSID"),
138 my @previous_biblio_searches = map {
139 $_->{type} eq 'biblio' ? $_ : ()
140 } @$previous_searches;
142 my @previous_authority_searches = map {
143 $_->{type} eq 'authority' ? $_ : ()
144 } @$previous_searches;
147 current_biblio_searches => \@current_biblio_searches,
148 current_authority_searches => \@current_authority_searches,
149 previous_biblio_searches => \@previous_biblio_searches,
150 previous_authority_searches => \@previous_authority_searches,
156 $template->param(searchhistoryview => 1);
158 output_html_with_http_headers $cgi, $cookie, $template->output, undef, { force_no_caching => 1 };