Bug 5549: Formatting duedates in catalogue scripts
[koha.git] / C4 / Record.pm
index 990bb50..89bde32 100644 (file)
@@ -77,7 +77,32 @@ Returns an ISO-2709 scalar
 
 sub marc2marc {
        my ($marc,$to_flavour,$from_flavour,$encoding) = @_;
-       my $error = "Feature not yet implemented\n";
+       my $error;
+    if ($to_flavour =~ m/marcstd/) {
+        my $marc_record_obj;
+        if ($marc =~ /^MARC::Record/) { # it's already a MARC::Record object
+            $marc_record_obj = $marc;
+        } else { # it's not a MARC::Record object, make it one
+            eval { $marc_record_obj = MARC::Record->new_from_usmarc($marc) }; # handle exceptions
+
+# conversion to MARC::Record object failed, populate $error
+                if ($@) { $error .="\nCreation of MARC::Record object failed: ".$MARC::File::ERROR };
+        }
+        unless ($error) {
+            my @privatefields;
+            foreach my $field ($marc_record_obj->fields()) {
+                if ($field->tag() =~ m/9/ && ($field->tag() != '490' || C4::Context->preference("marcflavour") eq 'UNIMARC')) {
+                    push @privatefields, $field;
+                } elsif (! ($field->is_control_field())) {
+                    $field->delete_subfield(code => '9') if ($field->subfield('9'));
+                }
+            }
+            $marc_record_obj->delete_field($_) for @privatefields;
+            $marc = $marc_record_obj->as_usmarc();
+        }
+    } else {
+        $error = "Feature not yet implemented\n";
+    }
        return ($error,$marc);
 }
 
@@ -266,12 +291,19 @@ sub marc2modsxml {
 sub marc2endnote {
     my ($marc) = @_;
        my $marc_rec_obj =  MARC::Record->new_from_usmarc($marc);
-       my $f260 = $marc_rec_obj->field('260');
-       my $f260a = $f260->subfield('a') if $f260;
+    my ( $abstract, $f260a, $f710a );
+    my $f260 = $marc_rec_obj->field('260');
+    if ($f260) {
+        $f260a = $f260->subfield('a') if $f260;
+    }
     my $f710 = $marc_rec_obj->field('710');
-    my $f710a = $f710->subfield('a') if $f710;
-       my $f500 = $marc_rec_obj->field('500');
-       my $abstract = $f500->subfield('a') if $f500;
+    if ($f710) {
+        $f710a = $f710->subfield('a');
+    }
+    my $f500 = $marc_rec_obj->field('500');
+    if ($f500) {
+        $abstract = $f500->subfield('a');
+    }
        my $fields = {
                DB => C4::Context->preference("LibraryName"),
                Title => $marc_rec_obj->title(),        
@@ -359,7 +391,7 @@ sub marcrecord2csv {
     my $output;
 
     # Getting the record
-    my $record = GetMarcBiblio($biblio);
+    my $record = GetMarcBiblio($biblio, 1);
     next unless $record;
     # Getting the framework
     my $frameworkcode = GetFrameworkCode($biblio);
@@ -391,7 +423,7 @@ sub marcrecord2csv {
     # Getting the marcfields as an array
     my @marcfieldsarray = split('\|', $marcfieldslist);
 
-   # Separating the marcfields from the the user-supplied headers
+   # Separating the marcfields from the user-supplied headers
     my @marcfields;
     foreach (@marcfieldsarray) {
         my @result = split('=', $_);