Bug 12150: Add missing space and correct innerHTML typo
[koha.git] / opac / opac-search-history.pl
1 #!/usr/bin/perl
2
3 # Copyright 2013 BibLibre SARL
4 #
5 # This file is part of Koha.
6 #
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
10 # version.
11 #
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.
15 #
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.
19
20 use Modern::Perl;
21
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);
33
34
35 my $cgi = new CGI;
36
37 # Getting the template and auth
38 my ($template, $loggedinuser, $cookie) = get_template_and_user(
39     {
40         template_name => "opac-search-history.tmpl",
41         query => $cgi,
42         type => "opac",
43         authnotrequired => 1,
44         flagsrequired => {borrowers => 1},
45         debug => 1,
46     }
47 );
48
49 my $type = $cgi->param('type');
50 my $action = $cgi->param('action') || q{};
51 my $previous = $cgi->param('previous');
52
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;
63         }
64         C4::Search::History::set_to_session({ cgi => $cgi, search_history => \@searches });
65
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         });
75
76         my @current_biblio_searches = map {
77             $_->{type} eq 'biblio' ? $_ : ()
78         } @current_searches;
79
80         my @current_authority_searches = map {
81             $_->{type} eq 'authority' ? $_ : ()
82         } @current_searches;
83
84         $template->param(
85             current_biblio_searches => \@current_biblio_searches,
86             current_authority_searches => \@current_authority_searches,
87         );
88     }
89 } else {
90     # And if the user is logged in, we deal with the database
91     my $dbh = C4::Context->dbh;
92
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(
99             {
100                 userid => $loggedinuser,
101                 sessionid => $sessionid,
102                 type => $type,
103                 previous => $previous
104             }
105         );
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);
109
110     # Showing search history
111     } else {
112         my $current_searches = C4::Search::History::get({
113             userid => $loggedinuser,
114             sessionid => $cgi->cookie("CGISESSID")
115         });
116         my @current_biblio_searches = map {
117             $_->{type} eq 'biblio' ? $_ : ()
118         } @$current_searches;
119
120         my @current_authority_searches = map {
121             $_->{type} eq 'authority' ? $_ : ()
122         } @$current_searches;
123
124         my $previous_searches = C4::Search::History::get({
125             userid => $loggedinuser,
126             sessionid => $cgi->cookie("CGISESSID"),
127             previous => 1
128         });
129
130         my @previous_biblio_searches = map {
131             $_->{type} eq 'biblio' ? $_ : ()
132         } @$previous_searches;
133
134         my @previous_authority_searches = map {
135             $_->{type} eq 'authority' ? $_ : ()
136         } @$previous_searches;
137
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,
143
144         );
145     }
146 }
147
148 $template->param(searchhistoryview => 1);
149
150 output_html_with_http_headers $cgi, $cookie, $template->output;