Skip to content

Commit

Permalink
In get_biblionumbers check Authority if biblio itself doesn't have ge…
Browse files Browse the repository at this point in the history
…ographic data
  • Loading branch information
tadzik committed Sep 23, 2024
1 parent 8e433d8 commit e22108d
Showing 1 changed file with 45 additions and 28 deletions.
73 changes: 45 additions & 28 deletions Koha/Plugin/HKS3/GeoSearch/GeoSearchController.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use JSON;

use Koha::Plugin::HKS3::GeoSearch;

use C4::AuthoritiesMarc;
use C4::Biblio;
use MARC::File::XML ( DefaultEncoding => 'utf8' );
use Koha::Caches;
Expand All @@ -15,6 +16,32 @@ sub cache {
return Koha::Caches->get_instance(__PACKAGE__);
}

sub get_record_location {
my $record = shift;
my $geofield = $record->field('034');

if ($geofield) {
my $lat = $geofield->subfield('s');
my $lng = $geofield->subfield('t');
if ($lat and $lng) {
return { coordinates => [ $lat, $lng ] };
}

my $west = $geofield->subfield('d');
my $east = $geofield->subfield('e');
my $north = $geofield->subfield('f');
my $south = $geofield->subfield('g');
if ($west and $east and $north and $south) {
return {
bounds => [
[ 0+$north, 0+$west ],
[ 0+$south, 0+$east ]
],
};
}
}
}

sub get_biblionumbers {
my $c = shift->openapi->valid_input or return;
my $biblionumbers = $c->validation->every_param('bn');
Expand All @@ -23,38 +50,28 @@ sub get_biblionumbers {
for my $biblio_number (@$biblionumbers) {
my $marcxml = C4::Biblio::GetXmlBiblio( $biblio_number );
my $record = MARC::Record->new_from_xml( $marcxml, 'UTF-8', 'MARC21' );
my $geofield = $record->field('034');
if ($geofield) {
my $location = get_record_location($record);

if (!$location) {
warn "No location found for $biblio_number, checking authority";
my $authid = $record->subfield('651', '9');
warn "Authid is $authid";
if ($authid) {
my $authrecord = C4::AuthoritiesMarc::GetAuthority( $authid );
$location = get_record_location($authrecord);
}
}

if ($location) {
my $title = sprintf(
"<a href='/cgi-bin/koha/opac-detail.pl?biblionumber=%d'>%s</a>",
$biblio_number, $record->field('245')->subfield("a")
);

my $lat = $geofield->subfield('s');
my $lng = $geofield->subfield('t');
if ($lat and $lng) {
push @data, {
bn => $biblio_number,
coordinates => [ $lat, $lng ],
title => $title,
};
next;
}

my $west = $geofield->subfield('d');
my $east = $geofield->subfield('e');
my $north = $geofield->subfield('f');
my $south = $geofield->subfield('g');
if ($west and $east and $north and $south) {
push @data, {
bn => $biblio_number,
bounds => [
[ 0+$north, 0+$west ],
[ 0+$south, 0+$east ]
],
title => $title,
};
}
push @data, {
bn => $biblio_number,
title => $title,
%$location,
};
}
}

Expand Down

0 comments on commit e22108d

Please sign in to comment.