first draft for 'set' setting type
removed 'array' setting type
This commit is contained in:
parent
6ac2726bb9
commit
f0d0a072b8
@ -20,8 +20,8 @@ class Setting {
|
|||||||
const TYPE_INT = 'int';
|
const TYPE_INT = 'int';
|
||||||
const TYPE_REAL = 'real';
|
const TYPE_REAL = 'real';
|
||||||
const TYPE_BOOL = 'bool';
|
const TYPE_BOOL = 'bool';
|
||||||
const TYPE_ARRAY = 'array';
|
const TYPE_SET = 'set';
|
||||||
const ARRAY_DELIMITER = ';;';
|
const VALUE_DELIMITER = ';;';
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Public Properties
|
* Public Properties
|
||||||
@ -32,85 +32,62 @@ class Setting {
|
|||||||
public $type = null;
|
public $type = null;
|
||||||
public $value = null;
|
public $value = null;
|
||||||
public $default = null;
|
public $default = null;
|
||||||
|
public $set = null;
|
||||||
public $fetchTime = null;
|
public $fetchTime = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a new Setting
|
* Construct a new Setting
|
||||||
*
|
*
|
||||||
* @param bool $fetched
|
* @param mixed $object
|
||||||
|
* @param string $settingName
|
||||||
|
* @param mixed $defaultValue
|
||||||
*/
|
*/
|
||||||
public function __construct($fetched = false) {
|
public function __construct($object, $settingName, $defaultValue) {
|
||||||
if ($fetched) {
|
if ($object === false) {
|
||||||
$this->value = self::castValue($this->value, $this->type);
|
// Fetched from Database
|
||||||
$this->default = self::castValue($this->default, $this->type);
|
$this->value = $this->castValue($this->value);
|
||||||
|
$this->default = $this->castValue($this->default);
|
||||||
|
$this->set = $this->castValue($this->set);
|
||||||
$this->fetchTime = time();
|
$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 string $value
|
||||||
* @param mixed $value
|
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public static function castValue($value, $type) {
|
private function castValue($value) {
|
||||||
if ($type === self::TYPE_INT) {
|
if ($this->type === self::TYPE_INT) {
|
||||||
return (int)$value;
|
return (int)$value;
|
||||||
}
|
}
|
||||||
if ($type === self::TYPE_REAL) {
|
if ($this->type === self::TYPE_REAL) {
|
||||||
return (float)$value;
|
return (float)$value;
|
||||||
}
|
}
|
||||||
if ($type === self::TYPE_BOOL) {
|
if ($this->type === self::TYPE_BOOL) {
|
||||||
return (bool)$value;
|
return (bool)$value;
|
||||||
}
|
}
|
||||||
if ($type === self::TYPE_STRING) {
|
if ($this->type === self::TYPE_STRING) {
|
||||||
return (string)$value;
|
return (string)$value;
|
||||||
}
|
}
|
||||||
if ($type === self::TYPE_ARRAY) {
|
if ($this->type === self::TYPE_SET) {
|
||||||
return explode(self::ARRAY_DELIMITER, $value);
|
return explode(self::VALUE_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);
|
|
||||||
}
|
}
|
||||||
|
trigger_error("Unsupported Setting Value Type: '" . print_r($this->type, true) . "'!");
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,12 +111,55 @@ class Setting {
|
|||||||
return self::TYPE_STRING;
|
return self::TYPE_STRING;
|
||||||
}
|
}
|
||||||
if (is_array($value)) {
|
if (is_array($value)) {
|
||||||
return self::TYPE_ARRAY;
|
return self::TYPE_SET;
|
||||||
}
|
}
|
||||||
trigger_error("Unsupported Setting Value Type: '" . print_r($value, true) . "'!");
|
trigger_error("Unsupported Setting Value Type: '" . print_r($value, true) . "'!");
|
||||||
return null;
|
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
|
* Check if the Settings belongs to the given Class
|
||||||
*
|
*
|
||||||
|
@ -49,24 +49,33 @@ class SettingManager implements CallbackListener {
|
|||||||
*/
|
*/
|
||||||
private function initTables() {
|
private function initTables() {
|
||||||
$mysqli = $this->maniaControl->database->mysqli;
|
$mysqli = $this->maniaControl->database->mysqli;
|
||||||
$defaultType = "'" . Setting::TYPE_STRING . "'";
|
|
||||||
$typeSet = Setting::getTypeSet();
|
|
||||||
$settingTableQuery = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_SETTINGS . "` (
|
$settingTableQuery = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_SETTINGS . "` (
|
||||||
`index` int(11) NOT NULL AUTO_INCREMENT,
|
`index` INT(11) NOT NULL AUTO_INCREMENT,
|
||||||
`class` varchar(100) NOT NULL,
|
`class` VARCHAR(100) NOT NULL,
|
||||||
`setting` varchar(150) NOT NULL,
|
`setting` VARCHAR(150) NOT NULL,
|
||||||
`type` set({$typeSet}) NOT NULL DEFAULT {$defaultType},
|
`type` VARCHAR(50) NOT NULL,
|
||||||
`value` varchar(100) NOT NULL,
|
`value` VARCHAR(100) NOT NULL,
|
||||||
`default` 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,
|
`changed` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||||
PRIMARY KEY (`index`),
|
PRIMARY KEY (`index`),
|
||||||
UNIQUE KEY `settingId` (`class`,`setting`)
|
UNIQUE KEY `settingId` (`class`,`setting`)
|
||||||
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Settings and Configurations' AUTO_INCREMENT=1;";
|
) 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) {
|
if ($mysqli->error) {
|
||||||
trigger_error($mysqli->error, E_USER_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 */
|
/** @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();
|
$result->close();
|
||||||
|
|
||||||
return $setting;
|
return $setting;
|
||||||
@ -189,7 +198,7 @@ class SettingManager implements CallbackListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @var Setting $setting */
|
/** @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();
|
$result->free();
|
||||||
|
|
||||||
// Store setting
|
// Store setting
|
||||||
@ -207,13 +216,8 @@ class SettingManager implements CallbackListener {
|
|||||||
* @return Setting
|
* @return Setting
|
||||||
*/
|
*/
|
||||||
public function initSetting($object, $settingName, $defaultValue) {
|
public function initSetting($object, $settingName, $defaultValue) {
|
||||||
$setting = new Setting();
|
$setting = new Setting($object, $settingName, $defaultValue);
|
||||||
$setting->class = ClassUtil::getClass($object);
|
$saved = $this->saveSetting($setting);
|
||||||
$setting->setting = $settingName;
|
|
||||||
$setting->type = Setting::getValueType($defaultValue);
|
|
||||||
$setting->value = $defaultValue;
|
|
||||||
$setting->default = $defaultValue;
|
|
||||||
$saved = $this->saveSetting($setting);
|
|
||||||
if ($saved) {
|
if ($saved) {
|
||||||
return $setting;
|
return $setting;
|
||||||
}
|
}
|
||||||
@ -366,7 +370,7 @@ class SettingManager implements CallbackListener {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
$settings = array();
|
$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;
|
$settings[$setting->index] = $setting;
|
||||||
}
|
}
|
||||||
$result->free();
|
$result->free();
|
||||||
@ -388,7 +392,7 @@ class SettingManager implements CallbackListener {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
$settings = array();
|
$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;
|
$settings[$setting->index] = $setting;
|
||||||
}
|
}
|
||||||
$result->free();
|
$result->free();
|
||||||
|
Loading…
Reference in New Issue
Block a user