MT 2116, Follow-up : Fix subfield separator
[koha.git] / C4 / Biblio.pm
index 0633d95..1866718 100755 (executable)
@@ -228,21 +228,13 @@ unless you can guarantee that C<ModBiblioMarc> will be called.
 =cut
 
 sub AddBiblio {
-    my $record = shift;
-    my $frameworkcode = shift;
-    my $options = @_ ? shift : undef;
-    my $defer_marc_save = 0;
-    if (defined $options and exists $options->{'defer_marc_save'} and $options->{'defer_marc_save'}) {
-        $defer_marc_save = 1;
-    }
-
-    my ($biblionumber,$biblioitemnumber,$error);
+    my ( $record, $frameworkcode, $options ) = @_;
     my $dbh = C4::Context->dbh;
     # transform the data into koha-table style data
     my $olddata = TransformMarcToKoha( $dbh, $record, $frameworkcode );
-    ($biblionumber,$error) = _koha_add_biblio( $dbh, $olddata, $frameworkcode );
+    my ($biblionumber) = _koha_add_biblio( $dbh, $olddata, $frameworkcode );
     $olddata->{'biblionumber'} = $biblionumber;
-    ($biblioitemnumber,$error) = _koha_add_biblioitem( $dbh, $olddata );
+    my ($biblioitemnumber) = _koha_add_biblioitem( $dbh, $olddata );
 
     _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
 
@@ -250,7 +242,7 @@ sub AddBiblio {
     _koha_marc_update_biblioitem_cn_sort($record, $olddata, $frameworkcode);
     
     # now add the record
-    ModBiblioMarc( $record, $biblionumber, $frameworkcode ) unless $defer_marc_save;
+    ModBiblioMarc( $record, $biblionumber, $frameworkcode ) unless $$options{defer_marc_save} ||= 0;
       
     logaction("CATALOGUING", "ADD", $biblionumber, "biblio") if C4::Context->preference("CataloguingLog");
     return ( $biblionumber, $biblioitemnumber );
@@ -1259,8 +1251,10 @@ Return the summary of a record.
 sub GetBiblioSummary {
     my $recorddata =shift @_;
     
+    return unless $recorddata;
     my $marcflavour = C4::Context->preference("marcflavour");
     my $marc=MARC::Record::new_from_xml($recorddata,"utf-8",$marcflavour);
+    return unless $marc;
     
     my $str;
     
@@ -1812,14 +1806,18 @@ sub TransformKohaToMarcOneField {
     }
     $sth->execute( $frameworkcode, $kohafieldname );
     if ( ( $tagfield, $tagsubfield ) = $sth->fetchrow ) {
-        my $tag = $record->field($tagfield);
-        if ($tag) {
-            $tag->update( $tagsubfield => $value );
-            $record->delete_field($tag);
-            $record->insert_fields_ordered($tag);
-        }
-        else {
-            $record->add_fields( $tagfield, " ", " ", $tagsubfield => $value );
+        my @values = split(/\s?\|\s?/, $value, -1);
+        
+        foreach my $itemvalue (@values){
+            my $tag = $record->field($tagfield);
+            if ($tag) {
+                $tag->add_subfields( $tagsubfield => $itemvalue );
+                $record->delete_field($tag);
+                $record->insert_fields_ordered($tag);
+            }
+            else {
+                $record->add_fields( $tagfield, " ", " ", $tagsubfield => $itemvalue );
+            }
         }
     }
     return $record;
@@ -3301,78 +3299,33 @@ Internal function to add a biblioitem
 
 sub _koha_add_biblioitem {
     my ( $dbh, $biblioitem ) = @_;
-    my $error;
+    my @fields = qw/ biblionumber
+    cn_class cn_item cn_sort cn_source cn_suffix
+    collectionissn collectiontitle collectionvolume
+    editionresponsibility editionstatement
+    illus isbn issn itemtype lccn marc
+    notes number pages place
+    publicationyear publishercode size
+    totalissues url
+    volume volumedate volumedesc
+    /;
+
+    ($$biblioitem{cn_sort}) = GetClassSort( @$biblioitem{qw/ biblioitems.cn_source cn_class cn_item /} ); 
+
+    my $query = 'INSERT INTO biblioitems SET '
+       .  join ( ',', map { "$_ =?" } @fields )
+       .  ';'
+    ; 
 
-    my ($cn_sort) = GetClassSort($biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
-    my $query =
-    "INSERT INTO biblioitems SET
-        biblionumber    = ?,
-        volume          = ?,
-        number          = ?,
-        itemtype        = ?,
-        isbn            = ?,
-        issn            = ?,
-        publicationyear = ?,
-        publishercode   = ?,
-        volumedate      = ?,
-        volumedesc      = ?,
-        collectiontitle = ?,
-        collectionissn  = ?,
-        collectionvolume= ?,
-        editionstatement= ?,
-        editionresponsibility = ?,
-        illus           = ?,
-        pages           = ?,
-        notes           = ?,
-        size            = ?,
-        place           = ?,
-        lccn            = ?,
-        marc            = ?,
-        url             = ?,
-        cn_source       = ?,
-        cn_class        = ?,
-        cn_item         = ?,
-        cn_suffix       = ?,
-        cn_sort         = ?,
-        totalissues     = ?
-        ";
     my $sth = $dbh->prepare($query);
-    $sth->execute(
-        $biblioitem->{'biblionumber'},
-        $biblioitem->{'volume'},
-        $biblioitem->{'number'},
-        $biblioitem->{'itemtype'},
-        $biblioitem->{'isbn'},
-        $biblioitem->{'issn'},
-        $biblioitem->{'publicationyear'},
-        $biblioitem->{'publishercode'},
-        $biblioitem->{'volumedate'},
-        $biblioitem->{'volumedesc'},
-        $biblioitem->{'collectiontitle'},
-        $biblioitem->{'collectionissn'},
-        $biblioitem->{'collectionvolume'},
-        $biblioitem->{'editionstatement'},
-        $biblioitem->{'editionresponsibility'},
-        $biblioitem->{'illus'},
-        $biblioitem->{'pages'},
-        $biblioitem->{'bnotes'},
-        $biblioitem->{'size'},
-        $biblioitem->{'place'},
-        $biblioitem->{'lccn'},
-        $biblioitem->{'marc'},
-        $biblioitem->{'url'},
-        $biblioitem->{'biblioitems.cn_source'},
-        $biblioitem->{'cn_class'},
-        $biblioitem->{'cn_item'},
-        $biblioitem->{'cn_suffix'},
-        $cn_sort,
-        $biblioitem->{'totalissues'}
-    );
+    $sth->execute( @$biblioitem{@fields} );
     my $bibitemnum = $dbh->{'mysql_insertid'};
-    if ( $dbh->errstr ) {
-        $error.="ERROR in _koha_add_biblioitem $query".$dbh->errstr;
-        warn $error;
-    }
+    my $error = '';
+    $dbh->errstr and warn $error .=
+       'ERROR in _koha_add_biblioitem '
+       . $query
+       . $dbh->errstr
+    ;
     $sth->finish();
     return ($bibitemnum,$error);
 }