From 66da038348e9c474fd04adcd6a34da884832ce5f Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Thu, 3 Dec 2020 15:11:14 -0300 Subject: [PATCH] Bug 24254: Add Koha::Items->filter_by_visible_in_opac This patch adds a method based on the original idea from Nick, but on Koha::Items. The idea is to build a proper filter, based on the current rules for hiding things, directly on the DBIC query. The caller takes care of knowing if the filtering should apply (i.e. checking the patron category exceptions) and then it would do something like: my @items; if ( ) { @items = $biblio->items->filter_by_visible_in_opac( { rules => $rules } ); } else { # still want to enforce 'hidelostitems' @items = $biblio->items->filter_by_visible_in_opac; } To test: 1. Apply this patches 2. Run: $ kshell k$ prove t/db_dependent/Koha/Items.t => SUCCESS: Tests pass! 3. Look at the use cases on the tests, read the code => SUCCESS: It all makes sense 4. Compare with Koha::Item->hidden_in_opac => SUCCESS: It all makes sense 5. Sign off :-D Signed-off-by: Kyle M Hall Signed-off-by: Martin Renvoize Signed-off-by: Kyle M Hall Signed-off-by: Martin Renvoize Signed-off-by: Andrew Fuerste-Henry (cherry picked from commit 11bdc847a38fa3631e39fbc19c05ea7e5e64afd3) Signed-off-by: Victor Grousset/tuxayo --- Koha/Items.pm | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/Koha/Items.pm b/Koha/Items.pm index 7b86c5a5e0..5d5e65718c 100644 --- a/Koha/Items.pm +++ b/Koha/Items.pm @@ -33,11 +33,39 @@ Koha::Items - Koha Item object set class =head1 API -=head2 Class Methods +=head2 Class methods =cut -=head3 type +=head3 filter_by_visible_in_opac + + my $filered_items = $items->filter_by_visible_in_opac({ rules => $rules }); + +Returns a new resultset, containing those items that are not expected to be hidden in OPAC. +If no I are passed, it returns the whole resultset, with the only caveat that the +I system preference is honoured. + +=cut + +sub filter_by_visible_in_opac { + my ($self, $params) = @_; + + my $rules = $params->{rules} // {}; + + my $search_params; + foreach my $field (keys %$rules){ + $search_params->{$field}->{'-not_in'} = $rules->{$field}; + } + + $search_params->{itemlost}->{'<='} = 0 + if C4::Context->preference('hidelostitems'); + + return $self->search( $search_params ); +} + +=head2 Internal methods + +=head3 _type =cut -- 2.39.5