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 ( $action eq 'delete') {
57 # Deleting session's search history
58 my @id = $cgi->param('id');
59 my $all = not scalar( @id );
61 my $type = $cgi->param('type');
64 @searches = C4::Search::History::get_from_session({ cgi => $cgi });
66 @searches = map { $_->{type} ne $type ? $_ : () } @searches;
69 @searches = map { my $search = $_; ( grep {/^$search->{id}$/} @id ) ? () : $_ } @searches;
72 C4::Search::History::set_to_session({ cgi => $cgi, search_history => \@searches });
74 # Redirecting to this same url so the user won't see the search history link in the header
75 my $uri = $cgi->url();
76 print $cgi->redirect(-uri => $uri);
77 # Showing search history
79 # Getting the searches from session
80 my @current_searches = C4::Search::History::get_from_session({
84 my @current_biblio_searches = map {
85 $_->{type} eq 'biblio' ? $_ : ()
88 my @current_authority_searches = map {
89 $_->{type} eq 'authority' ? $_ : ()
93 current_biblio_searches => \@current_biblio_searches,
94 current_authority_searches => \@current_authority_searches,
98 # And if the user is logged in, we deal with the database
99 my $dbh = C4::Context->dbh;
101 # Deleting search history
102 if ( $action eq 'delete' ) {
103 my @id = $cgi->param('id');
105 C4::Search::History::delete(
107 id => [ $cgi->param('id') ],
111 C4::Search::History::delete(
113 userid => $loggedinuser,
117 # Redirecting to this same url so the user won't see the search history link in the header
118 my $uri = $cgi->url();
119 print $cgi->redirect($uri);
121 # Showing search history
123 my $current_searches = C4::Search::History::get({
124 userid => $loggedinuser,
125 sessionid => $cgi->cookie("CGISESSID")
127 my @current_biblio_searches = map {
128 $_->{type} eq 'biblio' ? $_ : ()
129 } @$current_searches;
131 my @current_authority_searches = map {
132 $_->{type} eq 'authority' ? $_ : ()
133 } @$current_searches;
135 my $previous_searches = C4::Search::History::get({
136 userid => $loggedinuser,
137 sessionid => $cgi->cookie("CGISESSID"),
141 my @previous_biblio_searches = map {
142 $_->{type} eq 'biblio' ? $_ : ()
143 } @$previous_searches;
145 my @previous_authority_searches = map {
146 $_->{type} eq 'authority' ? $_ : ()
147 } @$previous_searches;
150 current_biblio_searches => \@current_biblio_searches,
151 current_authority_searches => \@current_authority_searches,
152 previous_biblio_searches => \@previous_biblio_searches,
153 previous_authority_searches => \@previous_authority_searches,
159 $template->param(searchhistoryview => 1);
161 output_html_with_http_headers $cgi, $cookie, $template->output;