Bug 26819: (QA follow-up) authorized_value should be authorised_value
[koha.git] / opac / opac-browse.pl
1 #!/usr/bin/perl
2
3 # This is a CGI script that handles the browse feature.
4
5 # Copyright 2015 Catalyst IT
6 #
7 # This file is part of Koha.
8 #
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.
13 #
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.
18 #
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>.
21
22 use Modern::Perl;
23 use CGI qw ( -utf8 );
24
25 use C4::Auth;
26 use C4::Context;
27 use C4::Output;
28
29 use Koha::SearchEngine::Elasticsearch;
30 use Koha::SearchEngine::Elasticsearch::Browse;
31 use Koha::SearchEngine::Elasticsearch::QueryBuilder;
32 use Koha::SearchEngine::Elasticsearch::Search;
33
34 use JSON;
35 use Unicode::Collate;
36
37 my $query = CGI->new;
38 binmode STDOUT, ':encoding(UTF-8)';
39
40 # If calling via JS, 'api' is used to route to correct step in process
41 my $api = $query->param('api');
42
43 if ( !$api ) {
44     my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
45         {
46             template_name   => "opac-browse.tt",
47             query           => $query,
48             type            => "opac",
49             authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
50         }
51     );
52    $template->param();
53     output_html_with_http_headers $query, $cookie, $template->output;
54
55
56 }
57 elsif ( $api eq 'GetSuggestions' ) {
58     my $fuzzie = $query->param('fuzziness');
59     my $prefix = $query->param('prefix');
60     my $field  = $query->param('field');
61
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 } );
65
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'
73     );
74     print to_json( \@sorted );
75 }
76 elsif ( $api eq 'GetResults' ) {
77     my $term  = $query->param('term');
78     my $field = $query->param('field');
79
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 } );
83
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'
90     );
91     print to_json( \@output );
92 }
93
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,
104           {
105             id => $biblionumber,
106             title    => $biblio->title,
107             author  => $biblio->author,
108           };
109     };
110     my @sorted = sort { lc($a->{title}) cmp lc($b->{title}) } @output;
111     return @sorted;
112 }