improved setting manager

This commit is contained in:
Steffen Schröder 2013-11-10 15:11:50 +01:00
parent 16fb90a9ed
commit 5bcf0ac4e7

View File

@ -73,6 +73,23 @@ class SettingManager {
return true;
}
/**
* Get class name from param
*
* @param mixed $object
* @return string
*/
private function getClassName($object) {
if (is_object($object)) {
return get_class($object);
}
if (is_string($object)) {
return $object;
}
trigger_error('Invalid class param. ' . $object);
return (string) $object;
}
/**
* Get type for given parameter
*
@ -137,7 +154,6 @@ class SettingManager {
if ($type === null) {
$type = $this->getType($value);
}
if ($type === self::TYPE_ARRAY) {
return implode($this->arrayDelimiter, $value);
}
@ -156,10 +172,7 @@ class SettingManager {
if ($default === null || is_object($default)) {
return false;
}
$className = $object;
if (is_object($object)) {
$className = get_class($object);
}
$className = $this->getClassName($object);
$type = $this->getType($default);
$default = $this->formatSetting($default, $type);
$mysqli = $this->maniaControl->database->mysqli;
@ -198,10 +211,7 @@ class SettingManager {
* @return mixed
*/
public function getSetting($object, $settingName, $default = null) {
$className = $object;
if (is_object($object)) {
$className = get_class($object);
}
$className = $this->getClassName($object);
$mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "SELECT `type`, `value` FROM `" . self::TABLE_SETTINGS . "`
WHERE `class` = ?
@ -239,10 +249,7 @@ class SettingManager {
* @return bool
*/
public function setSetting($object, $settingName, $value) {
$className = $object;
if (is_object($object)) {
$className = get_class($object);
}
$className = $this->getClassName($object);
$mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "UPDATE `" . self::TABLE_SETTINGS . "`
SET `value` = ?
@ -263,6 +270,35 @@ class SettingManager {
$settingStatement->close();
return true;
}
/**
* Reset a setting to its default value
*
* @param object $object
* @param string $settingname
* @return bool
*/
public function resetSetting($object, $settingname) {
$className = $this->getClassName($object);
$mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "UPDATE `" . self::TABLE_SETTINGS . "`
SET `value` = `default`
WHERE `class` = ?
AND `setting` = ?;";
$settingStatement = $mysqli->prepare($settingQuery);
if ($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
$settingStatement->bind_param('ss', $className, $settingName);
$settingStatement->execute();
if ($settingStatement->error) {
trigger_error($settingStatement->error);
return false;
}
$settingStatement->close();
return true;
}
}
?>