View | Details | Raw Unified | Return to bug 10306
Collapse All | Expand All

(-)a/C4/Biblio.pm (-113 / +74 lines)
Lines 23-28 use Modern::Perl; Link Here
23
use Carp;
23
use Carp;
24
24
25
use Encode qw( decode is_utf8 );
25
use Encode qw( decode is_utf8 );
26
use List::MoreUtils qw( uniq );
26
use MARC::Record;
27
use MARC::Record;
27
use MARC::File::USMARC;
28
use MARC::File::USMARC;
28
use MARC::File::XML;
29
use MARC::File::XML;
Lines 721-727 sub GetBiblioData { Link Here
721
    return ($data);
722
    return ($data);
722
}    # sub GetBiblioData
723
}    # sub GetBiblioData
723
724
724
=head2 &GetBiblioItemData
725
=head2 GetBiblioItemData
725
726
726
  $itemdata = &GetBiblioItemData($biblioitemnumber);
727
  $itemdata = &GetBiblioItemData($biblioitemnumber);
727
728
Lines 732-738 that C<biblioitems.notes> is given as C<$itemdata-E<gt>{bnotes}>. Link Here
732
733
733
=cut
734
=cut
734
735
735
#'
736
sub GetBiblioItemData {
736
sub GetBiblioItemData {
737
    my ($biblioitemnumber) = @_;
737
    my ($biblioitemnumber) = @_;
738
    my $dbh                = C4::Context->dbh;
738
    my $dbh                = C4::Context->dbh;
Lines 2560-2566 sub TransformHtmlToMarc { Link Here
2560
2560
2561
=head2 TransformMarcToKoha
2561
=head2 TransformMarcToKoha
2562
2562
2563
  $result = TransformMarcToKoha( $record, $frameworkcode )
2563
  $result = TransformMarcToKoha( $record, $frameworkcode, $limit )
2564
2564
2565
Extract data from a MARC bib record into a hashref representing
2565
Extract data from a MARC bib record into a hashref representing
2566
Koha biblio, biblioitems, and items fields. 
2566
Koha biblio, biblioitems, and items fields. 
Lines 2572-2682 hash_ref Link Here
2572
2572
2573
sub TransformMarcToKoha {
2573
sub TransformMarcToKoha {
2574
    my ( $record, $frameworkcode, $limit_table ) = @_;
2574
    my ( $record, $frameworkcode, $limit_table ) = @_;
2575
    $frameworkcode //= q{};
2576
    $limit_table //= q{};
2575
2577
2576
    my $result = {};
2578
    my $result = {};
2577
    if (!defined $record) {
2579
    if (!defined $record) {
2578
        carp('TransformMarcToKoha called with undefined record');
2580
        carp('TransformMarcToKoha called with undefined record');
2579
        return $result;
2581
        return $result;
2580
    }
2582
    }
2581
    $limit_table = $limit_table || 0;
2582
    $frameworkcode //= '';
2583
2584
    my $inverted_field_map = _get_inverted_marc_field_map($frameworkcode);
2585
2586
    my %tables = ();
2587
    if ( defined $limit_table && $limit_table eq 'items' ) {
2588
        $tables{'items'} = 1;
2589
    } else {
2590
        $tables{'items'}       = 1;
2591
        $tables{'biblio'}      = 1;
2592
        $tables{'biblioitems'} = 1;
2593
    }
2594
2595
    # traverse through record
2596
  MARCFIELD: foreach my $field ( $record->fields() ) {
2597
        my $tag = $field->tag();
2598
        next MARCFIELD unless exists $inverted_field_map->{$tag};
2599
        if ( $field->is_control_field() ) {
2600
            my $kohafields = $inverted_field_map->{$tag}->{list};
2601
          ENTRY: foreach my $entry ( @{$kohafields} ) {
2602
                my ( $subfield, $table, $column ) = @{$entry};
2603
                my $value = $field->data;
2604
                next ENTRY unless exists $tables{$table};
2605
                next ENTRY if !$value;
2606
                my $key = _disambiguate( $table, $column );
2607
                if ( $result->{$key} ) {
2608
                    $result->{$key} .= " | " . $value
2609
                        unless $result->{$key} eq $value;
2610
                } else {
2611
                    $result->{$key} = $value;
2612
                }
2613
            }
2614
        } else {
2615
2616
            # deal with subfields
2617
          MARCSUBFIELD: foreach my $sf ( $field->subfields() ) {
2618
                my $code = $sf->[0];
2619
                next MARCSUBFIELD unless exists $inverted_field_map->{$tag}->{sfs}->{$code};
2620
                my $value = $sf->[1];
2621
              SFENTRY: foreach my $entry ( @{ $inverted_field_map->{$tag}->{sfs}->{$code} } ) {
2622
                    my ( $table, $column ) = @{$entry};
2623
                    next SFENTRY unless exists $tables{$table};
2624
                    next SFENTRY if !$value;
2625
                    my $key = _disambiguate( $table, $column );
2626
                    if ( $result->{$key} ) {
2627
                        $result->{$key} .= " | " . $value
2628
                            unless $result->{$key} eq $value;
2629
                    } else {
2630
                        $result->{$key} = $value;
2631
                    }
2632
                }
2633
            }
2634
        }
2635
    }
2636
2583
2637
    # modify copyrightdate to keep only the 1st year found
2584
    my %tables = ( biblio => 1, biblioitems => 1, items => 1 );
2638
    if ( exists $result->{'copyrightdate'} ) {
2585
    if( $limit_table eq 'items' ) {
2639
        my $temp = $result->{'copyrightdate'};
2586
        %tables = ( items => 1 );
2640
        $temp =~ m/c(\d\d\d\d)/;
2641
        if ( $temp =~ m/c(\d\d\d\d)/ and $1 > 0 ) {    # search cYYYY first
2642
            $result->{'copyrightdate'} = $1;
2643
        } else {                                       # if no cYYYY, get the 1st date.
2644
            $temp =~ m/(\d\d\d\d)/;
2645
            $result->{'copyrightdate'} = $1;
2646
        }
2647
    }
2648
2649
    # modify publicationyear to keep only the 1st year found
2650
    if ( exists $result->{'publicationyear'} ) {
2651
        my $temp = $result->{'publicationyear'};
2652
        if ( $temp =~ m/c(\d\d\d\d)/ and $1 > 0 ) {    # search cYYYY first
2653
            $result->{'publicationyear'} = $1;
2654
        } else {                                       # if no cYYYY, get the 1st date.
2655
            $temp =~ m/(\d\d\d\d)/;
2656
            $result->{'publicationyear'} = $1;
2657
        }
2658
    }
2587
    }
2659
2588
2660
    return $result;
2661
}
2662
2663
sub _get_inverted_marc_field_map {
2664
    my ( $frameworkcode ) = @_;
2665
    my $field_map = {};
2666
    my $mss = GetMarcSubfieldStructure( $frameworkcode );
2589
    my $mss = GetMarcSubfieldStructure( $frameworkcode );
2667
2668
    foreach my $kohafield ( keys %{ $mss } ) {
2590
    foreach my $kohafield ( keys %{ $mss } ) {
2669
        foreach my $fld ( @{ $mss->{$kohafield} } ) {
2591
        my ( $table, $column ) = split /[.]/, $kohafield, 2;
2670
            my $tag = $fld->{tagfield};
2592
        next unless $tables{$table};
2671
            my $subfield = $fld->{tagsubfield};
2593
        my $val = TransformMarcToKohaOneField(
2672
            my ( $table, $column ) = split /[.]/, $kohafield, 2;
2594
            $kohafield, $record, $frameworkcode,
2673
            push @{ $field_map->{$tag}->{list} },
2595
        );
2674
                [ $subfield, $table, $column ];
2596
        next if !defined $val;
2675
            push @{ $field_map->{$tag}->{sfs}->{$subfield} },
2597
        my $key = _disambiguate( $table, $column );
2676
                [ $table, $column ];
2598
        $result->{$key} = $val;
2677
        }
2678
    }
2599
    }
2679
    return $field_map;
2600
    return $result;
2680
}
2601
}
2681
2602
2682
=head2 _disambiguate
2603
=head2 _disambiguate
Lines 2708-2722 more. Link Here
2708
2629
2709
=cut
2630
=cut
2710
2631
2711
sub CountItemsIssued {
2712
    my ($biblionumber) = @_;
2713
    my $dbh            = C4::Context->dbh;
2714
    my $sth            = $dbh->prepare('SELECT COUNT(*) as issuedCount FROM items, issues WHERE items.itemnumber = issues.itemnumber AND items.biblionumber = ?');
2715
    $sth->execute($biblionumber);
2716
    my $row = $sth->fetchrow_hashref();
2717
    return $row->{'issuedCount'};
2718
}
2719
2720
sub _disambiguate {
2632
sub _disambiguate {
2721
    my ( $table, $column ) = @_;
2633
    my ( $table, $column ) = @_;
2722
    if ( $column eq "cn_sort" or $column eq "cn_source" ) {
2634
    if ( $column eq "cn_sort" or $column eq "cn_source" ) {
Lines 2729-2747 sub _disambiguate { Link Here
2729
2641
2730
=head2 TransformMarcToKohaOneField
2642
=head2 TransformMarcToKohaOneField
2731
2643
2732
    $value = TransformMarcToKohaOneField( 'biblio.title', $record, $frameworkcode );
2644
    $val = TransformMarcToKohaOneField( 'biblio.title', $marc, $frameworkcode );
2733
2645
2734
=cut
2646
=cut
2735
2647
2736
sub TransformMarcToKohaOneField {
2648
sub TransformMarcToKohaOneField {
2737
    my ( $kohafield, $marc, $frameworkcode ) = @_;
2649
    my ( $kohafield, $marc, $frameworkcode ) = @_;
2738
2650
2739
    # It is not really useful to repeat all code from TransformMarcToKoha here.
2651
    my ( @rv, $retval );
2740
    # Since it is fast enough (by caching), we just extract the field.
2652
    my @mss = GetMarcSubfieldStructureFromKohaField($kohafield, $frameworkcode);
2741
    my $koharec = TransformMarcToKoha( $marc, $frameworkcode );
2653
    foreach my $fldhash ( @mss ) {
2742
    my @temp = split /\./, $kohafield, 2;
2654
        my $tag = $fldhash->{tagfield};
2743
    $kohafield = _disambiguate( @temp ) if @temp > 1;
2655
        my $sub = $fldhash->{tagsubfield};
2744
    return $koharec ? $koharec->{$kohafield} : undef;
2656
        foreach my $fld ( $marc->field($tag) ) {
2657
            if( $sub eq '@' || $fld->is_control_field ) {
2658
                push @rv, $fld->data if $fld->data;
2659
            } else {
2660
                push @rv, grep { $_ } $fld->subfield($sub);
2661
            }
2662
        }
2663
    }
2664
    return unless @rv;
2665
    $retval = join ' | ', uniq(@rv);
2666
2667
    # Additional polishing for individual kohafields
2668
    if( $kohafield =~ /copyrightdate|publicationyear/ ) {
2669
        $retval = _adjust_pubyear( $retval );
2670
    }
2671
2672
    return $retval;
2673
}
2674
2675
=head2 _adjust_pubyear
2676
2677
    Helper routine for TransformMarcToKohaOneField
2678
2679
=cut
2680
2681
sub _adjust_pubyear {
2682
    my $retval = shift;
2683
    # modify return value to keep only the 1st year found
2684
    if( $retval =~ m/c(\d\d\d\d)/ and $1 > 0 ) { # search cYYYY first
2685
        $retval = $1;
2686
    } else {
2687
        # if no cYYYY, get the 1st date.
2688
        $retval =~ m/(\d\d\d\d)/;
2689
        $retval = $1;
2690
    }
2691
    return $retval;
2692
}
2693
2694
=head2 CountItemsIssued
2695
2696
    my $count = CountItemsIssued( $biblionumber );
2697
2698
=cut
2699
2700
sub CountItemsIssued {
2701
    my ($biblionumber) = @_;
2702
    my $dbh            = C4::Context->dbh;
2703
    my $sth            = $dbh->prepare('SELECT COUNT(*) as issuedCount FROM items, issues WHERE items.itemnumber = issues.itemnumber AND items.biblionumber = ?');
2704
    $sth->execute($biblionumber);
2705
    my $row = $sth->fetchrow_hashref();
2706
    return $row->{'issuedCount'};
2745
}
2707
}
2746
2708
2747
=head2 ModZebra
2709
=head2 ModZebra
2748
- 

Return to bug 10306