diff --git a/Koha/Virtualshelfcontents.pm b/Koha/Virtualshelfcontents.pm index b499b9813f..2870babb6b 100644 --- a/Koha/Virtualshelfcontents.pm +++ b/Koha/Virtualshelfcontents.pm @@ -31,9 +31,23 @@ Koha::Virtualshelfcontents - Koha Virtualshelfcontents Object class =head2 Class Methods +=head3 filter_by_not_suppressed + + my $contents = $shelf->get_contents->filter_by_not_suppressed; + +Filters out shelf contents whose linked biblio has opac_suppressed set to true. + =cut -=head3 type +sub filter_by_not_suppressed { + my ($self) = @_; + + return $self->search( { 'biblionumber.opac_suppressed' => 0 }, { join => 'biblionumber' } ); +} + +=head2 Internal methods + +=head3 _type =cut diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-shelves.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-shelves.tt index 4115996b00..294f678086 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-shelves.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-shelves.tt @@ -46,7 +46,7 @@ data-shelfnumber="[% shelf.shelfnumber | html %]" data-shelfname="[% shelf.shelfname | html %]" data-shared="[% shelf.is_shared | html %]" - data-count="[% contents.count | html %]" + data-count="[% contents_count | html %]" > [% IF ( context == "list" ) %] @@ -258,8 +258,7 @@ [% IF ( itemsloop ) %] - [% SET contents = shelf.get_contents %] - [% IF ( contents.count ) %]

This list contains [% contents.count | html %] titles

[% END %] + [% IF ( contents_count ) %]

This list contains [% contents_count | html %] titles

[% END %]
@@ -738,11 +737,18 @@ - [% FOREACH s IN shelves %] - [% SET contents = s.get_contents %] + [% FOREACH entry IN shelves_list %] + [% SET s = entry.shelf %] [% s.shelfname | html %] - [% IF contents.count %][% contents.count | html %] [% IF contents.count == 1 %]item[% ELSE %]items[% END %][% ELSE %]Empty[% END %] + [% IF entry.contents_count %] + [% entry.contents_count | html %] + [% IF entry.contents_count == 1 %]item[% ELSE %]items[% END %] + [% ELSE %] + Empty + [% END %] [% IF s.is_private %] [% IF s.is_shared %]Shared[% ELSE %]Private[% END %] @@ -762,7 +768,7 @@ [% END %] [% IF s.can_be_deleted( loggedinusernumber ) %] - [% PROCESS delete_shelf shelf = s context = "list" %] + [% PROCESS delete_shelf shelf = s context = "list" contents_count = entry.contents_count %] [% END %] [% IF s.is_private AND s.can_be_managed( loggedinusernumber ) AND Koha.Preference('OpacAllowSharingPrivateLists') %] Share diff --git a/opac/opac-shelves.pl b/opac/opac-shelves.pl index 436f485d62..389f3aa6c3 100755 --- a/opac/opac-shelves.pl +++ b/opac/opac-shelves.pl @@ -331,8 +331,20 @@ if ( $op eq 'view' ) { } else { $rows = C4::Context->preference('OPACnumSearchResults') || 20; } - my $order_by = $sortfield eq 'itemcallnumber' ? 'items.cn_sort' : $sortfield; - my $contents = $shelf->get_contents->search( + my $order_by = $sortfield eq 'itemcallnumber' ? 'items.cn_sort' : $sortfield; + my $shelf_contents = $shelf->get_contents; + + if ( C4::Context->preference('OpacSuppression') ) { + my $ip_in_range = 0; + if ( my $ip_range = C4::Context->preference('OpacSuppressionByIPRange') ) { + my $ip_address = $ENV{'REMOTE_ADDR'}; + $ip_in_range = ( $ip_address =~ /^$ip_range/ ); + } + $shelf_contents = $shelf_contents->filter_by_not_suppressed + unless $ip_in_range; + } + + my $contents = $shelf_contents->search( {}, { distinct => 'biblionumber', @@ -463,6 +475,7 @@ if ( $op eq 'view' ) { can_delete_shelf => $shelf->can_be_deleted($loggedinuser), can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser), can_add_biblios => $shelf->can_biblios_be_added($loggedinuser), + contents_count => $shelf_contents->count, itemsloop => \@items_info, sortfield => $sortfield, direction => $direction, @@ -528,8 +541,27 @@ if ( $op eq 'view' ) { } my $pager = $shelves->pager; + + # Pre-compute contents count per shelf, respecting suppression + my $apply_suppression = C4::Context->preference('OpacSuppression'); + my $ip_in_range = 0; + if ( $apply_suppression && ( my $ip_range = C4::Context->preference('OpacSuppressionByIPRange') ) ) { + my $ip_address = $ENV{'REMOTE_ADDR'}; + $ip_in_range = ( $ip_address =~ /^$ip_range/ ); + } + + my @shelves_list; + while ( my $s = $shelves->next ) { + my $shelf_contents = $s->get_contents; + $shelf_contents = $shelf_contents->filter_by_not_suppressed + if $apply_suppression && !$ip_in_range; + push @shelves_list, { shelf => $s, contents_count => $shelf_contents->count }; + } + $shelves->reset; + $template->param( shelves => $shelves, + shelves_list => \@shelves_list, pagination_bar => pagination_bar( q||, $pager->last_page - $pager->first_page + 1, $page, "page", diff --git a/t/db_dependent/Virtualshelves.t b/t/db_dependent/Virtualshelves.t index 7f77bcf926..dad8960e53 100755 --- a/t/db_dependent/Virtualshelves.t +++ b/t/db_dependent/Virtualshelves.t @@ -17,7 +17,7 @@ use Modern::Perl; use Test::NoWarnings; -use Test::More tests => 11; +use Test::More tests => 12; use Test::Exception; use DateTime::Duration; @@ -1659,6 +1659,44 @@ subtest 'filter_by_readable() tests' => sub { $schema->storage->txn_rollback; }; +subtest 'filter_by_not_suppressed() tests' => sub { + + plan tests => 3; + + $schema->storage->txn_begin; + + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + + my $biblio_visible = $builder->build_sample_biblio; + my $biblio_suppressed = $builder->build_sample_biblio; + $biblio_suppressed->opac_suppressed(1)->store; + + my $shelf = Koha::Virtualshelf->new( + { + shelfname => 'test_suppression_shelf', + owner => $patron->borrowernumber, + public => 1, + } + )->store; + + $shelf->add_biblio( $biblio_visible->biblionumber, $patron->borrowernumber ); + $shelf->add_biblio( $biblio_suppressed->biblionumber, $patron->borrowernumber ); + + my $contents = $shelf->get_contents; + is( $contents->count, 2, 'Shelf has 2 entries' ); + + my $filtered = $shelf->get_contents->filter_by_not_suppressed; + is( $filtered->count, 1, 'filter_by_not_suppressed returns only non-suppressed records' ); + + is( + $filtered->next->biblionumber, + $biblio_visible->biblionumber, + 'The returned content is the visible biblio' + ); + + $schema->storage->txn_rollback; +}; + sub teardown { $dbh->do(q|DELETE FROM virtualshelfshares|); $dbh->do(q|DELETE FROM virtualshelfcontents|);