Bug 12497: Fix search history non-accessible when OPAC was private
[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
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use C4::Auth qw(:DEFAULT get_session);
23 use CGI qw ( -utf8 );
24 use C4::Context;
25 use C4::Output;
26 use C4::Log;
27 use C4::Items;
28 use C4::Debug;
29 use C4::Search::History;
30
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.tt",
41         query => $cgi,
42         type => "opac",
43         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
44         debug => 1,
45     }
46 );
47
48 my $type = $cgi->param('type');
49 my $action = $cgi->param('action') || q{};
50 my $previous = $cgi->param('previous');
51
52 # If the user is not logged in, we deal with the session
53 unless ( $loggedinuser ) {
54     # Deleting search history
55     if ( $action eq 'delete') {
56         # Deleting session's search history
57         my @id = $cgi->multi_param('id');
58         my $all = not scalar( @id );
59
60         my $type = $cgi->param('type');
61         my @searches = ();
62         unless ( $all ) {
63             @searches = C4::Search::History::get_from_session({ cgi => $cgi });
64             if ( $type ) {
65                 @searches = map { $_->{type} ne $type ? $_ : () } @searches;
66             }
67             if ( @id ) {
68                 @searches = map { my $search = $_; ( grep {/^$search->{id}$/} @id ) ? () : $_ } @searches;
69             }
70         }
71         C4::Search::History::set_to_session({ cgi => $cgi, search_history => \@searches });
72
73         # Redirecting to this same url so the user won't see the search history link in the header
74         print $cgi->redirect(-uri => '/cgi-bin/koha/opac-search-history.pl');
75     # Showing search history
76     } else {
77         # Getting the searches from session
78         my @current_searches = C4::Search::History::get_from_session({
79             cgi => $cgi,
80         });
81
82         my @current_biblio_searches = map {
83             $_->{type} eq 'biblio' ? $_ : ()
84         } @current_searches;
85
86         my @current_authority_searches = map {
87             $_->{type} eq 'authority' ? $_ : ()
88         } @current_searches;
89
90         $template->param(
91             current_biblio_searches => \@current_biblio_searches,
92             current_authority_searches => \@current_authority_searches,
93         );
94     }
95 } else {
96     # And if the user is logged in, we deal with the database
97     my $dbh = C4::Context->dbh;
98
99     # Deleting search history
100     if ( $action eq 'delete' ) {
101         my @id = $cgi->multi_param('id');
102         if ( @id ) {
103             C4::Search::History::delete(
104                 {
105                     userid => $loggedinuser,
106                     id     => [ $cgi->param('id') ],
107                 }
108             );
109         } else {
110             C4::Search::History::delete(
111                 {
112                    userid => $loggedinuser,
113                 }
114             );
115         }
116         # Redirecting to this same url so the user won't see the search history link in the header
117         print $cgi->redirect(-uri => '/cgi-bin/koha/opac-search-history.pl');
118
119     # Showing search history
120     } else {
121         my $current_searches = C4::Search::History::get({
122             userid => $loggedinuser,
123             sessionid => $cgi->cookie("CGISESSID")
124         });
125         my @current_biblio_searches = map {
126             $_->{type} eq 'biblio' ? $_ : ()
127         } @$current_searches;
128
129         my @current_authority_searches = map {
130             $_->{type} eq 'authority' ? $_ : ()
131         } @$current_searches;
132
133         my $previous_searches = C4::Search::History::get({
134             userid => $loggedinuser,
135             sessionid => $cgi->cookie("CGISESSID"),
136             previous => 1
137         });
138
139         my @previous_biblio_searches = map {
140             $_->{type} eq 'biblio' ? $_ : ()
141         } @$previous_searches;
142
143         my @previous_authority_searches = map {
144             $_->{type} eq 'authority' ? $_ : ()
145         } @$previous_searches;
146
147         $template->param(
148             current_biblio_searches => \@current_biblio_searches,
149             current_authority_searches => \@current_authority_searches,
150             previous_biblio_searches => \@previous_biblio_searches,
151             previous_authority_searches => \@previous_authority_searches,
152
153         );
154     }
155 }
156
157 $template->param(searchhistoryview => 1);
158
159 output_html_with_http_headers $cgi, $cookie, $template->output, undef, { force_no_caching => 1 };