From 2b0d399fb90b27a3def943cf71253ea0553e5940 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 20 Nov 2014 17:14:35 +0100 Subject: [PATCH] Bug 11998: Add Koha::Config::SystemPreference This (dirty) patch extract the syspref set and delete routine into a module. It fixes the circular dependency and the logging problem. Signed-off-by: Brendan Gallagher --- C4/Context.pm | 44 ++++++++-------------------------- Koha/Config/SystemPreference.pm | 50 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 34 deletions(-) create mode 100644 Koha/Config/SystemPreference.pm diff --git a/C4/Context.pm b/C4/Context.pm index f021929..fc812d4 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -18,6 +18,9 @@ package C4::Context; use strict; use warnings; + +use Koha::Config::SystemPreference; + use vars qw($VERSION $AUTOLOAD $context @context_stack $servers $memcached $ismemcached); BEGIN { if ($ENV{'HTTP_USER_AGENT'}) { @@ -629,40 +632,15 @@ preference. sub set_preference { my ( $self, $var, $value, $expl, $type, $options ) = @_; - $var = lc($var); - my $dbh = C4::Context->dbh or return 0; + $var = lc $var; - my $db_type = $dbh->selectrow_array( - "SELECT type FROM systempreferences WHERE variable = ?", - {}, $var ); + my $set = Koha::Config::SystemPreference::set( $var, $value, $expl, $type, $options ); - $value = 0 if ( $db_type && $db_type eq 'YesNo' && $value eq '' ); + $syspref_cache->set_in_cache("syspref_$var", $value) + if $use_syspref_cache and $set; - my ( $query, @params, $logtype ); - if ( !defined($db_type) ) { - $query = ' -INSERT INTO systempreferences - ( variable, value, explanation, type, options ) -VALUES( ?, ?, ?, ?, ? )'; - @params = ( $var, $value, $expl, $type, $options ); - $logtype = 'ADD'; - } - else { - $query = ' -INSERT INTO systempreferences - ( variable, value ) -VALUES( ?, ? ) -ON DUPLICATE KEY UPDATE value = VALUES(value)'; - @params = ( $var, $value ); - $logtype = 'MODIFY'; - } - my $sth = $dbh->prepare($query); - if ( $sth->execute(@params) ) { - $syspref_cache->set_in_cache("syspref_$var", $value) if $use_syspref_cache; - logaction( $dbh, userenv(), 'SYSTEMPREFERENCE', $logtype, undef, $var . " | " . $value ); - } - $sth->finish; + return $set; } =head2 delete_preference @@ -681,15 +659,13 @@ sub delete_preference { # We want to log the previous value my $val = C4::Context->preference($var); my $dbh = C4::Context->dbh or die "Unable to talk to the database"; - my $sth = $dbh->prepare("DELETE FROM systempreferences WHERE variable=?"); - my $res = $sth->execute($var); - if ($res) { + + if ( Koha::Config::SystemPreference::delete($var) ) { $syspref_cache->clear_from_cache("syspref_$var") if $use_syspref_cache; logaction( $dbh, userenv(), 'SYSTEMPREFERENCE', 'DELETE', undef, $var . " | " . ( $val // 'undefined' ) ); return 1; } - warn "Unable to delete syspref: " . $sth->errstr; return 0; } # AUTOLOAD diff --git a/Koha/Config/SystemPreference.pm b/Koha/Config/SystemPreference.pm new file mode 100644 index 0000000..791fe44 --- /dev/null +++ b/Koha/Config/SystemPreference.pm @@ -0,0 +1,50 @@ +package Koha::Config::SystemPreference; + +use Koha::Database; +use C4::Log qw( logaction ); + +sub set { + my ( $variable, $value, $expl, $type, $options ) = @_; + + my $dbh = C4::Context->dbh or return 0; + + my $pref = Koha::Database->new->schema->resultset('Systempreference')->find($variable); + + if ( $pref ) { + $value = 0 if ( $pref->type && $pref->type eq 'YesNo' && $pref->type eq '' ); + } + + $pref = Koha::Database->new->schema->resultset('Systempreference')->update_or_new( + { + variable => $variable, + value => $value, + explanation => $expl, + type => $type, + options => $options, + } + ); + + if ( $pref->in_storage ) { + logaction('SYSTEMPREFERENCE', 'MODIFY', undef, $variable . ' | ' . $value); + return 1; + } + + my $inserted = $pref->insert; + logaction('SYSTEMPREFERENCE', 'ADD', undef, $variable . ' | ' . $value) if $inserted; + + return $inserted; +} + +sub delete { + my ( $variable ) = @_; + + my $deleted = Koha::Database->new->schema->resultset('Systempreference')->find($variable)->delete; + + return unless $deleted; + + logaction( 'SYSTEMPREFERENCE', 'DELETE', undef, $logstring ); + + return 1 +} + +1 -- 1.7.10.4