From da539e705450e9727334071f94a08be97db628c8 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/SysPref.pm | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ Koha/SysPrefs.pm | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+), 21 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..22ca265 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::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 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; -- 1.7.2.5