From 847571e9186873aa3325185d5e3b74b50e57729f Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Thu, 6 Apr 2023 10:48:04 -0300 Subject: [PATCH] Bug 33431: Make C4::Circulation use C4::Context->yaml_preference This patch removes manual YAML handling for sysprefs in C4::Circulation. It also makes C4::Context->yaml_preference not warn when undef is retrieved from the sysprefs. To test: 1. Run: $ ktd --shell k$ prove t/db_dependent/Circulation* => SUCCESS: Tests pass! 2. Apply this patch 3. Repeat 1 => SUCCESS: Tests pass! 4. Sign off :-D Signed-off-by: David Nind --- C4/Circulation.pm | 60 +++++++++++++++++------------------------------ C4/Context.pm | 2 +- 2 files changed, 22 insertions(+), 40 deletions(-) diff --git a/C4/Circulation.pm b/C4/Circulation.pm index 562cad7267..ccc1c63e8d 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -21,7 +21,6 @@ package C4::Circulation; use Modern::Perl; use DateTime; use POSIX qw( floor ); -use YAML::XS; use Encode; use C4::Context; @@ -1798,21 +1797,12 @@ sub AddIssue { } } - my $yaml = C4::Context->preference('UpdateNotForLoanStatusOnCheckout'); - if ($yaml) { - $yaml = "$yaml\n\n"; - - my $rules; - eval { $rules = YAML::XS::Load(Encode::encode_utf8($yaml)); }; - if ($@) { - warn "Unable to parse UpdateNotForLoanStatusOnCheckout syspref : $@"; - } - else { - foreach my $key ( keys %$rules ) { - if ( $item_object->notforloan eq $key ) { - $item_object->notforloan($rules->{$key})->store({ log_action => 0, skip_record_index => 1 }); - last; - } + my $rules = C4::Context->yaml_preference('UpdateNotForLoanStatusOnCheckout'); + if ($rules) { + foreach my $key ( keys %$rules ) { + if ( $item_object->notforloan eq $key ) { + $item_object->notforloan($rules->{$key})->store({ log_action => 0, skip_record_index => 1 }); + last; } } } @@ -2215,30 +2205,22 @@ sub AddReturn { $messages->{ $loc_msg_key } = $loc_messages->{ $loc_msg_key }; } - my $yaml = C4::Context->preference('UpdateNotForLoanStatusOnCheckin'); - if ($yaml) { - $yaml = "$yaml\n\n"; # YAML is anal on ending \n. Surplus does not hurt - my $rules; - eval { $rules = YAML::XS::Load(Encode::encode_utf8($yaml)); }; - if ($@) { - warn "Unable to parse UpdateNotForLoanStatusOnCheckin syspref : $@"; - } - else { - if ( defined $rules->{$item->itype} ) { - foreach my $notloan_rule_key (keys %{ $rules->{$item->itype}} ) { - if ( $item->notforloan eq $notloan_rule_key ) { - $messages->{'NotForLoanStatusUpdated'} = { from => $item->notforloan, to => $rules->{$item->itype}->{$notloan_rule_key} }; - $item->notforloan($rules->{$item->itype}->{$notloan_rule_key})->store({ log_action => 0, skip_record_index => 1, skip_holds_queue => 1 }); - last; - } + my $rules = C4::Context->yaml_preference('UpdateNotForLoanStatusOnCheckin'); + if ($rules) { + if ( defined $rules->{$item->itype} ) { + foreach my $notloan_rule_key (keys %{ $rules->{$item->itype}} ) { + if ( $item->notforloan eq $notloan_rule_key ) { + $messages->{'NotForLoanStatusUpdated'} = { from => $item->notforloan, to => $rules->{$item->itype}->{$notloan_rule_key} }; + $item->notforloan($rules->{$item->itype}->{$notloan_rule_key})->store({ log_action => 0, skip_record_index => 1, skip_holds_queue => 1 }); + last; } - } elsif ( defined $rules->{'_ALL_'} ) { - foreach my $notloan_rule_key (keys %{ $rules->{'_ALL_'}} ) { - if ( $item->notforloan eq $notloan_rule_key ) { - $messages->{'NotForLoanStatusUpdated'} = { from => $item->notforloan, to => $rules->{'_ALL_'}->{$notloan_rule_key} }; - $item->notforloan($rules->{'_ALL_'}->{$notloan_rule_key})->store({ log_action => 0, skip_record_index => 1, skip_holds_queue => 1 }); - last; - } + } + } elsif ( defined $rules->{'_ALL_'} ) { + foreach my $notloan_rule_key (keys %{ $rules->{'_ALL_'}} ) { + if ( $item->notforloan eq $notloan_rule_key ) { + $messages->{'NotForLoanStatusUpdated'} = { from => $item->notforloan, to => $rules->{'_ALL_'}->{$notloan_rule_key} }; + $item->notforloan($rules->{'_ALL_'}->{$notloan_rule_key})->store({ log_action => 0, skip_record_index => 1, skip_holds_queue => 1 }); + last; } } } diff --git a/C4/Context.pm b/C4/Context.pm index 091e4783a4..0169b10f85 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -342,7 +342,7 @@ the value cannot be properly decoded as YAML. sub yaml_preference { my ( $self, $preference ) = @_; - my $yaml = eval { YAML::XS::Load( Encode::encode_utf8( $self->preference( $preference ) ) ); }; + my $yaml = eval { YAML::XS::Load( Encode::encode_utf8( $self->preference( $preference ) // '' ) ); }; if ($@) { warn "Unable to parse $preference syspref : $@"; return; -- 2.30.2