Koha/opac/opac-browse.pl
Owen Leonard e12e0ef016 Bug 27200: (follow-up) Browse search interface update
This patch updates the browse search interface to confirm to new
interface patterns in the Bootstrap 4 framework, especially in the form
markup.

The results list has been changed to a Bootstrap "Collapse" component
configured as an accordion
(https://getbootstrap.com/docs/4.5/components/collapse/#accordion-example).

Instead of loading the bibliographic record results below the list of
terms returned, the bibliographic results are now displayed in the
"panel" expanded below the selected term. Subtitle has been added to the
information displayed about the bibliographic record.

To test you must be using ElasticSearch and the OpacBrowseSearch
preference must be enabled. Apply the patch and rebuild the OPAC CSS
(https://wiki.koha-community.org/wiki/Working_with_SCSS_in_the_OPAC_and_staff_client).

- Open the "Browse search" page in the OPAC.
- Test various searches: Author, Subject, and Title.
- When results are found, the should be displayed as a Bootstrap-styled
  accordion widget. Clicking any individual result should expand the
  panel containing the corresponding records.
- Clicking the record link should open the bibliographic detail page in
  a new window.
- When no results are found, a Bootstrap-style "alert" box should
  appear.

Signed-off-by: Victor Grousset/tuxayo <victor@tuxayo.net>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2020-12-26 17:58:43 +01:00

113 lines
3.5 KiB
Perl
Executable file

#!/usr/bin/perl
# This is a CGI script that handles the browse feature.
# Copyright 2015 Catalyst IT
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use CGI qw ( -utf8 );
use C4::Auth;
use C4::Context;
use C4::Output;
use Koha::SearchEngine::Elasticsearch;
use Koha::SearchEngine::Elasticsearch::Browse;
use Koha::SearchEngine::Elasticsearch::QueryBuilder;
use Koha::SearchEngine::Elasticsearch::Search;
use JSON;
use Unicode::Collate;
my $query = CGI->new;
binmode STDOUT, ':encoding(UTF-8)';
# If calling via JS, 'api' is used to route to correct step in process
my $api = $query->param('api');
if ( !$api ) {
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
{
template_name => "opac-browse.tt",
query => $query,
type => "opac",
authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
}
);
$template->param();
output_html_with_http_headers $query, $cookie, $template->output;
}
elsif ( $api eq 'GetSuggestions' ) {
my $fuzzie = $query->param('fuzziness');
my $prefix = $query->param('prefix');
my $field = $query->param('field');
# Under a persistent environment, we should probably not reinit this every time.
my $browser = Koha::SearchEngine::Elasticsearch::Browse->new( { index => 'biblios' } );
my $res = $browser->browse( $prefix, $field, { fuzziness => $fuzzie } );
my %seen;
my @sorted =
grep { !$seen{$_->{text}}++ }
sort { lc($a->{text}) cmp lc($b->{text}) } @$res;
print CGI::header(
-type => 'application/json',
-charset => 'utf-8'
);
print to_json( \@sorted );
}
elsif ( $api eq 'GetResults' ) {
my $term = $query->param('term');
my $field = $query->param('field');
my $builder = Koha::SearchEngine::Elasticsearch::QueryBuilder->new( { index => 'biblios' } );
my $searcher = Koha::SearchEngine::Elasticsearch::Search->new(
{ index => $Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX } );
my $query = { query => { term => { $field.".raw" => $term } } } ;
my $results = $searcher->search( $query, undef, 500 );
my @output = _filter_for_output( $results->{hits}->{hits} );
print CGI::header(
-type => 'application/json',
-charset => 'utf-8'
);
print to_json( \@output );
}
# This should probably be done with some templatey gizmo
# in the future.
sub _filter_for_output {
my ($records) = @_;
my @output;
foreach my $rec (@$records) {
my $biblionumber = $rec->{_id};
my $biblio = Koha::Biblios->find( $biblionumber );
next unless $biblio;
push @output,
{
id => $biblionumber,
title => $biblio->title,
subtitle => $biblio->subtitle,
author => $biblio->author,
};
};
my @sorted = sort { lc($a->{title}) cmp lc($b->{title}) } @output;
return @sorted;
}