Bug 32824: Fix cataloguing/value_builder/unimarc_field_100.pl
[koha-ffzg.git] / Koha / Biblio.pm
index 3c8e5cb..5bf369c 100644 (file)
@@ -595,12 +595,12 @@ sub get_components_query {
 
             if ( !defined($pf003) ) {
                 # search for 773$w='Host001'
-                $searchstr .= "rcn:" . $pf001->data();
+                $searchstr .= "rcn:\"" . $pf001->data()."\"";
             }
             else {
                 $searchstr .= "(";
                 # search for (773$w='Host001' and 003='Host003') or 773$w='(Host003)Host001'
-                $searchstr .= "(rcn:" . $pf001->data() . " AND cni:" . $pf003->data() . ")";
+                $searchstr .= "(rcn:\"" . $pf001->data() . "\" AND cni:\"" . $pf003->data() . "\")";
                 $searchstr .= " OR rcn:\"" . $pf003->data() . " " . $pf001->data() . "\"";
                 $searchstr .= ")";
             }
@@ -616,13 +616,13 @@ sub get_components_query {
         $cleaned_title = $builder->clean_search_term($cleaned_title);
         $searchstr = qq#Host-item:("$cleaned_title")#;
     }
-    my ($error, $query_str) = $builder->build_query_compat( undef, [$searchstr], undef, undef, [$sort], 0 );
+    my ($error, $query ,$query_str) = $builder->build_query_compat( undef, [$searchstr], undef, undef, [$sort], 0 );
     if( $error ){
         warn $error;
         return;
     }
 
-    return ($query_str, $sort);
+    return ($query, $query_str, $sort);
 }
 
 =head3 subscriptions
@@ -1205,7 +1205,7 @@ sub to_api_mapping {
 
     $host = $biblio->get_marc_host;
     # OR:
-    ( $host, $relatedparts ) = $biblio->get_marc_host;
+    ( $host, $relatedparts, $hostinfo ) = $biblio->get_marc_host;
 
     Returns host biblio record from MARC21 773 (undef if no 773 present).
     It looks at the first 773 field with MARCorgCode or only a control
@@ -1214,6 +1214,12 @@ sub to_api_mapping {
     If there are, the sub returns undef.
     Called in list context, it also returns 773$g (related parts).
 
+    If there is no $w, we use $0 (host biblionumber) or $9 (host itemnumber)
+    to search for the host record. If there is also no $0 and no $9, we search
+    using author and title. Failing all of that, we return an undef host and
+    form a concatenation of strings with 773$agt for host information,
+    returned when called in list context.
+
 =cut
 
 sub get_marc_host {
@@ -1237,12 +1243,35 @@ sub get_marc_host {
             last;
         }
     }
+
+    my $engine = Koha::SearchEngine::Search->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
+    my $bibno;
+    if ( !$hostfld and $record->subfield('773','t') ) {
+        # not linked using $w
+        my $unlinkedf = $record->field('773');
+        my $host;
+        if ( C4::Context->preference("EasyAnalyticalRecords") ) {
+            if ( $unlinkedf->subfield('0') ) {
+                # use 773$0 host biblionumber
+                $bibno = $unlinkedf->subfield('0');
+            } elsif ( $unlinkedf->subfield('9') ) {
+                # use 773$9 host itemnumber
+                my $linkeditemnumber = $unlinkedf->subfield('9');
+                $bibno = Koha::Items->find( $linkeditemnumber )->biblionumber;
+            }
+        }
+        if ( $bibno ) {
+            my $host = Koha::Biblios->find($bibno) or return;
+            return wantarray ? ( $host, $unlinkedf->subfield('g') ) : $host;
+        }
+        # just return plaintext and no host record
+        my $hostinfo = join( ", ", $unlinkedf->subfield('a'), $unlinkedf->subfield('t'), $unlinkedf->subfield('g') );
+        return wantarray ? ( undef, $unlinkedf->subfield('g'), $hostinfo ) : undef;
+    }
     return if !$hostfld;
     my $rcn = $hostfld->subfield('w');
 
     # Look for control number with/without orgcode
-    my $engine = Koha::SearchEngine::Search->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
-    my $bibno;
     for my $try (1..2) {
         my ( $error, $results, $total_hits ) = $engine->simple_search_compat( 'Control-number='.$rcn, 0,1 );
         if( !$error and $total_hits == 1 ) {