Intranet po file updates
[koha.git] / C4 / Biblio.pm
index e5471b2..c0777ab 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 );
@@ -654,8 +646,10 @@ sub GetBiblioItemData {
         FROM biblio LEFT JOIN biblioitems on biblio.biblionumber=biblioitems.biblionumber ";
     unless(C4::Context->preference('item-level_itypes')) { 
         $query .= "LEFT JOIN itemtypes on biblioitems.itemtype=itemtypes.itemtype ";
-    }    
-    $query .= " WHERE biblioitemnumber = ? ";
+    } else {
+       $query .= "LEFT JOIN items on biblio.biblionumber=items.biblionumber ";
+    }
+    $query .= " WHERE biblioitems.biblioitemnumber = ? ";
     my $sth       =  $dbh->prepare($query);
     my $data;
     $sth->execute($biblioitemnumber);
@@ -816,19 +810,16 @@ sub GetISBDView {
               else {
                 my @subf = $field->subfields;
                 for my $i ( 0 .. $#subf ) {
-                my $valuecode   = $subf[$i][1];
-                my $subfieldcode  = $subf[$i][0];
-                my $subfieldvalue =
-                GetAuthorisedValueDesc( $tag, $subf[$i][0],
-                  $subf[$i][1], '', $tagslib );
-                my $tagsubf = $tag . $subfieldcode;
-    
-                $calculated =~ s/                  # replace all {{}} codes by the value code.
-                                  \{\{$tagsubf\}\} # catch the {{actualcode}}
-                                /
-                                  $valuecode     # replace by the value code
-                               /gx;
-    
+                    my $valuecode   = $subf[$i][1];
+                    my $subfieldcode  = $subf[$i][0];
+                    my $subfieldvalue =
+                    GetAuthorisedValueDesc( $tag, $subf[$i][0],
+                      $subf[$i][1], '', $tagslib );
+                    my $tagsubf = $tag . $subfieldcode;
+        
+                    $calculated =~ s/\{\{$tagsubf\}\}/$valuecode/gx;
+                    $calculated =~
+                        s/\{(.?.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue$2\{$1$tagsubf$2\}/g;
                 }
     
                 # field builded, store the result
@@ -1262,8 +1253,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;
     
@@ -1608,13 +1601,13 @@ sub GetMarcUrls {
 
     my @marcurls;
     for my $field ( $record->field('856') ) {
-        my $marcurl;
         my @notes;
         for my $note ( $field->subfield('z') ) {
             push @notes, { note => $note };
         }
         my @urls = $field->subfield('u');
         foreach my $url (@urls) {
+            my $marcurl;
             if ( $marcflavour eq 'MARC21' ) {
                 my $s3   = $field->subfield('3');
                 my $link = $field->subfield('y');
@@ -1815,14 +1808,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;
@@ -3003,6 +3000,8 @@ sub _koha_marc_update_bib_ids {
     # we drop the original field
     # we add the new builded field.
     my ($biblio_tag, $biblio_subfield ) = GetMarcFromKohaField("biblio.biblionumber",$frameworkcode);
+    unless ( $biblio_tag ) { die "can't GetMarcFromKohaField for framework $frameworkcode"; }
+
     my ($biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField("biblioitems.biblioitemnumber",$frameworkcode);
 
     if ($biblio_tag != $biblioitem_tag) {
@@ -3304,78 +3303,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);
 }