TrackManiaControl/application/core/Settings/SettingManager.php

455 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;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Callbacks\CallbackListener;
/**
* Class managing Settings and Configurations
2013-12-09 11:34:02 +01:00
*
* @author steeffeen & kremsy
*/
class SettingManager implements CallbackListener {
/**
* 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 a new Setting Manager
*
2014-01-05 22:51:21 +01:00
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
$this->initTables();
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_AFTERINIT, $this, 'handleAfterInit');
}
/**
* Handle After Init Callback
*
* @param array $callback
*/
public function handleAfterInit(array $callback) {
$this->deleteUnusedSettings();
}
/**
* 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 . ",'"
. self::TYPE_INT . "'" . ",'"
. self::TYPE_REAL . "'" . ",'"
. self::TYPE_BOOL . "'" . ",'"
. 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;
}
$success = $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 $success;
}
2013-11-10 15:11:50 +01:00
/**
* Get Class Name of a Parameter
2013-11-10 15:11:50 +01:00
*
* @param mixed $param
2013-11-10 15:11:50 +01:00
* @return string
*/
private function getClassName($param) {
if (is_object($param)) {
return get_class($param);
2013-11-10 15:11:50 +01:00
}
if (is_string($param)) {
return $param;
2013-11-10 15:11:50 +01:00
}
trigger_error('Invalid class param. ' . $param);
return (string) $param;
2013-11-10 15:11:50 +01:00
}
/**
* Get Type of a 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
2013-11-10 13:23:35 +01:00
*
* @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 the given Object
*
* @param mixed $object
2014-01-05 22:51:21 +01:00
* @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`),
`changed` = NOW();";
$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;
}
/**
* Get a Setting by its Index
*
* @param int $settingIndex
* @param mixed $default
* @return mixed
*/
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 null;
}
if ($result->num_rows <= 0) {
$result->close();
return $default;
2014-01-05 22:51:21 +01:00
}
$row = $result->fetch_object();
$result->close();
return $row;
}
/**
* Get Setting by Name for the given Object
2014-01-05 22:51:21 +01:00
*
* @param mixed $object
* @param string $settingName
* @param mixed $default
* @return mixed
2014-01-05 22:51:21 +01:00
*/
public function getSetting($object, $settingName, $default = null) {
$className = $this->getClassName($object);
$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
/**
* Set a Setting for the given Object
2014-01-05 22:51:21 +01:00
*
* @param mixed $object
2014-01-05 22:51:21 +01:00
* @param string $settingName
* @param mixed $value
2013-11-10 13:23:35 +01:00
* @return bool
*/
public function setSetting($object, $settingName, $value) {
$className = $this->getClassName($object);
$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
/**
* Reset a Setting to its default Value
*
* @param mixed $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
2013-12-09 10:19:29 +01:00
*
* @param mixed $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 for the given Class
2014-01-18 20:05:50 +01:00
*
* @param mixed $object
2014-01-18 20:05:50 +01:00
* @return array
*/
public function getSettingsByClass($object) {
$className = $this->getClassName($object);
2014-01-18 20:05:50 +01:00
$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;
}
/**
* Delete all unused Settings that haven't been initialized during the current Startup
*
* @return bool
*/
private function deleteUnusedSettings() {
$mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "DELETE FROM `" . self::TABLE_SETTINGS . "`
WHERE `changed` < NOW() - INTERVAL 1 HOUR;";
$settingStatement = $mysqli->prepare($settingQuery);
if ($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
$success = $settingStatement->execute();
if ($settingStatement->error) {
trigger_error($settingStatement->error);
$settingStatement->close();
return false;
}
$settingStatement->close();
return $success;
}
}