TrackManiaControl/application/core/Settings/SettingManager.php

423 lines
11 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;
use ManiaControl\Utils\ClassUtil;
/**
* Class managing Settings and Configurations
2014-05-02 17:50:30 +02:00
*
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class SettingManager implements CallbackListener {
/*
* Constants
*/
2014-05-13 14:15:00 +02:00
const TABLE_SETTINGS = 'mc_settings';
const CB_SETTING_CHANGED = 'SettingManager.SettingChanged';
2014-05-13 16:57:07 +02:00
/** @deprecated Use CB_SETTING_CHANGED */
const CB_SETTINGS_CHANGED = 'SettingManager.SettingChanged';
2014-05-02 17:50:30 +02:00
/*
* Private Properties
*/
private $maniaControl = null;
2014-04-20 19:25:06 +02:00
private $storedSettings = array();
/**
* Construct a new Setting Manager
2014-05-02 17:50:30 +02:00
*
2014-01-05 22:51:21 +01:00
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
$this->initTables();
2014-02-12 21:12:51 +01:00
2014-05-02 17:50:30 +02:00
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_AFTERINIT, $this, 'handleAfterInit');
}
/**
2014-05-13 14:15:00 +02:00
* Initialize the necessary Database Tables
2014-05-02 17:50:30 +02:00
*
* @return bool
*/
private function initTables() {
2014-05-02 17:50:30 +02:00
$mysqli = $this->maniaControl->database->mysqli;
2014-05-13 14:15:00 +02:00
$defaultType = "'" . Setting::TYPE_STRING . "'";
$typeSet = Setting::getTypeSet();
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;";
2014-05-02 17:50:30 +02:00
$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;
}
2014-05-02 17:50:30 +02:00
/**
* Handle After Init Callback
*/
public function handleAfterInit() {
$this->deleteUnusedSettings();
}
/**
* Delete all unused Settings that haven't been initialized during the current Startup
*
* @return bool
*/
private function deleteUnusedSettings() {
2014-05-13 14:15:00 +02:00
$mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "DELETE FROM `" . self::TABLE_SETTINGS . "`
2014-05-02 17:50:30 +02:00
WHERE `changed` < NOW() - INTERVAL 1 HOUR;";
2014-05-13 14:15:00 +02:00
$result = $mysqli->query($settingQuery);
2014-05-02 17:50:30 +02:00
if ($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
2014-05-13 14:15:00 +02:00
if ($result) {
$this->storedSettings = array();
return true;
2014-05-02 17:50:30 +02:00
}
2014-05-13 14:15:00 +02:00
return false;
2014-05-02 17:50:30 +02:00
}
/**
2014-05-13 14:15:00 +02:00
* Get a Setting by its Index
2014-05-02 17:50:30 +02:00
*
2014-05-13 14:15:00 +02:00
* @param int $settingIndex
* @param mixed $defaultValue
* @return Setting
2014-05-02 17:50:30 +02:00
*/
2014-05-13 14:15:00 +02:00
public function getSettingByIndex($settingIndex, $defaultValue = null) {
$mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "SELECT * FROM `" . self::TABLE_SETTINGS . "`
WHERE `index` = {$settingIndex};";
$result = $mysqli->query($settingQuery);
2014-05-02 17:50:30 +02:00
if ($mysqli->error) {
trigger_error($mysqli->error);
2014-05-13 14:15:00 +02:00
return null;
2014-05-02 17:50:30 +02:00
}
2014-05-13 14:15:00 +02:00
if ($result->num_rows <= 0) {
$result->close();
return $defaultValue;
2014-05-02 17:50:30 +02:00
}
2014-05-13 14:15:00 +02:00
/** @var Setting $setting */
$setting = $result->fetch_object(Setting::CLASS_NAME, array(true));
$result->close();
2013-11-10 15:11:50 +01:00
2014-05-13 14:15:00 +02:00
return $setting;
}
2013-11-10 13:23:35 +01:00
/**
2014-05-13 14:15:00 +02:00
* Set a Setting for the given Object
2014-05-02 17:50:30 +02:00
*
2014-05-13 14:15:00 +02:00
* @param mixed $object
* @param string $settingName
2014-05-02 17:50:30 +02:00
* @param mixed $value
2014-05-13 14:15:00 +02:00
* @return Setting
2013-11-10 13:23:35 +01:00
*/
2014-05-13 14:15:00 +02:00
public function setSetting($object, $settingName, $value) {
$className = ClassUtil::getClass($object);
2013-11-10 13:23:35 +01:00
$setting = $this->getSettingObject($object, $settingName);
2014-05-13 14:15:00 +02:00
if ($setting) {
$setting->value = $value;
if (!$this->saveSetting($setting)) {
return null;
}
} else {
$setting = $this->initSetting($object, $settingName, $value);
2014-01-05 22:51:21 +01:00
}
2014-05-13 14:15:00 +02:00
$this->storedSettings[$className . $settingName] = $setting;
// Trigger Settings Changed Callback
$this->maniaControl->callbackManager->triggerCallback(self::CB_SETTING_CHANGED, $setting);
return $setting;
2014-01-05 22:51:21 +01:00
}
/**
* Get Setting by Name for the given Object
2014-05-02 17:50:30 +02:00
*
* @param mixed $object
* @param string $settingName
2014-05-02 17:50:30 +02:00
* @param mixed $default
2014-05-13 14:15:00 +02:00
* @return Setting
2014-01-05 22:51:21 +01:00
*/
public function getSettingObject($object, $settingName, $default = null) {
2014-05-13 14:15:00 +02:00
$settingClass = ClassUtil::getClass($object);
2014-05-02 17:50:30 +02:00
2014-05-13 14:15:00 +02:00
// Retrieve from Storage if possible
if (isset($this->storedSettings[$settingClass . $settingName])) {
return $this->storedSettings[$settingClass . $settingName];
2014-04-20 19:25:06 +02:00
}
2014-05-02 17:50:30 +02:00
2014-05-13 14:15:00 +02:00
// Fetch setting
$mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "SELECT * FROM `" . self::TABLE_SETTINGS . "`
WHERE `class` = '" . $mysqli->escape_string($settingClass) . "'
AND `setting` = '" . $mysqli->escape_string($settingName) . "';";
$result = $mysqli->query($settingQuery);
2014-01-27 09:07:43 +01:00
if ($mysqli->error) {
trigger_error($mysqli->error);
return null;
}
2014-05-13 14:15:00 +02:00
if ($result->num_rows <= 0) {
if ($default === null) {
return null;
}
$setting = $this->initSetting($object, $settingName, $default);
return $setting;
}
2014-05-02 17:50:30 +02:00
2014-05-13 14:15:00 +02:00
/** @var Setting $setting */
$setting = $result->fetch_object(Setting::CLASS_NAME, array(true));
$result->free();
// Store setting
$this->storedSettings[$settingClass . $settingName] = $setting;
return $setting;
}
2013-11-10 13:23:35 +01:00
2014-01-05 22:51:21 +01:00
/**
2014-05-13 14:15:00 +02:00
* Initialize a Setting for the given Object
2014-05-02 17:50:30 +02:00
*
* @param mixed $object
2014-01-05 22:51:21 +01:00
* @param string $settingName
2014-05-13 14:15:00 +02:00
* @param mixed $defaultValue
* @return Setting
2013-11-10 13:23:35 +01:00
*/
2014-05-13 14:15:00 +02:00
public function initSetting($object, $settingName, $defaultValue) {
$setting = new Setting();
$setting->class = ClassUtil::getClass($object);
$setting->setting = $settingName;
$setting->type = Setting::getValueType($defaultValue);
$setting->value = $defaultValue;
$setting->default = $defaultValue;
$saved = $this->saveSetting($setting);
if ($saved) {
return $setting;
}
return null;
}
2014-05-02 17:50:30 +02:00
2014-05-13 14:15:00 +02:00
/**
* Save the given Setting in the Database
*
* @param Setting $setting
* @return bool
*/
public function saveSetting(Setting &$setting) {
2014-05-02 17:50:30 +02:00
$mysqli = $this->maniaControl->database->mysqli;
2014-05-13 14:15:00 +02:00
$settingQuery = "INSERT INTO `" . self::TABLE_SETTINGS . "` (
`class`,
`setting`,
`type`,
`value`,
`default`
) VALUES (
?, ?, ?,
@value := ?,
@value
) ON DUPLICATE KEY UPDATE
`index` = LAST_INSERT_ID(`index`),
`type` = VALUES(`type`),
`value` = IF(`default` = VALUES(`default`), `value`, VALUES(`default`)),
`default` = VALUES(`default`),
`changed` = NOW();";
2013-11-10 13:23:35 +01:00
$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;
}
2014-05-13 14:15:00 +02:00
$formattedValue = $setting->getFormattedValue();
$settingStatement->bind_param('ssss', $setting->class, $setting->setting, $setting->type, $formattedValue);
$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;
}
2014-05-13 14:15:00 +02:00
$setting->index = $settingStatement->insert_id;
2013-11-10 13:23:35 +01:00
$settingStatement->close();
2014-05-13 14:15:00 +02:00
return true;
2013-11-10 13:23:35 +01:00
}
2013-11-10 15:11:50 +01:00
/**
* @deprecated
* @see SettingManager::getSettingValue()
*/
public function getSetting($object, $settingName, $default = null) {
return $this->getSettingValue($object, $settingName, $default);
}
2014-05-13 16:03:26 +02:00
/**
* Get the Setting Value directly
*
* @param mixed $object
* @param string $settingName
* @param mixed $default
* @return mixed
*/
public function getSettingValue($object, $settingName, $default = null) {
$setting = $this->getSettingObject($object, $settingName, $default);
2014-05-13 16:03:26 +02:00
if ($setting) {
return $setting->value;
}
return null;
}
2014-05-02 17:50:30 +02:00
/**
2014-05-13 14:15:00 +02:00
* Reset a Setting to its Default Value
2014-05-02 17:50:30 +02:00
*
* @param mixed $object
* @param string $settingName
2013-11-10 15:11:50 +01:00
* @return bool
*/
2014-05-13 14:15:00 +02:00
public function resetSetting($object, $settingName = null) {
if ($object instanceof Setting) {
$className = $object->class;
$settingName = $object->setting;
} else {
$className = ClassUtil::getClass($object);
}
$mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "UPDATE `" . self::TABLE_SETTINGS . "`
2013-11-10 15:11:50 +01:00
SET `value` = `default`
2014-05-13 14:15:00 +02:00
WHERE `class` = '" . $mysqli->escape_string($className) . "'
AND `setting` = '" . $mysqli->escape_string($settingName) . "';";
$result = $mysqli->query($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;
}
if (isset($this->storedSettings[$className . $settingName])) {
unset($this->storedSettings[$className . $settingName]);
}
2014-05-13 14:15:00 +02:00
return $result;
2013-12-09 10:19:29 +01:00
}
/**
* Delete a Setting
2014-05-02 17:50:30 +02:00
*
* @param mixed $object
* @param string $settingName
2013-12-09 10:19:29 +01:00
* @return bool
*/
2014-05-13 14:15:00 +02:00
public function deleteSetting($object, $settingName = null) {
if ($object instanceof Setting) {
$className = $object->class;
$settingName = $object->setting;
} else {
$className = ClassUtil::getClass($object);
}
$mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "DELETE FROM `" . self::TABLE_SETTINGS . "`
WHERE `class` = '" . $mysqli->escape_string($className) . "'
AND `setting` = '" . $mysqli->escape_string($settingName) . "';";
$result = $mysqli->query($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;
}
2014-05-13 14:15:00 +02:00
if (isset($this->storedSettings[$className . $settingName])) {
unset($this->storedSettings[$className . $settingName]);
}
2014-05-13 14:15:00 +02:00
return $result;
2013-11-10 15:11:50 +01:00
}
2014-01-18 20:05:50 +01:00
/**
* Get all Settings for the given Class
2014-05-02 17:50:30 +02:00
*
* @param mixed $object
2014-01-18 20:05:50 +01:00
* @return array
*/
public function getSettingsByClass($object) {
2014-05-13 14:15:00 +02:00
$className = ClassUtil::getClass($object);
2014-05-02 17:50:30 +02:00
$mysqli = $this->maniaControl->database->mysqli;
2014-05-13 14:15:00 +02:00
$query = "SELECT * FROM `" . self::TABLE_SETTINGS . "`
WHERE `class` = '" . $mysqli->escape_string($className) . "'
2014-01-18 20:05:50 +01:00
ORDER BY `setting` ASC;";
2014-05-02 17:50:30 +02:00
$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();
2014-05-13 14:15:00 +02:00
while ($setting = $result->fetch_object(Setting::CLASS_NAME, array(true))) {
2014-01-18 20:05:50 +01:00
$settings[$setting->index] = $setting;
}
$result->free();
return $settings;
}
/**
2014-05-13 14:15:00 +02:00
* Get all Settings
2014-05-02 17:50:30 +02:00
*
* @return array
*/
public function getSettings() {
$mysqli = $this->maniaControl->database->mysqli;
2014-05-02 17:50:30 +02:00
$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();
2014-05-13 14:15:00 +02:00
while ($setting = $result->fetch_object(Setting::CLASS_NAME, array(true))) {
$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
*
* @param bool $hidePluginClasses
2014-04-30 03:45:54 +02:00
* @return array
*/
public function getSettingClasses($hidePluginClasses = false) {
2014-04-30 03:45:54 +02:00
$mysqli = $this->maniaControl->database->mysqli;
2014-05-02 17:50:30 +02:00
$query = "SELECT DISTINCT `class` FROM `" . self::TABLE_SETTINGS . "`
2014-04-30 03:45:54 +02:00
ORDER BY `class` ASC;";
$result = $mysqli->query($query);
if ($mysqli->error) {
trigger_error($mysqli->error);
return null;
}
$settingClasses = array();
2014-05-13 14:15:00 +02:00
while ($row = $result->fetch_object()) {
if (!$hidePluginClasses || !PluginManager::isPluginClass($row->class)) {
array_push($settingClasses, $row->class);
2014-04-30 03:45:54 +02:00
}
}
$result->free();
return $settingClasses;
}
}