Lines 33-38
use C4::Csv; #marc2csv
Link Here
|
33 |
use C4::Koha; #marc2csv |
33 |
use C4::Koha; #marc2csv |
34 |
use C4::XSLT (); |
34 |
use C4::XSLT (); |
35 |
use YAML; #marcrecords2csv |
35 |
use YAML; #marcrecords2csv |
|
|
36 |
use Template; |
36 |
use Text::CSV::Encoded; #marc2csv |
37 |
use Text::CSV::Encoded; #marc2csv |
37 |
|
38 |
|
38 |
use vars qw($VERSION @ISA @EXPORT); |
39 |
use vars qw($VERSION @ISA @EXPORT); |
Lines 420-433
C<$itemnumbers> a list of itemnumbers to export
Link Here
|
420 |
|
421 |
|
421 |
=cut |
422 |
=cut |
422 |
|
423 |
|
423 |
|
|
|
424 |
sub marcrecord2csv { |
424 |
sub marcrecord2csv { |
425 |
my ($biblio, $id, $header, $csv, $fieldprocessing, $itemnumbers) = @_; |
425 |
my ($biblio, $id, $header, $csv, $fieldprocessing, $itemnumbers) = @_; |
426 |
my $output; |
426 |
my $output; |
427 |
|
427 |
|
428 |
# Getting the record |
428 |
# Getting the record |
429 |
my $record = GetMarcBiblio($biblio); |
429 |
my $record = GetMarcBiblio($biblio); |
430 |
next unless $record; |
430 |
return unless $record; |
431 |
C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblio, $itemnumbers ); |
431 |
C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblio, $itemnumbers ); |
432 |
# Getting the framework |
432 |
# Getting the framework |
433 |
my $frameworkcode = GetFrameworkCode($biblio); |
433 |
my $frameworkcode = GetFrameworkCode($biblio); |
Lines 460-567
sub marcrecord2csv {
Link Here
|
460 |
my @marcfieldsarray = split('\|', $marcfieldslist); |
460 |
my @marcfieldsarray = split('\|', $marcfieldslist); |
461 |
|
461 |
|
462 |
# Separating the marcfields from the user-supplied headers |
462 |
# Separating the marcfields from the user-supplied headers |
463 |
my @marcfields; |
463 |
my @csv_structures; |
464 |
foreach (@marcfieldsarray) { |
464 |
foreach (@marcfieldsarray) { |
465 |
my @result = split('=', $_); |
465 |
my @result = split('=', $_); |
466 |
if (scalar(@result) == 2) { |
466 |
my $content = ( @result == 2 ) |
467 |
push @marcfields, { header => $result[0], field => $result[1] }; |
467 |
? $result[1] |
|
|
468 |
: $result[0]; |
469 |
my @fields; |
470 |
while ( $content =~ m|(\d{3})\$?(.)?|g ) { |
471 |
my $fieldtag = $1; |
472 |
my $subfieldtag = $2 || undef; |
473 |
push @fields, { fieldtag => $fieldtag, subfieldtag => $subfieldtag }; |
474 |
} |
475 |
if ( @result == 2) { |
476 |
push @csv_structures, { header => $result[0], content => $content, fields => \@fields }; |
468 |
} else { |
477 |
} else { |
469 |
push @marcfields, { field => $result[0] } |
478 |
push @csv_structures, { content => $content, fields => \@fields } |
470 |
} |
479 |
} |
471 |
} |
480 |
} |
472 |
|
481 |
|
473 |
# If we have to insert the headers |
482 |
my ( @marcfieldsheaders, @csv_rows ); |
474 |
if ($header) { |
483 |
my $dbh = C4::Context->dbh; |
475 |
my @marcfieldsheaders; |
|
|
476 |
my $dbh = C4::Context->dbh; |
477 |
|
478 |
# For each field or subfield |
479 |
foreach (@marcfields) { |
480 |
|
481 |
my $field = $_->{field}; |
482 |
# Remove any blank char that might have unintentionally insered into the tag name |
483 |
$field =~ s/\s+//g; |
484 |
|
485 |
# If we have a user-supplied header, we use it |
486 |
if (exists $_->{header}) { |
487 |
push @marcfieldsheaders, $_->{header}; |
488 |
} else { |
489 |
# If not, we get the matching tag name from koha |
490 |
if (index($field, '$') > 0) { |
491 |
my ($fieldtag, $subfieldtag) = split('\$', $field); |
492 |
my $query = "SELECT liblibrarian FROM marc_subfield_structure WHERE tagfield=? AND tagsubfield=?"; |
493 |
my $sth = $dbh->prepare($query); |
494 |
$sth->execute($fieldtag, $subfieldtag); |
495 |
my @results = $sth->fetchrow_array(); |
496 |
push @marcfieldsheaders, $results[0]; |
497 |
} else { |
498 |
my $query = "SELECT liblibrarian FROM marc_tag_structure WHERE tagfield=?"; |
499 |
my $sth = $dbh->prepare($query); |
500 |
$sth->execute($field); |
501 |
my @results = $sth->fetchrow_array(); |
502 |
push @marcfieldsheaders, $results[0]; |
503 |
} |
504 |
} |
505 |
} |
506 |
$csv->combine(@marcfieldsheaders); |
507 |
$output = $csv->string() . "\n"; |
508 |
} |
509 |
|
484 |
|
510 |
# For each marcfield to export |
485 |
my $field_list; |
511 |
my @fieldstab; |
486 |
for my $field ( $record->fields ) { |
512 |
foreach (@marcfields) { |
487 |
my $fieldtag = $field->tag; |
513 |
my $marcfield = $_->{field}; |
488 |
my $values; |
514 |
# If it is a subfield |
489 |
if ( $field->is_control_field ) { |
515 |
if (index($marcfield, '$') > 0) { |
490 |
$values = $field->data(); |
516 |
my ($fieldtag, $subfieldtag) = split('\$', $marcfield); |
491 |
} else { |
517 |
my @fields = $record->field($fieldtag); |
492 |
$values->{indicator}{1} = $field->indicator(1); |
518 |
my @tmpfields; |
493 |
$values->{indicator}{2} = $field->indicator(2); |
519 |
|
494 |
for my $subfield ( $field->subfields ) { |
520 |
# For each field |
495 |
my $subfieldtag = $subfield->[0]; |
521 |
foreach my $field (@fields) { |
496 |
my $value = $subfield->[1]; |
522 |
|
497 |
push @{ $values->{$subfieldtag} }, $value; |
523 |
# We take every matching subfield |
498 |
} |
524 |
my @subfields = $field->subfield($subfieldtag); |
499 |
} |
525 |
foreach my $subfield (@subfields) { |
500 |
# We force the key as an integer (trick for 00X and OXX fields) |
526 |
|
501 |
push @{ $field_list->{fields}{0+$fieldtag} }, $values; |
527 |
# Getting authorised value |
502 |
} |
528 |
my $authvalues = GetKohaAuthorisedValuesFromField($fieldtag, $subfieldtag, $frameworkcode, undef); |
|
|
529 |
push @tmpfields, (defined $authvalues->{$subfield}) ? $authvalues->{$subfield} : $subfield; |
530 |
} |
531 |
} |
532 |
push (@fieldstab, join($subfieldseparator, @tmpfields)); |
533 |
# Or a field |
534 |
} else { |
535 |
my @fields = ($record->field($marcfield)); |
536 |
my $authvalues = GetKohaAuthorisedValuesFromField($marcfield, undef, $frameworkcode, undef); |
537 |
|
503 |
|
538 |
my @valuesarray; |
504 |
# For each field or subfield |
539 |
foreach (@fields) { |
505 |
foreach my $csv_structure (@csv_structures) { |
540 |
my $value; |
506 |
my @field_values; |
|
|
507 |
my $tags = $csv_structure->{fields}; |
508 |
my $content = $csv_structure->{content}; |
541 |
|
509 |
|
542 |
# If it is a control field |
510 |
if ( $header ) { |
543 |
if ($_->is_control_field) { |
511 |
# If we have a user-supplied header, we use it |
544 |
$value = defined $authvalues->{$_->as_string} ? $authvalues->{$_->as_string} : $_->as_string; |
512 |
if ( exists $csv_structure->{header} ) { |
|
|
513 |
push @marcfieldsheaders, $csv_structure->{header}; |
545 |
} else { |
514 |
} else { |
546 |
# If it is a field, we gather all subfields, joined by the subfield separator |
515 |
# If not, we get the matching tag name from koha |
547 |
my @subvaluesarray; |
516 |
my $tag = $tags->[0]; |
548 |
my @subfields = $_->subfields; |
517 |
if ( $tag->{subfieldtag} ) { |
549 |
foreach my $subfield (@subfields) { |
518 |
my $query = "SELECT liblibrarian FROM marc_subfield_structure WHERE tagfield=? AND tagsubfield=?"; |
550 |
push (@subvaluesarray, defined $authvalues->{$subfield->[1]} ? $authvalues->{$subfield->[1]} : $subfield->[1]); |
519 |
my @results = $dbh->selectrow_array( $query, {}, $tag->{subfieldtag} ); |
|
|
520 |
push @marcfieldsheaders, $results[0]; |
521 |
} else { |
522 |
my $query = "SELECT liblibrarian FROM marc_tag_structure WHERE tagfield=?"; |
523 |
my @results = $dbh->selectrow_array( $query, {}, $tag->{fieldtag} ); |
524 |
push @marcfieldsheaders, $results[0]; |
551 |
} |
525 |
} |
552 |
$value = join ($subfieldseparator, @subvaluesarray); |
|
|
553 |
} |
526 |
} |
|
|
527 |
} |
554 |
|
528 |
|
555 |
# Field processing |
529 |
# TT tags exist |
556 |
eval $fieldprocessing if ($fieldprocessing); |
530 |
if ( $content =~ m|\[\%.*\%\]| ) { |
|
|
531 |
my $tt = Template->new(); |
532 |
my $template = $content; |
533 |
my $vars; |
534 |
# Replace 00X and 0XX with X or XX |
535 |
$content =~ s|fields.00(\d)|fields.$1|g; |
536 |
$content =~ s|fields.0(\d{2})|fields.$1|g; |
537 |
my $tt_output; |
538 |
$tt->process( \$content, $field_list, \$tt_output ); |
539 |
push @csv_rows, $tt_output; |
540 |
} else { |
541 |
for my $tag ( @$tags ) { |
542 |
my @fields = $record->field( $tag->{fieldtag} ); |
543 |
# If it is a subfield |
544 |
my @loop_values; |
545 |
if ( $tag->{subfieldtag} ) { |
546 |
# For each field |
547 |
foreach my $field (@fields) { |
548 |
my @subfields = $field->subfield( $tag->{subfieldtag} ); |
549 |
foreach my $subfield (@subfields) { |
550 |
my $authvalues = GetKohaAuthorisedValuesFromField( $tag->{fieldtag}, $tag->{subfieldtag}, $frameworkcode, undef); |
551 |
push @loop_values, (defined $authvalues->{$subfield}) ? $authvalues->{$subfield} : $subfield; |
552 |
} |
553 |
} |
554 |
|
555 |
# Or a field |
556 |
} else { |
557 |
my $authvalues = GetKohaAuthorisedValuesFromField( $tag->{fieldtag}, undef, $frameworkcode, undef); |
558 |
|
559 |
foreach my $field ( @fields ) { |
560 |
my $value; |
561 |
|
562 |
# If it is a control field |
563 |
if ($field->is_control_field) { |
564 |
$value = defined $authvalues->{$field->as_string} ? $authvalues->{$field->as_string} : $field->as_string; |
565 |
} else { |
566 |
# If it is a field, we gather all subfields, joined by the subfield separator |
567 |
my @subvaluesarray; |
568 |
my @subfields = $field->subfields; |
569 |
foreach my $subfield (@subfields) { |
570 |
push (@subvaluesarray, defined $authvalues->{$subfield->[1]} ? $authvalues->{$subfield->[1]} : $subfield->[1]); |
571 |
} |
572 |
$value = join ($subfieldseparator, @subvaluesarray); |
573 |
} |
574 |
|
575 |
# Field processing |
576 |
my $marcfield = $tag->{fieldtag}; # This line fixes a retrocompatibility concern |
577 |
# The "processing" could be based on the $marcfield variable. |
578 |
eval $fieldprocessing if ($fieldprocessing); |
579 |
|
580 |
push @loop_values, $value; |
581 |
} |
557 |
|
582 |
|
558 |
push @valuesarray, $value; |
583 |
} |
|
|
584 |
push @field_values, { |
585 |
fieldtag => $tag->{fieldtag}, |
586 |
subfieldtag => $tag->{subfieldtag}, |
587 |
values => \@loop_values, |
588 |
}; |
589 |
} |
590 |
for my $field_value ( @field_values ) { |
591 |
if ( $field_value->{subfieldtag} ) { |
592 |
push @csv_rows, join( $subfieldseparator, @{ $field_value->{values} } ); |
593 |
} else { |
594 |
push @csv_rows, join( $fieldseparator, @{ $field_value->{values} } ); |
595 |
} |
596 |
} |
559 |
} |
597 |
} |
560 |
push (@fieldstab, join($fieldseparator, @valuesarray)); |
598 |
} |
561 |
} |
|
|
562 |
}; |
563 |
|
599 |
|
564 |
$csv->combine(@fieldstab); |
600 |
|
|
|
601 |
if ( $header ) { |
602 |
$csv->combine(@marcfieldsheaders); |
603 |
$output = $csv->string() . "\n"; |
604 |
} |
605 |
$csv->combine(@csv_rows); |
565 |
$output .= $csv->string() . "\n"; |
606 |
$output .= $csv->string() . "\n"; |
566 |
|
607 |
|
567 |
return $output; |
608 |
return $output; |
568 |
- |
|
|