From 2e1342d34acb640647b9055114555d9f056f9617 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 6 Apr 2021 11:28:35 -0300 Subject: [PATCH] Bug 23583: Handle OpacHiddenItems with yaml_preference This patch makes all places in Koha that rely on OpacHiddenItems actually use C4::Context->yaml_preference instead of manually calling the YAML libraries and handling it. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/Items* \ t/db_dependent/Koha/Item* \ t/db_dependent/Koha/Filter/EmbedItems.t => SUCCESS: Tests pass! 3. Try hiding things with opac-ISBDdetail.pl and opac-search.pl => SUCCESS: Things work the same! 4. Sign off :-D --- C4/Items.pm | 18 +++++------------- Koha/Filter/MARC/EmbedItems.pm | 8 ++------ opac/opac-ISBDdetail.pl | 8 +------- opac/opac-search.pl | 16 ++-------------- 4 files changed, 10 insertions(+), 40 deletions(-) diff --git a/C4/Items.pm b/C4/Items.pm index aecf96af1f0..726c7e4cd80 100644 --- a/C4/Items.pm +++ b/C4/Items.pm @@ -49,7 +49,6 @@ BEGIN { use Carp; use Try::Tiny; -use Encode; use C4::Context; use C4::Koha; use C4::Biblio; @@ -58,7 +57,6 @@ use MARC::Record; use C4::ClassSource; use C4::Log; use List::MoreUtils qw(any); -use YAML::XS; use DateTime::Format::MySQL; use Data::Dumper; # used as part of logging item record changes, not just for # debugging; so please don't remove this @@ -988,17 +986,11 @@ sub GetHiddenItemnumbers { } my @resultitems; - my $yaml = C4::Context->preference('OpacHiddenItems'); - return () if (! $yaml =~ /\S/ ); - $yaml = "$yaml\n\n"; # YAML is anal on ending \n. Surplus does not hurt - my $hidingrules; - eval { - $hidingrules = YAML::XS::Load(Encode::encode_utf8($yaml)); - }; - if ($@) { - warn "Unable to parse OpacHiddenItems syspref : $@"; - return (); - } + my $hidingrules = C4::Context->yaml_preference('OpacHiddenItems'); + + return + unless $hidingrules; + my $dbh = C4::Context->dbh; # For each item diff --git a/Koha/Filter/MARC/EmbedItems.pm b/Koha/Filter/MARC/EmbedItems.pm index 337a4dbbae7..ae8ec34a263 100644 --- a/Koha/Filter/MARC/EmbedItems.pm +++ b/Koha/Filter/MARC/EmbedItems.pm @@ -28,13 +28,9 @@ my $biblio = Koha::Biblios->find( { prefetch => [ items, metadata ] } ); -my $opachiddenitems_rules; -eval { - my $yaml = C4::Context->preference('OpacHiddenItems') . "\n\n"; - $opachiddenitems_rules = YAML::XS::Load(Encode::encode_utf8($yaml)); -}; +my $rules = C4::Context->yaml_preference('OpacHiddenItems'); -my @items = grep { !$_->hidden_in_opac({ rules => $opachiddenitems_rules }) @{$biblio->items}; +my @items = grep { !$_->hidden_in_opac({ rules => $rules }) @{$biblio->items}; my $record = $biblio->metadata->record; my $processor = Koha::RecordProcessor->new( diff --git a/opac/opac-ISBDdetail.pl b/opac/opac-ISBDdetail.pl index 6278177e10b..d1076e76ef8 100755 --- a/opac/opac-ISBDdetail.pl +++ b/opac/opac-ISBDdetail.pl @@ -40,8 +40,6 @@ the items attached to the biblio =cut use Modern::Perl; -use YAML::XS; -use Encode; use C4::Auth; use C4::Context; @@ -81,11 +79,7 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user( my $patron = Koha::Patrons->find($loggedinuser); -my $opachiddenitems_rules; -eval { - my $yaml = C4::Context->preference('OpacHiddenItems') . "\n\n"; - $opachiddenitems_rules = YAML::XS::Load(Encode::encode_utf8($yaml)); -}; +my $opachiddenitems_rules = C4::Context->yaml_preference('OpacHiddenItems'); unless ( $patron and $patron->category->override_hidden_items ) { # only skip this check if there's a logged in user diff --git a/opac/opac-search.pl b/opac/opac-search.pl index dd0d4884dc2..a93bc197236 100755 --- a/opac/opac-search.pl +++ b/opac/opac-search.pl @@ -30,7 +30,6 @@ use Modern::Perl; use C4::Context; use List::MoreUtils q/any/; use Try::Tiny; -use YAML::XS; use Encode; use Data::Dumper; # TODO remove @@ -242,17 +241,7 @@ my $cnt; my $advanced_search_types = C4::Context->preference("AdvancedSearchTypes") || "itemtypes"; my @advanced_search_types = split(/\|/, $advanced_search_types); -my $hidingrules = {}; -my $yaml = C4::Context->preference('OpacHiddenItems'); -if ( $yaml =~ /\S/ ) { - $yaml = "$yaml\n\n"; # YAML expects trailing newline. Surplus does not hurt. - eval { - $hidingrules = YAML::XS::Load(Encode::encode_utf8($yaml)); - }; - if ($@) { - warn "Unable to parse OpacHiddenItems syspref : $@"; - } -} +my $hidingrules = C4::Context->yaml_preference('OpacHiddenItems') // {}; my @sorted_itemtypes = sort { $itemtypes->{$a}->{translated_description} cmp $itemtypes->{$b}->{translated_description} } keys %$itemtypes; foreach my $advanced_srch_type (@advanced_search_types) { @@ -811,8 +800,7 @@ for (my $i=0;$i<@servers;$i++) { $template->param(searchdesc => 1); } $template->param(results_per_page => $results_per_page); - my $hide = C4::Context->preference('OpacHiddenItems'); - $hide = ($hide =~ m/\S/) if $hide; # Just in case it has some spaces/new lines + my $hide = ($hidingrules) ? 1 : 0; my $branch = ''; if (C4::Context->userenv){ $branch = C4::Context->userenv->{branch}; -- 2.31.1