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