X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;ds=sidebyside;f=authorities%2Fysearch.pl;h=b669c08f3348d6630369f98351972ea17d1811aa;hb=5d6c092921919526ade501facb1220f8a108a08f;hp=65841232f9b7069ea199a38a2cc96c132ade0ce7;hpb=4f44847c11add59ab7d0c55aeffac1fa2835cc1f;p=koha_fer diff --git a/authorities/ysearch.pl b/authorities/ysearch.pl index 65841232f9..b669c08f33 100755 --- a/authorities/ysearch.pl +++ b/authorities/ysearch.pl @@ -29,29 +29,27 @@ This script allows ajax call for dynamic authorities search 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 $searchstr = $query->param('term'); + my @value = $query->param('term'); my $searchtype = $query->param('querytype'); - my @value; - given ($searchtype) { - when (/^marclist$/) { @value = (undef, undef, $searchstr); } - when (/^mainentry$/) { @value = (undef, $searchstr, undef); } - when (/^mainmainentry$/) { @value = ($searchstr, undef, undef); } - } my @marclist = ($searchtype); my $authtypecode = $query->param('authtypecode'); my @and_or = $query->param('and_or'); @@ -64,17 +62,26 @@ if ( $auth_status ne "ok" ) { 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';