@@ -, +, @@ --- C4/Context.pm | 36 +++++++++++---------------- Koha/Objects.pm | 2 + Koha/SysPref.pm | 52 ++++++++++++++++++++++++++++++++++++++++ Koha/SysPrefs.pm | 58 +++++++++++++++++++++++++++++++++++++++++++++ t/db_dependent/sysprefs.t | 22 +++++++++------- 5 files changed, 139 insertions(+), 31 deletions(-) create mode 100644 Koha/SysPref.pm create mode 100644 Koha/SysPrefs.pm --- a/C4/Context.pm +++ a/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::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::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::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 --- a/Koha/Objects.pm +++ a/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; --- a/Koha/SysPref.pm +++ a/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 System Preference Object class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub type { + return 'Systempreference'; +} + +=head1 AUTHOR + +Kyle M Hall + +=cut + +1; --- a/Koha/SysPrefs.pm +++ a/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 System Preference 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; --- a/t/db_dependent/sysprefs.t +++ a/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' ); --