From c7a70909c6c1ace24c60122309d921b9f15a54ab Mon Sep 17 00:00:00 2001 From: David Cook Date: Wed, 22 Jun 2022 07:07:48 +0000 Subject: [PATCH] Bug 17904: Make OPAC "Most popular" respect hidden record sysprefs At the moment, OpacSuppression and OpacHiddenItems don't affect the output of the OPAC's "Most popular" page. This patch adds support for the OpacSuppression and OpacHiddenItems system preferences, so that items are excluded from the "Most popular" calculation based on the hidden rules. Test plan: 0a. Apply patches 0b. koha-plack --restart kohadev 0c. Log into Koha as "koha" user 1. Go to http://localhost:8081/cgi-bin/koha/admin/preferences.pl?op=search&searchfield=OpacTopissue 2. Enable "OpacTopissue" 3. Go to http://localhost:8081/cgi-bin/koha/circ/circulation.pl?borrowernumber=51 4. Checkout 39999000001310 5. Checkout 39999000005578 6. Checkout 39999000004571 7. Checkout 39999000005592 8. Go to http://localhost:8080/cgi-bin/koha/opac-topissues.pl?limit=10&branch=&itemtype=&timeLimit=999&do_it=1 9. See 3 titles 10. Go to http://localhost:8081/cgi-bin/koha/admin/preferences.pl?op=search&searchfield=OpacHiddenItems 11. Change "OpacHiddenItems" to the following (within the single quotes): barcode: [39999000001310,39999000005578] location: ['RANDOM','THAT','THIS'] 12. Go to http://localhost:8080/cgi-bin/koha/opac-topissues.pl?limit=10&branch=&itemtype=&timeLimit=999&do_it=1 13. See 2 titles ("Gairm" is hidden, as is one of the items for "Continuous delivery /") 14. Go to http://localhost:8081/cgi-bin/koha/admin/preferences.pl?op=search&searchfield=OpacHiddenItemsExceptions 15. Update the syspref to 'S' 16. Go to http://localhost:8080/cgi-bin/koha/opac-topissues.pl?limit=10&branch=&itemtype=&timeLimit=999&do_it=1 17. See 3 titles again 18. Go to http://localhost:8081/cgi-bin/koha/admin/preferences.pl?op=search&searchfield=OpacSuppression 19. Change syspref to "Hide" 20. Go to http://localhost:8081/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=114 21. Change 942$n to "Yes" and save record 22. Go to http://localhost:8080/cgi-bin/koha/opac-topissues.pl?limit=10&branch=&itemtype=&timeLimit=999&do_it=1 23. See 2 titles ("Clean code : a handbook of agile software craftsmanship / Robert C. Martin ... [et al.]" is suppressed https://bugs.koha-community.org/show_bug.cgi?id=19704 --- C4/Circulation.pm | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/C4/Circulation.pm b/C4/Circulation.pm index f5df973d1d..f7ed4d0159 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -4321,6 +4321,7 @@ sub GetTopIssues { FROM biblio b LEFT JOIN items i ON (i.biblionumber = b.biblionumber) LEFT JOIN biblioitems bi ON (bi.biblionumber = b.biblionumber) + LEFT JOIN biblio_metadata bm ON (bm.biblionumber = b.biblionumber) }; my (@where_strs, @where_args); @@ -4347,6 +4348,58 @@ sub GetTopIssues { push @where_args, $newness; } + my $opachiddenitems_rules = C4::Context->yaml_preference('OpacHiddenItems'); + if ( $opachiddenitems_rules ){ + #NOTE: OpacHiddenItemsHidesRecord isn't relevant here since the bib won't appear if there is no items to appear anyway + my $userenv = C4::Context->userenv; + my $logged_in_user = Koha::Patrons->find( $userenv->{number} ) if $userenv; + my $logged_in_categorycode = $logged_in_user->categorycode if $logged_in_user; + my $hide_items = 1; + if (my $exceptions = C4::Context->preference('OpacHiddenItemsExceptions') && $logged_in_categorycode){ + foreach my $except (split(/\|/, $exceptions)){ + if ( $except eq $logged_in_categorycode ){ + $hide_items = 0; + last; + } + } + } + if ($hide_items){ + my %where = (); + my @keys = keys %$opachiddenitems_rules; + foreach my $key (@keys){ + #NOTE: Add "i." table alias to field names + #NOTE: Generate SQL::Abstract data structure + $where{ "i." . $key } = { '!=' => [ -and => @{$opachiddenitems_rules->{$key}} ] }; + } + if (%where){ + require SQL::Abstract; + my $sql = SQL::Abstract->new; + my ($stmt, @bind) = $sql->where(\%where); + if ($stmt){ + $stmt =~ s/^ WHERE //; + push(@where_strs,$stmt); + push(@where_args,@bind); + } + } + } + } + + if ( C4::Context->preference('OpacSuppression') ){ + my $hide_items = 1; + if (C4::Context->preference('OpacSuppressionByIPRange')) { + my $IPAddress = $ENV{'REMOTE_ADDR'}; + my $IPRange = C4::Context->preference('OpacSuppressionByIPRange'); + if ($IPAddress =~ /^$IPRange/){ + $hide_items = 0; + } + } + if ($hide_items){ + my $stmt = q#( ExtractValue(bm.metadata,'//datafield[@tag="942"]/subfield[@code="n"]') <> ? )#; + push(@where_strs, $stmt); + push(@where_args, 1); + } + } + if (@where_strs) { $query .= 'WHERE ' . join(' AND ', @where_strs); } -- 2.30.2