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

(-)a/C4/Biblio.pm (-40 / +36 lines)
Lines 310-316 sub ModBiblio { Link Here
310
310
311
    $frameworkcode = "" if !$frameworkcode || $frameworkcode eq "Default"; # XXX
311
    $frameworkcode = "" if !$frameworkcode || $frameworkcode eq "Default"; # XXX
312
312
313
    _strip_item_fields($record, $frameworkcode);
313
    _strip_item_fields($record);
314
314
315
    # update biblionumber and biblioitemnumber in MARC
315
    # update biblionumber and biblioitemnumber in MARC
316
    # FIXME - this is assuming a 1 to 1 relationship between
316
    # FIXME - this is assuming a 1 to 1 relationship between
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 353-361 MARC bib. Link Here
353
353
354
sub _strip_item_fields {
354
sub _strip_item_fields {
355
    my $record = shift;
355
    my $record = 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
356
    # 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 );
357
    my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField( "items.itemnumber" );
359
358
360
    # delete any item fields from incoming record to avoid
359
    # delete any item fields from incoming record to avoid
361
    # duplication or incorrect data - use AddItem() or ModItem()
360
    # duplication or incorrect data - use AddItem() or ModItem()
Lines 1089-1102 sub GetMarcSubfieldStructure { Link Here
1089
sub GetMarcFromKohaField {
1088
sub GetMarcFromKohaField {
1090
    my ( $kohafield ) = @_;
1089
    my ( $kohafield ) = @_;
1091
    return unless $kohafield;
1090
    return unless $kohafield;
1092
    # The next call uses the Default framework since it is AUTHORITATIVE
1091
1093
    # for all Koha to MARC mappings.
1092
    my $cache     = Koha::Caches->get_instance();
1094
    my $mss = GetMarcSubfieldStructure( '', { unsafe => 1 } ); # Do not change framework
1093
    my $cache_key = "MarcFromKohaField-$kohafield";
1095
    my @retval;
1094
    my $cached    = $cache->get_from_cache($cache_key);
1096
    foreach( @{ $mss->{$kohafield} } ) {
1095
    if ( !defined $cached ) {
1097
        push @retval, $_->{tagfield}, $_->{tagsubfield};
1096
        # The next call uses the Default framework since it is AUTHORITATIVE
1098
    }
1097
        # for all Koha to MARC mappings.
1099
    return wantarray ? @retval : ( @retval ? $retval[0] : undef );
1098
        my $mss = GetMarcSubfieldStructure( '', { unsafe => 1 } ); # Do not change framework
1099
        my @retval;
1100
        foreach( @{ $mss->{$kohafield} } ) {
1101
            push @retval, $_->{tagfield}, $_->{tagsubfield};
1102
        }
1103
        $cached = \@retval;
1104
        $cache->set_in_cache( $cache_key, $cached );
1105
    }
1106
    return wantarray ? @$cached : ( @$cached ? $cached->[0] : undef );
1100
}
1107
}
1101
1108
1102
=head2 GetMarcSubfieldStructureFromKohaField
1109
=head2 GetMarcSubfieldStructureFromKohaField
Lines 2792-2827 sub EmbedItemsInMarcBiblio { Link Here
2792
2799
2793
    $itemnumbers = [] unless defined $itemnumbers;
2800
    $itemnumbers = [] unless defined $itemnumbers;
2794
2801
2795
    my $frameworkcode = GetFrameworkCode($biblionumber);
2802
    _strip_item_fields($marc);
2796
    _strip_item_fields($marc, $frameworkcode);
2803
2804
    my $hidingrules = {};
2805
    my $yaml = $opac ? C4::Context->preference('OpacHiddenItems') : '';
2806
    if ( $yaml =~ /\S/ ) {
2807
        $yaml = "$yaml\n\n"; # YAML is anal on ending \n. Surplus does not hurt
2808
        eval {
2809
            $hidingrules = YAML::Load($yaml);
2810
        };
2811
        if ($@) {
2812
            carp "Unable to parse OpacHiddenItems syspref : $@";
2813
        }
2814
    }
2797
2815
2798
    # ... and embed the current items
2799
    my $dbh = C4::Context->dbh;
2800
    my $sth = $dbh->prepare("SELECT itemnumber FROM items WHERE biblionumber = ?");
2801
    $sth->execute($biblionumber);
2802
    my @item_fields;
2803
    my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField( "items.itemnumber", $frameworkcode );
2804
    my @items;
2805
    my $opachiddenitems = $opac
2806
      && ( C4::Context->preference('OpacHiddenItems') !~ /^\s*$/ );
2807
    require C4::Items;
2816
    require C4::Items;
2808
    while ( my ($itemnumber) = $sth->fetchrow_array ) {
2817
2809
        next if @$itemnumbers and not grep { $_ == $itemnumber } @$itemnumbers;
2818
    my $item_fields = C4::Items::GetMarcItemFields( $biblionumber, $itemnumbers, $hidingrules );
2810
        my $i = $opachiddenitems ? C4::Items::GetItem($itemnumber) : undef;
2819
2811
        push @items, { itemnumber => $itemnumber, item => $i };
2820
    $marc->append_fields(@$item_fields) if ( @$item_fields );
2812
    }
2813
    my @hiddenitems =
2814
      $opachiddenitems
2815
      ? C4::Items::GetHiddenItemnumbers( map { $_->{item} } @items )
2816
      : ();
2817
    # Convert to a hash for quick searching
2818
    my %hiddenitems = map { $_ => 1 } @hiddenitems;
2819
    foreach my $itemnumber ( map { $_->{itemnumber} } @items ) {
2820
        next if $hiddenitems{$itemnumber};
2821
        my $item_marc = C4::Items::GetMarcItem( $biblionumber, $itemnumber );
2822
        push @item_fields, $item_marc->field($itemtag);
2823
    }
2824
    $marc->append_fields(@item_fields);
2825
}
2821
}
2826
2822
2827
=head1 INTERNAL FUNCTIONS
2823
=head1 INTERNAL FUNCTIONS
(-)a/C4/Items.pm (-5 / +71 lines)
Lines 1520-1525 sub Item2Marc { Link Here
1520
	return $itemmarc;
1520
	return $itemmarc;
1521
}
1521
}
1522
1522
1523
=head2 GetMarcItemFields
1524
1525
  my @marc_fields = GetMarcItemFields($biblionumber);
1526
1527
Returns an array of MARC::Record objects of the items for the biblio.
1528
1529
=cut
1530
1531
sub GetMarcItemFields {
1532
	my ( $biblionumber, $itemnumbers, $hidingrules ) = @_;
1533
1534
    my $item_level_itype = C4::Context->preference('item-level_itypes');
1535
    # This is so much faster than using Koha::Items->search that it makes sense even if it's ugly.
1536
    my $sth = C4::Context->dbh->prepare('SELECT * FROM items WHERE biblionumber = ?');
1537
    $sth->execute($biblionumber);
1538
    my $items = $sth->fetchall_arrayref({});
1539
    $sth->finish();
1540
    my @item_fields;
1541
    my ($itemtag, $itemsubfield) = GetMarcFromKohaField('items.itemnumber');
1542
1543
    ITEMLOOP: foreach my $item (@$items) {
1544
1545
        # Check itemnumbers
1546
        next if (@$itemnumbers && !any { $_ == $item->{itemnumber} } @$itemnumbers);
1547
1548
        # Check hiding rules
1549
        if (defined $hidingrules) {
1550
            foreach my $field (keys %$hidingrules) {
1551
                my $val = $item->{$field};
1552
                $val = '' unless defined $val;
1553
1554
                # If the results matches the values in the hiding rules, skip the item
1555
                if (any { $val eq $_ } @{$hidingrules->{$field}}) {
1556
                    next ITEMLOOP;
1557
                }
1558
            }
1559
        }
1560
1561
        # Set correct item type
1562
        if (!$item_level_itype || !$item->{itype}) {
1563
            warn 'item-level_itypes set but no itemtype set for item (' . $item->{itemnumber} . ')' if (!$item->{itype});
1564
            my $biblioitem = Koha::Biblioitems->find($item->{biblioitemnumber});
1565
            $item->{itype} = $biblioitem->itemtype();
1566
        }
1567
1568
        my $mungeditem = {
1569
            map {
1570
                defined($item->{$_}) && $item->{$_} ne '' ? ("items.$_" => $item->{$_}) : ()
1571
            } keys %{ $item }
1572
        };
1573
        my $itemmarc = TransformKohaToMarc($mungeditem);
1574
1575
        my $unlinked_item_subfields = _parse_unlinked_item_subfields_from_xml($mungeditem->{'items.more_subfields_xml'});
1576
        if (defined $unlinked_item_subfields and $#$unlinked_item_subfields > -1) {
1577
            foreach my $field ($itemmarc->field($itemtag)){
1578
                $field->add_subfields(@$unlinked_item_subfields);
1579
            }
1580
        }
1581
1582
        push @item_fields, $itemmarc->field($itemtag);
1583
    }
1584
1585
    return \@item_fields;
1586
}
1587
1523
=head1 PRIVATE FUNCTIONS AND VARIABLES
1588
=head1 PRIVATE FUNCTIONS AND VARIABLES
1524
1589
1525
The following functions are not meant to be called
1590
The following functions are not meant to be called
Lines 1656-1665 sub _do_column_fixes_for_mod { Link Here
1656
        (not defined $item->{'withdrawn'} or $item->{'withdrawn'} eq '')) {
1721
        (not defined $item->{'withdrawn'} or $item->{'withdrawn'} eq '')) {
1657
        $item->{'withdrawn'} = 0;
1722
        $item->{'withdrawn'} = 0;
1658
    }
1723
    }
1659
    if (exists $item->{location}
1724
    if (exists $item->{'location'}
1660
        and $item->{location} ne 'CART'
1725
        and defined $item->{'location'}
1661
        and $item->{location} ne 'PROC'
1726
        and $item->{'location'} ne 'CART'
1662
        and not $item->{permanent_location}
1727
        and $item->{'location'} ne 'PROC'
1728
        and (not defined $item->{'permanent_location'} or not $item->{'permanent_location'})
1663
    ) {
1729
    ) {
1664
        $item->{'permanent_location'} = $item->{'location'};
1730
        $item->{'permanent_location'} = $item->{'location'};
1665
    }
1731
    }
Lines 2324-2330 sub _SearchItems_build_where_fragment { Link Here
2324
        push @columns, Koha::Database->new()->schema()->resultset('Biblioitem')->result_source->columns;
2390
        push @columns, Koha::Database->new()->schema()->resultset('Biblioitem')->result_source->columns;
2325
        my @operators = qw(= != > < >= <= like);
2391
        my @operators = qw(= != > < >= <= like);
2326
        my $field = $filter->{field};
2392
        my $field = $filter->{field};
2327
        if ( (0 < grep /^$field$/, @columns) or (substr($field, 0, 5) eq 'marc:') ) {
2393
        if ( defined $field and ((0 < grep /^$field$/, @columns) or (substr($field, 0, 5) eq 'marc:')) ) {
2328
            my $op = $filter->{operator};
2394
            my $op = $filter->{operator};
2329
            my $query = $filter->{query};
2395
            my $query = $filter->{query};
2330
2396
(-)a/misc/migration_tools/bulkmarcimport.pl (-1 / +1 lines)
Lines 481-487 RECORD: while ( ) { Link Here
481
            # Work on a clone so that if there are real errors, we can maybe
481
            # Work on a clone so that if there are real errors, we can maybe
482
            # fix them up later.
482
            # fix them up later.
483
			my $clone_record = $record->clone();
483
			my $clone_record = $record->clone();
484
            C4::Biblio::_strip_item_fields($clone_record, '');
484
            C4::Biblio::_strip_item_fields($clone_record);
485
            # This sets the marc fields if there was an error, and also calls
485
            # This sets the marc fields if there was an error, and also calls
486
            # defer_marc_save.
486
            # defer_marc_save.
487
            ModBiblioMarc( $clone_record, $biblionumber, $framework );
487
            ModBiblioMarc( $clone_record, $biblionumber, $framework );
(-)a/t/db_dependent/Items.t (-7 / +3 lines)
Lines 667-673 subtest 'C4::Items::_build_default_values_for_mod_marc' => sub { Link Here
667
    Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => '9', kohafield => 'items.itemnumber' })->store;
667
    Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => '9', kohafield => 'items.itemnumber' })->store;
668
    Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => 'p', kohafield => 'items.barcode' })->store;
668
    Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => 'p', kohafield => 'items.barcode' })->store;
669
    Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => 'y', kohafield => 'items.itype' })->store;
669
    Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => 'y', kohafield => 'items.itype' })->store;
670
    Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
670
    Koha::Caches->get_instance()->flush_all();
671
671
672
    my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype};
672
    my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype};
673
673
Lines 708-716 subtest 'C4::Items::_build_default_values_for_mod_marc' => sub { Link Here
708
    Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '952', tagsubfield => 'p' })->delete;
708
    Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '952', tagsubfield => 'p' })->delete;
709
709
710
    # And make sure the caches are cleared
710
    # And make sure the caches are cleared
711
    my $cache = Koha::Caches->get_instance();
711
    Koha::Caches->get_instance()->flush_all();
712
    $cache->clear_from_cache("default_value_for_mod_marc-");
713
    $cache->clear_from_cache("MarcSubfieldStructure-");
714
712
715
    # Update the MARC field with another value
713
    # Update the MARC field with another value
716
    $item_record->delete_fields( $barcode_field );
714
    $item_record->delete_fields( $barcode_field );
Lines 725-732 subtest 'C4::Items::_build_default_values_for_mod_marc' => sub { Link Here
725
    $item = GetItem($item_itemnumber);
723
    $item = GetItem($item_itemnumber);
726
    is ( $item->{barcode}, $a_barcode, 'items.barcode is not mapped anymore, so the DB column has not been updated' );
724
    is ( $item->{barcode}, $a_barcode, 'items.barcode is not mapped anymore, so the DB column has not been updated' );
727
725
728
    $cache->clear_from_cache("default_value_for_mod_marc-");
726
    Koha::Caches->get_instance()->flush_all();
729
    $cache->clear_from_cache( "MarcSubfieldStructure-" );
730
    $schema->storage->txn_rollback;
727
    $schema->storage->txn_rollback;
731
};
728
};
732
729
733
- 

Return to bug 20664