From d5ed0ca81a8404f34e6b0a63774cf8207d268f4a Mon Sep 17 00:00:00 2001
From: Fridolyn SOMERS <fridolyn.somers@biblibre.com>
Date: Thu, 13 Dec 2012 14:38:11 +0100
Subject: [PATCH] [SIGNED OFF] Bug 9282: authorities auto-completion in mainmainentry

Signed-off-by: Mathieu Saby <mathieu.saby@univ-rennes2.fr>
---
 C4/AuthoritiesMarc.pm  |   59 ++++++++++++++++++++++++++++++++++++------------
 authorities/ysearch.pl |   41 +++++++++++++++++++++------------
 2 files changed, 72 insertions(+), 28 deletions(-)

diff --git a/C4/AuthoritiesMarc.pm b/C4/AuthoritiesMarc.pm
index 4a3392f..c81d517 100644
--- a/C4/AuthoritiesMarc.pm
+++ b/C4/AuthoritiesMarc.pm
@@ -1021,7 +1021,11 @@ sub BuildSummary {
 # 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..')) {
@@ -1029,7 +1033,12 @@ sub BuildSummary {
         }
         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 :
@@ -1040,6 +1049,7 @@ sub BuildSummary {
                 field   => $_->tag,
                 type    => $type,
                 heading => $heading,
+                hemain  => $_->subfield('a'),
                 search  => $heading,
                 authid  => $_->subfield('9'),
             }
@@ -1090,9 +1100,17 @@ sub BuildSummary {
                 $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
@@ -1103,9 +1121,19 @@ sub BuildSummary {
                 $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
@@ -1118,18 +1146,20 @@ sub BuildSummary {
             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') || ''
                 };
             }
         }
@@ -1157,6 +1187,7 @@ sub BuildSummary {
         }
     }
     $summary{mainentry} = $authorized[0]->{heading};
+    $summary{mainmainentry} = $authorized[0]->{hemain};
     $summary{authorized} = \@authorized;
     $summary{notes} = \@notes;
     $summary{seefrom} = \@seefrom;
diff --git a/authorities/ysearch.pl b/authorities/ysearch.pl
index 6584123..a117f0f 100755
--- a/authorities/ysearch.pl
+++ b/authorities/ysearch.pl
@@ -29,18 +29,22 @@ 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;
 }
 
@@ -63,18 +67,27 @@ if ( $auth_status ne "ok" ) {
     my $startfrom = 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';
-- 
1.7.9.5