From 814d4fd05bf6a23aba1493b3e2117869438ea5f6 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 Signed-off-by: Pedro Amorim Signed-off-by: Katrin Fischer --- C4/Circulation.pm | 70 ++++++++++++++++++----------------------------- C4/Context.pm | 2 +- 2 files changed, 28 insertions(+), 44 deletions(-) diff --git a/C4/Circulation.pm b/C4/Circulation.pm index 6df7ff8341..896dc1d0fe 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; @@ -1795,21 +1794,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; } } } @@ -2214,34 +2204,28 @@ 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 } ) - unless $rules->{ $item->itype }->{$notloan_rule_key} eq 'ONLYMESSAGE'; - 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 } ) + unless $rules->{ $item->itype }->{$notloan_rule_key} eq 'ONLYMESSAGE'; + 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 } ) - unless $rules->{ '_ALL_' }->{$notloan_rule_key} eq 'ONLYMESSAGE'; - 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 } ) + unless $rules->{'_ALL_'}->{$notloan_rule_key} eq 'ONLYMESSAGE'; + 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.39.5