2013-11-10 12:51:41 +01:00
|
|
|
<?php
|
2013-12-09 11:25:30 +01:00
|
|
|
|
2014-01-27 09:07:43 +01:00
|
|
|
namespace ManiaControl\Settings;
|
|
|
|
|
2014-02-12 21:07:54 +01:00
|
|
|
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;
|
2013-11-10 12:51:41 +01:00
|
|
|
|
|
|
|
/**
|
2014-02-12 21:07:54 +01:00
|
|
|
* Class managing Settings and Configurations
|
2014-04-30 02:27:17 +02:00
|
|
|
*
|
|
|
|
* @author steeffeen & kremsy
|
2014-04-12 12:14:37 +02:00
|
|
|
* @copyright ManiaControl Copyright © 2014 ManiaControl Team
|
2014-04-30 02:27:17 +02:00
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
2013-11-10 12:51:41 +01:00
|
|
|
*/
|
2014-02-12 21:07:54 +01:00
|
|
|
class SettingManager implements CallbackListener {
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
2013-11-10 12:51:41 +01:00
|
|
|
* Constants
|
|
|
|
*/
|
2014-04-30 02:27:17 +02:00
|
|
|
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';
|
2014-02-12 19:19:08 +01:00
|
|
|
const CB_SETTINGS_CHANGED = 'SettingManager.SettingsChanged';
|
2014-04-30 02:27:17 +02:00
|
|
|
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
|
|
|
* Private Properties
|
2013-11-10 12:51:41 +01:00
|
|
|
*/
|
|
|
|
private $maniaControl = null;
|
2013-11-10 13:23:35 +01:00
|
|
|
private $arrayDelimiter = ';;';
|
2014-04-20 19:25:06 +02:00
|
|
|
private $storedSettings = array();
|
2013-11-10 12:51:41 +01:00
|
|
|
|
|
|
|
/**
|
2014-02-12 21:07:54 +01:00
|
|
|
* Construct a new Setting Manager
|
2014-04-30 02:27:17 +02:00
|
|
|
*
|
2014-01-05 22:51:21 +01:00
|
|
|
* @param ManiaControl $maniaControl
|
2013-11-10 12:51:41 +01:00
|
|
|
*/
|
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
|
|
|
$this->maniaControl = $maniaControl;
|
|
|
|
$this->initTables();
|
2014-04-30 02:27:17 +02:00
|
|
|
|
2014-02-19 12:53:06 +01:00
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_AFTERINIT, $this, 'handleAfterInit');
|
2014-02-12 21:07:54 +01:00
|
|
|
}
|
2014-02-12 21:12:51 +01:00
|
|
|
|
2014-02-12 21:07:54 +01:00
|
|
|
/**
|
|
|
|
* Handle After Init Callback
|
|
|
|
*/
|
2014-02-19 15:44:00 +01:00
|
|
|
public function handleAfterInit() {
|
2014-02-12 21:07:54 +01:00
|
|
|
$this->deleteUnusedSettings();
|
2013-11-10 12:51:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-12 21:07:54 +01:00
|
|
|
* Initialize necessary Database Tables
|
2014-04-30 02:27:17 +02:00
|
|
|
*
|
2013-11-10 12:51:41 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function initTables() {
|
2014-04-30 02:27:17 +02:00
|
|
|
$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 . "` (
|
2013-11-10 12:51:41 +01:00
|
|
|
`index` int(11) NOT NULL AUTO_INCREMENT,
|
2014-02-15 14:58:19 +01:00
|
|
|
`class` varchar(100) NOT NULL,
|
|
|
|
`setting` varchar(150) NOT NULL,
|
2014-02-13 14:58:04 +01:00
|
|
|
`type` set({$typeSet}) NOT NULL DEFAULT {$defaultType},
|
|
|
|
`value` varchar(100) NOT NULL,
|
|
|
|
`default` varchar(100) NOT NULL,
|
2013-11-10 12:51:41 +01:00
|
|
|
`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-04-30 02:27:17 +02:00
|
|
|
$result = $mysqli->query($settingTableQuery);
|
2014-01-27 09:07:43 +01:00
|
|
|
if ($mysqli->error) {
|
2013-11-10 12:51:41 +01:00
|
|
|
trigger_error($mysqli->error, E_USER_ERROR);
|
|
|
|
}
|
2014-04-30 02:27:17 +02:00
|
|
|
|
2014-04-20 19:25:06 +02:00
|
|
|
return $result;
|
2013-11-10 12:51:41 +01:00
|
|
|
}
|
|
|
|
|
2013-11-10 15:11:50 +01:00
|
|
|
/**
|
2014-02-12 21:07:54 +01:00
|
|
|
* Get Class Name of a Parameter
|
2014-04-30 02:27:17 +02:00
|
|
|
*
|
2014-02-12 21:07:54 +01:00
|
|
|
* @param mixed $param
|
2013-11-10 15:11:50 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2014-02-12 21:07:54 +01:00
|
|
|
private function getClassName($param) {
|
|
|
|
if (is_object($param)) {
|
|
|
|
return get_class($param);
|
2013-11-10 15:11:50 +01:00
|
|
|
}
|
2014-02-12 21:07:54 +01:00
|
|
|
if (is_string($param)) {
|
|
|
|
return $param;
|
2013-11-10 15:11:50 +01:00
|
|
|
}
|
2014-02-12 21:07:54 +01:00
|
|
|
trigger_error('Invalid class param. ' . $param);
|
2014-04-30 02:27:17 +02:00
|
|
|
return (string) $param;
|
2013-11-10 15:11:50 +01:00
|
|
|
}
|
|
|
|
|
2013-11-10 12:51:41 +01:00
|
|
|
/**
|
2014-02-12 21:07:54 +01:00
|
|
|
* Get Type of a Parameter
|
2014-04-30 02:27:17 +02:00
|
|
|
*
|
2014-01-05 22:51:21 +01:00
|
|
|
* @param mixed $param
|
2013-11-10 12:51:41 +01:00
|
|
|
* @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;
|
2013-11-10 12:51:41 +01:00
|
|
|
}
|
2014-01-27 09:07:43 +01:00
|
|
|
if (is_real($param)) {
|
2013-11-10 13:23:35 +01:00
|
|
|
return self::TYPE_REAL;
|
2013-11-10 12:51:41 +01:00
|
|
|
}
|
2014-01-27 09:07:43 +01:00
|
|
|
if (is_bool($param)) {
|
2013-11-10 13:23:35 +01:00
|
|
|
return self::TYPE_BOOL;
|
2013-11-10 12:51:41 +01:00
|
|
|
}
|
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;
|
2013-11-10 12:51:41 +01:00
|
|
|
}
|
|
|
|
trigger_error('Unsupported setting type. ' . print_r($param, true));
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-12 21:07:54 +01:00
|
|
|
* Cast a Setting to the given Type
|
2014-04-30 02:27:17 +02:00
|
|
|
*
|
2014-01-05 22:51:21 +01:00
|
|
|
* @param string $type
|
2014-04-30 02:27:17 +02:00
|
|
|
* @param mixed $value
|
2013-11-10 12:51:41 +01:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
private function castSetting($type, $value) {
|
2014-01-27 09:07:43 +01:00
|
|
|
if ($type === self::TYPE_INT) {
|
2014-04-30 02:27:17 +02:00
|
|
|
return (int) $value;
|
2013-11-10 12:51:41 +01:00
|
|
|
}
|
2014-01-27 09:07:43 +01:00
|
|
|
if ($type === self::TYPE_REAL) {
|
2014-04-30 02:27:17 +02:00
|
|
|
return (float) $value;
|
2013-11-10 12:51:41 +01:00
|
|
|
}
|
2014-01-27 09:07:43 +01:00
|
|
|
if ($type === self::TYPE_BOOL) {
|
2014-04-30 02:27:17 +02:00
|
|
|
return (bool) $value;
|
2013-11-10 12:51:41 +01:00
|
|
|
}
|
2014-01-27 09:07:43 +01:00
|
|
|
if ($type === self::TYPE_STRING) {
|
2014-04-30 02:27:17 +02:00
|
|
|
return (string) $value;
|
2013-11-10 12:51:41 +01:00
|
|
|
}
|
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));
|
2013-11-10 12:51:41 +01:00
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2013-11-10 13:23:35 +01:00
|
|
|
/**
|
2014-02-12 21:07:54 +01:00
|
|
|
* Format a Setting for saving it to the Database
|
2014-04-30 02:27:17 +02: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) {
|
2013-11-12 19:33:25 +01:00
|
|
|
return ($value ? 1 : 0);
|
|
|
|
}
|
2013-11-10 13:23:35 +01:00
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2013-11-10 12:51:41 +01:00
|
|
|
/**
|
2014-02-12 21:07:54 +01:00
|
|
|
* Initialize a Setting for the given Object
|
2014-04-30 02:27:17 +02:00
|
|
|
*
|
|
|
|
* @param mixed $object
|
2014-01-05 22:51:21 +01:00
|
|
|
* @param string $settingName
|
2014-04-30 02:27:17 +02:00
|
|
|
* @param mixed $default
|
2013-11-10 12:51:41 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function initSetting($object, $settingName, $default) {
|
2014-04-30 02:27:17 +02:00
|
|
|
if (is_null($default) || is_object($default)) {
|
2013-11-10 12:51:41 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-04-30 02:27:17 +02:00
|
|
|
$className = $this->getClassName($object);
|
|
|
|
$type = $this->getType($default);
|
|
|
|
$default = $this->formatSetting($default, $type);
|
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
|
|
|
$settingQuery = "INSERT INTO `" . self::TABLE_SETTINGS . "` (
|
2013-11-10 12:51:41 +01:00
|
|
|
`class`,
|
|
|
|
`setting`,
|
|
|
|
`type`,
|
|
|
|
`value`,
|
|
|
|
`default`
|
|
|
|
) VALUES (
|
|
|
|
?, ?, ?,
|
|
|
|
@value := ?,
|
|
|
|
@value
|
2013-11-10 21:58:06 +01:00
|
|
|
) ON DUPLICATE KEY UPDATE
|
2013-11-12 15:48:25 +01:00
|
|
|
`type` = VALUES(`type`),
|
2014-02-13 14:58:04 +01:00
|
|
|
`value` = IF(`default` = VALUES(`default`), `value`, VALUES(`default`)),
|
2014-02-12 21:07:54 +01:00
|
|
|
`default` = VALUES(`default`),
|
|
|
|
`changed` = NOW();";
|
2013-11-10 12:51:41 +01:00
|
|
|
$settingStatement = $mysqli->prepare($settingQuery);
|
2014-01-27 09:07:43 +01:00
|
|
|
if ($mysqli->error) {
|
2013-11-10 12:51:41 +01:00
|
|
|
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) {
|
2013-11-10 12:51:41 +01:00
|
|
|
trigger_error($settingStatement->error);
|
2013-11-10 21:58:06 +01:00
|
|
|
$settingStatement->close();
|
2013-11-10 12:51:41 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$settingStatement->close();
|
2013-12-09 10:19:29 +01:00
|
|
|
return $success;
|
2013-11-10 12:51:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-12 21:07:54 +01:00
|
|
|
* Get a Setting by its Index
|
2014-04-30 02:27:17 +02:00
|
|
|
*
|
|
|
|
* @param int $settingIndex
|
2014-02-12 21:07:54 +01:00
|
|
|
* @param mixed $default
|
|
|
|
* @return mixed
|
2013-11-10 12:51:41 +01:00
|
|
|
*/
|
2014-01-05 22:51:21 +01:00
|
|
|
public function getSettingByIndex($settingIndex, $default = false) {
|
2014-04-30 02:27:17 +02:00
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
2014-01-05 22:51:21 +01:00
|
|
|
$settingQuery = "SELECT * FROM `" . self::TABLE_SETTINGS . "`
|
|
|
|
WHERE `index` = {$settingIndex};";
|
2014-04-30 02:27:17 +02:00
|
|
|
$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);
|
2014-02-12 21:07:54 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-12 21:07:54 +01:00
|
|
|
* Get Setting by Name for the given Object
|
2014-04-30 02:27:17 +02:00
|
|
|
*
|
|
|
|
* @param mixed $object
|
2014-02-12 21:07:54 +01:00
|
|
|
* @param string $settingName
|
2014-04-30 02:27:17 +02:00
|
|
|
* @param mixed $default
|
2014-02-12 21:07:54 +01:00
|
|
|
* @return mixed
|
2014-01-05 22:51:21 +01:00
|
|
|
*/
|
2014-02-12 21:07:54 +01:00
|
|
|
public function getSetting($object, $settingName, $default = null) {
|
2014-04-20 19:25:06 +02:00
|
|
|
$className = $this->getClassName($object);
|
2014-04-30 02:27:17 +02:00
|
|
|
|
|
|
|
// 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];
|
|
|
|
}
|
2014-04-30 02:27:17 +02:00
|
|
|
|
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
|
|
|
$settingQuery = "SELECT `type`, `value` FROM `" . self::TABLE_SETTINGS . "`
|
2013-11-10 12:51:41 +01:00
|
|
|
WHERE `class` = ?
|
|
|
|
AND `setting` = ?;";
|
|
|
|
$settingStatement = $mysqli->prepare($settingQuery);
|
2014-01-27 09:07:43 +01:00
|
|
|
if ($mysqli->error) {
|
2013-11-10 12:51:41 +01:00
|
|
|
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) {
|
2013-11-10 12:51:41 +01:00
|
|
|
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);
|
2013-11-10 12:51:41 +01:00
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
$settingStatement->bind_result($type, $value);
|
|
|
|
$settingStatement->fetch();
|
|
|
|
$settingStatement->free_result();
|
|
|
|
$settingStatement->close();
|
|
|
|
$setting = $this->castSetting($type, $value);
|
2014-04-30 02:27:17 +02:00
|
|
|
|
|
|
|
// Store setting in the ram
|
2014-04-20 19:25:06 +02:00
|
|
|
$this->storedSettings[$className . $settingName] = $setting;
|
2013-11-10 12:51:41 +01:00
|
|
|
return $setting;
|
|
|
|
}
|
2013-11-10 13:23:35 +01:00
|
|
|
|
2014-01-05 22:51:21 +01:00
|
|
|
/**
|
2014-02-12 21:07:54 +01:00
|
|
|
* Set a Setting for the given Object
|
2014-04-30 02:27:17 +02:00
|
|
|
*
|
|
|
|
* @param mixed $object
|
2014-01-05 22:51:21 +01:00
|
|
|
* @param string $settingName
|
2014-04-30 02:27:17 +02:00
|
|
|
* @param mixed $value
|
2013-11-10 13:23:35 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2014-02-12 21:07:54 +01:00
|
|
|
public function setSetting($object, $settingName, $value) {
|
2014-04-30 02:27:17 +02:00
|
|
|
$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;
|
|
|
|
}
|
2014-04-30 02:27:17 +02:00
|
|
|
$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);
|
2013-11-12 16:52:23 +01:00
|
|
|
$settingStatement->close();
|
2013-11-10 13:23:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$settingStatement->close();
|
2014-04-30 02:27:17 +02:00
|
|
|
|
|
|
|
$this->storedSettings[$className . $settingName] = $value;
|
|
|
|
|
|
|
|
// Trigger settings changed Callback
|
2014-02-19 15:44:00 +01:00
|
|
|
$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
|
|
|
|
2014-01-05 21:48:49 +01:00
|
|
|
/**
|
2014-02-12 21:07:54 +01:00
|
|
|
* Reset a Setting to its default Value
|
2014-04-30 02:27:17 +02:00
|
|
|
*
|
|
|
|
* @param mixed $object
|
2013-12-09 13:45:58 +01:00
|
|
|
* @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) {
|
2014-04-30 02:27:17 +02:00
|
|
|
$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();
|
2014-04-30 02:27:17 +02:00
|
|
|
if (isset($this->storedSettings[$className . $settingName])) {
|
|
|
|
unset($this->storedSettings[$className . $settingName]);
|
|
|
|
}
|
2013-12-09 10:19:29 +01:00
|
|
|
return $success;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-12 21:07:54 +01:00
|
|
|
* Delete a Setting
|
2014-04-30 02:27:17 +02:00
|
|
|
*
|
|
|
|
* @param mixed $object
|
2013-12-09 13:45:58 +01:00
|
|
|
* @param string $settingName
|
2013-12-09 10:19:29 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function deleteSetting($object, $settingName) {
|
2014-04-30 02:27:17 +02:00
|
|
|
$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();
|
2014-04-30 02:27:17 +02:00
|
|
|
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
|
|
|
}
|
2013-11-28 04:48:04 +01:00
|
|
|
|
2014-01-18 20:05:50 +01:00
|
|
|
/**
|
2014-02-12 21:07:54 +01:00
|
|
|
* Get all Settings for the given Class
|
2014-04-30 02:27:17 +02:00
|
|
|
*
|
2014-02-12 21:07:54 +01:00
|
|
|
* @param mixed $object
|
2014-01-18 20:05:50 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2014-02-12 21:07:54 +01:00
|
|
|
public function getSettingsByClass($object) {
|
|
|
|
$className = $this->getClassName($object);
|
2014-04-30 02:27:17 +02:00
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
2014-04-30 05:27:23 +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-04-30 02:27:17 +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-04-30 02:27:17 +02:00
|
|
|
while ($setting = $result->fetch_object()) {
|
2014-01-18 20:05:50 +01:00
|
|
|
$settings[$setting->index] = $setting;
|
|
|
|
}
|
|
|
|
$result->free();
|
|
|
|
return $settings;
|
|
|
|
}
|
|
|
|
|
2013-11-28 04:48:04 +01:00
|
|
|
/**
|
|
|
|
* Get all settings
|
2014-04-30 02:27:17 +02:00
|
|
|
*
|
2013-11-28 04:48:04 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getSettings() {
|
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
2014-04-30 02:27:17 +02:00
|
|
|
$query = "SELECT * FROM `" . self::TABLE_SETTINGS . "`
|
2013-11-28 04:48:04 +01:00
|
|
|
ORDER BY `class` ASC, `setting` ASC;";
|
|
|
|
$result = $mysqli->query($query);
|
2014-01-27 09:07:43 +01:00
|
|
|
if ($mysqli->error) {
|
2013-11-28 04:48:04 +01:00
|
|
|
trigger_error($mysqli->error);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$settings = array();
|
2014-04-30 02:27:17 +02:00
|
|
|
while ($setting = $result->fetch_object()) {
|
2014-01-05 21:48:49 +01:00
|
|
|
$settings[$setting->index] = $setting;
|
2013-11-28 04:48:04 +01:00
|
|
|
}
|
|
|
|
$result->free();
|
|
|
|
return $settings;
|
|
|
|
}
|
2014-02-12 21:12:51 +01:00
|
|
|
|
2014-04-30 03:45:54 +02:00
|
|
|
/**
|
|
|
|
* Get all Setting Classes
|
2014-05-02 16:13:45 +02:00
|
|
|
*
|
|
|
|
* @param bool $hidePluginClasses
|
2014-04-30 03:45:54 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
2014-04-30 05:27:23 +02:00
|
|
|
public function getSettingClasses($hidePluginClasses = false) {
|
2014-04-30 03:45:54 +02:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2014-02-12 21:07:54 +01:00
|
|
|
/**
|
|
|
|
* Delete all unused Settings that haven't been initialized during the current Startup
|
2014-04-30 02:27:17 +02:00
|
|
|
*
|
2014-02-12 21:07:54 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function deleteUnusedSettings() {
|
2014-04-30 02:27:17 +02:00
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
|
|
|
$settingQuery = "DELETE FROM `" . self::TABLE_SETTINGS . "`
|
2014-02-12 21:07:54 +01:00
|
|
|
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();
|
2014-04-30 02:27:17 +02:00
|
|
|
$this->storedSettings = array();
|
2014-02-12 21:07:54 +01:00
|
|
|
return $success;
|
|
|
|
}
|
2013-11-10 12:51:41 +01:00
|
|
|
}
|