From 3af2da045a416e80cfe1cfc0d739269a81fb2c98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20Schro=CC=88der?= Date: Sun, 18 May 2014 21:46:00 +0200 Subject: [PATCH] SET setting type fixes --- application/core/Settings/Setting.php | 53 ++++++++++++-------- application/core/Settings/SettingManager.php | 19 ++++--- 2 files changed, 46 insertions(+), 26 deletions(-) diff --git a/application/core/Settings/Setting.php b/application/core/Settings/Setting.php index cb78232a..968555df 100644 --- a/application/core/Settings/Setting.php +++ b/application/core/Settings/Setting.php @@ -45,9 +45,11 @@ class Setting { public function __construct($object, $settingName, $defaultValue) { if ($object === false) { // Fetched from Database - $this->value = $this->castValue($this->value); - $this->default = $this->castValue($this->default); - $this->set = $this->castValue($this->set); + $this->value = $this->castValue($this->value); + $this->default = $this->castValue($this->default); + if ($this->set) { + $this->set = $this->castValue($this->set, $this->type); + } $this->fetchTime = time(); } else { // Created by Values @@ -68,26 +70,30 @@ class Setting { /** * Cast the Value based on the Setting Type * - * @param string $value + * @param mixed $value + * @param string $type * @return mixed */ - private function castValue($value) { - if ($this->type === self::TYPE_INT) { + private static function castValue($value, $type = null) { + if ($type === null) { + $type = self::getValueType($value); + } + if ($type === self::TYPE_INT) { return (int)$value; } - if ($this->type === self::TYPE_REAL) { + if ($type === self::TYPE_REAL) { return (float)$value; } - if ($this->type === self::TYPE_BOOL) { + if ($type === self::TYPE_BOOL) { return (bool)$value; } - if ($this->type === self::TYPE_STRING) { + if ($type === self::TYPE_STRING) { return (string)$value; } - if ($this->type === self::TYPE_SET) { + if ($type === self::TYPE_SET) { return explode(self::VALUE_DELIMITER, $value); } - trigger_error("Unsupported Setting Value Type: '" . print_r($this->type, true) . "'!"); + trigger_error("Unsupported Setting Value Type: '" . print_r($type, true) . "'!"); return $value; } @@ -97,7 +103,7 @@ class Setting { * @param mixed $value * @return string */ - public static function getValueType($value) { + private static function getValueType($value) { if (is_int($value)) { return self::TYPE_INT; } @@ -123,20 +129,24 @@ class Setting { * @return string */ public function getFormattedValue() { - return $this->formatValue($this->value); + return self::formatValue($this->value); } /** - * Format the given Value based on the Setting Type + * Format the given Value based on the Type * - * @param mixed $value + * @param mixed $value + * @param string $type * @return string */ - private function formatValue($value) { - if ($this->type === self::TYPE_BOOL) { + private static function formatValue($value, $type = null) { + if ($type === null) { + $type = self::getValueType($value); + } + if ($type === self::TYPE_BOOL) { return ($value ? 1 : 0); } - if ($this->type === self::TYPE_SET) { + if ($type === self::TYPE_SET) { return implode(self::VALUE_DELIMITER, $value); } return $value; @@ -148,7 +158,7 @@ class Setting { * @return string */ public function getFormattedDefault() { - return $this->formatValue($this->default); + return self::formatValue($this->default); } /** @@ -157,7 +167,10 @@ class Setting { * @return string */ public function getFormattedSet() { - return $this->formatValue($this->set); + if ($this->type === self::TYPE_SET) { + return self::formatValue($this->set); + } + return ''; } /** diff --git a/application/core/Settings/SettingManager.php b/application/core/Settings/SettingManager.php index a9c616a1..31d8df5c 100644 --- a/application/core/Settings/SettingManager.php +++ b/application/core/Settings/SettingManager.php @@ -237,24 +237,31 @@ class SettingManager implements CallbackListener { `setting`, `type`, `value`, - `default` + `default`, + `set` ) VALUES ( - ?, ?, ?, - @value := ?, - @value + ?, + ?, + ?, + ?, + ?, + ? ) ON DUPLICATE KEY UPDATE `index` = LAST_INSERT_ID(`index`), `type` = VALUES(`type`), `value` = IF(`default` = VALUES(`default`), `value`, VALUES(`default`)), `default` = VALUES(`default`), + `set` = VALUES(`set`), `changed` = NOW();"; $settingStatement = $mysqli->prepare($settingQuery); if ($mysqli->error) { trigger_error($mysqli->error); return false; } - $formattedValue = $setting->getFormattedValue(); - $settingStatement->bind_param('ssss', $setting->class, $setting->setting, $setting->type, $formattedValue); + $formattedValue = $setting->getFormattedValue(); + $formattedDefault = $setting->getFormattedDefault(); + $formattedSet = $setting->getFormattedSet(); + $settingStatement->bind_param('ssssss', $setting->class, $setting->setting, $setting->type, $formattedValue, $formattedDefault, $formattedSet); $settingStatement->execute(); if ($settingStatement->error) { trigger_error($settingStatement->error);