From f0d0a072b803510d538a3e0df1b2d3e1c89fad4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20Schro=CC=88der?= Date: Wed, 14 May 2014 00:28:53 +0200 Subject: [PATCH] first draft for 'set' setting type removed 'array' setting type --- application/core/Settings/Setting.php | 138 +++++++++++-------- application/core/Settings/SettingManager.php | 46 ++++--- 2 files changed, 104 insertions(+), 80 deletions(-) diff --git a/application/core/Settings/Setting.php b/application/core/Settings/Setting.php index 319b4b1c..cb78232a 100644 --- a/application/core/Settings/Setting.php +++ b/application/core/Settings/Setting.php @@ -20,8 +20,8 @@ class Setting { const TYPE_INT = 'int'; const TYPE_REAL = 'real'; const TYPE_BOOL = 'bool'; - const TYPE_ARRAY = 'array'; - const ARRAY_DELIMITER = ';;'; + const TYPE_SET = 'set'; + const VALUE_DELIMITER = ';;'; /* * Public Properties @@ -32,85 +32,62 @@ class Setting { public $type = null; public $value = null; public $default = null; + public $set = null; public $fetchTime = null; /** * Construct a new Setting * - * @param bool $fetched + * @param mixed $object + * @param string $settingName + * @param mixed $defaultValue */ - public function __construct($fetched = false) { - if ($fetched) { - $this->value = self::castValue($this->value, $this->type); - $this->default = self::castValue($this->default, $this->type); + 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->fetchTime = time(); + } else { + // Created by Values + $this->class = ClassUtil::getClass($object); + $this->setting = (string)$settingName; + $this->type = self::getValueType($defaultValue); + if ($this->type === self::TYPE_SET) { + // Save Set and use first Value as Default + $this->set = $defaultValue; + $this->value = reset($defaultValue); + } else { + $this->value = $defaultValue; + } + $this->default = $this->value; } } /** - * Cast a Setting to the given Type + * Cast the Value based on the Setting Type * - * @param string $type - * @param mixed $value + * @param string $value * @return mixed */ - public static function castValue($value, $type) { - if ($type === self::TYPE_INT) { + private function castValue($value) { + if ($this->type === self::TYPE_INT) { return (int)$value; } - if ($type === self::TYPE_REAL) { + if ($this->type === self::TYPE_REAL) { return (float)$value; } - if ($type === self::TYPE_BOOL) { + if ($this->type === self::TYPE_BOOL) { return (bool)$value; } - if ($type === self::TYPE_STRING) { + if ($this->type === self::TYPE_STRING) { return (string)$value; } - if ($type === self::TYPE_ARRAY) { - return explode(self::ARRAY_DELIMITER, $value); - } - trigger_error("Unsupported Setting Value Type: '" . print_r($type, true) . "'!"); - return $value; - } - - /** - * Get the Set String for the available Types - * - * @return string - */ - public static function getTypeSet() { - $typeSet = "'" . self::TYPE_STRING . "','" . self::TYPE_INT . "','" . self::TYPE_REAL . "','" . self::TYPE_BOOL . "','" . self::TYPE_ARRAY . "'"; - return $typeSet; - } - - /** - * Get the Formatted Value of the Setting - * - * @return mixed - */ - public function getFormattedValue() { - $formattedValue = self::formatValue($this->value, $this->type); - return $formattedValue; - } - - /** - * Format a Value for saving it to the Database - * - * @param mixed $value - * @param string $type - * @return mixed - */ - public static function formatValue($value, $type = null) { - if ($type === null) { - $type = self::getValueType($value); - } - if ($type === self::TYPE_ARRAY) { - return implode(self::ARRAY_DELIMITER, $value); - } - if ($type === self::TYPE_BOOL) { - return ($value ? 1 : 0); + if ($this->type === self::TYPE_SET) { + return explode(self::VALUE_DELIMITER, $value); } + trigger_error("Unsupported Setting Value Type: '" . print_r($this->type, true) . "'!"); return $value; } @@ -134,12 +111,55 @@ class Setting { return self::TYPE_STRING; } if (is_array($value)) { - return self::TYPE_ARRAY; + return self::TYPE_SET; } trigger_error("Unsupported Setting Value Type: '" . print_r($value, true) . "'!"); return null; } + /** + * Get the Formatted Value of the Setting + * + * @return string + */ + public function getFormattedValue() { + return $this->formatValue($this->value); + } + + /** + * Format the given Value based on the Setting Type + * + * @param mixed $value + * @return string + */ + private function formatValue($value) { + if ($this->type === self::TYPE_BOOL) { + return ($value ? 1 : 0); + } + if ($this->type === self::TYPE_SET) { + return implode(self::VALUE_DELIMITER, $value); + } + return $value; + } + + /** + * Get the Formatted Default of the Setting + * + * @return string + */ + public function getFormattedDefault() { + return $this->formatValue($this->default); + } + + /** + * Get the Formatted Set of the Setting + * + * @return string + */ + public function getFormattedSet() { + return $this->formatValue($this->set); + } + /** * Check if the Settings belongs to the given Class * diff --git a/application/core/Settings/SettingManager.php b/application/core/Settings/SettingManager.php index e3ab7fef..a9c616a1 100644 --- a/application/core/Settings/SettingManager.php +++ b/application/core/Settings/SettingManager.php @@ -49,24 +49,33 @@ class SettingManager implements CallbackListener { */ private function initTables() { $mysqli = $this->maniaControl->database->mysqli; - $defaultType = "'" . Setting::TYPE_STRING . "'"; - $typeSet = Setting::getTypeSet(); $settingTableQuery = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_SETTINGS . "` ( - `index` int(11) NOT NULL AUTO_INCREMENT, - `class` varchar(100) NOT NULL, - `setting` varchar(150) NOT NULL, - `type` set({$typeSet}) NOT NULL DEFAULT {$defaultType}, - `value` varchar(100) NOT NULL, - `default` varchar(100) NOT NULL, + `index` INT(11) NOT NULL AUTO_INCREMENT, + `class` VARCHAR(100) NOT NULL, + `setting` VARCHAR(150) NOT NULL, + `type` VARCHAR(50) NOT NULL, + `value` VARCHAR(100) NOT NULL, + `default` VARCHAR(100) NOT NULL, + `set` VARCHAR(100) NOT NULL, `changed` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`index`), UNIQUE KEY `settingId` (`class`,`setting`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Settings and Configurations' AUTO_INCREMENT=1;"; - $result = $mysqli->query($settingTableQuery); + $result1 = $mysqli->query($settingTableQuery); if ($mysqli->error) { trigger_error($mysqli->error, E_USER_ERROR); } - return $result; + // TODO: remove (added in 0.143) + $alterTableQuery1 = "ALTER TABLE `" . self::TABLE_SETTINGS . "` + CHANGE `type` `type` VARCHAR(50) NOT NULL;"; + $result2 = $mysqli->query($alterTableQuery1); + if ($mysqli->error) { + trigger_error($mysqli->error); + } + $alterTableQuery2 = "ALTER TABLE `" . self::TABLE_SETTINGS . "` + ADD `set` VARCHAR(100) NOT NULL;"; + $mysqli->query($alterTableQuery2); + return ($result1 && $result2); } /** @@ -119,7 +128,7 @@ class SettingManager implements CallbackListener { } /** @var Setting $setting */ - $setting = $result->fetch_object(Setting::CLASS_NAME, array(true)); + $setting = $result->fetch_object(Setting::CLASS_NAME, array(false, null, null)); $result->close(); return $setting; @@ -189,7 +198,7 @@ class SettingManager implements CallbackListener { } /** @var Setting $setting */ - $setting = $result->fetch_object(Setting::CLASS_NAME, array(true)); + $setting = $result->fetch_object(Setting::CLASS_NAME, array(false, null, null)); $result->free(); // Store setting @@ -207,13 +216,8 @@ class SettingManager implements CallbackListener { * @return Setting */ public function initSetting($object, $settingName, $defaultValue) { - $setting = new Setting(); - $setting->class = ClassUtil::getClass($object); - $setting->setting = $settingName; - $setting->type = Setting::getValueType($defaultValue); - $setting->value = $defaultValue; - $setting->default = $defaultValue; - $saved = $this->saveSetting($setting); + $setting = new Setting($object, $settingName, $defaultValue); + $saved = $this->saveSetting($setting); if ($saved) { return $setting; } @@ -366,7 +370,7 @@ class SettingManager implements CallbackListener { return null; } $settings = array(); - while ($setting = $result->fetch_object(Setting::CLASS_NAME, array(true))) { + while ($setting = $result->fetch_object(Setting::CLASS_NAME, array(false, null, null))) { $settings[$setting->index] = $setting; } $result->free(); @@ -388,7 +392,7 @@ class SettingManager implements CallbackListener { return null; } $settings = array(); - while ($setting = $result->fetch_object(Setting::CLASS_NAME, array(true))) { + while ($setting = $result->fetch_object(Setting::CLASS_NAME, array(false, null, null))) { $settings[$setting->index] = $setting; } $result->free();