From 8b559c53aa7fcb4e273863614b2376de69621828 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 20 Feb 2026 11:12:57 +0100 Subject: [PATCH] Bug 41891: Move C4::Context->preference to Koha::Context::Preferences->get --- C4/Context.pm | 23 ++---------- Koha/Context/Preferences.pm | 70 +++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 21 deletions(-) create mode 100644 Koha/Context/Preferences.pm diff --git a/C4/Context.pm b/C4/Context.pm index 6c51b3b862c..03b4da86910 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -46,8 +46,8 @@ use ZOOM; use List::MoreUtils qw(any); use Koha::Caches; -use Koha::Config::SysPref; use Koha::Config::SysPrefs; +use Koha::Context::Preferences; use Koha::Config; use Koha; @@ -266,26 +266,7 @@ sub preference { my $self = shift; my $var = shift; # The system preference to return - return Encode::decode_utf8( $ENV{"OVERRIDE_SYSPREF_$var"} ) - if defined $ENV{"OVERRIDE_SYSPREF_$var"}; - - $var = lc $var; - - if ($use_syspref_cache) { - my $syspref_cache = Koha::Caches->get_instance('syspref'); - my $cached_var = $syspref_cache->get_from_cache("syspref_$var"); - return $cached_var if defined $cached_var; - } - - my $syspref; - eval { $syspref = Koha::Config::SysPrefs->find( lc $var ) }; - my $value = $syspref ? $syspref->value() : undef; - - if ($use_syspref_cache) { - my $syspref_cache = Koha::Caches->get_instance('syspref'); - $syspref_cache->set_in_cache( "syspref_$var", $value ); - } - return $value; + return Koha::Context::Preferences->get_value( $var, { use_cache => $use_syspref_cache } ); } =head2 yaml_preference diff --git a/Koha/Context/Preferences.pm b/Koha/Context/Preferences.pm new file mode 100644 index 00000000000..de669e66d71 --- /dev/null +++ b/Koha/Context/Preferences.pm @@ -0,0 +1,70 @@ +package Koha::Context::Preferences; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Encode; + +use Koha::Caches; +use Koha::Config::SysPrefs; + +=head1 NAME + +Koha::Context::Preferences - Get system preferences without any other unecessary module dependencies + +=head1 SYNOPSIS + + Koha::Context::Preferences->get_value($variable); + +=head1 METHODS + +=cut + +=head3 set + +Koha::Context::Preferences->get_value($variable) will return the effective value of the system preference + +=cut + +sub get_value { + my ( $class, $var, $params ) = @_; + $params ||= {}; + my $use_syspref_cache = $params->{use_syspref_cache} || 0; + + return Encode::decode_utf8( $ENV{"OVERRIDE_SYSPREF_$var"} ) + if defined $ENV{"OVERRIDE_SYSPREF_$var"}; + + $var = lc $var; + + if ($use_syspref_cache) { + my $syspref_cache = Koha::Caches->get_instance('syspref'); + my $cached_var = $syspref_cache->get_from_cache("syspref_$var"); + return $cached_var if defined $cached_var; + } + + my $syspref; + eval { $syspref = Koha::Config::SysPrefs->find( lc $var ) }; + my $value = $syspref ? $syspref->value() : undef; + + if ($use_syspref_cache) { + my $syspref_cache = Koha::Caches->get_instance('syspref'); + $syspref_cache->set_in_cache( "syspref_$var", $value ); + } + return $value; +} + +1; -- 2.43.0