Bug 18289 - Fix t/Prices.t having a Test::DBIx::Class cache issue
[koha.git] / opac / opac-search-history.pl
blobda1196b3054d49138f10fee14a839f21207344c8
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 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;
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 => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
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 ( $action eq 'delete') {
57 # Deleting session's search history
58 my @id = $cgi->multi_param('id');
59 my $all = not scalar( @id );
61 my $type = $cgi->param('type');
62 my @searches = ();
63 unless ( $all ) {
64 @searches = C4::Search::History::get_from_session({ cgi => $cgi });
65 if ( $type ) {
66 @searches = map { $_->{type} ne $type ? $_ : () } @searches;
68 if ( @id ) {
69 @searches = map { my $search = $_; ( grep {/^$search->{id}$/} @id ) ? () : $_ } @searches;
72 C4::Search::History::set_to_session({ cgi => $cgi, search_history => \@searches });
74 # Redirecting to this same url so the user won't see the search history link in the header
75 print $cgi->redirect(-uri => '/cgi-bin/koha/opac-search-history.pl');
76 # Showing search history
77 } else {
78 # Getting the searches from session
79 my @current_searches = C4::Search::History::get_from_session({
80 cgi => $cgi,
81 });
83 my @current_biblio_searches = map {
84 $_->{type} eq 'biblio' ? $_ : ()
85 } @current_searches;
87 my @current_authority_searches = map {
88 $_->{type} eq 'authority' ? $_ : ()
89 } @current_searches;
91 $template->param(
92 current_biblio_searches => \@current_biblio_searches,
93 current_authority_searches => \@current_authority_searches,
96 } else {
97 # And if the user is logged in, we deal with the database
98 my $dbh = C4::Context->dbh;
100 # Deleting search history
101 if ( $action eq 'delete' ) {
102 my @id = $cgi->multi_param('id');
103 if ( @id ) {
104 C4::Search::History::delete(
106 userid => $loggedinuser,
107 id => [ $cgi->param('id') ],
110 } else {
111 C4::Search::History::delete(
113 userid => $loggedinuser,
117 # Redirecting to this same url so the user won't see the search history link in the header
118 print $cgi->redirect(-uri => '/cgi-bin/koha/opac-search-history.pl');
120 # Showing search history
121 } else {
122 my $current_searches = C4::Search::History::get({
123 userid => $loggedinuser,
124 sessionid => $cgi->cookie("CGISESSID")
126 my @current_biblio_searches = map {
127 $_->{type} eq 'biblio' ? $_ : ()
128 } @$current_searches;
130 my @current_authority_searches = map {
131 $_->{type} eq 'authority' ? $_ : ()
132 } @$current_searches;
134 my $previous_searches = C4::Search::History::get({
135 userid => $loggedinuser,
136 sessionid => $cgi->cookie("CGISESSID"),
137 previous => 1
140 my @previous_biblio_searches = map {
141 $_->{type} eq 'biblio' ? $_ : ()
142 } @$previous_searches;
144 my @previous_authority_searches = map {
145 $_->{type} eq 'authority' ? $_ : ()
146 } @$previous_searches;
148 $template->param(
149 current_biblio_searches => \@current_biblio_searches,
150 current_authority_searches => \@current_authority_searches,
151 previous_biblio_searches => \@previous_biblio_searches,
152 previous_authority_searches => \@previous_authority_searches,
158 $template->param(searchhistoryview => 1);
160 output_html_with_http_headers $cgi, $cookie, $template->output, undef, { force_no_caching => 1 };