MT 1587 : CSV export for cart and shelves, with the ability to define different expor...
[koha_fer] / C4 / Record.pm
index 63bba6d..a892c5c 100644 (file)
@@ -29,6 +29,9 @@ use Biblio::EndnoteStyle;
 use Unicode::Normalize; # _entity_encode
 use XML::LibXSLT;
 use XML::LibXML;
+use C4::Biblio; #marc2bibtex
+use C4::Csv; #marc2csv
+use Text::CSV; #marc2csv
 
 use vars qw($VERSION @ISA @EXPORT);
 
@@ -46,6 +49,8 @@ $VERSION = 3.00;
   &marcxml2marc
   &marc2dcxml
   &marc2modsxml
+  &marc2bibtex
+  &marc2csv
 
   &html2marcxml
   &html2marc
@@ -248,7 +253,7 @@ sub marc2dcxml {
                $crosswalk = MARC::Crosswalk::DublinCore->new( qualified => 1 );
        }
        my $dcxml = $crosswalk->as_dublincore($marc_record_obj);
-       my $dcxmlfinal = "<?xml version=\"1.0\"?>\n";
+       my $dcxmlfinal = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
        $dcxmlfinal .= "<metadata
   xmlns=\"http://example.org/myapp/\"
   xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
@@ -278,7 +283,7 @@ sub marc2modsxml {
        my ($marc) = @_;
        # grab the XML, run it through our stylesheet, push it out to the browser
        my $xmlrecord = marc2marcxml($marc);
-       my $xslfile = C4::Context->config('intranetdir')."/koha-tmpl/intranet-tmpl/prog/en/xslt/MARC21slim2MODS3-1.xsl";
+       my $xslfile = C4::Context->config('intrahtdocs')."/prog/en/xslt/MARC21slim2MODS3-1.xsl";
        my $parser = XML::LibXML->new();
        my $xslt = XML::LibXSLT->new();
        my $source = $parser->parse_string($xmlrecord);
@@ -322,6 +327,99 @@ sub marc2endnote {
        
 }
 
+=head2 marc2csv - Convert from UNIMARC to CSV
+
+=over 4
+
+my ($csv) = marc2csv($record, $csvprofileid);
+
+Returns a CSV scalar
+
+=over 2
+
+C<$record> - a MARC::Record object
+
+C<$csvprofileid> - the id of the CSV profile to use for the export (see export_format.export_format_id and the GetCsvProfiles function in C4::Csv)
+
+=back
+
+=back
+
+=cut
+
+
+sub marc2csv {
+    my ($record, $id, $header) = @_;
+    my $output;
+    my $csv = Text::CSV->new();
+
+    # Get the information about the csv profile
+    my $marcfieldslist = GetMarcFieldsForCsv($id);
+
+    # Getting the marcfields as an array
+    my @marcfields = split('\|', $marcfieldslist);
+
+    # If we have to insert the headers
+    if ($header) {
+       my @marcfieldsheaders;
+
+       my $dbh   = C4::Context->dbh;
+
+       # For each field or subfield
+       foreach (@marcfields) {
+           # We get the matching tag name
+           if (index($_, '$') > 0) {
+               my ($fieldtag, $subfieldtag) = split('\$', $_);
+               my $query = "SELECT liblibrarian FROM marc_subfield_structure WHERE tagfield=? AND tagsubfield=?";
+               my $sth = $dbh->prepare($query);
+               $sth->execute($fieldtag, $subfieldtag);
+               my @results = $sth->fetchrow_array();
+               push @marcfieldsheaders, @results[0];
+           } else {
+               my $query = "SELECT liblibrarian FROM marc_tag_structure WHERE tagfield=?";
+               my $sth = $dbh->prepare($query);
+               $sth->execute($_);
+               my @results = $sth->fetchrow_array();
+               push @marcfieldsheaders, @results[0];
+           }
+       }
+       $csv->combine(@marcfieldsheaders);
+       $output = $csv->string() . "\n";        
+    }
+
+    # For each marcfield to export
+    my @fieldstab;
+    foreach my $marcfield (@marcfields) {
+       # If it is a subfield
+       if (index($marcfield, '$') > 0) {
+           my ($fieldtag, $subfieldtag) = split('\$', $marcfield);
+           my @fields = $record->field($fieldtag);
+           my @tmpfields;
+
+           # For each field
+           foreach my $field (@fields) {
+
+               # We take every matching subfield
+               my @subfields = $field->subfield($subfieldtag);
+               foreach my $subfield (@subfields) {
+                   push @tmpfields, $subfield;
+               }
+           }
+           push (@fieldstab, join(',', @tmpfields));           
+       # Or a field
+       } else {
+           my @fields = ($record->field($marcfield));
+           push (@fieldstab, join(',', map($_->as_string(), @fields)));                
+        }
+    };
+
+    $csv->combine(@fieldstab);
+    $output .= $csv->string() . "\n";
+   
+    return $output;
+
+}
+
 
 =head2 html2marcxml
 
@@ -381,7 +479,7 @@ sub html2marcxml {
                                                $marcxml.="<leader>@$values[$i]</leader>\n";
                                                $first=1;
                                        # rest of the fixed fields
-                                       } elsif (@$tags[$i] < 010) { #FIXME: <10 was the way it was, there might even be a better way
+                                       } elsif (@$tags[$i] lt '010') { # don't compare numerically 010 == 8
                                                $marcxml.="<controlfield tag=\"@$tags[$i]\">@$values[$i]</controlfield>\n";
                                                $first=1;
                                        } else {
@@ -564,6 +662,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