Bug 15117: Transfer Order: Better user information and translation handling
[koha.git] / opac / opac-search-history.pl
blob736a88eed2750d75598d6d1cdbfafb2e496d41e2
1 #!/usr/bin/perl
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>.
20 use Modern::Perl;
22 use C4::Auth qw(:DEFAULT get_session);
23 use CGI;
24 use C4::Context;
25 use C4::Output;
26 use C4::Log;
27 use C4::Items;
28 use C4::Debug;
29 use C4::Dates;
30 use C4::Search::History;
31 use URI::Escape;
32 use POSIX qw(strftime);
35 my $cgi = new CGI;
37 # Getting the template and auth
38 my ($template, $loggedinuser, $cookie) = get_template_and_user(
40 template_name => "opac-search-history.tt",
41 query => $cgi,
42 type => "opac",
43 authnotrequired => 1,
44 flagsrequired => {borrowers => 1},
45 debug => 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');
59 my @searches = ();
60 if ( $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
70 } else {
71 # Getting the searches from session
72 my @current_searches = C4::Search::History::get_from_session({
73 cgi => $cgi,
74 });
76 my @current_biblio_searches = map {
77 $_->{type} eq 'biblio' ? $_ : ()
78 } @current_searches;
80 my @current_authority_searches = map {
81 $_->{type} eq 'authority' ? $_ : ()
82 } @current_searches;
84 $template->param(
85 current_biblio_searches => \@current_biblio_searches,
86 current_authority_searches => \@current_authority_searches,
89 } else {
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")
97 : q{};
98 C4::Search::History::delete(
100 userid => $loggedinuser,
101 sessionid => $sessionid,
102 type => $type,
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
111 } else {
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"),
127 previous => 1
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;
138 $template->param(
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;