+
+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 $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;
+ my $fields = {
+ DB => C4::Context->preference("LibraryName"),
+ Title => $marc_rec_obj->title(),
+ Author => $marc_rec_obj->author(),
+ Publisher => $f710a,
+ City => $f260a,
+ Year => $marc_rec_obj->publication_date,
+ Abstract => $abstract,
+ };
+ my $endnote;
+ my $style = new Biblio::EndnoteStyle();
+ my $template;
+ $template.= "DB - DB\n" if C4::Context->preference("LibraryName");
+ $template.="T1 - Title\n" if $marc_rec_obj->title();
+ $template.="A1 - Author\n" if $marc_rec_obj->author();
+ $template.="PB - Publisher\n" if $f710a;
+ $template.="CY - City\n" if $f260a;
+ $template.="Y1 - Year\n" if $marc_rec_obj->publication_date;
+ $template.="AB - Abstract\n" if $abstract;
+ my ($text, $errmsg) = $style->format($template, $fields);
+ return ($text);
+
+}
+
+=head2 marc2csv - Convert several records from UNIMARC to CSV
+Pre and postprocessing can be done through a YAML file
+
+=over 4
+
+my ($csv) = marc2csv($biblios, $csvprofileid);
+
+Returns a CSV scalar
+
+=over 2
+
+C<$biblio> - a list of biblionumbers
+
+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 ($biblios, $id) = @_;
+ my $output;
+ my $csv = Text::CSV::Encoded->new();
+
+ # Getting yaml file
+ my $configfile = "../tools/csv-profiles/$id.yaml";
+ my ($preprocess, $postprocess, $fieldprocessing);
+ if (-e $configfile){
+ ($preprocess,$postprocess, $fieldprocessing) = YAML::LoadFile($configfile);
+ }
+
+ # Preprocessing
+ eval $preprocess if ($preprocess);
+
+ my $firstpass = 1;
+ foreach my $biblio (@$biblios) {
+ $output .= marcrecord2csv($biblio, $id, $firstpass, $csv, $fieldprocessing) ;
+ $firstpass = 0;
+ }
+
+ # Postprocessing
+ eval $postprocess if ($postprocess);
+
+ return $output;
+}
+
+=head2 marcrecord2csv - Convert a single record from UNIMARC to CSV
+
+=over 4
+
+my ($csv) = marcrecord2csv($biblio, $csvprofileid, $header);
+
+Returns a CSV scalar
+
+=over 2
+
+C<$biblio> - a biblionumber
+
+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)
+
+C<$header> - true if the headers are to be printed (typically at first pass)
+
+C<$csv> - an already initialised Text::CSV object
+
+=back
+
+=back
+
+=cut
+
+
+sub marcrecord2csv {
+ my ($biblio, $id, $header, $csv, $fieldprocessing) = @_;
+ my $output;
+
+ # Getting the record
+ my $record = GetMarcBiblio($biblio);
+
+ # Getting the framework
+ my $frameworkcode = GetFrameworkCode($biblio);
+
+ # Getting information about the csv profile
+ my $profile = GetCsvProfile($id);
+
+ # Getting output encoding
+ my $encoding = $profile->{encoding} || 'utf8';
+ # Getting separators
+ my $csvseparator = $profile->{csv_separator} || ',';
+ my $fieldseparator = $profile->{field_separator} || '#';
+ my $subfieldseparator = $profile->{subfield_separator} || '|';
+
+ # TODO: Be more generic (in case we have to handle other protected chars or more separators)
+ if ($csvseparator eq '\t') { $csvseparator = "\t" }
+ if ($fieldseparator eq '\t') { $fieldseparator = "\t" }
+ if ($subfieldseparator eq '\t') { $subfieldseparator = "\t" }
+
+
+ $csv = $csv->encoding_out($encoding) ;
+ $csv->sep_char($csvseparator);
+
+ # Getting the marcfields
+ my $marcfieldslist = $profile->{marcfields};
+
+ # Getting the marcfields as an array
+ my @marcfieldsarray = split('\|', $marcfieldslist);
+
+ # Separating the marcfields from the the user-supplied headers
+ my @marcfields;
+ foreach (@marcfieldsarray) {
+ my @result = split('=', $_);
+ if (scalar(@result) == 2) {
+ push @marcfields, { header => $result[0], field => $result[1] };
+ } else {
+ push @marcfields, { field => $result[0] }
+ }
+ }
+
+ # If we have to insert the headers
+ if ($header) {
+ my @marcfieldsheaders;
+ my $dbh = C4::Context->dbh;
+
+ # For each field or subfield
+ foreach (@marcfields) {
+
+ my $field = $_->{field};
+
+ # If we have a user-supplied header, we use it
+ if (exists $_->{header}) {
+ push @marcfieldsheaders, $_->{header};
+ } else {
+ # If not, we get the matching tag name from koha
+ if (index($field, '$') > 0) {
+ my ($fieldtag, $subfieldtag) = split('\$', $field);
+ 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($field);
+ my @results = $sth->fetchrow_array();
+ push @marcfieldsheaders, $results[0];
+ }
+ }
+ }
+ $csv->combine(@marcfieldsheaders);
+ $output = $csv->string() . "\n";
+ }
+
+ # For each marcfield to export
+ 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);
+ 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) {
+
+ # Getting authorised value
+ my $authvalues = GetKohaAuthorisedValuesFromField($fieldtag, $subfieldtag, $frameworkcode, undef);
+ push @tmpfields, (defined $authvalues->{$subfield}) ? $authvalues->{$subfield} : $subfield;
+ }
+ }
+ push (@fieldstab, join($subfieldseparator, @tmpfields));
+ # Or a field
+ } else {
+ my @fields = ($record->field($marcfield));
+ my $authvalues = GetKohaAuthorisedValuesFromField($marcfield, undef, $frameworkcode, undef);
+
+ my @valuesarray;
+ foreach (@fields) {
+ my $value;
+
+ # 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);
+
+ push @valuesarray, $value;
+ }
+ push (@fieldstab, join($fieldseparator, @valuesarray));
+ }
+ };
+
+ $csv->combine(@fieldstab);
+ $output .= $csv->string() . "\n";
+
+ return $output;
+
+}
+
+