64c696dde77ddf5f6e8c7e77098b5175da00ac8d
[srvgit] / 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::Search::History;
29
30 use URI::Escape;
31 use POSIX qw(strftime);
32
33
34 my $cgi = CGI->new;
35
36 # Getting the template and auth
37 my ($template, $loggedinuser, $cookie) = get_template_and_user(
38     {
39         template_name => "opac-search-history.tt",
40         query => $cgi,
41         type => "opac",
42         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
43     }
44 );
45
46 unless ( C4::Context->preference("EnableOpacSearchHistory") ) {
47     print $cgi->redirect("/cgi-bin/koha/errors/404.pl"); # escape early
48     exit;
49 }
50
51 my $type = $cgi->param('type');
52 my $action = $cgi->param('action') || q{};
53 my $previous = $cgi->param('previous');
54
55 # If the user is not logged in, we deal with the session
56 unless ( $loggedinuser ) {
57     # Deleting search history
58     if ( $action eq 'delete') {
59         # Deleting session's search history
60         my @id = $cgi->multi_param('id');
61         my $all = not scalar( @id );
62
63         my $type = $cgi->param('type');
64         my @searches = ();
65         unless ( $all ) {
66             @searches = C4::Search::History::get_from_session({ cgi => $cgi });
67             if ( $type ) {
68                 @searches = map { $_->{type} ne $type ? $_ : () } @searches;
69             }
70             if ( @id ) {
71                 @searches = map { my $search = $_; ( grep { $_ eq $search->{id} } @id ) ? () : $_ } @searches;
72             }
73         }
74         C4::Search::History::set_to_session({ cgi => $cgi, search_history => \@searches });
75
76         # Redirecting to this same url so the user won't see the search history link in the header
77         print $cgi->redirect(-uri => '/cgi-bin/koha/opac-search-history.pl');
78     # Showing search history
79     } else {
80         # Getting the searches from session
81         my @current_searches = C4::Search::History::get_from_session({
82             cgi => $cgi,
83         });
84
85         my @current_biblio_searches = map {
86             $_->{type} eq 'biblio' ? $_ : ()
87         } @current_searches;
88
89         my @current_authority_searches = map {
90             $_->{type} eq 'authority' ? $_ : ()
91         } @current_searches;
92
93         $template->param(
94             current_biblio_searches => \@current_biblio_searches,
95             current_authority_searches => \@current_authority_searches,
96         );
97     }
98 } else {
99     # And if the user is logged in, we deal with the database
100     my $dbh = C4::Context->dbh;
101
102     # Deleting search history
103     if ( $action eq 'delete' ) {
104         my @id = $cgi->multi_param('id');
105         if ( @id ) {
106             C4::Search::History::delete(
107                 {
108                     userid => $loggedinuser,
109                     id     => [ $cgi->param('id') ],
110                 }
111             );
112         } else {
113             C4::Search::History::delete(
114                 {
115                    userid => $loggedinuser,
116                 }
117             );
118         }
119         # Redirecting to this same url so the user won't see the search history link in the header
120         print $cgi->redirect(-uri => '/cgi-bin/koha/opac-search-history.pl');
121
122     # Showing search history
123     } else {
124         my $current_searches = C4::Search::History::get({
125             userid => $loggedinuser,
126             sessionid => $cgi->cookie("CGISESSID")
127         });
128         my @current_biblio_searches = map {
129             $_->{type} eq 'biblio' ? $_ : ()
130         } @$current_searches;
131
132         my @current_authority_searches = map {
133             $_->{type} eq 'authority' ? $_ : ()
134         } @$current_searches;
135
136         my $previous_searches = C4::Search::History::get({
137             userid => $loggedinuser,
138             sessionid => $cgi->cookie("CGISESSID"),
139             previous => 1
140         });
141
142         my @previous_biblio_searches = map {
143             $_->{type} eq 'biblio' ? $_ : ()
144         } @$previous_searches;
145
146         my @previous_authority_searches = map {
147             $_->{type} eq 'authority' ? $_ : ()
148         } @$previous_searches;
149
150         $template->param(
151             current_biblio_searches => \@current_biblio_searches,
152             current_authority_searches => \@current_authority_searches,
153             previous_biblio_searches => \@previous_biblio_searches,
154             previous_authority_searches => \@previous_authority_searches,
155
156         );
157     }
158 }
159
160 $template->param(searchhistoryview => 1);
161
162 output_html_with_http_headers $cgi, $cookie, $template->output, undef, { force_no_caching => 1 };