From 7052a42dcc43ec2d9c3ba727b43d69ae23729e51 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Wed, 8 Apr 2015 06:38:34 -0400 Subject: [PATCH] Bug 13967 - System preferences need a package System preferences should have a package based on Koha::Object to remove the need for direct manipulation via SQL. Test Plan: 1) Apply this patch 2) prove t/db_dependent/sysprefs.t --- C4/Context.pm | 36 +++++++++++---------------- Koha/Config/SysPref.pm | 52 ++++++++++++++++++++++++++++++++++++++++ Koha/Config/SysPrefs.pm | 58 +++++++++++++++++++++++++++++++++++++++++++++ Koha/Objects.pm | 2 + t/db_dependent/sysprefs.t | 22 +++++++++------- 5 files changed, 139 insertions(+), 31 deletions(-) create mode 100644 Koha/Config/SysPref.pm create mode 100644 Koha/Config/SysPrefs.pm diff --git a/C4/Context.pm b/C4/Context.pm index cc93af4..2459f4b 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -101,13 +101,15 @@ use DBIx::Connector; use Encode; use ZOOM; use XML::Simple; -use C4::Boolean; -use C4::Debug; use POSIX (); use DateTime::TimeZone; use Module::Load::Conditional qw(can_load); use Carp; +use C4::Boolean; +use C4::Debug; +use Koha::Config::SysPrefs; + =head1 NAME C4::Context - Maintain and manipulate the context of a Koha script @@ -554,14 +556,8 @@ sub preference { if ( defined $ENV{"OVERRIDE_SYSPREF_$var"} ) { $value = $ENV{"OVERRIDE_SYSPREF_$var"}; } else { - # Look up systempreferences.variable==$var - my $sql = q{ - SELECT value - FROM systempreferences - WHERE variable = ? - LIMIT 1 - }; - $value = $dbh->selectrow_array( $sql, {}, lc $var ); + my $syspref = Koha::Config::SysPrefs->find( lc $var ); + $value = $syspref ? $syspref->value() : undef; } $sysprefs{lc $var} = $value; @@ -632,23 +628,21 @@ sub set_preference { my $var = lc(shift); my $value = shift; - my $dbh = C4::Context->dbh or return 0; - - my $type = $dbh->selectrow_array( "SELECT type FROM systempreferences WHERE variable = ?", {}, $var ); + my $syspref = Koha::Config::SysPrefs->find( $var ); + my $type = $syspref ? $syspref->type() : undef; $value = 0 if ( $type && $type eq 'YesNo' && $value eq '' ); - my $sth = $dbh->prepare( " - INSERT INTO systempreferences - ( variable, value ) - VALUES( ?, ? ) - ON DUPLICATE KEY UPDATE value = VALUES(value) - " ); + if ($syspref) { + $syspref = $syspref->set( { value => $value } )->store(); + } + else { + $syspref = Koha::Config::SysPref->new( { variable => $var, value => $value } )->store(); + } - if($sth->execute( $var, $value )) { + if ($syspref) { $sysprefs{$var} = $value; } - $sth->finish; } # AUTOLOAD diff --git a/Koha/Config/SysPref.pm b/Koha/Config/SysPref.pm new file mode 100644 index 0000000..e18c222 --- /dev/null +++ b/Koha/Config/SysPref.pm @@ -0,0 +1,52 @@ +package Koha::Config::SysPref; + +# Copyright ByWater Solutions 2014 +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Carp; + +use Koha::Database; + +use base qw(Koha::Object); + +=head1 NAME + +Koha::Config::SysPref - Koha System Preference Object class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub type { + return 'Systempreference'; +} + +=head1 AUTHOR + +Kyle M Hall + +=cut + +1; diff --git a/Koha/Config/SysPrefs.pm b/Koha/Config/SysPrefs.pm new file mode 100644 index 0000000..552d0c7 --- /dev/null +++ b/Koha/Config/SysPrefs.pm @@ -0,0 +1,58 @@ +package Koha::Config::SysPrefs; + +# Copyright ByWater Solutions 2014 +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Carp; + +use Koha::Database; + +use Koha::Config::SysPref; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::Config::SysPrefs - Koha System Preference object set class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub type { + return 'Systempreference'; +} + +sub object_class { + return 'Koha::Config::SysPref'; +} + +=head1 AUTHOR + +Kyle M Hall + +=cut + +1; diff --git a/Koha/Objects.pm b/Koha/Objects.pm index f347157..8ef6390 100644 --- a/Koha/Objects.pm +++ b/Koha/Objects.pm @@ -84,6 +84,8 @@ sub find { my $result = $self->_resultset()->find($id); + return unless $result; + my $object = $self->object_class()->_new_from_dbic( $result ); return $object; diff --git a/t/db_dependent/sysprefs.t b/t/db_dependent/sysprefs.t index ca88740..5b82750 100755 --- a/t/db_dependent/sysprefs.t +++ b/t/db_dependent/sysprefs.t @@ -19,7 +19,7 @@ # along with Koha; if not, see . use Modern::Perl; -use Test::More tests => 3; +use Test::More tests => 4; use C4::Context; # Start transaction @@ -27,20 +27,22 @@ my $dbh = C4::Context->dbh; $dbh->{RaiseError} = 1; $dbh->{AutoCommit} = 0; -my $opacheader = C4::Context->preference('opacheader'); +my $opacheader = C4::Context->preference('opacheader'); my $newopacheader = "newopacheader"; -C4::Context->set_preference('OPACHEADER', $newopacheader); -ok(C4::Context->preference('opacheader') eq $newopacheader); +C4::Context->set_preference( 'OPACHEADER', $newopacheader ); +is( C4::Context->preference('opacheader'), $newopacheader ); -C4::Context->set_preference('opacheader', $opacheader); -ok(C4::Context->preference('OPACHEADER') eq $opacheader); +C4::Context->set_preference( 'opacheader', $opacheader ); +is( C4::Context->preference('OPACHEADER'), $opacheader ); $ENV{OVERRIDE_SYSPREF_opacheader} = 'this is an override'; C4::Context->clear_syspref_cache(); -is(C4::Context->preference('opacheader'), - 'this is an override', - 'system preference value overridden from environment' +is( + C4::Context->preference('opacheader'), + 'this is an override', + 'system preference value overridden from environment' ); -$dbh->rollback; +C4::Context->set_preference( 'IDoNotExist', 'NonExistent' ); +is( C4::Context->preference('IDoNotExist'), 'NonExistent', 'Test creation of non-existant system preferencer' ); -- 1.7.2.5