From 0b52a79472d22ab7be1491f431d9dfaf42170b42 Mon Sep 17 00:00:00 2001 From: Mark Tompsett Date: Fri, 18 Aug 2017 18:09:00 +0000 Subject: [PATCH] [SIGNED-OFF] Bug 14385: Add borrower category overrides These files generally add borcat parameter to GetMarcBiblio. Others might include correction of filtering of items (opac-basket), or a comment as to why no changes were done (opac-search). In the case of opac-search, correcting the first FIXME will likely correct the OpacHiddenItems issues on tags. As such, that is beyond this bugs scope. Some code had loop optimizations and fixes made, like a 'next unless $record' when the biblio shouldn't even be in the list. Signed-off-by: Aleisha Amohia --- C4/Biblio.pm | 26 +++++++++++++++++++++----- opac/opac-basket.pl | 30 +++++++++++++++++++++++++++++- opac/opac-detail.pl | 4 +++- opac/opac-downloadcart.pl | 11 ++++++++++- opac/opac-downloadshelf.pl | 11 ++++++++++- opac/opac-export.pl | 16 +++++++++++++++- opac/opac-search.pl | 2 ++ opac/opac-sendbasket.pl | 6 +++++- opac/opac-sendshelf.pl | 14 +++++++++----- opac/opac-user.pl | 8 +++++++- 10 files changed, 111 insertions(+), 17 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index be8b791..9707ae3 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -1139,7 +1139,8 @@ sub GetMarcSubfieldStructureFromKohaField { my $record = GetMarcBiblio({ biblionumber => $biblionumber, embed_items => $embeditems, - opac => $opac }); + opac => $opac, + borcat => $patron_category }); Returns MARC::Record representing a biblio record, or C if the biblionumber doesn't exist. @@ -1163,6 +1164,12 @@ set to true to include item information. set to true to make the result suited for OPAC view. This causes things like OpacHiddenItems to be applied. +=item C<$borcat> + +If the OpacHiddenItemsExceptions system preference is set, this patron category +can be used to make visible OPAC items which would be normally hidden. +It only makes sense in combination both embed_items and opac values true. + =back =cut @@ -1178,6 +1185,7 @@ sub GetMarcBiblio { my $biblionumber = $params->{biblionumber}; my $embeditems = $params->{embed_items} || 0; my $opac = $params->{opac} || 0; + my $borcat = $params->{borcat} // q{}; if (not defined $biblionumber) { carp 'GetMarcBiblio called with undefined biblionumber'; @@ -1208,7 +1216,8 @@ sub GetMarcBiblio { C4::Biblio::EmbedItemsInMarcBiblio({ marc_record => $record, biblionumber => $biblionumber, - opac => $opac }) + opac => $opac, + borcat => $borcat }) if ($embeditems); return $record; @@ -2891,7 +2900,8 @@ sub ModZebra { marc_record => $marc, biblionumber => $biblionumber, item_numbers => $itemnumbers, - opac => $opac }); + opac => $opac, + borcat => $patron_category }); Given a MARC::Record object containing a bib record, modify it to include the items attached to it as 9XX @@ -2900,11 +2910,14 @@ if $itemnumbers is defined, only specified itemnumbers are embedded. If $opac is true, then opac-relevant suppressions are included. +If opac filtering will be done, borcat should be passed to properly +override if necessary. + =cut sub EmbedItemsInMarcBiblio { my ($params) = @_; - my ($marc, $biblionumber, $itemnumbers, $opac); + my ($marc, $biblionumber, $itemnumbers, $opac, $borcat); $marc = $params->{marc_record}; if ( !$marc ) { carp 'EmbedItemsInMarcBiblio: No MARC record passed'; @@ -2913,6 +2926,7 @@ sub EmbedItemsInMarcBiblio { $biblionumber = $params->{biblionumber}; $itemnumbers = $params->{item_numbers}; $opac = $params->{opac}; + $borcat = $params->{borcat} // q{}; $itemnumbers = [] unless defined $itemnumbers; @@ -2942,7 +2956,9 @@ sub EmbedItemsInMarcBiblio { my @items2pass = map { $_->{item} } @items; my @hiddenitems = $opachiddenitems - ? C4::Items::GetHiddenItemnumbers({ items => \@items2pass }) + ? C4::Items::GetHiddenItemnumbers({ + items => \@items2pass, + borcat => $borcat }) : (); # Convert to a hash for quick searching my %hiddenitems = map { $_ => 1 } @hiddenitems; diff --git a/opac/opac-basket.pl b/opac/opac-basket.pl index f842587..6c8c88d 100755 --- a/opac/opac-basket.pl +++ b/opac/opac-basket.pl @@ -18,6 +18,8 @@ use Modern::Perl; use CGI qw ( -utf8 ); +use List::Util qw ( none ); + use C4::Koha; use C4::Biblio; use C4::Items; @@ -58,6 +60,13 @@ if (C4::Context->preference('TagsEnabled')) { } } +my $borcat = q{}; +if ( C4::Context->preference('OpacHiddenItemsExceptions') ) { + # we need to fetch the borrower info here, so we can pass the category + my $borrower = Koha::Patron->find( { borrowernumber -> $borrowernumber } ); + $borcat = $borrower ? $borrower->categorycode : $borcat; +} + my $record_processor = Koha::RecordProcessor->new({ filters => 'ViewPolicy' }); foreach my $biblionumber ( @bibs ) { $template->param( biblionumber => $biblionumber ); @@ -65,6 +74,8 @@ foreach my $biblionumber ( @bibs ) { my $dat = &GetBiblioData($biblionumber); next unless $dat; + # No filtering on the item records needed for the record itself + # since the only reason item information is grabbed is because of branchcodes. my $record = &GetMarcBiblio({ biblionumber => $biblionumber }); my $framework = &GetFrameworkCode( $biblionumber ); $record_processor->options({ @@ -78,7 +89,24 @@ foreach my $biblionumber ( @bibs ) { my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour ); my $marcseriesarray = GetMarcSeries ($record,$marcflavour); my $marcurlsarray = GetMarcUrls ($record,$marcflavour); - my @items = &GetItemsInfo( $biblionumber ); + + # grab all the items... + my @all_items = &GetItemsInfo( $biblionumber ); + + # determine which ones should be hidden / visible + my @hidden_items = GetHiddenItemnumbers({ items => \@all_items, borcat => $borcat }); + + # If every item is hidden, then the biblio should be hidden too. + next if (scalar @all_items >=1 && scalar @hidden_items == scalar @all_items); + + # copy the visible ones into the items array. + my @items; + foreach my $item (@all_items) { + if ( none { $item->{itemnumber} ne $_ } @hidden_items ) { + push @items, $item; + } + } + my $subtitle = GetRecordValue('subtitle', $record, GetFrameworkCode($biblionumber)); my $hasauthors = 0; diff --git a/opac/opac-detail.pl b/opac/opac-detail.pl index 5bac860..4374c95 100755 --- a/opac/opac-detail.pl +++ b/opac/opac-detail.pl @@ -99,7 +99,9 @@ if ( scalar @all_items >= 1 ) { } } -my $record = GetMarcBiblio({ biblionumber => $biblionumber }); +my $record = GetMarcBiblio({ + biblionumber => $biblionumber, + opac => 1 }); if ( ! $record ) { print $query->redirect("/cgi-bin/koha/errors/404.pl"); # escape early exit; diff --git a/opac/opac-downloadcart.pl b/opac/opac-downloadcart.pl index 76fa095..f0ec288 100755 --- a/opac/opac-downloadcart.pl +++ b/opac/opac-downloadcart.pl @@ -49,6 +49,13 @@ my $dbh = C4::Context->dbh; if ($bib_list && $format) { + my $borcat = q{}; + if ( C4::Context->preference('OpacHiddenItemsExceptions') ) { + # we need to fetch the borrower info here, so we can pass the category + my $borrower = Koha::Patrons->find( { borrowernumber => $borrowernumber } ); + $borcat = $borrower ? $borrower->categorycode : $borcat; + } + my @bibs = split( /\//, $bib_list ); my $marcflavour = C4::Context->preference('marcflavour'); @@ -70,7 +77,9 @@ if ($bib_list && $format) { my $record = GetMarcBiblio({ biblionumber => $biblio, - embed_items => 1 }); + embed_items => 1, + opac => 1, + borcat => $borcat }); my $framework = &GetFrameworkCode( $biblio ); $record_processor->options({ interface => 'opac', diff --git a/opac/opac-downloadshelf.pl b/opac/opac-downloadshelf.pl index 007dd5f..ac6e910 100755 --- a/opac/opac-downloadshelf.pl +++ b/opac/opac-downloadshelf.pl @@ -50,6 +50,13 @@ my ( $template, $borrowernumber, $cookie ) = get_template_and_user ( } ); +my $borcat = q{}; +if ( C4::Context->preference('OpacHiddenItemsExceptions') ) { + # we need to fetch the borrower info here, so we can pass the category + my $borrower = Koha::Patrons->find( { borrowernumber => $borrowernumber } ); + $borcat = $borrower ? $borrower->categorycode : $borcat; +} + my $shelfnumber = $query->param('shelfnumber'); my $format = $query->param('format'); my $context = $query->param('context'); @@ -83,7 +90,9 @@ if ( $shelf and $shelf->can_be_viewed( $borrowernumber ) ) { my $record = GetMarcBiblio({ biblionumber => $biblionumber, - embed_items => 1 }); + embed_items => 1, + opac => 1, + borcat => $borcat }); my $framework = &GetFrameworkCode( $biblionumber ); $record_processor->options({ interface => 'opac', diff --git a/opac/opac-export.pl b/opac/opac-export.pl index 6023d78..c31c07e 100755 --- a/opac/opac-export.pl +++ b/opac/opac-export.pl @@ -35,11 +35,25 @@ my $biblionumber = $query->param("bib")||0; $biblionumber = int($biblionumber); my $error = q{}; +# Determine logged in user's patron category. +# Blank if not logged in. +my $userenv = C4::Context->userenv; +my $borcat = q{}; +if ($userenv) { + my $borrowernumber = $userenv->{'number'}; + if ($borrowernumber) { + $borrower = Koha::Patrons->find( { borrowernumber => $borrowernumber } ); + $borcat = $borrower ? $borrower->categorycode : $borcat; + } +} + my $include_items = ($format =~ /bibtex/) ? 0 : 1; my $marc = $biblionumber ? GetMarcBiblio({ biblionumber => $biblionumber, - embed_items => $include_items }) + embed_items => $include_items, + opac => 1, + borcat => $borcat }) : undef; if(!$marc) { diff --git a/opac/opac-search.pl b/opac/opac-search.pl index 3cd7adc..45d1eef 100755 --- a/opac/opac-search.pl +++ b/opac/opac-search.pl @@ -602,6 +602,8 @@ if ($tag) { $DEBUG and printf STDERR "taglist (%s biblionumber)\nmarclist (%s records)\n", scalar(@$taglist), scalar(@marclist); $results_hashref->{biblioserver}->{RECORDS} = \@marclist; # FIXME: tag search and standard search should work together, not exclusively + # FIXME: Because search and standard search don't work together OpacHiddenItems + # displays search results which should be hidden. # FIXME: No facets for tags search. } elsif ($build_grouped_results) { eval { diff --git a/opac/opac-sendbasket.pl b/opac/opac-sendbasket.pl index c51e8c4..7238685 100755 --- a/opac/opac-sendbasket.pl +++ b/opac/opac-sendbasket.pl @@ -59,6 +59,7 @@ if ( $email_add ) { }); my $email = Koha::Email->new(); my $patron = Koha::Patrons->find( $borrowernumber ); + my $borcat = $patron ? $patron->categorycode : q{}; my $user_email = GetFirstValidEmailAddress($borrowernumber) || C4::Context->preference('KohaAdminEmailAddress'); @@ -89,7 +90,10 @@ if ( $email_add ) { next unless $dat; my $record = GetMarcBiblio({ biblionumber => $biblionumber, - embed_items => 1 }); + embed_items => 1, + opac => 1, + borcat => $borcat }); + my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour ); my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour ); diff --git a/opac/opac-sendshelf.pl b/opac/opac-sendshelf.pl index 3c19e61..2cc02ff 100755 --- a/opac/opac-sendshelf.pl +++ b/opac/opac-sendshelf.pl @@ -80,6 +80,9 @@ if ( $email ) { } ); + my $patron = Koha::Patrons->find( $borrowernumber ); + my $borcat = $patron ? $patron->categorycode : q{}; + my $shelf = Koha::Virtualshelves->find( $shelfid ); my $contents = $shelf->get_contents; my $marcflavour = C4::Context->preference('marcflavour'); @@ -88,11 +91,14 @@ if ( $email ) { while ( my $content = $contents->next ) { my $biblionumber = $content->biblionumber; - my $fw = GetFrameworkCode($biblionumber); - my $dat = GetBiblioData($biblionumber); my $record = GetMarcBiblio({ biblionumber => $biblionumber, - embed_items => 1 }); + embed_items => 1, + opac => 1, + borcat => $borcat }); + next unless $record; + my $fw = GetFrameworkCode($biblionumber); + my $dat = GetBiblioData($biblionumber); my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour ); my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour ); my $subtitle = GetRecordValue('subtitle', $record, $fw); @@ -112,8 +118,6 @@ if ( $email ) { push( @results, $dat ); } - my $patron = Koha::Patrons->find( $borrowernumber ); - $template2->param( BIBLIO_RESULTS => \@results, comment => $comment, diff --git a/opac/opac-user.pl b/opac/opac-user.pl index bc39c80..09ca395 100755 --- a/opac/opac-user.pl +++ b/opac/opac-user.pl @@ -89,6 +89,8 @@ if (!$borrowernumber) { # get borrower information .... my $borr = Koha::Patrons->find( $borrowernumber )->unblessed; +# unblessed is a hash vs. object/undef. Hence the used of curly braces here. +my $borcat = $borr ? $borr->{categorycode} : q{}; my ( $today_year, $today_month, $today_day) = Today(); my ($warning_year, $warning_month, $warning_day) = split /-/, $borr->{'dateexpiry'}; @@ -204,7 +206,11 @@ if ($issues){ } $issue->{'charges'} = $charges; $issue->{'rentalfines'} = $rentalfines; - my $marcrecord = GetMarcBiblio({ biblionumber => $issue->{'biblionumber'} }); + my $marcrecord = GetMarcBiblio({ + biblionumber => $issue->{'biblionumber'}, + embed_items => 1, + opac => 1, + borcat => $borcat }); $issue->{'subtitle'} = GetRecordValue('subtitle', $marcrecord, GetFrameworkCode($issue->{'biblionumber'})); # check if item is renewable my ($status,$renewerror) = CanBookBeRenewed( $borrowernumber, $issue->{'itemnumber'} ); -- 2.1.4