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

(-)a/C4/Biblio.pm (-45 / +56 lines)
Lines 344-350 sub ModBiblio { Link Here
344
344
345
=head2 _strip_item_fields
345
=head2 _strip_item_fields
346
346
347
  _strip_item_fields($record, $frameworkcode)
347
  _strip_item_fields($record)
348
348
349
Utility routine to remove item tags from a
349
Utility routine to remove item tags from a
350
MARC bib.
350
MARC bib.
Lines 355-361 sub _strip_item_fields { Link Here
355
    my $record = shift;
355
    my $record = shift;
356
    my $frameworkcode = shift;
356
    my $frameworkcode = shift;
357
    # get the items before and append them to the biblio before updating the record, atm we just have the biblio
357
    # get the items before and append them to the biblio before updating the record, atm we just have the biblio
358
    my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField( "items.itemnumber", $frameworkcode );
358
    my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField( "items.itemnumber" );
359
359
360
    # delete any item fields from incoming record to avoid
360
    # delete any item fields from incoming record to avoid
361
    # duplication or incorrect data - use AddItem() or ModItem()
361
    # duplication or incorrect data - use AddItem() or ModItem()
Lines 1053-1066 sub GetMarcSubfieldStructure { Link Here
1053
sub GetMarcFromKohaField {
1053
sub GetMarcFromKohaField {
1054
    my ( $kohafield ) = @_;
1054
    my ( $kohafield ) = @_;
1055
    return unless $kohafield;
1055
    return unless $kohafield;
1056
    # The next call uses the Default framework since it is AUTHORITATIVE
1056
1057
    # for all Koha to MARC mappings.
1057
    my $cache     = Koha::Caches->get_instance();
1058
    my $mss = GetMarcSubfieldStructure( '' ); # Do not change framework
1058
    my $cache_key = "MarcFromKohaField-$kohafield";
1059
    my @retval;
1059
    my $cached    = $cache->get_from_cache($cache_key);
1060
    foreach( @{ $mss->{$kohafield} } ) {
1060
    if ( !defined $cached ) {
1061
        push @retval, $_->{tagfield}, $_->{tagsubfield};
1061
        # The next call uses the Default framework since it is AUTHORITATIVE
1062
    }
1062
        # for all Koha to MARC mappings.
1063
    return wantarray ? @retval : ( @retval ? $retval[0] : undef );
1063
        my $mss = GetMarcSubfieldStructure( '' ); # Do not change framework
1064
        my @retval;
1065
        foreach( @{ $mss->{$kohafield} } ) {
1066
            push @retval, $_->{tagfield}, $_->{tagsubfield};
1067
        }
1068
        $cached = \@retval;
1069
        $cache->set_in_cache( $cache_key, $cached );
1070
    }
1071
    return wantarray ? @$cached : ( @$cached ? $cached->[0] : undef );
1064
}
1072
}
1065
1073
1066
=head2 GetMarcSubfieldStructureFromKohaField
1074
=head2 GetMarcSubfieldStructureFromKohaField
Lines 1137-1146 sub GetMarcBiblio { Link Here
1137
        return;
1145
        return;
1138
    }
1146
    }
1139
1147
1140
    my $dbh          = C4::Context->dbh;
1148
    # Use state to speed up repeated calls in batch processes
1141
    my $sth          = $dbh->prepare("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber=? ");
1149
    state $sth = C4::Context->dbh->prepare("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber=? ");
1142
    $sth->execute($biblionumber);
1150
    $sth->execute($biblionumber);
1143
    my $row     = $sth->fetchrow_hashref;
1151
    my $row     = $sth->fetchrow_hashref;
1152
    $sth->finish;
1144
    my $biblioitemnumber = $row->{'biblioitemnumber'};
1153
    my $biblioitemnumber = $row->{'biblioitemnumber'};
1145
    my $marcxml = GetXmlBiblio( $biblionumber );
1154
    my $marcxml = GetXmlBiblio( $biblionumber );
1146
    $marcxml = StripNonXmlChars( $marcxml );
1155
    $marcxml = StripNonXmlChars( $marcxml );
Lines 1179-1195 The XML should only contain biblio information (item information is no longer st Link Here
1179
1188
1180
sub GetXmlBiblio {
1189
sub GetXmlBiblio {
1181
    my ($biblionumber) = @_;
1190
    my ($biblionumber) = @_;
1182
    my $dbh = C4::Context->dbh;
1183
    return unless $biblionumber;
1191
    return unless $biblionumber;
1184
    my ($marcxml) = $dbh->selectrow_array(
1192
1193
    # Use state to speed up repeated calls in batch processes
1194
    state $sth = C4::Context->dbh->prepare(
1185
        q|
1195
        q|
1186
        SELECT metadata
1196
        SELECT metadata
1187
        FROM biblio_metadata
1197
        FROM biblio_metadata
1188
        WHERE biblionumber=?
1198
        WHERE biblionumber=?
1189
            AND format='marcxml'
1199
            AND format='marcxml'
1190
            AND marcflavour=?
1200
            AND marcflavour=?
1191
    |, undef, $biblionumber, C4::Context->preference('marcflavour')
1201
        |
1192
    );
1202
    );
1203
1204
    $sth->execute( $biblionumber, C4::Context->preference('marcflavour') );
1205
    my ($marcxml) = $sth->fetchrow();
1206
    $sth->finish;
1193
    return $marcxml;
1207
    return $marcxml;
1194
}
1208
}
1195
1209
Lines 2090-2099 sub UpsertMarcControlField { Link Here
2090
2104
2091
sub GetFrameworkCode {
2105
sub GetFrameworkCode {
2092
    my ($biblionumber) = @_;
2106
    my ($biblionumber) = @_;
2093
    my $dbh            = C4::Context->dbh;
2107
    # Use state to speed up repeated calls in batch processes
2094
    my $sth            = $dbh->prepare("SELECT frameworkcode FROM biblio WHERE biblionumber=?");
2108
    state $sth         = C4::Context->dbh->prepare("SELECT frameworkcode FROM biblio WHERE biblionumber=?");
2095
    $sth->execute($biblionumber);
2109
    $sth->execute($biblionumber);
2096
    my ($frameworkcode) = $sth->fetchrow;
2110
    my ($frameworkcode) = $sth->fetchrow;
2111
    $sth->finish;
2097
    return $frameworkcode;
2112
    return $frameworkcode;
2098
}
2113
}
2099
2114
Lines 2756-2791 sub EmbedItemsInMarcBiblio { Link Here
2756
2771
2757
    $itemnumbers = [] unless defined $itemnumbers;
2772
    $itemnumbers = [] unless defined $itemnumbers;
2758
2773
2759
    my $frameworkcode = GetFrameworkCode($biblionumber);
2774
    _strip_item_fields($marc);
2760
    _strip_item_fields($marc, $frameworkcode);
2775
2776
    my $cache     = Koha::Caches->get_instance();
2777
    my $cache_key = "OpacHiddenItems-parsed" . ($opac ? '-opac' : '');
2778
    my $hidingrules    = $cache->get_from_cache($cache_key);
2779
    if ( !defined $hidingrules ) {
2780
        my $yaml = $opac ? C4::Context->preference('OpacHiddenItems') : '';
2781
        if ( $yaml =~ /\S/ ) {
2782
            $yaml = "$yaml\n\n"; # YAML is anal on ending \n. Surplus does not hurt
2783
            eval {
2784
                $hidingrules = YAML::Load($yaml);
2785
            };
2786
            if ($@) {
2787
                carp "Unable to parse OpacHiddenItems syspref : $@";
2788
            }
2789
        } else {
2790
            $hidingrules = {};
2791
        }
2792
        $cache->set_in_cache( $cache_key, $hidingrules );
2793
    }
2761
2794
2762
    # ... and embed the current items
2763
    my $dbh = C4::Context->dbh;
2764
    my $sth = $dbh->prepare("SELECT itemnumber FROM items WHERE biblionumber = ?");
2765
    $sth->execute($biblionumber);
2766
    my @item_fields;
2767
    my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField( "items.itemnumber", $frameworkcode );
2768
    my @items;
2769
    my $opachiddenitems = $opac
2770
      && ( C4::Context->preference('OpacHiddenItems') !~ /^\s*$/ );
2771
    require C4::Items;
2795
    require C4::Items;
2772
    while ( my ($itemnumber) = $sth->fetchrow_array ) {
2796
2773
        next if @$itemnumbers and not grep { $_ == $itemnumber } @$itemnumbers;
2797
    my $item_fields = C4::Items::GetMarcItemFields( $biblionumber, $itemnumbers, $hidingrules );
2774
        my $i = $opachiddenitems ? C4::Items::GetItem($itemnumber) : undef;
2798
2775
        push @items, { itemnumber => $itemnumber, item => $i };
2799
    $marc->append_fields(@$item_fields) if ( @$item_fields );
2776
    }
2777
    my @hiddenitems =
2778
      $opachiddenitems
2779
      ? C4::Items::GetHiddenItemnumbers( map { $_->{item} } @items )
2780
      : ();
2781
    # Convert to a hash for quick searching
2782
    my %hiddenitems = map { $_ => 1 } @hiddenitems;
2783
    foreach my $itemnumber ( map { $_->{itemnumber} } @items ) {
2784
        next if $hiddenitems{$itemnumber};
2785
        my $item_marc = C4::Items::GetMarcItem( $biblionumber, $itemnumber );
2786
        push @item_fields, $item_marc->field($itemtag);
2787
    }
2788
    $marc->append_fields(@item_fields);
2789
}
2800
}
2790
2801
2791
=head1 INTERNAL FUNCTIONS
2802
=head1 INTERNAL FUNCTIONS
(-)a/C4/Items.pm (-1 / +66 lines)
Lines 21-26 package C4::Items; Link Here
21
use strict;
21
use strict;
22
#use warnings; FIXME - Bug 2505
22
#use warnings; FIXME - Bug 2505
23
23
24
use Modern::Perl;
24
use Carp;
25
use Carp;
25
use C4::Context;
26
use C4::Context;
26
use C4::Koha;
27
use C4::Koha;
Lines 1524-1529 sub Item2Marc { Link Here
1524
	return $itemmarc;
1525
	return $itemmarc;
1525
}
1526
}
1526
1527
1528
=head2 GetMarcItemFields
1529
1530
  my @marc_fields = GetMarcItemFields($biblionumber);
1531
1532
Returns an array of MARC::Record objects of the items for the biblio.
1533
1534
=cut
1535
1536
sub GetMarcItemFields {
1537
	my ( $biblionumber, $itemnumbers, $hidingrules ) = @_;
1538
1539
    my $item_level_itype = C4::Context->preference('item-level_itypes');
1540
    # This is so much faster than using Koha::Items->search that it makes sense even if it's ugly.
1541
    state $sth = C4::Context->dbh->prepare( 'SELECT * FROM items WHERE biblionumber = ?' );
1542
    $sth->execute( $biblionumber );
1543
    my $items = $sth->fetchall_arrayref({});
1544
    $sth->finish();
1545
    my @item_fields;
1546
    my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField( 'items.itemnumber' );
1547
1548
    ITEMLOOP: foreach my $item (@$items) {
1549
1550
        # Check itemnumbers
1551
        next if ( @$itemnumbers && !any { $_ == $item->itemnumber } @$itemnumbers );
1552
1553
        # Check hiding rules
1554
        if ( defined $hidingrules ) {
1555
            foreach my $field (keys %$hidingrules) {
1556
                my $val = $item->{$field};
1557
                $val = '' unless defined $val;
1558
1559
                # If the results matches the values in the hiding rules, skip the item
1560
                if (any { $val eq $_ } @{$hidingrules->{$field}}) {
1561
                    next ITEMLOOP;
1562
                }
1563
            }
1564
        }
1565
1566
        # Set correct item type
1567
        if ( !$item_level_itype || !$item->{itype} ) {
1568
            warn 'item-level_itypes set but no itemtype set for item (' . $item->{itemnumber} . ')' if ( !$item->{itype} );
1569
            my $biblioitem = Koha::Biblioitem::find( $item->{biblioitemnumber} );
1570
            $item->itype = $biblioitem->itemtype();
1571
        }
1572
1573
        my $mungeditem = {
1574
            map {
1575
                defined($item->{$_}) && $item->{$_} ne '' ? ("items.$_" => $item->{$_}) : ()
1576
            } keys %{ $item }
1577
        };
1578
        my $itemmarc = TransformKohaToMarc($mungeditem);
1579
1580
        my $unlinked_item_subfields = _parse_unlinked_item_subfields_from_xml($mungeditem->{'items.more_subfields_xml'});
1581
        if (defined $unlinked_item_subfields and $#$unlinked_item_subfields > -1) {
1582
            foreach my $field ($itemmarc->field($itemtag)){
1583
                $field->add_subfields(@$unlinked_item_subfields);
1584
            }
1585
        }
1586
1587
        push @item_fields, $itemmarc->field( $itemtag );
1588
    }
1589
1590
    return \@item_fields;
1591
}
1592
1527
=head1 PRIVATE FUNCTIONS AND VARIABLES
1593
=head1 PRIVATE FUNCTIONS AND VARIABLES
1528
1594
1529
The following functions are not meant to be called
1595
The following functions are not meant to be called
1530
- 

Return to bug 20664