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; 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 * Get type for given parameter
* *
@ -137,7 +154,6 @@ class SettingManager {
if ($type === null) { if ($type === null) {
$type = $this->getType($value); $type = $this->getType($value);
} }
if ($type === self::TYPE_ARRAY) { if ($type === self::TYPE_ARRAY) {
return implode($this->arrayDelimiter, $value); return implode($this->arrayDelimiter, $value);
} }
@ -156,10 +172,7 @@ class SettingManager {
if ($default === null || is_object($default)) { if ($default === null || is_object($default)) {
return false; return false;
} }
$className = $object; $className = $this->getClassName($object);
if (is_object($object)) {
$className = get_class($object);
}
$type = $this->getType($default); $type = $this->getType($default);
$default = $this->formatSetting($default, $type); $default = $this->formatSetting($default, $type);
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
@ -198,10 +211,7 @@ class SettingManager {
* @return mixed * @return mixed
*/ */
public function getSetting($object, $settingName, $default = null) { public function getSetting($object, $settingName, $default = null) {
$className = $object; $className = $this->getClassName($object);
if (is_object($object)) {
$className = get_class($object);
}
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "SELECT `type`, `value` FROM `" . self::TABLE_SETTINGS . "` $settingQuery = "SELECT `type`, `value` FROM `" . self::TABLE_SETTINGS . "`
WHERE `class` = ? WHERE `class` = ?
@ -239,10 +249,7 @@ class SettingManager {
* @return bool * @return bool
*/ */
public function setSetting($object, $settingName, $value) { public function setSetting($object, $settingName, $value) {
$className = $object; $className = $this->getClassName($object);
if (is_object($object)) {
$className = get_class($object);
}
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "UPDATE `" . self::TABLE_SETTINGS . "` $settingQuery = "UPDATE `" . self::TABLE_SETTINGS . "`
SET `value` = ? SET `value` = ?
@ -263,6 +270,35 @@ class SettingManager {
$settingStatement->close(); $settingStatement->close();
return true; 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;
}
} }
?> ?>