# construct UNIMARC summary, that is quite different from MARC21 one
# accepted form
foreach my $field ($record->field('2..')) {
- push @authorized, { heading => $field->as_string('abcdefghijlmnopqrstuvwxyz'), field => $field->tag() };
+ push @authorized, {
+ heading => $field->as_string('abcdefghijlmnopqrstuvwxyz'),
+ hemain => $field->subfield('a'),
+ field => $field->tag(),
+ };
}
# rejected form(s)
foreach my $field ($record->field('3..')) {
}
foreach my $field ($record->field('4..')) {
my $thesaurus = $field->subfield('2') ? "thes. : ".$thesaurus{"$field->subfield('2')"}." : " : '';
- push @seefrom, { heading => $thesaurus . $field->as_string('abcdefghijlmnopqrstuvwxyz'), type => 'seefrom', field => $field->tag() };
+ push @seefrom, {
+ heading => $thesaurus . $field->as_string('abcdefghijlmnopqrstuvwxyz'),
+ hemain => $field->subfield('a'),
+ type => 'seefrom',
+ field => $field->tag(),
+ };
}
# see :
field => $_->tag,
type => $type,
heading => $heading,
+ hemain => $_->subfield('a'),
search => $heading,
authid => $_->subfield('9'),
}
$subfields_to_report = 'vxyz';
}
if ($subfields_to_report) {
- push @authorized, { heading => $field->as_string($subfields_to_report), field => $tag };
+ push @authorized, {
+ heading => $field->as_string($subfields_to_report),
+ hemain => $field->subfield( substr($subfields_to_report, 0, 1) ),
+ field => $tag,
+ };
} else {
- push @authorized, { heading => $field->as_string(), field => $tag };
+ push @authorized, {
+ heading => $field->as_string(),
+ hemain => $field->subfield('a'),
+ field => $tag,
+ };
}
}
foreach my $field ($record->field('4..')) { #See From
$type = 'earlier' if $type && $type ne 'n';
}
if ($type eq 'subfi') {
- push @seefrom, { heading => $field->as_string($marc21subfields), type => ($field->subfield('i') || ''), field => $field->tag() };
+ push @seefrom, {
+ heading => $field->as_string($marc21subfields),
+ hemain => $field->subfield( substr($marc21subfields, 0, 1) ),
+ type => ($field->subfield('i') || ''),
+ field => $field->tag(),
+ };
} else {
- push @seefrom, { heading => $field->as_string($marc21subfields), type => $type, field => $field->tag() };
+ push @seefrom, {
+ heading => $field->as_string($marc21subfields),
+ hemain => $field->subfield( substr($marc21subfields, 0, 1) ),
+ type => $type,
+ field => $field->tag(),
+ };
}
}
foreach my $field ($record->field('5..')) { #See Also
if ($type eq 'subfi') {
push @seealso, {
heading => $field->as_string($marc21subfields),
- type => $field->subfield('i'),
- field => $field->tag(),
- search => $field->as_string($marc21subfields) || '',
- authid => $field->subfield('9') || ''
+ hemain => $field->subfield( substr($marc21subfields, 0, 1) ),
+ type => $field->subfield('i'),
+ field => $field->tag(),
+ search => $field->as_string($marc21subfields) || '',
+ authid => $field->subfield('9') || ''
};
} else {
push @seealso, {
heading => $field->as_string($marc21subfields),
- type => $type,
- field => $field->tag(),
- search => $field->as_string($marc21subfields) || '',
- authid => $field->subfield('9') || ''
+ hemain => $field->subfield( substr($marc21subfields, 0, 1) ),
+ type => $type,
+ field => $field->tag(),
+ search => $field->as_string($marc21subfields) || '',
+ authid => $field->subfield('9') || ''
};
}
}
}
}
$summary{mainentry} = $authorized[0]->{heading};
+ $summary{mainmainentry} = $authorized[0]->{hemain};
$summary{authorized} = \@authorized;
$summary{notes} = \@notes;
$summary{seefrom} = \@seefrom;
use CGI;
use Modern::Perl;
+use JSON;
+
use C4::Context;
use C4::Charset;
use C4::AuthoritiesMarc;
use C4::Auth qw/check_cookie_auth/;
+use C4::Output;
my $query = new CGI;
-binmode STDOUT, ':encoding(UTF-8)';
-print $query->header( -type => 'text/plain', -charset => 'UTF-8' );
+my ( $auth_status, $sessionID ) = check_cookie_auth( $query->cookie('CGISESSID'), { catalogue => 1 } );
-my ( $auth_status, $sessionID ) = check_cookie_auth( $query->cookie('CGISESSID'), { } );
if ( $auth_status ne "ok" ) {
+ # send empty response
+ my $reply = CGI->new("");
+ print $reply->header(-type => 'text/html');
exit 0;
}
my ( $results, $total ) = SearchAuthorities( \@marclist, \@and_or, \@excluding, \@operator, \@value, $startfrom * $resultsperpage, $resultsperpage, $authtypecode, $orderby );
-print "[";
-my $i = 0;
+ my %used_summaries; # hash to avoid duplicates
+ my @summaries;
foreach my $result (@$results) {
- if($i > 0){ print ","; }
- my $value = '';
my $authorized = $result->{'summary'}->{'authorized'};
- foreach my $heading (@$authorized) {
- $value .= $heading->{'heading'} . ' ';
+ my $summary = join(
+ ' ',
+ map {
+ ( $searchtype eq 'mainmainentry' )
+ ? $_->{'hemain'}
+ : $_->{'heading'}
+ } @$authorized
+ );
+ $summary =~ s/^\s+//;
+ $summary =~ s/\s+$//;
+ $summary = nsb_clean($summary);
+ # test if already added ignoring case
+ unless ( exists $used_summaries{ lc($summary) } ) {
+ push @summaries, { 'summary' => $summary };
+ $used_summaries{ lc($summary) } = 1;
}
- $value = "{\"summary\":\"" . $value . "\"" . "}";
- print nsb_clean($value) . "\n";
- $i++;
}
-print "]";
+
+output_with_http_headers $query, undef, to_json(\@summaries, { utf8 => 1 }), 'json';