Bug 9823: QA follow-up for GetReservesFromBiblionumber calls
[koha.git] / opac / search.pl
1 #!/usr/bin/perl
2
3 # Copyright 2012 BibLibre
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::Context;
23 use CGI;
24 use C4::Auth;
25 use C4::Koha;
26 use C4::Output;
27 use Koha::SearchEngine::Search;
28 use Koha::SearchEngine::QueryBuilder;
29 use Koha::SearchEngine::FacetsBuilder;
30
31 my $cgi = new CGI;
32
33 my $template_name;
34 my $template_type = "basic";
35 if ( $cgi->param("idx") or $cgi->param("q") ) {
36     $template_name = 'search/results.tt';
37 } else {
38     $template_name = 'search/advsearch.tt';
39     $template_type = 'advsearch';
40 }
41
42 # load the template
43 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
44     {   template_name   => $template_name,
45         query           => $cgi,
46         type            => "opac",
47         authnotrequired => 1,
48     }
49 );
50
51 my $format = $cgi->param("format") || 'html';
52
53
54
55
56 # load the Type stuff
57 my $itemtypes = GetItemTypes;
58
59 my $page = $cgi->param("page") || 1;
60 my $count = $cgi->param('count') || C4::Context->preference('OPACnumSearchResults') || 20;
61 $count = 5;
62 my $q = $cgi->param("q");
63 my $builder = Koha::SearchEngine::QueryBuilder->new;
64 $q = $builder->build_query( $q );
65 my $search_service = Koha::SearchEngine::Search->new;
66
67 # load the sorting stuff
68 my $sort_by = $cgi->param('sort_by')
69         || C4::Context->preference('OPACdefaultSortField') . ' ' . C4::Context->preference('OPACdefaultSortOrder');
70
71 my $search_engine_config = Koha::SearchEngine->new->config;
72 my $sortable_indexes = $search_engine_config->sortable_indexes;
73 my ( $sort_indexname, $sort_order );
74 ( $sort_indexname, $sort_order ) = ($1, $2) if ( $sort_by =~ m/^(.*) (asc|desc)$/ );
75 my $sort_by_indexname = eval {
76     [
77         map {
78             $_->{code} eq $sort_indexname
79                 ? 'srt_' . $_->{type} . '_' . $_->{code} . ' ' . $sort_order
80                 : ()
81         } @$sortable_indexes
82     ]->[0]
83 };
84
85 # This array is used to build facets GUI
86 my %filters;
87 my @tplfilters;
88 for my $filter ( $cgi->param('filters') ) {
89     next if not $filter;
90     my ($k, @v) = $filter =~ /(?: \\. | [^:] )+/xg;
91     my $v = join ':', @v;
92     push @{$filters{$k}}, $v;
93     $v =~ s/^"(.*)"$/$1/; # Remove quotes around
94     push @tplfilters, {
95         'var' => $k,
96         'val' => $v,
97     };
98 }
99 push @{$filters{recordtype}}, 'biblio';
100
101 my $results = $search_service->search(
102     $q,
103     \%filters,
104     {
105         page => $page,
106         count => $count,
107         sort => $sort_by_indexname,
108         facets => 1,
109         fl => ["ste_title", "str_author", 'int_biblionumber'],
110     }
111 );
112
113 if ($results->{error}){
114     $template->param(query_error => $results->{error});
115     output_with_http_headers $cgi, $cookie, $template->output, 'html';
116     exit;
117 }
118
119
120 # populate results with records
121 my @r;
122 for my $searchresult ( @{ $results->items } ) {
123     my $biblionumber = $searchresult->{values}->{recordid};
124
125     my $nr;
126     while ( my ($k, $v) = each %{$searchresult->{values}} ) {
127         my $nk = $k;
128         $nk =~ s/^[^_]*_(.*)$/$1/;
129         $nr->{$nk} = ref $v ? shift @{$v} : $v;
130     }
131     push( @r, $nr );
132 }
133
134 # build facets
135 my $facets_builder = Koha::SearchEngine::FacetsBuilder->new;
136 my @facets_loop = $facets_builder->build_facets( $results, $search_engine_config->facetable_indexes, \%filters );
137
138 my $total = $results->{pager}->{total_entries};
139 my $pager = Data::Pagination->new(
140     $total,
141     $count,
142     20,
143     $page,
144 );
145
146 # params we want to pass for all actions require another query (pagination, sort, facets)
147 my @follower_params = map { {
148     var => 'filters',
149     val => $_->{var}.':"'.$_->{val}.'"'
150 } } @tplfilters;
151 push @follower_params, { var => 'q', val => $q};
152 push @follower_params, { var => 'sort_by', val => $sort_by};
153
154 # Pager template params
155 $template->param(
156     previous_page    => $pager->{'prev_page'},
157     next_page        => $pager->{'next_page'},
158     PAGE_NUMBERS     => [ map { { page => $_, current => $_ == $page } } @{ $pager->{'numbers_of_set'} } ],
159     current_page     => $page,
160     follower_params  => \@follower_params,
161     total            => $total,
162     SEARCH_RESULTS   => \@r,
163     query            => $q,
164     count            => $count,
165     sort_by          => $sort_by,
166     sortable_indexes => $sortable_indexes,
167     facets_loop      => \@facets_loop,
168     filters          => \@tplfilters,
169 );
170
171 my $content_type = ( $format eq 'rss' or $format eq 'atom' ) ? $format : 'html';
172 output_with_http_headers $cgi, $cookie, $template->output, $content_type;