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

(-)a/Koha/Items.pm (-26 / +15 lines)
Lines 39-47 Koha::Items - Koha Item object set class Link Here
39
39
40
=head3 filter_by_for_loan
40
=head3 filter_by_for_loan
41
41
42
$items->filter_by_not_for_loan;
42
    my $filtered_items = $items->filter_by_for_loan;
43
43
44
Return the items of the set that are not for loan
44
Return the items of the set that are loanable
45
45
46
=cut
46
=cut
47
47
Lines 61-67 sub filter_by_for_loan { Link Here
61
Returns a new resultset, containing those items that are not expected to be hidden in OPAC
61
Returns a new resultset, containing those items that are not expected to be hidden in OPAC
62
for the passed I<Koha::Patron> object that is passed.
62
for the passed I<Koha::Patron> object that is passed.
63
63
64
The I<OpacHiddenItems> and I<hidelostitems> system preferences are honoured.
64
The I<OpacHiddenItems>, I<hidelostitems> and I<OpacHiddenItemsExceptions> system preferences
65
are honoured.
65
66
66
=cut
67
=cut
67
68
Lines 72-79 sub filter_by_visible_in_opac { Link Here
72
73
73
    my $result = $self;
74
    my $result = $self;
74
75
76
    # Filter out OpacHiddenItems unless disabled by OpacHiddenItemsExceptions
75
    unless ( $patron and $patron->category->override_hidden_items ) {
77
    unless ( $patron and $patron->category->override_hidden_items ) {
76
        $result = $result->filter_out_opachiddenitems;
78
        my $rules = C4::Context->yaml_preference('OpacHiddenItems') // {};
79
80
        my $rules_params;
81
        foreach my $field (keys %$rules){
82
            $rules_params->{$field}->{'-not_in'} = $rules->{$field};
83
        }
84
85
        $result = $result->search( $rules_params );
77
    }
86
    }
78
87
79
    if (C4::Context->preference('hidelostitems')) {
88
    if (C4::Context->preference('hidelostitems')) {
Lines 83-110 sub filter_by_visible_in_opac { Link Here
83
    return $result;
92
    return $result;
84
}
93
}
85
94
86
=head3 filter_out_opachiddenitems
87
88
    my $filered_items = $items->filter_out_opachiddenitems;
89
90
Returns a new resultset, containing those items that are not expected to be hidden in OPAC.
91
The I<OpacHiddenItems> system preference is honoured.
92
93
=cut
94
95
sub filter_out_opachiddenitems {
96
    my ($self) = @_;
97
98
    my $rules = C4::Context->yaml_preference('OpacHiddenItems') // {};
99
100
    my $rules_params;
101
    foreach my $field (keys %$rules){
102
        $rules_params->{$field}->{'-not_in'} = $rules->{$field};
103
    }
104
105
    return $self->search( $rules_params );
106
}
107
108
=head3 filter_out_lost
95
=head3 filter_out_lost
109
96
110
    my $filered_items = $items->filter_out_lost;
97
    my $filered_items = $items->filter_out_lost;
Lines 142-147 sub object_class { Link Here
142
=head1 AUTHOR
129
=head1 AUTHOR
143
130
144
Kyle M Hall <kyle@bywatersolutions.com>
131
Kyle M Hall <kyle@bywatersolutions.com>
132
Tomas Cohen Arazi <tomascohen@theke.io>
133
Martin Renvoize <martin.renvoize@ptfs-europe.com>
145
134
146
=cut
135
=cut
147
136
(-)a/t/db_dependent/Koha/Items.t (-86 / +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 => 14;
23
23
24
use Test::MockModule;
24
use Test::MockModule;
25
use Test::Exception;
25
use Test::Exception;
Lines 1501-1587 subtest 'filter_out_lost() tests' => sub { Link Here
1501
    $schema->storage->txn_rollback;
1501
    $schema->storage->txn_rollback;
1502
};
1502
};
1503
1503
1504
subtest 'filter_out_opachiddenitems() tests' => sub {
1505
1506
    plan tests => 6;
1507
1508
    $schema->storage->txn_begin;
1509
1510
    # have a fresh biblio
1511
    my $biblio = $builder->build_sample_biblio;
1512
    # have two itemtypes
1513
    my $itype_1 = $builder->build_object({ class => 'Koha::ItemTypes' });
1514
    my $itype_2 = $builder->build_object({ class => 'Koha::ItemTypes' });
1515
    # have 5 items on that biblio
1516
    my $item_1 = $builder->build_sample_item(
1517
        {
1518
            biblionumber => $biblio->biblionumber,
1519
            itype        => $itype_1->itemtype,
1520
            withdrawn    => 1
1521
        }
1522
    );
1523
    my $item_2 = $builder->build_sample_item(
1524
        {
1525
            biblionumber => $biblio->biblionumber,
1526
            itype        => $itype_2->itemtype,
1527
            withdrawn    => 2
1528
        }
1529
    );
1530
    my $item_3 = $builder->build_sample_item(
1531
        {
1532
            biblionumber => $biblio->biblionumber,
1533
            itype        => $itype_1->itemtype,
1534
            withdrawn    => 3
1535
        }
1536
    );
1537
    my $item_4 = $builder->build_sample_item(
1538
        {
1539
            biblionumber => $biblio->biblionumber,
1540
            itype        => $itype_2->itemtype,
1541
            withdrawn    => 4
1542
        }
1543
    );
1544
    my $item_5 = $builder->build_sample_item(
1545
        {
1546
            biblionumber => $biblio->biblionumber,
1547
            itype        => $itype_1->itemtype,
1548
            withdrawn    => 5
1549
        }
1550
    );
1551
    my $item_6 = $builder->build_sample_item(
1552
        {
1553
            biblionumber => $biblio->biblionumber,
1554
            itype        => $itype_1->itemtype,
1555
            withdrawn    => 5
1556
        }
1557
    );
1558
1559
    my $rules = undef;
1560
1561
    my $mocked_context = Test::MockModule->new('C4::Context');
1562
    $mocked_context->mock( 'yaml_preference', sub {
1563
        return $rules;
1564
    });
1565
1566
    is( $biblio->items->filter_out_opachiddenitems->count, 6, 'No rules passed' );
1567
1568
    $rules = {};
1569
1570
    $rules = { withdrawn => [ 1, 2 ] };
1571
    is( $biblio->items->filter_out_opachiddenitems->count, 4, 'Rules on withdrawn' );
1572
1573
    $rules = { itype => [ $itype_1->itemtype ] };
1574
    is( $biblio->items->filter_out_opachiddenitems->count, 2, 'Rules on itype' );
1575
1576
    $rules = { withdrawn => [ 1, 2 ], itype => [ $itype_1->itemtype ] };
1577
    is( $biblio->items->filter_out_opachiddenitems->count, 1, 'Rules on itype and withdrawn' );
1578
    is( $biblio->items->filter_out_opachiddenitems->next->itemnumber,
1579
        $item_4->itemnumber,
1580
        'The right item is returned'
1581
    );
1582
1583
    $rules = { withdrawn => [ 1, 2 ], itype => [ $itype_2->itemtype ] };
1584
    is( $biblio->items->filter_out_opachiddenitems->count, 3, 'Rules on itype and withdrawn' );
1585
1586
    $schema->storage->txn_rollback;
1587
};
1588
- 

Return to bug 24254