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