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

(-)a/C4/Items.pm (-5 / +27 lines)
Lines 2599-2604 sub SearchItemsByField { Link Here
2599
sub _SearchItems_build_where_fragment {
2599
sub _SearchItems_build_where_fragment {
2600
    my ($filter) = @_;
2600
    my ($filter) = @_;
2601
2601
2602
    my $dbh = C4::Context->dbh;
2603
2602
    my $where_fragment;
2604
    my $where_fragment;
2603
    if (exists($filter->{conjunction})) {
2605
    if (exists($filter->{conjunction})) {
2604
        my (@where_strs, @where_args);
2606
        my (@where_strs, @where_args);
Lines 2635-2647 sub _SearchItems_build_where_fragment { Link Here
2635
            if ($field =~ /^marc:(\d{3})(?:\$(\w))?$/) {
2637
            if ($field =~ /^marc:(\d{3})(?:\$(\w))?$/) {
2636
                my $marcfield = $1;
2638
                my $marcfield = $1;
2637
                my $marcsubfield = $2;
2639
                my $marcsubfield = $2;
2638
                my $xpath;
2640
                my ($kohafield) = $dbh->selectrow_array(q|
2639
                if ($marcfield < 10) {
2641
                    SELECT kohafield FROM marc_subfield_structure
2640
                    $xpath = "//record/controlfield[\@tag=\"$marcfield\"]";
2642
                    WHERE tagfield=? AND tagsubfield=? AND frameworkcode=''
2643
                |, undef, $marcfield, $marcsubfield);
2644
2645
                if ($kohafield) {
2646
                    $column = $kohafield;
2641
                } else {
2647
                } else {
2642
                    $xpath = "//record/datafield[\@tag=\"$marcfield\"]/subfield[\@code=\"$marcsubfield\"]";
2648
                    # MARC field is not linked to a DB field so we need to use
2649
                    # ExtractValue on biblioitems.marcxml or
2650
                    # items.more_subfields_xml, depending on the MARC field.
2651
                    my $xpath;
2652
                    my $sqlfield;
2653
                    my ($itemfield) = GetMarcFromKohaField('items.itemnumber');
2654
                    if ($marcfield eq $itemfield) {
2655
                        $sqlfield = 'more_subfields_xml';
2656
                        $xpath = '//record/datafield/subfield[@code="' . $marcsubfield . '"]';
2657
                    } else {
2658
                        $sqlfield = 'marcxml';
2659
                        if ($marcfield < 10) {
2660
                            $xpath = "//record/controlfield[\@tag=\"$marcfield\"]";
2661
                        } else {
2662
                            $xpath = "//record/datafield[\@tag=\"$marcfield\"]/subfield[\@code=\"$marcsubfield\"]";
2663
                        }
2664
                    }
2665
                    $column = "ExtractValue($sqlfield, '$xpath')";
2643
                }
2666
                }
2644
                $column = "ExtractValue(marcxml, '$xpath')";
2645
            } else {
2667
            } else {
2646
                $column = $field;
2668
                $column = $field;
2647
            }
2669
            }
(-)a/t/db_dependent/Items.t (-2 / +47 lines)
Lines 228-234 subtest q{Test Koha::Database->schema()->resultset('Item')->itemtype()} => sub { Link Here
228
};
228
};
229
229
230
subtest 'SearchItems test' => sub {
230
subtest 'SearchItems test' => sub {
231
    plan tests => 10;
231
    plan tests => 13;
232
232
233
    # Start transaction
233
    # Start transaction
234
    $dbh->{AutoCommit} = 0;
234
    $dbh->{AutoCommit} = 0;
Lines 337-342 subtest 'SearchItems test' => sub { Link Here
337
    }
337
    }
338
    ok($found, "item1 found");
338
    ok($found, "item1 found");
339
339
340
    my ($itemfield) = GetMarcFromKohaField('items.itemnumber', '');
341
342
    # Create item subfield 'z' without link
343
    $dbh->do('DELETE FROM marc_subfield_structure WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=""', undef, $itemfield);
344
    $dbh->do('INSERT INTO marc_subfield_structure (tagfield, tagsubfield, frameworkcode) VALUES (?, "z", "")', undef, $itemfield);
345
346
    # Clear cache
347
    $C4::Context::context->{marcfromkohafield} = undef;
348
    $C4::Biblio::inverted_field_map = undef;
349
350
    my $item3_record = new MARC::Record;
351
    $item3_record->append_fields(
352
        new MARC::Field($itemfield, '', '', 'z' => 'foobar')
353
    );
354
    my (undef, undef, $item3_itemnumber) = AddItemFromMarc($item3_record,
355
        $biblionumber);
356
357
    # Search item where item subfield z is "foobar"
358
    $filter = {
359
        field => 'marc:' . $itemfield . '$z',
360
        query => 'foobar',
361
        operator => 'like',
362
    };
363
    ($items, $total_results) = SearchItems($filter);
364
    ok(scalar @$items == 1, 'found 1 item with $z = "foobar"');
365
366
    # Link $z to items.itemnotes (and make sure there is no other subfields
367
    # linked to it)
368
    $dbh->do('DELETE FROM marc_subfield_structure WHERE kohafield="items.itemnotes" AND frameworkcode=""', undef, $itemfield);
369
    $dbh->do('UPDATE marc_subfield_structure SET kohafield="items.itemnotes" WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=""', undef, $itemfield);
370
371
    # Clear cache
372
    $C4::Context::context->{marcfromkohafield} = undef;
373
    $C4::Biblio::inverted_field_map = undef;
374
375
    ModItemFromMarc($item3_record, $biblionumber, $item3_itemnumber);
376
377
    # Make sure the link is used
378
    my $item3 = GetItem($item3_itemnumber);
379
    ok($item3->{itemnotes} eq 'foobar', 'itemnotes eq "foobar"');
380
381
    # Do the same search again.
382
    # This time it will search in items.itemnotes
383
    ($items, $total_results) = SearchItems($filter);
384
    ok(scalar @$items == 1, 'found 1 item with itemnotes = "foobar"');
385
340
    $dbh->rollback;
386
    $dbh->rollback;
341
};
387
};
342
388
343
- 

Return to bug 13562