From 2839c03c75af8eaebf1479cc7c2f8f30aae1fffa Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 20 Nov 2020 12:11:07 +0100 Subject: [PATCH] Bug 15448: Clean existing code in opac-detail This patch groups the code related to OpacHiddenItems and hidelostitems. It certainly fixed few issues (we are now filtering after EasyAnalyticalRecords), maybe not... At least now the code is at the same place. --- opac/opac-detail.pl | 110 ++++++++++++++++++++++++++------------------ 1 file changed, 64 insertions(+), 46 deletions(-) diff --git a/opac/opac-detail.pl b/opac/opac-detail.pl index 8b935af155..38fa2d1d55 100755 --- a/opac/opac-detail.pl +++ b/opac/opac-detail.pl @@ -82,18 +82,6 @@ my ( $template, $borrowernumber, $cookie ) = get_template_and_user( } ); -my @all_items = GetItemsInfo($biblionumber); -if( $specific_item ) { - @all_items = grep { $_->{itemnumber} == $query->param('itemnumber') } @all_items; - $template->param( specific_item => 1 ); -} -my @hiddenitems; -my $patron = Koha::Patrons->find( $borrowernumber ); -our $borcat= q{}; -if ( C4::Context->preference('OpacHiddenItemsExceptions') ) { - $borcat = $patron ? $patron->categorycode : q{}; -} - my $record = GetMarcBiblio({ biblionumber => $biblionumber, opac => 1 }); @@ -102,16 +90,7 @@ if ( ! $record ) { exit; } -if ( scalar @all_items >= 1 ) { - push @hiddenitems, - GetHiddenItemnumbers( { items => \@all_items, borcat => $borcat } ); - - if (scalar @hiddenitems == scalar @all_items ) { - print $query->redirect("/cgi-bin/koha/errors/404.pl"); # escape early - exit; - } -} - +our $logged_in_patron = Koha::Patrons->find( $borrowernumber ); my $biblio = Koha::Biblios->find( $biblionumber ); my $framework = $biblio ? $biblio->frameworkcode : q{}; my $record_processor = Koha::RecordProcessor->new({ @@ -280,7 +259,7 @@ if ($session->param('busc')) { my @newresults; my $search_context = { 'interface' => 'opac', - 'category' => $borcat + 'category' => $logged_in_patron ? $logged_in_patron->categorycode : undef, }; for (my $i=0;$i<@servers;$i++) { my $server = $servers[$i]; @@ -507,6 +486,7 @@ if ($session->param('busc')) { for (my $j = 0; $j < @arrBiblios; $j++) { next unless ($arrBiblios[$j]); $dataBiblioPaging = Koha::Biblios->find( $arrBiblios[$j] ) if ($arrBiblios[$j] != $biblionumber); + next unless $dataBiblioPaging; push @listResults, {index => $j + 1 + $offset, biblionumber => $arrBiblios[$j], title => ($arrBiblios[$j] == $biblionumber)?'':$dataBiblioPaging->title, author => ($arrBiblios[$j] != $biblionumber && $dataBiblioPaging->author)?$dataBiblioPaging->author:'', url => ($arrBiblios[$j] == $biblionumber)?'':'opac-detail.pl?biblionumber=' . $arrBiblios[$j]}; } $template->param('listResults' => \@listResults) if (@listResults); @@ -519,6 +499,12 @@ $template->param( OPACShowCheckoutName => C4::Context->preference("OPACShowCheckoutName"), ); +my @all_items = GetItemsInfo($biblionumber); +if( $specific_item ) { + @all_items = grep { $_->{itemnumber} == $query->param('itemnumber') } @all_items; + $template->param( specific_item => 1 ); +} + if ( C4::Context->preference('EasyAnalyticalRecords') ) { # adding items linked via host biblios my $analyticfield = '773'; @@ -539,24 +525,56 @@ if ( C4::Context->preference('EasyAnalyticalRecords') ) { } } -my @items; - -# Are there items to hide? -my $hideitems; -$hideitems = 1 if C4::Context->preference('hidelostitems') or scalar(@hiddenitems) > 0; +my @filtered_items; +if ( @all_items ) { + @filtered_items = filter_items( { items => \@all_items, patron => $logged_in_patron, } ); -# Hide items -if ($hideitems) { - for my $itm (@all_items) { - if ( C4::Context->preference('hidelostitems') ) { - push @items, $itm unless $itm->{itemlost} or any { $itm->{'itemnumber'} eq $_ } @hiddenitems; - } else { - push @items, $itm unless any { $itm->{'itemnumber'} eq $_ } @hiddenitems; + unless ( @filtered_items ) { + print $query->redirect("/cgi-bin/koha/errors/404.pl"); # escape early + exit; } } -} else { + +sub filter_items { + my ( $params ) = @_; + my @all_items = @{$params->{items}}; + my $patron = $params->{patron}; + my @hiddenitems; + my $patron_category = + ( C4::Context->preference('OpacHiddenItemsExceptions') && $patron ) + ? $patron->categorycode + : undef; + + if ( scalar @all_items >= 1 ) { + push @hiddenitems, + GetHiddenItemnumbers( { items => \@all_items, borcat => $patron_category } ); + + return () if scalar @hiddenitems == scalar @all_items; + } + + my @items; + # Are there items to hide? + my $hideitems; + + # Hide items + if ( C4::Context->preference('hidelostitems') || @hiddenitems ) { + for my $itm (@all_items) { + if ( C4::Context->preference('hidelostitems') ) { + push @items, $itm + unless $itm->{itemlost} + or any { $itm->{'itemnumber'} eq $_ } @hiddenitems; + } + else { + push @items, $itm + unless any { $itm->{'itemnumber'} eq $_ } @hiddenitems; + } + } + } # Or not - @items = @all_items; + else { + @items = @all_items; + } + return @items; } my $branch = ''; @@ -580,7 +598,7 @@ if ( C4::Context->preference('HighlightOwnItemsOnOPAC') ) { my @our_items; my @other_items; - foreach my $item ( @items ) { + foreach my $item ( @filtered_items ) { if ( $item->{branchcode} eq $branchcode ) { $item->{'this_branch'} = 1; push( @our_items, $item ); @@ -589,7 +607,7 @@ if ( C4::Context->preference('HighlightOwnItemsOnOPAC') ) { } } - @items = ( @our_items, @other_items ); + @filtered_items= ( @our_items, @other_items ); } } @@ -650,7 +668,7 @@ foreach my $subscription (@subscriptions) { push @subs, \%cell; } -$dat->{'count'} = scalar(@items); +$dat->{'count'} = scalar(@filtered_items); my (%item_reserves, %priority); @@ -709,13 +727,13 @@ if ( C4::Context->preference('OPACAcquisitionDetails' ) ) { my $allow_onshelf_holds; my ( $itemloop_has_images, $otheritemloop_has_images ); -if ( not $viewallitems and @items > $max_items_to_display ) { +if ( not $viewallitems and @filtered_items > $max_items_to_display ) { $template->param( too_many_items => 1, - items_count => scalar( @items ), + items_count => scalar( @filtered_items ), ); } else { - for my $itm (@items) { + for my $itm (@filtered_items) { my $item = Koha::Items->find( $itm->{itemnumber} ); $itm->{holds_count} = $item_reserves{ $itm->{itemnumber} }; $itm->{priority} = $priority{ $itm->{itemnumber} }; @@ -727,7 +745,7 @@ if ( not $viewallitems and @items > $max_items_to_display ) { && !$itemtypes->{$itm->{'itype'}}->{notforloan} && $itm->{'itemnumber'}; - $allow_onshelf_holds = Koha::CirculationRules->get_onshelfholds_policy( { item => $item, patron => $patron } ) + $allow_onshelf_holds = Koha::CirculationRules->get_onshelfholds_policy( { item => $item, patron => $logged_in_patron } ) unless $allow_onshelf_holds; # get collection code description, too @@ -843,7 +861,7 @@ if( C4::Context->preference('ArticleRequests') ) { OpacStarRatings => C4::Context->preference("OpacStarRatings"), ); -if (C4::Context->preference("AlternateHoldingsField") && scalar @items == 0) { +if (C4::Context->preference("AlternateHoldingsField") && scalar @filtered_items == 0) { my $fieldspec = C4::Context->preference("AlternateHoldingsField"); my $subfields = substr $fieldspec, 3; my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' '; @@ -1257,7 +1275,7 @@ if (C4::Context->preference('OpacHighlightedWords')) { $template->{VARS}->{'trackclicks'} = C4::Context->preference('TrackClicks'); if ( C4::Context->preference('UseCourseReserves') ) { - foreach my $i ( @items ) { + foreach my $i ( @filtered_items ) { $i->{'course_reserves'} = GetItemCourseReservesInfo( itemnumber => $i->{'itemnumber'} ); } } -- 2.20.1