Bug 22454: Add Koha::Item::hidden_in_opac method
authorTomas Cohen Arazi <tomascohen@theke.io>
Tue, 5 Mar 2019 12:44:59 +0000 (09:44 -0300)
committerNick Clemens <nick@bywatersolutions.com>
Wed, 13 Mar 2019 05:31:31 +0000 (05:31 +0000)
This patch adds a hidden_in_opac method that does the same calculation
done in GetHiddenItemnumbers, but for a single item, and doesn't get the
OpacHiddenItems syspref, but expects them to be passed as parameters (to
avoid multiple reads).

To test:
- Apply this patches
- Run:
  $ kshell
 k$ prove t/db_dependent/Koha/Item.t
=> SUCCESS: Tests pass!

Signed-off-by: Michal Denar <black23@gmail.com>

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

Koha/Item.pm

index f3799ef..66e0f9c 100644 (file)
@@ -20,6 +20,7 @@ package Koha::Item;
 use Modern::Perl;
 
 use Carp;
+use List::MoreUtils qw(any);
 
 use Koha::Database;
 use Koha::DateUtils qw( dt_from_string );
@@ -192,6 +193,40 @@ sub can_article_request {
     return q{};
 }
 
+=head3 hidden_in_opac
+
+my $bool = $item->hidden_in_opac({ [ rules => $rules ] })
+
+Returns true if item fields match the hidding criteria defined in $rules.
+Returns false otherwise.
+
+Takes HASHref that can have the following parameters:
+    OPTIONAL PARAMETERS:
+    $rules : { <field> => [ value_1, ... ], ... }
+
+Note: $rules inherits its structure from the parsed YAML from reading
+the I<OpacHiddenItems> system preference.
+
+=cut
+
+sub hidden_in_opac {
+    my ( $self, $params ) = @_;
+
+    my $rules = $params->{rules} // {};
+
+    my $hidden_in_opac = 0;
+
+    foreach my $field ( keys %{$rules} ) {
+
+        if ( any { $self->$field eq $_ } @{ $rules->{$field} } ) {
+            $hidden_in_opac = 1;
+            last;
+        }
+    }
+
+    return $hidden_in_opac;
+}
+
 =head3 can_be_transferred
 
 $item->can_be_transferred({ to => $to_library, from => $from_library })