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

(-)a/C4/Biblio.pm (-48 / +39 lines)
Lines 291-297 sub ModBiblio { Link Here
291
291
292
    $frameworkcode = "" if !$frameworkcode || $frameworkcode eq "Default"; # XXX
292
    $frameworkcode = "" if !$frameworkcode || $frameworkcode eq "Default"; # XXX
293
293
294
    _strip_item_fields($record, $frameworkcode);
294
    _strip_item_fields($record);
295
295
296
    # update biblionumber and biblioitemnumber in MARC
296
    # update biblionumber and biblioitemnumber in MARC
297
    # FIXME - this is assuming a 1 to 1 relationship between
297
    # FIXME - this is assuming a 1 to 1 relationship between
Lines 325-331 sub ModBiblio { Link Here
325
325
326
=head2 _strip_item_fields
326
=head2 _strip_item_fields
327
327
328
  _strip_item_fields($record, $frameworkcode)
328
  _strip_item_fields($record)
329
329
330
Utility routine to remove item tags from a
330
Utility routine to remove item tags from a
331
MARC bib.
331
MARC bib.
Lines 334-342 MARC bib. Link Here
334
334
335
sub _strip_item_fields {
335
sub _strip_item_fields {
336
    my $record = shift;
336
    my $record = shift;
337
    my $frameworkcode = shift;
338
    # get the items before and append them to the biblio before updating the record, atm we just have the biblio
337
    # get the items before and append them to the biblio before updating the record, atm we just have the biblio
339
    my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField( "items.itemnumber", $frameworkcode );
338
    my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField( "items.itemnumber" );
340
339
341
    # delete any item fields from incoming record to avoid
340
    # delete any item fields from incoming record to avoid
342
    # duplication or incorrect data - use AddItem() or ModItem()
341
    # duplication or incorrect data - use AddItem() or ModItem()
Lines 1076-1089 sub GetMarcSubfieldStructure { Link Here
1076
sub GetMarcFromKohaField {
1075
sub GetMarcFromKohaField {
1077
    my ( $kohafield ) = @_;
1076
    my ( $kohafield ) = @_;
1078
    return unless $kohafield;
1077
    return unless $kohafield;
1079
    # The next call uses the Default framework since it is AUTHORITATIVE
1078
1080
    # for all Koha to MARC mappings.
1079
    my $cache     = Koha::Caches->get_instance();
1081
    my $mss = GetMarcSubfieldStructure( '', { unsafe => 1 } ); # Do not change framework
1080
    my $cache_key = "MarcFromKohaField-$kohafield";
1082
    my @retval;
1081
    my $cached    = $cache->get_from_cache($cache_key);
1083
    foreach( @{ $mss->{$kohafield} } ) {
1082
    if ( !defined $cached ) {
1084
        push @retval, $_->{tagfield}, $_->{tagsubfield};
1083
        # The next call uses the Default framework since it is AUTHORITATIVE
1085
    }
1084
        # for all Koha to MARC mappings.
1086
    return wantarray ? @retval : ( @retval ? $retval[0] : undef );
1085
        my $mss = GetMarcSubfieldStructure( '', { unsafe => 1 } ); # Do not change framework
1086
        my @retval;
1087
        foreach( @{ $mss->{$kohafield} } ) {
1088
            push @retval, $_->{tagfield}, $_->{tagsubfield};
1089
        }
1090
        $cached = \@retval;
1091
        $cache->set_in_cache( $cache_key, $cached );
1092
    }
1093
    return wantarray ? @$cached : ( @$cached ? $cached->[0] : undef );
1087
}
1094
}
1088
1095
1089
=head2 GetMarcSubfieldStructureFromKohaField
1096
=head2 GetMarcSubfieldStructureFromKohaField
Lines 2792-2798 override if necessary. Link Here
2792
2799
2793
sub EmbedItemsInMarcBiblio {
2800
sub EmbedItemsInMarcBiblio {
2794
    my ($params) = @_;
2801
    my ($params) = @_;
2795
    my ($marc, $biblionumber, $itemnumbers, $opac, $borcat);
2802
    my ($marc, $biblionumber, $itemnumbers, $use_hidingrules, $borcat);
2796
    $marc = $params->{marc_record};
2803
    $marc = $params->{marc_record};
2797
    if ( !$marc ) {
2804
    if ( !$marc ) {
2798
        carp 'EmbedItemsInMarcBiblio: No MARC record passed';
2805
        carp 'EmbedItemsInMarcBiblio: No MARC record passed';
Lines 2800-2848 sub EmbedItemsInMarcBiblio { Link Here
2800
    }
2807
    }
2801
    $biblionumber = $params->{biblionumber};
2808
    $biblionumber = $params->{biblionumber};
2802
    $itemnumbers = $params->{item_numbers};
2809
    $itemnumbers = $params->{item_numbers};
2803
    $opac = $params->{opac};
2810
    $use_hidingrules = $params->{opac};
2804
    $borcat = $params->{borcat} // q{};
2811
    $borcat = $params->{borcat} // q{};
2805
2812
2806
    $itemnumbers = [] unless defined $itemnumbers;
2813
    $itemnumbers = [] unless defined $itemnumbers;
2807
2814
2808
    my $frameworkcode = GetFrameworkCode($biblionumber);
2815
    _strip_item_fields($marc);
2809
    _strip_item_fields($marc, $frameworkcode);
2810
2816
2811
    # ... and embed the current items
2817
    my $hidingrules = {};
2812
    my $dbh = C4::Context->dbh;
2818
    if ($use_hidingrules && $borcat && !UseHidingRulesWithBorrowerCategory($borcat)) {
2813
    my $sth = $dbh->prepare("SELECT itemnumber FROM items WHERE biblionumber = ?");
2819
        # don't hide anything from this borrower category
2814
    $sth->execute($biblionumber);
2820
        $use_hidingrules = 0;
2815
    my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField( "items.itemnumber", $frameworkcode );
2821
    }
2816
2822
2817
    my @item_fields; # Array holding the actual MARC data for items to be included.
2823
    my $yaml = $use_hidingrules ? C4::Context->preference('OpacHiddenItems') : '';
2818
    my @items;       # Array holding items which are both in the list (sitenumbers)
2824
    if ( $yaml =~ /\S/ ) {
2819
                     # and on this biblionumber
2825
        $yaml = "$yaml\n\n"; # YAML is anal on ending \n. Surplus does not hurt
2826
        eval {
2827
            $hidingrules = YAML::Load($yaml);
2828
        };
2829
        if ($@) {
2830
            carp "Unable to parse OpacHiddenItems syspref : $@";
2831
        }
2832
    }
2820
2833
2821
    # Flag indicating if there is potential hiding.
2834
    my $item_fields = C4::Items::GetMarcItemFields( $biblionumber, $itemnumbers, $hidingrules );
2822
    my $opachiddenitems = $opac
2823
      && ( C4::Context->preference('OpacHiddenItems') !~ /^\s*$/ );
2824
2835
2825
    require C4::Items;
2836
    $marc->append_fields(@$item_fields) if ( @$item_fields );
2826
    while ( my ($itemnumber) = $sth->fetchrow_array ) {
2827
        next if @$itemnumbers and not grep { $_ == $itemnumber } @$itemnumbers;
2828
        my $i = $opachiddenitems ? C4::Items::GetItem($itemnumber) : undef;
2829
        push @items, { itemnumber => $itemnumber, item => $i };
2830
    }
2831
    my @items2pass = map { $_->{item} } @items;
2832
    my @hiddenitems =
2833
      $opachiddenitems
2834
      ? C4::Items::GetHiddenItemnumbers({
2835
            items  => \@items2pass,
2836
            borcat => $borcat })
2837
      : ();
2838
    # Convert to a hash for quick searching
2839
    my %hiddenitems = map { $_ => 1 } @hiddenitems;
2840
    foreach my $itemnumber ( map { $_->{itemnumber} } @items ) {
2841
        next if $hiddenitems{$itemnumber};
2842
        my $item_marc = C4::Items::GetMarcItem( $biblionumber, $itemnumber );
2843
        push @item_fields, $item_marc->field($itemtag);
2844
    }
2845
    $marc->append_fields(@item_fields);
2846
}
2837
}
2847
2838
2848
=head1 INTERNAL FUNCTIONS
2839
=head1 INTERNAL FUNCTIONS
(-)a/C4/Items.pm (-12 / +100 lines)
Lines 44-49 BEGIN { Link Here
44
        GetHostItemsInfo
44
        GetHostItemsInfo
45
        get_hostitemnumbers_of
45
        get_hostitemnumbers_of
46
        GetHiddenItemnumbers
46
        GetHiddenItemnumbers
47
        UseHidingRulesWithBorrowerCategory
47
        ItemSafeToDelete
48
        ItemSafeToDelete
48
        DelItemCheck
49
        DelItemCheck
49
        MoveItemFromBiblio
50
        MoveItemFromBiblio
Lines 1247-1261 to be excluded Link Here
1247
sub GetHiddenItemnumbers {
1248
sub GetHiddenItemnumbers {
1248
    my $params = shift;
1249
    my $params = shift;
1249
    my $items = $params->{items};
1250
    my $items = $params->{items};
1250
    if (my $exceptions = C4::Context->preference('OpacHiddenItemsExceptions') and $params->{'borcat'}){
1251
1251
        foreach my $except (split(/\|/, $exceptions)){
1252
    if ($params->{'borcat'} && !UseHidingRulesWithBorrowerCategory($params->{'borcat'})) {
1252
            if ($params->{'borcat'} eq $except){
1253
        # Don't hide anything from this borrower category
1253
                return; # we don't hide anything for this borrower category
1254
        return;
1254
            }
1255
        }
1256
    }
1255
    }
1257
    my @resultitems;
1258
1256
1257
    my @resultitems;
1259
    my $yaml = C4::Context->preference('OpacHiddenItems');
1258
    my $yaml = C4::Context->preference('OpacHiddenItems');
1260
    return () if (! $yaml =~ /\S/ );
1259
    return () if (! $yaml =~ /\S/ );
1261
    $yaml = "$yaml\n\n"; # YAML is anal on ending \n. Surplus does not hurt
1260
    $yaml = "$yaml\n\n"; # YAML is anal on ending \n. Surplus does not hurt
Lines 1298-1303 sub GetHiddenItemnumbers { Link Here
1298
    return @resultitems;
1297
    return @resultitems;
1299
}
1298
}
1300
1299
1300
=head2 UseHidingRulesWithBorrowerCategory
1301
1302
    if (UseHidingRulesWithBorrowerCategory($category}) { ... }
1303
1304
Checks if item hiding rules should be used with the given borrower category.
1305
1306
=cut
1307
1308
sub UseHidingRulesWithBorrowerCategory {
1309
    my ($borcat) = @_;
1310
1311
    if ($borcat && (my $exceptions = C4::Context->preference('OpacHiddenItemsExceptions'))) {
1312
        foreach my $except (split(/\|/, $exceptions)) {
1313
            if ($borcat eq $except) {
1314
                return 0;
1315
            }
1316
        }
1317
    }
1318
    return 1;
1319
}
1320
1301
=head1 LIMITED USE FUNCTIONS
1321
=head1 LIMITED USE FUNCTIONS
1302
1322
1303
The following functions, while part of the public API,
1323
The following functions, while part of the public API,
Lines 1365-1370 sub Item2Marc { Link Here
1365
	return $itemmarc;
1385
	return $itemmarc;
1366
}
1386
}
1367
1387
1388
=head2 GetMarcItemFields
1389
1390
  my @marc_fields = GetMarcItemFields($biblionumber);
1391
1392
Returns an array of MARC::Record objects of the items for the biblio.
1393
1394
=cut
1395
1396
sub GetMarcItemFields {
1397
	my ( $biblionumber, $itemnumbers, $hidingrules ) = @_;
1398
1399
    my $item_level_itype = C4::Context->preference('item-level_itypes');
1400
    # This is so much faster than using Koha::Items->search that it makes sense even if it's ugly.
1401
    my $query = 'SELECT * FROM items WHERE biblionumber = ?';
1402
    if (@$itemnumbers) {
1403
        $query .= ' AND itemnumber IN (' . join(',', @$itemnumbers) . ')';
1404
    }
1405
    my $sth = C4::Context->dbh->prepare($query);
1406
    $sth->execute($biblionumber);
1407
    my $items = $sth->fetchall_arrayref({});
1408
    $sth->finish();
1409
    my @item_fields;
1410
    my ($itemtag, $itemsubfield) = GetMarcFromKohaField('items.itemnumber');
1411
1412
    ITEMLOOP: foreach my $item (@$items) {
1413
1414
        # Check hiding rules
1415
        if (defined $hidingrules) {
1416
            foreach my $field (keys %$hidingrules) {
1417
                my $val = $item->{$field};
1418
                $val = '' unless defined $val;
1419
1420
                # If the results matches the values in the hiding rules, skip the item
1421
                if (any { $val eq $_ } @{$hidingrules->{$field}}) {
1422
                    next ITEMLOOP;
1423
                }
1424
            }
1425
        }
1426
1427
        # Set correct item type
1428
        if (!$item_level_itype || !$item->{itype}) {
1429
            warn 'item-level_itypes set but no itemtype set for item (' . $item->{itemnumber} . ')' if (!$item->{itype});
1430
            my $biblioitem = Koha::Biblioitems->find($item->{biblioitemnumber});
1431
            $item->{itype} = $biblioitem->itemtype();
1432
        }
1433
1434
        my $mungeditem = {
1435
            map {
1436
                defined($item->{$_}) && $item->{$_} ne '' ? ("items.$_" => $item->{$_}) : ()
1437
            } keys %{ $item }
1438
        };
1439
        my $itemmarc = TransformKohaToMarc($mungeditem);
1440
1441
        my $unlinked_item_subfields = _parse_unlinked_item_subfields_from_xml($mungeditem->{'items.more_subfields_xml'});
1442
        if (defined $unlinked_item_subfields and $#$unlinked_item_subfields > -1) {
1443
            foreach my $field ($itemmarc->field($itemtag)){
1444
                $field->add_subfields(@$unlinked_item_subfields);
1445
            }
1446
        }
1447
1448
        push @item_fields, $itemmarc->field($itemtag);
1449
    }
1450
1451
    return \@item_fields;
1452
}
1453
1454
1368
=head1 PRIVATE FUNCTIONS AND VARIABLES
1455
=head1 PRIVATE FUNCTIONS AND VARIABLES
1369
1456
1370
The following functions are not meant to be called
1457
The following functions are not meant to be called
Lines 1501-1510 sub _do_column_fixes_for_mod { Link Here
1501
        (not defined $item->{'withdrawn'} or $item->{'withdrawn'} eq '')) {
1588
        (not defined $item->{'withdrawn'} or $item->{'withdrawn'} eq '')) {
1502
        $item->{'withdrawn'} = 0;
1589
        $item->{'withdrawn'} = 0;
1503
    }
1590
    }
1504
    if (exists $item->{location}
1591
    if (exists $item->{'location'}
1505
        and $item->{location} ne 'CART'
1592
        and defined $item->{'location'}
1506
        and $item->{location} ne 'PROC'
1593
        and $item->{'location'} ne 'CART'
1507
        and not $item->{permanent_location}
1594
        and $item->{'location'} ne 'PROC'
1595
        and (not defined $item->{'permanent_location'} or not $item->{'permanent_location'})
1508
    ) {
1596
    ) {
1509
        $item->{'permanent_location'} = $item->{'location'};
1597
        $item->{'permanent_location'} = $item->{'location'};
1510
    }
1598
    }
Lines 2169-2175 sub _SearchItems_build_where_fragment { Link Here
2169
        push @columns, Koha::Database->new()->schema()->resultset('Biblioitem')->result_source->columns;
2257
        push @columns, Koha::Database->new()->schema()->resultset('Biblioitem')->result_source->columns;
2170
        my @operators = qw(= != > < >= <= like);
2258
        my @operators = qw(= != > < >= <= like);
2171
        my $field = $filter->{field};
2259
        my $field = $filter->{field};
2172
        if ( (0 < grep /^$field$/, @columns) or (substr($field, 0, 5) eq 'marc:') ) {
2260
        if ( defined $field and ((0 < grep /^$field$/, @columns) or (substr($field, 0, 5) eq 'marc:')) ) {
2173
            my $op = $filter->{operator};
2261
            my $op = $filter->{operator};
2174
            my $query = $filter->{query};
2262
            my $query = $filter->{query};
2175
2263
(-)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 698-704 subtest 'C4::Items::_build_default_values_for_mod_marc' => sub { Link Here
698
    Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => '9', kohafield => 'items.itemnumber' })->store;
698
    Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => '9', kohafield => 'items.itemnumber' })->store;
699
    Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => 'p', kohafield => 'items.barcode' })->store;
699
    Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => 'p', kohafield => 'items.barcode' })->store;
700
    Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => 'y', kohafield => 'items.itype' })->store;
700
    Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '952', tagsubfield => 'y', kohafield => 'items.itype' })->store;
701
    Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
701
    Koha::Caches->get_instance()->flush_all();
702
702
703
    my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype};
703
    my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype};
704
704
Lines 739-747 subtest 'C4::Items::_build_default_values_for_mod_marc' => sub { Link Here
739
    Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '952', tagsubfield => 'p' })->delete;
739
    Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '952', tagsubfield => 'p' })->delete;
740
740
741
    # And make sure the caches are cleared
741
    # And make sure the caches are cleared
742
    my $cache = Koha::Caches->get_instance();
742
    Koha::Caches->get_instance()->flush_all();
743
    $cache->clear_from_cache("default_value_for_mod_marc-");
744
    $cache->clear_from_cache("MarcSubfieldStructure-");
745
743
746
    # Update the MARC field with another value
744
    # Update the MARC field with another value
747
    $item_record->delete_fields( $barcode_field );
745
    $item_record->delete_fields( $barcode_field );
Lines 756-763 subtest 'C4::Items::_build_default_values_for_mod_marc' => sub { Link Here
756
    $item = GetItem($item_itemnumber);
754
    $item = GetItem($item_itemnumber);
757
    is ( $item->{barcode}, $a_barcode, 'items.barcode is not mapped anymore, so the DB column has not been updated' );
755
    is ( $item->{barcode}, $a_barcode, 'items.barcode is not mapped anymore, so the DB column has not been updated' );
758
756
759
    $cache->clear_from_cache("default_value_for_mod_marc-");
757
    Koha::Caches->get_instance()->flush_all();
760
    $cache->clear_from_cache( "MarcSubfieldStructure-" );
761
    $schema->storage->txn_rollback;
758
    $schema->storage->txn_rollback;
762
};
759
};
763
760
764
- 

Return to bug 20664