TrackManiaControl/application/core/Settings/SettingManager.php

492 lines
13 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\Callbacks\CallbackListener;
2014-02-12 21:12:51 +01:00
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\ManiaControl;
2014-04-30 03:45:54 +02:00
use ManiaControl\Plugins\PluginManager;
/**
* Class managing Settings and Configurations
*
* @author steeffeen & kremsy
* @copyright ManiaControl Copyright © 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
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 = ';;';
2014-04-20 19:25:06 +02:00
private $storedSettings = array();
/**
* 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();
2014-02-19 12:53:06 +01:00
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_AFTERINIT, $this, 'handleAfterInit');
}
2014-02-12 21:12:51 +01:00
/**
* Handle After Init Callback
*/
public function handleAfterInit() {
$this->deleteUnusedSettings();
}
/**
* Initialize necessary Database Tables
*
* @return bool
*/
private function initTables() {
$mysqli = $this->maniaControl->database->mysqli;
$defaultType = "'" . self::TYPE_STRING . "'";
$typeSet = $defaultType . ",'" . self::TYPE_INT . "','" . self::TYPE_REAL . "','" . self::TYPE_BOOL . "','" . self::TYPE_ARRAY . "'";
2014-04-20 19:25:06 +02:00
$settingTableQuery = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_SETTINGS . "` (
`index` int(11) NOT NULL AUTO_INCREMENT,
`class` varchar(100) NOT NULL,
`setting` varchar(150) NOT NULL,
`type` set({$typeSet}) NOT NULL DEFAULT {$defaultType},
`value` varchar(100) NOT NULL,
`default` varchar(100) 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;";
$result = $mysqli->query($settingTableQuery);
2014-01-27 09:07:43 +01:00
if ($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR);
}
2014-04-20 19:25:06 +02:00
return $result;
}
2013-11-10 15:11:50 +01:00
/**
* Get Class Name of a Parameter
*
* @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
*
* @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) {
if (is_null($default) || 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
*
* @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) {
2014-04-20 19:25:06 +02:00
$className = $this->getClassName($object);
// Check if setting is already in the ram
2014-04-20 19:25:06 +02:00
if (isset($this->storedSettings[$className . $settingName])) {
return $this->storedSettings[$className . $settingName];
}
$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-02-12 21:12:51 +01:00
$this->setSetting($className, $settingName, $default);
return $default;
}
$settingStatement->bind_result($type, $value);
$settingStatement->fetch();
$settingStatement->free_result();
$settingStatement->close();
$setting = $this->castSetting($type, $value);
// Store setting in the ram
2014-04-20 19:25:06 +02:00
$this->storedSettings[$className . $settingName] = $setting;
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
*
* @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);
var_dump($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;
}
$setting = $this->formatSetting($value);
$settingStatement->bind_param('sss', $setting, $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();
$this->storedSettings[$className . $settingName] = $value;
// Trigger settings changed Callback
$this->maniaControl->callbackManager->triggerCallback(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();
if (isset($this->storedSettings[$className . $settingName])) {
unset($this->storedSettings[$className . $settingName]);
}
2013-12-09 10:19:29 +01:00
return $success;
}
/**
* Delete a Setting
*
* @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();
if (isset($this->storedSettings[$className . $settingName])) {
unset($this->storedSettings[$className . $settingName]);
}
2013-12-09 10:19:29 +01:00
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
*
* @param mixed $object
2014-01-18 20:05:50 +01:00
* @return array
*/
public function getSettingsByClass($object) {
$className = $this->getClassName($object);
$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
*
* @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;
}
2014-02-12 21:12:51 +01:00
2014-04-30 03:45:54 +02:00
/**
* Get all Setting Classes
*
* @return array
*/
public function getSettingClasses($hidePluginClasses = true) {
$mysqli = $this->maniaControl->database->mysqli;
$query = "SELECT DISTINCT `class` FROM `" . self::TABLE_SETTINGS . "`
ORDER BY `class` ASC;";
$result = $mysqli->query($query);
if ($mysqli->error) {
trigger_error($mysqli->error);
return null;
}
$settingClasses = array();
while ($setting = $result->fetch_object()) {
if (!$hidePluginClasses || !PluginManager::isPluginClass($setting->class)) {
array_push($settingClasses, $setting->class);
}
}
$result->free();
return $settingClasses;
}
/**
* 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();
$this->storedSettings = array();
return $success;
}
}