TrackManiaControl/application/core/Settings/SettingManager.php

439 lines
12 KiB
PHP
Raw Normal View History

<?php
2013-12-09 11:25:30 +01:00
2014-01-27 09:07:43 +01:00
namespace ManiaControl\Settings;
use ManiaControl\ManiaControl;
/**
* Class managing settings and configurations
2013-12-09 11:34:02 +01:00
*
* @author steeffeen & kremsy
*/
class SettingManager {
/**
* Constants
*/
const TABLE_SETTINGS = 'mc_settings';
const TYPE_STRING = 'string';
const TYPE_INT = 'int';
const TYPE_REAL = 'real';
const TYPE_BOOL = 'bool';
const TYPE_ARRAY = 'array';
const CB_SETTINGS_CHANGED = 'SettingManager.SettingsChanged';
/**
* Private properties
*/
private $maniaControl = null;
2013-11-10 13:23:35 +01:00
private $arrayDelimiter = ';;';
/**
* Construct setting manager
*
2014-01-05 22:51:21 +01:00
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
$this->initTables();
}
/**
* Initialize necessary database tables
*
* @return bool
*/
private function initTables() {
$mysqli = $this->maniaControl->database->mysqli;
2013-11-10 13:23:35 +01:00
$defaultType = "'" . self::TYPE_STRING . "'";
$typeSet = $defaultType;
2013-11-10 13:23:35 +01:00
$typeSet .= ",'" . self::TYPE_INT . "'";
$typeSet .= ",'" . self::TYPE_REAL . "'";
$typeSet .= ",'" . self::TYPE_BOOL . "'";
$typeSet .= ",'" . self::TYPE_ARRAY . "'";
$settingTableQuery = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_SETTINGS . "` (
`index` int(11) NOT NULL AUTO_INCREMENT,
`class` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`setting` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
2013-11-10 13:28:39 +01:00
`type` set({$typeSet}) COLLATE utf8_unicode_ci NOT NULL DEFAULT {$defaultType},
2013-11-10 13:23:35 +01:00
`value` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`default` varchar(100) COLLATE utf8_unicode_ci 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;";
$settingTableStatement = $mysqli->prepare($settingTableQuery);
2014-01-27 09:07:43 +01:00
if ($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR);
return false;
}
$settingTableStatement->execute();
2014-01-27 09:07:43 +01:00
if ($settingTableStatement->error) {
trigger_error($settingTableStatement->error, E_USER_ERROR);
return false;
}
$settingTableStatement->close();
return true;
}
2013-11-10 15:11:50 +01:00
/**
* Get class name from param
*
2014-01-05 22:51:21 +01:00
* @param mixed $object
2013-11-10 15:11:50 +01:00
* @return string
*/
private function getClassName($object) {
2014-01-27 09:07:43 +01:00
if (is_object($object)) {
2013-11-10 15:11:50 +01:00
return get_class($object);
}
2014-01-27 09:07:43 +01:00
if (is_string($object)) {
2013-11-10 15:11:50 +01:00
return $object;
}
trigger_error('Invalid class param. ' . $object);
return (string)$object;
2013-11-10 15:11:50 +01:00
}
/**
* Get type for given parameter
*
2014-01-05 22:51:21 +01:00
* @param mixed $param
* @return string
*/
private function getType($param) {
2014-01-27 09:07:43 +01:00
if (is_int($param)) {
2013-11-10 13:23:35 +01:00
return self::TYPE_INT;
}
2014-01-27 09:07:43 +01:00
if (is_real($param)) {
2013-11-10 13:23:35 +01:00
return self::TYPE_REAL;
}
2014-01-27 09:07:43 +01:00
if (is_bool($param)) {
2013-11-10 13:23:35 +01:00
return self::TYPE_BOOL;
}
2014-01-27 09:07:43 +01:00
if (is_string($param)) {
2013-11-10 13:23:35 +01:00
return self::TYPE_STRING;
}
2014-01-27 09:07:43 +01:00
if (is_array($param)) {
2013-11-10 13:23:35 +01:00
return self::TYPE_ARRAY;
}
trigger_error('Unsupported setting type. ' . print_r($param, true));
return null;
}
/**
* Cast a setting to the given type
*
2014-01-05 22:51:21 +01:00
* @param string $type
* @param mixed $value
* @return mixed
*/
private function castSetting($type, $value) {
2014-01-27 09:07:43 +01:00
if ($type === self::TYPE_INT) {
return (int)$value;
}
2014-01-27 09:07:43 +01:00
if ($type === self::TYPE_REAL) {
return (float)$value;
}
2014-01-27 09:07:43 +01:00
if ($type === self::TYPE_BOOL) {
return (bool)$value;
}
2014-01-27 09:07:43 +01:00
if ($type === self::TYPE_STRING) {
return (string)$value;
}
2014-01-27 09:07:43 +01:00
if ($type === self::TYPE_ARRAY) {
2013-11-10 13:23:35 +01:00
return explode($this->arrayDelimiter, $value);
}
2013-12-09 09:11:19 +01:00
trigger_error('Unsupported setting type. ' . print_r($type, true));
return $value;
}
2013-11-10 13:23:35 +01:00
/**
* Format a setting for saving it to the database
*
* @param mixed $value
2014-01-05 22:51:21 +01:00
* @param string $type
2013-11-10 13:23:35 +01:00
* @return mixed
*/
private function formatSetting($value, $type = null) {
2014-01-27 09:07:43 +01:00
if ($type === null) {
2013-11-10 13:23:35 +01:00
$type = $this->getType($value);
}
2014-01-27 09:07:43 +01:00
if ($type === self::TYPE_ARRAY) {
2013-11-10 13:23:35 +01:00
return implode($this->arrayDelimiter, $value);
}
2014-01-27 09:07:43 +01:00
if ($type === self::TYPE_BOOL) {
return ($value ? 1 : 0);
}
2013-11-10 13:23:35 +01:00
return $value;
}
/**
* Initialize a setting for given object
*
2014-01-05 22:51:21 +01:00
* @param object $object
* @param string $settingName
* @param mixed $default
* @return bool
*/
public function initSetting($object, $settingName, $default) {
2014-01-27 09:07:43 +01:00
if ($default === null || is_object($default)) {
return false;
}
$className = $this->getClassName($object);
$type = $this->getType($default);
$default = $this->formatSetting($default, $type);
$mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "INSERT INTO `" . self::TABLE_SETTINGS . "` (
`class`,
`setting`,
`type`,
`value`,
`default`
) VALUES (
?, ?, ?,
@value := ?,
@value
2013-11-10 21:58:06 +01:00
) ON DUPLICATE KEY UPDATE
`type` = VALUES(`type`),
`value` = IF(`default` = VALUES(`default`), `value`, VALUES(`default`)),
`default` = VALUES(`default`);";
$settingStatement = $mysqli->prepare($settingQuery);
2014-01-27 09:07:43 +01:00
if ($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
$settingStatement->bind_param('ssss', $className, $settingName, $type, $default);
2013-12-09 10:19:29 +01:00
$success = $settingStatement->execute();
2014-01-27 09:07:43 +01:00
if ($settingStatement->error) {
trigger_error($settingStatement->error);
2013-11-10 21:58:06 +01:00
$settingStatement->close();
return false;
}
$settingStatement->close();
2013-12-09 10:19:29 +01:00
return $success;
}
/**
2014-01-05 22:51:21 +01:00
* Get a Setting by its index
*
* @param $settingIndex
2014-01-05 22:51:21 +01:00
* @param bool $default
* @internal param null $default
* @internal param $className
* @internal param \ManiaControl\gIndex $settin
2014-01-27 09:07:43 +01:00
* @return mixed null
*/
2014-01-05 22:51:21 +01:00
public function getSettingByIndex($settingIndex, $default = false) {
$mysqli = $this->maniaControl->database->mysqli;
2014-01-05 22:51:21 +01:00
$settingQuery = "SELECT * FROM `" . self::TABLE_SETTINGS . "`
WHERE `index` = {$settingIndex};";
$result = $mysqli->query($settingQuery);
2014-01-27 09:07:43 +01:00
if (!$result) {
2014-01-05 22:51:21 +01:00
trigger_error($mysqli->error);
return false;
}
2014-01-05 22:51:21 +01:00
$row = $result->fetch_object();
$result->close();
return $row;
}
/**
* Gets a Setting Via it's class name
*
* @param $className
* @param $settingName
* @param $value
*/
public function getSettingByClassName($className, $settingName, $default = null) {
$mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "SELECT `type`, `value` FROM `" . self::TABLE_SETTINGS . "`
WHERE `class` = ?
AND `setting` = ?;";
$settingStatement = $mysqli->prepare($settingQuery);
2014-01-27 09:07:43 +01:00
if ($mysqli->error) {
trigger_error($mysqli->error);
return null;
}
$settingStatement->bind_param('ss', $className, $settingName);
$settingStatement->execute();
2014-01-27 09:07:43 +01:00
if ($settingStatement->error) {
trigger_error($settingStatement->error);
return null;
}
$settingStatement->store_result();
2014-01-27 09:07:43 +01:00
if ($settingStatement->num_rows <= 0) {
2014-01-05 22:51:21 +01:00
$this->updateSetting($className, $settingName, $default);
return $default;
}
$settingStatement->bind_result($type, $value);
$settingStatement->fetch();
$settingStatement->free_result();
$settingStatement->close();
$setting = $this->castSetting($type, $value);
return $setting;
}
2013-11-10 13:23:35 +01:00
2014-01-05 22:51:21 +01:00
/**
* Get setting by name for given object
*
* @param object $object
* @param string $settingName
* @param mixed $default
2014-01-05 22:51:21 +01:00
* @return mixed
*/
public function getSetting($object, $settingName, $default = null) {
$className = $this->getClassName($object);
return $this->getSettingByClassName($className, $settingName, $default);
}
2013-11-10 13:23:35 +01:00
/**
* Updates a Setting
2014-01-05 22:51:21 +01:00
*
* @param $className
* @param $settingName
* @param $value
2013-11-10 13:23:35 +01:00
* @return bool
*/
2014-01-05 22:51:21 +01:00
public function updateSetting($className, $settingName, $value) {
$mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "UPDATE `" . self::TABLE_SETTINGS . "`
2013-11-10 13:23:35 +01:00
SET `value` = ?
WHERE `class` = ?
AND `setting` = ?;";
$settingStatement = $mysqli->prepare($settingQuery);
2014-01-27 09:07:43 +01:00
if ($mysqli->error) {
2013-11-10 13:23:35 +01:00
trigger_error($mysqli->error);
return false;
}
$value = $this->formatSetting($value);
$settingStatement->bind_param('sss', $value, $className, $settingName);
2013-12-09 10:19:29 +01:00
$success = $settingStatement->execute();
2014-01-27 09:07:43 +01:00
if ($settingStatement->error) {
2013-11-10 13:23:35 +01:00
trigger_error($settingStatement->error);
$settingStatement->close();
2013-11-10 13:23:35 +01:00
return false;
}
$settingStatement->close();
2014-02-12 19:37:34 +01:00
//Trigger settings changed Callback
$this->maniaControl->callbackManager->triggerCallback(self::CB_SETTINGS_CHANGED, array(self::CB_SETTINGS_CHANGED, $className, $settingName, $value));
2013-12-09 10:19:29 +01:00
return $success;
2013-11-10 13:23:35 +01:00
}
2013-11-10 15:11:50 +01:00
/**
* Set a setting for the given object
*
2014-01-05 22:51:21 +01:00
* @param object $object
* @param string $settingName
* @param mixed $value
* @return bool
*/
public function setSetting($object, $settingName, $value) {
$className = $this->getClassName($object);
$this->updateSetting($className, $settingName, $value);
}
2013-11-10 15:11:50 +01:00
/**
* Reset a setting to its default value
*
2014-01-05 22:51:21 +01:00
* @param object $object
* @param string $settingName
2013-11-10 15:11:50 +01:00
* @return bool
*/
2013-12-09 10:19:29 +01:00
public function resetSetting($object, $settingName) {
$className = $this->getClassName($object);
$mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "UPDATE `" . self::TABLE_SETTINGS . "`
2013-11-10 15:11:50 +01:00
SET `value` = `default`
WHERE `class` = ?
AND `setting` = ?;";
$settingStatement = $mysqli->prepare($settingQuery);
2014-01-27 09:07:43 +01:00
if ($mysqli->error) {
2013-11-10 15:11:50 +01:00
trigger_error($mysqli->error);
return false;
}
$settingStatement->bind_param('ss', $className, $settingName);
2013-12-09 10:19:29 +01:00
$success = $settingStatement->execute();
2014-01-27 09:07:43 +01:00
if ($settingStatement->error) {
2013-11-10 15:11:50 +01:00
trigger_error($settingStatement->error);
2013-12-09 10:19:29 +01:00
$settingStatement->close();
2013-11-10 15:11:50 +01:00
return false;
}
$settingStatement->close();
2013-12-09 10:19:29 +01:00
return $success;
}
/**
* Delete a setting from the database
*
2014-01-05 22:51:21 +01:00
* @param object $object
* @param string $settingName
2013-12-09 10:19:29 +01:00
* @return bool
*/
public function deleteSetting($object, $settingName) {
$className = $this->getClassName($object);
$mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "DELETE FROM `" . self::TABLE_SETTINGS . "`
2013-12-09 10:19:29 +01:00
WHERE `class` = ?
AND `setting` = ?;";
$settingStatement = $mysqli->prepare($settingQuery);
2014-01-27 09:07:43 +01:00
if ($mysqli->error) {
2013-12-09 10:19:29 +01:00
trigger_error($mysqli->error);
return false;
}
$settingStatement->bind_param('ss', $className, $settingName);
$success = $settingStatement->execute();
2014-01-27 09:07:43 +01:00
if ($settingStatement->error) {
2013-12-09 10:19:29 +01:00
trigger_error($settingStatement->error);
$settingStatement->close();
return false;
}
$settingStatement->close();
return $success;
2013-11-10 15:11:50 +01:00
}
2014-01-18 20:05:50 +01:00
/**
* Get all settings from a Defined class
*
* @param $className
* @return array
*/
public function getSettingsByClass($className) {
$mysqli = $this->maniaControl->database->mysqli;
$query = "SELECT * FROM `" . self::TABLE_SETTINGS . "` WHERE `class`= '" . $mysqli->escape_string($className) . "'
2014-01-18 20:05:50 +01:00
ORDER BY `setting` ASC;";
$result = $mysqli->query($query);
2014-01-27 09:07:43 +01:00
if ($mysqli->error) {
2014-01-18 20:05:50 +01:00
trigger_error($mysqli->error);
return null;
}
$settings = array();
while($setting = $result->fetch_object()) {
2014-01-18 20:05:50 +01:00
$settings[$setting->index] = $setting;
}
$result->free();
return $settings;
}
/**
* Get all settings
2014-01-05 22:51:21 +01:00
*
* @return array
*/
public function getSettings() {
$mysqli = $this->maniaControl->database->mysqli;
$query = "SELECT * FROM `" . self::TABLE_SETTINGS . "`
ORDER BY `class` ASC, `setting` ASC;";
$result = $mysqli->query($query);
2014-01-27 09:07:43 +01:00
if ($mysqli->error) {
trigger_error($mysqli->error);
return null;
}
$settings = array();
while($setting = $result->fetch_object()) {
$settings[$setting->index] = $setting;
}
$result->free();
return $settings;
}
}