(MT 3267) Add Homebranch and Holdingbranch search criterion on circulation stat.
[koha.git] / C4 / Record.pm
index d1e4cdb..25df9ad 100644 (file)
@@ -328,12 +328,12 @@ sub marc2endnote {
        
 }
 
-=head2 marcrecords2csv - Convert several records from UNIMARC to CSV
+=head2 marc2csv - Convert several records from UNIMARC to CSV
 Pre and postprocessing can be done through a YAML file
 
 =over 4
 
-my ($csv) = marcrecords2csv($biblios, $csvprofileid);
+my ($csv) = marc2csv($biblios, $csvprofileid);
 
 Returns a CSV scalar
 
@@ -360,7 +360,6 @@ sub marc2csv {
         ($preprocess,$postprocess, $fieldprocessing) = YAML::LoadFile($configfile);
     }
 
-    warn $fieldprocessing;
     # Preprocessing
     eval $preprocess if ($preprocess);
 
@@ -376,11 +375,11 @@ sub marc2csv {
     return $output;
 }
 
-=head2 marc2csv - Convert a single record from UNIMARC to CSV
+=head2 marcrecord2csv - Convert a single record from UNIMARC to CSV
 
 =over 4
 
-my ($csv) = marc2csv($biblio, $csvprofileid, $header);
+my ($csv) = marcrecord2csv($biblio, $csvprofileid, $header);
 
 Returns a CSV scalar
 
@@ -416,7 +415,6 @@ sub marcrecord2csv {
 
     # Getting output encoding
     my $encoding          = $profile->{encoding} || 'utf8';
-
     # Getting separators
     my $csvseparator      = $profile->{csv_separator}      || ',';
     my $fieldseparator    = $profile->{field_separator}    || '#';
@@ -427,7 +425,8 @@ sub marcrecord2csv {
     if ($fieldseparator eq '\t') { $fieldseparator = "\t" }
     if ($subfieldseparator eq '\t') { $subfieldseparator = "\t" }
 
-    $csv->encoding_out($encoding) if ($encoding ne 'utf8');
+
+    $csv = $csv->encoding_out($encoding) ;
     $csv->sep_char($csvseparator);
 
     # Getting the marcfields
@@ -486,6 +485,10 @@ sub marcrecord2csv {
     my @fieldstab;
     foreach (@marcfields) {
        my $marcfield = $_->{field};
+
+       # Remove any blank char that might have unintentionally insered into the tag name
+       $marcfield =~ s/\s+//g; 
+
        # If it is a subfield
        if (index($marcfield, '$') > 0) {
            my ($fieldtag, $subfieldtag) = split('\$', $marcfield);
@@ -514,8 +517,19 @@ sub marcrecord2csv {
            foreach (@fields) {
                my $value;
 
-               # Getting authorised value
-               $value = defined $authvalues->{$_->as_string} ? $authvalues->{$_->as_string} : $_->as_string;
+               # If it is a control field
+               if ($_->is_control_field) {
+                   $value = defined $authvalues->{$_->as_string} ? $authvalues->{$_->as_string} : $_->as_string;
+               } else {
+
+                   # If it is a field, we gather all subfields, joined by the subfield separator
+                   my @subvaluesarray;
+                   my @subfields = $_->subfields;
+                   foreach my $subfield (@subfields) {
+                       push (@subvaluesarray, defined $authvalues->{$subfield->[1]} ? $authvalues->{$subfield->[1]} : $subfield->[1]);
+                   }
+                   $value = join ($subfieldseparator, @subvaluesarray);
+               }
 
                # Field processing
                eval $fieldprocessing if ($fieldprocessing);
@@ -528,7 +542,7 @@ sub marcrecord2csv {
 
     $csv->combine(@fieldstab);
     $output .= $csv->string() . "\n";
-   
+
     return $output;
 
 }
@@ -775,6 +789,94 @@ sub changeEncoding {
        return ($error,$newrecord);
 }
 
+=head2 marc2bibtex - Convert from MARC21 and UNIMARC to BibTex
+
+=over 4
+
+my ($bibtex) = marc2bibtex($record, $id);
+
+Returns a BibTex scalar
+
+=over 2
+
+C<$record> - a MARC::Record object
+
+C<$id> - an id for the BibTex record (might be the biblionumber)
+
+=back
+
+=back
+
+=cut
+
+
+sub marc2bibtex {
+    my ($record, $id) = @_;
+    my $tex;
+
+    # Authors
+    my $marcauthors = GetMarcAuthors($record,C4::Context->preference("marcflavour"));
+    my $author;
+    for my $authors ( map { map { @$_ } values %$_  } @$marcauthors  ) {  
+       $author .= " and " if ($author && $$authors{value});
+       $author .= $$authors{value} if ($$authors{value}); 
+    }
+
+    # Defining the conversion hash according to the marcflavour
+    my %bh;
+    if (C4::Context->preference("marcflavour") eq "UNIMARC") {
+       
+       # FIXME, TODO : handle repeatable fields
+       # TODO : handle more types of documents
+
+       # Unimarc to bibtex hash
+       %bh = (
+
+           # Mandatory
+           author    => $author,
+           title     => $record->subfield("200", "a") || "",
+           editor    => $record->subfield("210", "g") || "",
+           publisher => $record->subfield("210", "c") || "",
+           year      => $record->subfield("210", "d") || $record->subfield("210", "h") || "",
+
+           # Optional
+           volume  =>  $record->subfield("200", "v") || "",
+           series  =>  $record->subfield("225", "a") || "",
+           address =>  $record->subfield("210", "a") || "",
+           edition =>  $record->subfield("205", "a") || "",
+           note    =>  $record->subfield("300", "a") || "",
+           url     =>  $record->subfield("856", "u") || ""
+       );
+    } else {
+
+       # Marc21 to bibtex hash
+       %bh = (
+
+           # Mandatory
+           author    => $author,
+           title     => $record->subfield("245", "a") || "",
+           editor    => $record->subfield("260", "f") || "",
+           publisher => $record->subfield("260", "b") || "",
+           year      => $record->subfield("260", "c") || $record->subfield("260", "g") || "",
+
+           # Optional
+           # unimarc to marc21 specification says not to convert 200$v to marc21
+           series  =>  $record->subfield("490", "a") || "",
+           address =>  $record->subfield("260", "a") || "",
+           edition =>  $record->subfield("250", "a") || "",
+           note    =>  $record->subfield("500", "a") || "",
+           url     =>  $record->subfield("856", "u") || ""
+       );
+    }
+
+    $tex .= "\@book{";
+    $tex .= join(",\n", $id, map { $bh{$_} ? qq(\t$_ = "$bh{$_}") : () } keys %bh);
+    $tex .= "\n}\n";
+
+    return $tex;
+}
+
+
 =head1 INTERNAL FUNCTIONS
 
 =head2 _entity_encode - Entity-encode an array of strings