From 0e7a02ff6589b8f7685c744887809c950975bc8e 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 | 37 ++++++++++++---------------- Koha/Objects.pm | 2 + Koha/SysPref.pm | 52 ++++++++++++++++++++++++++++++++++++++++ Koha/SysPrefs.pm | 58 +++++++++++++++++++++++++++++++++++++++++++++ t/db_dependent/sysprefs.t | 22 +++++++++------- 5 files changed, 140 insertions(+), 31 deletions(-) create mode 100644 Koha/SysPref.pm create mode 100644 Koha/SysPrefs.pm diff --git a/C4/Context.pm b/C4/Context.pm index cc93af4..33dfec6 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -101,13 +101,16 @@ 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::SysPref; +use Koha::SysPrefs; + =head1 NAME C4::Context - Maintain and manipulate the context of a Koha script @@ -554,14 +557,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::SysPrefs->find( lc $var ); + $value = $syspref ? $syspref->value() : undef; } $sysprefs{lc $var} = $value; @@ -632,23 +629,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::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::SysPref->new( { variable => $var, value => $value } )->store(); + } - if($sth->execute( $var, $value )) { + if ($syspref) { $sysprefs{$var} = $value; } - $sth->finish; } # AUTOLOAD 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/Koha/SysPref.pm b/Koha/SysPref.pm new file mode 100644 index 0000000..3193b37 --- /dev/null +++ b/Koha/SysPref.pm @@ -0,0 +1,52 @@ +package Koha::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::SysPref - Koha Sysstem 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/SysPrefs.pm b/Koha/SysPrefs.pm new file mode 100644 index 0000000..767b3f8 --- /dev/null +++ b/Koha/SysPrefs.pm @@ -0,0 +1,58 @@ +package Koha::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::SysPref; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::SysPrefs - Koha Borrower Object set class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub type { + return 'Systempreference'; +} + +sub object_class { + return 'Koha::SysPref'; +} + +=head1 AUTHOR + +Kyle M Hall + +=cut + +1; 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