Bug 24155: Fix parameter order to fix search_utf8.t
[koha.git] / opac / opac-browse.pl
blobcdc94bf4a69676a409a12e98b71806a5cf398184
1 #!/usr/bin/perl
3 # This is a CGI script that handles the browse feature.
5 # Copyright 2015 Catalyst IT
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Modern::Perl;
23 use CGI qw ( -utf8 );
25 use C4::Auth;
26 use C4::Context;
27 use C4::Output;
29 use Koha::SearchEngine::Elasticsearch;
30 use Koha::SearchEngine::Elasticsearch::Browse;
31 use Koha::SearchEngine::Elasticsearch::QueryBuilder;
32 use Koha::SearchEngine::Elasticsearch::Search;
34 use JSON;
35 use Unicode::Collate;
37 my $query = new CGI;
38 binmode STDOUT, ':encoding(UTF-8)';
40 # If calling via JS, 'api' is used to route to correct step in process
41 my $api = $query->param('api');
43 if ( !$api ) {
44 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
46 template_name => "opac-browse.tt",
47 query => $query,
48 type => "opac",
49 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
52 $template->param();
53 output_html_with_http_headers $query, $cookie, $template->output;
57 elsif ( $api eq 'GetSuggestions' ) {
58 my $fuzzie = $query->param('fuzziness');
59 my $prefix = $query->param('prefix');
60 my $field = $query->param('field');
62 # Under a persistent environment, we should probably not reinit this every time.
63 my $browser = Koha::SearchEngine::Elasticsearch::Browse->new( { index => 'biblios' } );
64 my $res = $browser->browse( $prefix, $field, { fuzziness => $fuzzie } );
66 my %seen;
67 my @sorted =
68 grep { !$seen{$_->{text}}++ }
69 sort { lc($a->{text}) cmp lc($b->{text}) } @$res;
70 print CGI::header(
71 -type => 'application/json',
72 -charset => 'utf-8'
74 print to_json( \@sorted );
76 elsif ( $api eq 'GetResults' ) {
77 my $term = $query->param('term');
78 my $field = $query->param('field');
80 my $builder = Koha::SearchEngine::Elasticsearch::QueryBuilder->new( { index => 'biblios' } );
81 my $searcher = Koha::SearchEngine::Elasticsearch::Search->new(
82 { index => $Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX } );
84 my $query = { query => { term => { $field.".raw" => $term } } } ;
85 my $results = $searcher->search( $query, undef, 500 );
86 my @output = _filter_for_output( $results->{hits}->{hits} );
87 print CGI::header(
88 -type => 'application/json',
89 -charset => 'utf-8'
91 print to_json( \@output );
94 # This should probably be done with some templatey gizmo
95 # in the future.
96 sub _filter_for_output {
97 my ($records) = @_;
98 my @output;
99 foreach my $rec (@$records) {
100 my $biblionumber = $rec->{_id};
101 my $biblio = Koha::Biblios->find( $biblionumber );
102 next unless $biblio;
103 push @output,
105 id => $biblionumber,
106 title => $biblio->title,
107 author => $biblio->author,
110 my @sorted = sort { lc($a->{title}) cmp lc($b->{title}) } @output;
111 return @sorted;