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 1410-1415 sub Item2Marc { Link Here
1410
	return $itemmarc;
1410
	return $itemmarc;
1411
}
1411
}
1412
1412
1413
=head2 GetMarcItemFields
1414
1415
  my @marc_fields = GetMarcItemFields($biblionumber);
1416
1417
Returns an array of MARC::Record objects of the items for the biblio.
1418
1419
=cut
1420
1421
sub GetMarcItemFields {
1422
	my ( $biblionumber, $itemnumbers, $hidingrules ) = @_;
1423
1424
    my $item_level_itype = C4::Context->preference('item-level_itypes');
1425
    # This is so much faster than using Koha::Items->search that it makes sense even if it's ugly.
1426
    my $sth = C4::Context->dbh->prepare('SELECT * FROM items WHERE biblionumber = ?');
1427
    $sth->execute($biblionumber);
1428
    my $items = $sth->fetchall_arrayref({});
1429
    $sth->finish();
1430
    my @item_fields;
1431
    my ($itemtag, $itemsubfield) = GetMarcFromKohaField('items.itemnumber');
1432
1433
    ITEMLOOP: foreach my $item (@$items) {
1434
1435
        # Check itemnumbers
1436
        next if (@$itemnumbers && !any { $_ == $item->{itemnumber} } @$itemnumbers);
1437
1438
        # Check hiding rules
1439
        if (defined $hidingrules) {
1440
            foreach my $field (keys %$hidingrules) {
1441
                my $val = $item->{$field};
1442
                $val = '' unless defined $val;
1443
1444
                # If the results matches the values in the hiding rules, skip the item
1445
                if (any { $val eq $_ } @{$hidingrules->{$field}}) {
1446
                    next ITEMLOOP;
1447
                }
1448
            }
1449
        }
1450
1451
        # Set correct item type
1452
        if (!$item_level_itype || !$item->{itype}) {
1453
            warn 'item-level_itypes set but no itemtype set for item (' . $item->{itemnumber} . ')' if (!$item->{itype});
1454
            my $biblioitem = Koha::Biblioitems->find($item->{biblioitemnumber});
1455
            $item->{itype} = $biblioitem->itemtype();
1456
        }
1457
1458
        my $mungeditem = {
1459
            map {
1460
                defined($item->{$_}) && $item->{$_} ne '' ? ("items.$_" => $item->{$_}) : ()
1461
            } keys %{ $item }
1462
        };
1463
        my $itemmarc = TransformKohaToMarc($mungeditem);
1464
1465
        my $unlinked_item_subfields = _parse_unlinked_item_subfields_from_xml($mungeditem->{'items.more_subfields_xml'});
1466
        if (defined $unlinked_item_subfields and $#$unlinked_item_subfields > -1) {
1467
            foreach my $field ($itemmarc->field($itemtag)){
1468
                $field->add_subfields(@$unlinked_item_subfields);
1469
            }
1470
        }
1471
1472
        push @item_fields, $itemmarc->field($itemtag);
1473
    }
1474
1475
    return \@item_fields;
1476
}
1477
1413
=head1 PRIVATE FUNCTIONS AND VARIABLES
1478
=head1 PRIVATE FUNCTIONS AND VARIABLES
1414
1479
1415
The following functions are not meant to be called
1480
The following functions are not meant to be called
Lines 1546-1555 sub _do_column_fixes_for_mod { Link Here
1546
        (not defined $item->{'withdrawn'} or $item->{'withdrawn'} eq '')) {
1611
        (not defined $item->{'withdrawn'} or $item->{'withdrawn'} eq '')) {
1547
        $item->{'withdrawn'} = 0;
1612
        $item->{'withdrawn'} = 0;
1548
    }
1613
    }
1549
    if (exists $item->{location}
1614
    if (exists $item->{'location'}
1550
        and $item->{location} ne 'CART'
1615
        and defined $item->{'location'}
1551
        and $item->{location} ne 'PROC'
1616
        and $item->{'location'} ne 'CART'
1552
        and not $item->{permanent_location}
1617
        and $item->{'location'} ne 'PROC'
1618
        and (not defined $item->{'permanent_location'} or not $item->{'permanent_location'})
1553
    ) {
1619
    ) {
1554
        $item->{'permanent_location'} = $item->{'location'};
1620
        $item->{'permanent_location'} = $item->{'location'};
1555
    }
1621
    }
Lines 2214-2220 sub _SearchItems_build_where_fragment { Link Here
2214
        push @columns, Koha::Database->new()->schema()->resultset('Biblioitem')->result_source->columns;
2280
        push @columns, Koha::Database->new()->schema()->resultset('Biblioitem')->result_source->columns;
2215
        my @operators = qw(= != > < >= <= like);
2281
        my @operators = qw(= != > < >= <= like);
2216
        my $field = $filter->{field};
2282
        my $field = $filter->{field};
2217
        if ( (0 < grep /^$field$/, @columns) or (substr($field, 0, 5) eq 'marc:') ) {
2283
        if ( defined $field and ((0 < grep /^$field$/, @columns) or (substr($field, 0, 5) eq 'marc:')) ) {
2218
            my $op = $filter->{operator};
2284
            my $op = $filter->{operator};
2219
            my $query = $filter->{query};
2285
            my $query = $filter->{query};
2220
2286
(-)a/misc/migration_tools/bulkmarcimport.pl (-1 / +1 lines)
Lines 486-492 RECORD: while ( ) { Link Here
486
            # Work on a clone so that if there are real errors, we can maybe
486
            # Work on a clone so that if there are real errors, we can maybe
487
            # fix them up later.
487
            # fix them up later.
488
			my $clone_record = $record->clone();
488
			my $clone_record = $record->clone();
489
            C4::Biblio::_strip_item_fields($clone_record, '');
489
            C4::Biblio::_strip_item_fields($clone_record);
490
            # This sets the marc fields if there was an error, and also calls
490
            # This sets the marc fields if there was an error, and also calls
491
            # defer_marc_save.
491
            # defer_marc_save.
492
            ModBiblioMarc( $clone_record, $biblionumber, $framework );
492
            ModBiblioMarc( $clone_record, $biblionumber, $framework );
(-)a/t/db_dependent/Items.t (-7 / +3 lines)
Lines 664-670 subtest 'C4::Items::_build_default_values_for_mod_marc' => sub { Link Here
664
    Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => '9', kohafield => 'items.itemnumber' })->store;
664
    Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => '9', kohafield => 'items.itemnumber' })->store;
665
    Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => 'p', kohafield => 'items.barcode' })->store;
665
    Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => 'p', kohafield => 'items.barcode' })->store;
666
    Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => 'y', kohafield => 'items.itype' })->store;
666
    Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => 'y', kohafield => 'items.itype' })->store;
667
    Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
667
    Koha::Caches->get_instance()->flush_all();
668
668
669
    my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype};
669
    my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype};
670
670
Lines 705-713 subtest 'C4::Items::_build_default_values_for_mod_marc' => sub { Link Here
705
    Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '952', tagsubfield => 'p' })->delete;
705
    Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '952', tagsubfield => 'p' })->delete;
706
706
707
    # And make sure the caches are cleared
707
    # And make sure the caches are cleared
708
    my $cache = Koha::Caches->get_instance();
708
    Koha::Caches->get_instance()->flush_all();
709
    $cache->clear_from_cache("default_value_for_mod_marc-");
710
    $cache->clear_from_cache("MarcSubfieldStructure-");
711
709
712
    # Update the MARC field with another value
710
    # Update the MARC field with another value
713
    $item_record->delete_fields( $barcode_field );
711
    $item_record->delete_fields( $barcode_field );
Lines 722-729 subtest 'C4::Items::_build_default_values_for_mod_marc' => sub { Link Here
722
    $item = GetItem($item_itemnumber);
720
    $item = GetItem($item_itemnumber);
723
    is ( $item->{barcode}, $a_barcode, 'items.barcode is not mapped anymore, so the DB column has not been updated' );
721
    is ( $item->{barcode}, $a_barcode, 'items.barcode is not mapped anymore, so the DB column has not been updated' );
724
722
725
    $cache->clear_from_cache("default_value_for_mod_marc-");
723
    Koha::Caches->get_instance()->flush_all();
726
    $cache->clear_from_cache( "MarcSubfieldStructure-" );
727
    $schema->storage->txn_rollback;
724
    $schema->storage->txn_rollback;
728
};
725
};
729
726
730
- 

Return to bug 20664