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

(-)a/Koha/Items.pm (-24 / +13 lines)
Lines 48-54 Koha::Items - Koha Item object set class Link Here
48
Returns a new resultset, containing those items that are not expected to be hidden in OPAC
48
Returns a new resultset, containing those items that are not expected to be hidden in OPAC
49
for the passed I<Koha::Patron> object that is passed.
49
for the passed I<Koha::Patron> object that is passed.
50
50
51
The I<OpacHiddenItems> and I<hidelostitems> system preferences are honoured.
51
The I<OpacHiddenItems>, I<hidelostitems> and I<OpacHiddenItemsExceptions> system preferences
52
are honoured.
52
53
53
=cut
54
=cut
54
55
Lines 59-66 sub filter_by_visible_in_opac { Link Here
59
60
60
    my $result = $self;
61
    my $result = $self;
61
62
63
    # Filter out OpacHiddenItems unless disabled by OpacHiddenItemsExceptions
62
    unless ( $patron and $patron->category->override_hidden_items ) {
64
    unless ( $patron and $patron->category->override_hidden_items ) {
63
        $result = $result->filter_out_opachiddenitems;
65
        my $rules = C4::Context->yaml_preference('OpacHiddenItems') // {};
66
67
        my $rules_params;
68
        foreach my $field (keys %$rules){
69
            $rules_params->{$field}->{'-not_in'} = $rules->{$field};
70
        }
71
72
        $result = $result->search( $rules_params );
64
    }
73
    }
65
74
66
    if (C4::Context->preference('hidelostitems')) {
75
    if (C4::Context->preference('hidelostitems')) {
Lines 70-97 sub filter_by_visible_in_opac { Link Here
70
    return $result;
79
    return $result;
71
}
80
}
72
81
73
=head3 filter_out_opachiddenitems
74
75
    my $filered_items = $items->filter_out_opachiddenitems;
76
77
Returns a new resultset, containing those items that are not expected to be hidden in OPAC.
78
The I<OpacHiddenItems> system preference is honoured.
79
80
=cut
81
82
sub filter_out_opachiddenitems {
83
    my ($self) = @_;
84
85
    my $rules = C4::Context->yaml_preference('OpacHiddenItems') // {};
86
87
    my $rules_params;
88
    foreach my $field (keys %$rules){
89
        $rules_params->{$field}->{'-not_in'} = $rules->{$field};
90
    }
91
92
    return $self->search( $rules_params );
93
}
94
95
=head3 filter_out_lost
82
=head3 filter_out_lost
96
83
97
    my $filered_items = $items->filter_out_lost;
84
    my $filered_items = $items->filter_out_lost;
Lines 129-134 sub object_class { Link Here
129
=head1 AUTHOR
116
=head1 AUTHOR
130
117
131
Kyle M Hall <kyle@bywatersolutions.com>
118
Kyle M Hall <kyle@bywatersolutions.com>
119
Tomas Cohen Arazi <tomascohen@theke.io>
120
Martin Renvoize <martin.renvoize@ptfs-europe.com>
132
121
133
=cut
122
=cut
134
123
(-)a/t/db_dependent/Koha/Items.t (-87 / +1 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 15;
22
use Test::More tests => 13;
23
23
24
use Test::MockModule;
24
use Test::MockModule;
25
use Test::Exception;
25
use Test::Exception;
Lines 423-510 subtest 'filter_out_lost() tests' => sub { Link Here
423
423
424
    $schema->storage->txn_rollback;
424
    $schema->storage->txn_rollback;
425
};
425
};
426
427
subtest 'filter_out_opachiddenitems() tests' => sub {
428
429
    plan tests => 6;
430
431
    $schema->storage->txn_begin;
432
433
    # have a fresh biblio
434
    my $biblio = $builder->build_sample_biblio;
435
    # have two itemtypes
436
    my $itype_1 = $builder->build_object({ class => 'Koha::ItemTypes' });
437
    my $itype_2 = $builder->build_object({ class => 'Koha::ItemTypes' });
438
    # have 5 items on that biblio
439
    my $item_1 = $builder->build_sample_item(
440
        {
441
            biblionumber => $biblio->biblionumber,
442
            itype        => $itype_1->itemtype,
443
            withdrawn    => 1
444
        }
445
    );
446
    my $item_2 = $builder->build_sample_item(
447
        {
448
            biblionumber => $biblio->biblionumber,
449
            itype        => $itype_2->itemtype,
450
            withdrawn    => 2
451
        }
452
    );
453
    my $item_3 = $builder->build_sample_item(
454
        {
455
            biblionumber => $biblio->biblionumber,
456
            itype        => $itype_1->itemtype,
457
            withdrawn    => 3
458
        }
459
    );
460
    my $item_4 = $builder->build_sample_item(
461
        {
462
            biblionumber => $biblio->biblionumber,
463
            itype        => $itype_2->itemtype,
464
            withdrawn    => 4
465
        }
466
    );
467
    my $item_5 = $builder->build_sample_item(
468
        {
469
            biblionumber => $biblio->biblionumber,
470
            itype        => $itype_1->itemtype,
471
            withdrawn    => 5
472
        }
473
    );
474
    my $item_6 = $builder->build_sample_item(
475
        {
476
            biblionumber => $biblio->biblionumber,
477
            itype        => $itype_1->itemtype,
478
            withdrawn    => 5
479
        }
480
    );
481
482
    my $rules = undef;
483
484
    my $mocked_context = Test::MockModule->new('C4::Context');
485
    $mocked_context->mock( 'yaml_preference', sub {
486
        return $rules;
487
    });
488
489
    is( $biblio->items->filter_out_opachiddenitems->count, 6, 'No rules passed' );
490
491
    $rules = {};
492
493
    $rules = { withdrawn => [ 1, 2 ] };
494
    is( $biblio->items->filter_out_opachiddenitems->count, 4, 'Rules on withdrawn' );
495
496
    $rules = { itype => [ $itype_1->itemtype ] };
497
    is( $biblio->items->filter_out_opachiddenitems->count, 2, 'Rules on itype' );
498
499
    $rules = { withdrawn => [ 1, 2 ], itype => [ $itype_1->itemtype ] };
500
    is( $biblio->items->filter_out_opachiddenitems->count, 1, 'Rules on itype and withdrawn' );
501
    is( $biblio->items->filter_out_opachiddenitems->next->itemnumber,
502
        $item_4->itemnumber,
503
        'The right item is returned'
504
    );
505
506
    $rules = { withdrawn => [ 1, 2 ], itype => [ $itype_2->itemtype ] };
507
    is( $biblio->items->filter_out_opachiddenitems->count, 3, 'Rules on itype and withdrawn' );
508
509
    $schema->storage->txn_rollback;
510
};
511
- 

Return to bug 24254