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-05-24 16:39:12 +02:00
|
|
|
use ManiaControl\Callbacks\Callbacks;
|
2017-03-26 19:44:55 +02:00
|
|
|
use ManiaControl\General\UsageInformationAble;
|
|
|
|
use ManiaControl\General\UsageInformationTrait;
|
2014-02-12 21:12:51 +01:00
|
|
|
use ManiaControl\ManiaControl;
|
2014-04-30 03:45:54 +02:00
|
|
|
use ManiaControl\Plugins\PluginManager;
|
2014-05-13 14:32:30 +02:00
|
|
|
use ManiaControl\Utils\ClassUtil;
|
2013-11-10 12:51:41 +01:00
|
|
|
|
|
|
|
/**
|
2014-07-25 16:28:47 +02:00
|
|
|
* Class managing ManiaControl Settings and Configurations
|
2014-05-02 17:50:30 +02:00
|
|
|
*
|
|
|
|
* @author ManiaControl Team <mail@maniacontrol.com>
|
2020-01-22 10:39:35 +01:00
|
|
|
* @copyright 2014-2020 ManiaControl Team
|
2014-05-02 17:50:30 +02:00
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
2013-11-10 12:51:41 +01:00
|
|
|
*/
|
2017-03-26 19:44:55 +02:00
|
|
|
class SettingManager implements CallbackListener, UsageInformationAble {
|
|
|
|
use UsageInformationTrait;
|
|
|
|
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
2013-11-10 12:51:41 +01:00
|
|
|
* Constants
|
|
|
|
*/
|
2014-05-13 14:15:00 +02:00
|
|
|
const TABLE_SETTINGS = 'mc_settings';
|
|
|
|
const CB_SETTING_CHANGED = 'SettingManager.SettingChanged';
|
2014-05-02 17:50:30 +02:00
|
|
|
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
2014-07-25 16:28:47 +02:00
|
|
|
* Private properties
|
2013-11-10 12:51:41 +01:00
|
|
|
*/
|
2014-07-25 16:39:07 +02:00
|
|
|
/** @var ManiaControl $maniaControl */
|
2013-11-10 12:51:41 +01:00
|
|
|
private $maniaControl = null;
|
2014-07-25 16:39:07 +02:00
|
|
|
/** @var Setting[] $storedSettings */
|
2014-04-20 19:25:06 +02:00
|
|
|
private $storedSettings = array();
|
2013-11-10 12:51:41 +01:00
|
|
|
|
|
|
|
/**
|
2014-07-25 16:28:47 +02:00
|
|
|
* Construct a new setting manager instance
|
2014-05-02 17:50:30 +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-02-12 21:12:51 +01:00
|
|
|
|
2014-07-25 16:28:47 +02:00
|
|
|
// Callbacks
|
2014-08-13 11:05:52 +02:00
|
|
|
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::AFTERINIT, $this, 'handleAfterInit');
|
2013-11-10 12:51:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-25 16:28:47 +02:00
|
|
|
* Initialize the necessary database tables
|
2014-05-02 17:50:30 +02:00
|
|
|
*
|
2013-11-10 12:51:41 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function initTables() {
|
2014-08-13 11:05:52 +02:00
|
|
|
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
2014-04-20 19:25:06 +02:00
|
|
|
$settingTableQuery = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_SETTINGS . "` (
|
2014-05-14 00:28:53 +02:00
|
|
|
`index` INT(11) NOT NULL AUTO_INCREMENT,
|
|
|
|
`class` VARCHAR(100) NOT NULL,
|
|
|
|
`setting` VARCHAR(150) NOT NULL,
|
|
|
|
`type` VARCHAR(50) NOT NULL,
|
2017-02-04 11:41:31 +01:00
|
|
|
`value` VARCHAR(150) NOT NULL,
|
2014-05-14 00:28:53 +02:00
|
|
|
`default` VARCHAR(100) NOT NULL,
|
|
|
|
`set` 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-06-22 18:26:01 +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);
|
|
|
|
}
|
2020-05-19 22:07:18 +02:00
|
|
|
|
|
|
|
$mysqli->query("ALTER TABLE `" . self::TABLE_SETTINGS . "` ADD `description` VARCHAR(500) DEFAULT NULL;");
|
|
|
|
if ($mysqli->error) {
|
|
|
|
// If not Duplicate
|
|
|
|
if ($mysqli->errno !== 1060) {
|
|
|
|
trigger_error($mysqli->error, E_USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
2020-08-02 10:32:27 +02:00
|
|
|
|
|
|
|
// Grow the size limit for plugins settings
|
2021-06-30 16:19:31 +02:00
|
|
|
$mysqli->query("ALTER TABLE `" . self::TABLE_SETTINGS . "` MODIFY `value` VARCHAR(1000);");
|
2020-08-02 10:32:27 +02:00
|
|
|
if ($mysqli->error) {
|
|
|
|
// If not Duplicate
|
|
|
|
if ($mysqli->errno !== 1060) {
|
|
|
|
trigger_error($mysqli->error, E_USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
2014-06-22 18:26:01 +02:00
|
|
|
return $result;
|
2013-11-10 12:51:41 +01:00
|
|
|
}
|
|
|
|
|
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-08-13 11:05:52 +02:00
|
|
|
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
2014-05-13 14:15:00 +02:00
|
|
|
$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) {
|
2014-05-18 23:35:39 +02:00
|
|
|
$this->clearStorage();
|
2014-05-13 14:15:00 +02:00
|
|
|
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-18 23:35:39 +02:00
|
|
|
* Clear the Settings Storage
|
|
|
|
*/
|
|
|
|
public function clearStorage() {
|
|
|
|
$this->storedSettings = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
* @see SettingManager::getSettingValueByIndex()
|
|
|
|
*/
|
|
|
|
public function getSettingByIndex($settingIndex, $defaultValue = null) {
|
|
|
|
return $this->getSettingValueByIndex($settingIndex, $defaultValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a Setting Value 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
|
2014-05-18 23:35:39 +02:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getSettingValueByIndex($settingIndex, $defaultValue = null) {
|
|
|
|
$setting = $this->getSettingObjectByIndex($settingIndex);
|
|
|
|
if (!$setting) {
|
|
|
|
return $defaultValue;
|
|
|
|
}
|
|
|
|
return $setting->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a Setting Object by its Index
|
|
|
|
*
|
|
|
|
* @param int $settingIndex
|
2014-05-13 14:15:00 +02:00
|
|
|
* @return Setting
|
2014-05-02 17:50:30 +02:00
|
|
|
*/
|
2014-05-18 23:35:39 +02:00
|
|
|
public function getSettingObjectByIndex($settingIndex) {
|
2014-08-13 11:05:52 +02:00
|
|
|
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
2014-05-13 14:15:00 +02:00
|
|
|
$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) {
|
2014-05-18 23:35:39 +02:00
|
|
|
$result->free();
|
|
|
|
return null;
|
2014-05-02 17:50:30 +02:00
|
|
|
}
|
|
|
|
|
2014-05-13 14:15:00 +02:00
|
|
|
/** @var Setting $setting */
|
2014-05-14 00:28:53 +02:00
|
|
|
$setting = $result->fetch_object(Setting::CLASS_NAME, array(false, null, null));
|
2014-05-18 23:35:39 +02:00
|
|
|
$result->free();
|
|
|
|
|
|
|
|
$this->storeSetting($setting);
|
2013-11-10 15:11:50 +01:00
|
|
|
|
2014-05-13 14:15:00 +02:00
|
|
|
return $setting;
|
2013-11-10 12:51:41 +01:00
|
|
|
}
|
|
|
|
|
2014-05-18 23:35:39 +02:00
|
|
|
/**
|
|
|
|
* Store the given Setting
|
|
|
|
*
|
|
|
|
* @param Setting $setting
|
|
|
|
*/
|
|
|
|
private function storeSetting(Setting $setting) {
|
|
|
|
$this->storedSettings[$setting->class . $setting->setting] = $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
|
|
|
*
|
2020-05-19 22:07:18 +02:00
|
|
|
* @param mixed $object
|
|
|
|
* @param string $settingName
|
|
|
|
* @param mixed $value
|
|
|
|
* @param string|null $description
|
2014-05-18 23:35:39 +02:00
|
|
|
* @return bool
|
2013-11-10 13:23:35 +01:00
|
|
|
*/
|
2020-05-19 22:07:18 +02:00
|
|
|
public function setSetting($object, $settingName, $value, $description = null) {
|
2014-07-04 18:48:39 +02:00
|
|
|
//TODO nowhere used, everywhere saveSettings used, is it depreciated?
|
2014-05-13 22:54:26 +02:00
|
|
|
$setting = $this->getSettingObject($object, $settingName);
|
2014-05-13 14:15:00 +02:00
|
|
|
if ($setting) {
|
|
|
|
$setting->value = $value;
|
2014-05-18 23:35:39 +02:00
|
|
|
$saved = $this->saveSetting($setting);
|
|
|
|
if (!$saved) {
|
|
|
|
return false;
|
2014-05-13 14:15:00 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-05-19 22:07:18 +02:00
|
|
|
$saved = $this->initSetting($object, $settingName, $value, $description);
|
2014-05-18 23:35:39 +02:00
|
|
|
if (!$saved) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$setting = $this->getSettingObject($object, $settingName, $value);
|
2014-01-05 22:51:21 +01:00
|
|
|
}
|
2014-05-13 14:15:00 +02:00
|
|
|
|
2014-05-18 23:35:39 +02:00
|
|
|
$this->storeSetting($setting);
|
2014-05-13 14:15:00 +02:00
|
|
|
|
2014-05-18 23:35:39 +02:00
|
|
|
return true;
|
2014-01-05 22:51:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-12 21:07:54 +01:00
|
|
|
* Get Setting by Name for the given Object
|
2014-05-02 17:50:30 +02:00
|
|
|
*
|
|
|
|
* @param mixed $object
|
2014-02-12 21:07:54 +01:00
|
|
|
* @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
|
|
|
*/
|
2014-05-13 22:54:26 +02: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
|
2014-05-18 23:35:39 +02:00
|
|
|
$storedSetting = $this->getStoredSetting($object, $settingName);
|
|
|
|
if ($storedSetting) {
|
|
|
|
return $storedSetting;
|
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
|
2014-08-13 11:05:52 +02:00
|
|
|
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
2014-05-13 14:15:00 +02:00
|
|
|
$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) {
|
2013-11-10 12:51:41 +01:00
|
|
|
trigger_error($mysqli->error);
|
|
|
|
return null;
|
|
|
|
}
|
2014-05-13 14:15:00 +02:00
|
|
|
if ($result->num_rows <= 0) {
|
2014-05-18 23:35:39 +02:00
|
|
|
$result->free();
|
2014-05-13 14:15:00 +02:00
|
|
|
if ($default === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-05-18 23:35:39 +02:00
|
|
|
$saved = $this->initSetting($object, $settingName, $default);
|
|
|
|
if ($saved) {
|
|
|
|
return $this->getSettingObject($object, $settingName, $default);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2013-11-10 12:51:41 +01:00
|
|
|
}
|
2014-05-02 17:50:30 +02:00
|
|
|
|
2014-05-13 14:15:00 +02:00
|
|
|
/** @var Setting $setting */
|
2014-05-14 00:28:53 +02:00
|
|
|
$setting = $result->fetch_object(Setting::CLASS_NAME, array(false, null, null));
|
2014-05-13 14:15:00 +02:00
|
|
|
$result->free();
|
|
|
|
|
2014-05-18 23:35:39 +02:00
|
|
|
$this->storeSetting($setting);
|
2014-05-13 14:15:00 +02:00
|
|
|
|
2013-11-10 12:51:41 +01:00
|
|
|
return $setting;
|
|
|
|
}
|
2013-11-10 13:23:35 +01:00
|
|
|
|
2014-05-18 23:35:39 +02:00
|
|
|
/**
|
|
|
|
* Retrieve a stored Setting
|
|
|
|
*
|
|
|
|
* @param mixed $settingClass
|
|
|
|
* @param string $settingName
|
|
|
|
* @return Setting
|
|
|
|
*/
|
|
|
|
private function getStoredSetting($settingClass, $settingName) {
|
|
|
|
$settingClass = ClassUtil::getClass($settingClass);
|
|
|
|
if (isset($this->storedSettings[$settingClass . $settingName])) {
|
|
|
|
return $this->storedSettings[$settingClass . $settingName];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2020-05-19 22:07:18 +02:00
|
|
|
* @param mixed $object
|
|
|
|
* @param string $settingName
|
|
|
|
* @param mixed $defaultValue
|
|
|
|
* @param string|null $description
|
2014-05-18 23:35:39 +02:00
|
|
|
* @return bool
|
2013-11-10 13:23:35 +01:00
|
|
|
*/
|
2020-05-19 22:07:18 +02:00
|
|
|
public function initSetting($object, $settingName, $defaultValue, $description = null) {
|
|
|
|
$setting = new Setting($object, $settingName, $defaultValue, $description);
|
2014-05-18 23:35:39 +02:00
|
|
|
return $this->saveSetting($setting, true);
|
2014-05-13 14:15:00 +02:00
|
|
|
}
|
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
|
2014-05-18 23:35:39 +02:00
|
|
|
* @param bool $init
|
2014-05-13 14:15:00 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2014-05-18 23:35:39 +02:00
|
|
|
public function saveSetting(Setting $setting, $init = false) {
|
2014-08-13 11:05:52 +02:00
|
|
|
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
2014-05-18 23:35:39 +02:00
|
|
|
if ($init) {
|
|
|
|
// Init - Keep old value if the default didn't change
|
|
|
|
$valueUpdateString = '`value` = IF(`default` = VALUES(`default`), `value`, VALUES(`default`))';
|
|
|
|
} else {
|
|
|
|
// Set - Update value in any case
|
|
|
|
$valueUpdateString = '`value` = VALUES(`value`)';
|
|
|
|
}
|
2014-05-13 14:15:00 +02:00
|
|
|
$settingQuery = "INSERT INTO `" . self::TABLE_SETTINGS . "` (
|
|
|
|
`class`,
|
|
|
|
`setting`,
|
|
|
|
`type`,
|
2020-05-19 22:07:18 +02:00
|
|
|
`description`,
|
2014-05-13 14:15:00 +02:00
|
|
|
`value`,
|
2014-05-18 21:46:00 +02:00
|
|
|
`default`,
|
|
|
|
`set`
|
2014-05-13 14:15:00 +02:00
|
|
|
) VALUES (
|
2020-05-19 22:07:18 +02:00
|
|
|
?, ?, ?, ?, ?, ?, ?
|
2014-05-13 14:15:00 +02:00
|
|
|
) ON DUPLICATE KEY UPDATE
|
|
|
|
`index` = LAST_INSERT_ID(`index`),
|
|
|
|
`type` = VALUES(`type`),
|
2014-05-18 23:35:39 +02:00
|
|
|
{$valueUpdateString},
|
2014-05-13 14:15:00 +02:00
|
|
|
`default` = VALUES(`default`),
|
2014-05-18 21:46:00 +02:00
|
|
|
`set` = VALUES(`set`),
|
2020-05-19 22:07:18 +02:00
|
|
|
`description` = VALUES(`description`),
|
2014-05-13 14:15:00 +02:00
|
|
|
`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-18 21:46:00 +02:00
|
|
|
$formattedValue = $setting->getFormattedValue();
|
|
|
|
$formattedDefault = $setting->getFormattedDefault();
|
|
|
|
$formattedSet = $setting->getFormattedSet();
|
2020-05-19 22:07:18 +02:00
|
|
|
$settingStatement->bind_param(
|
|
|
|
'sssssss',
|
|
|
|
$setting->class,
|
|
|
|
$setting->setting,
|
|
|
|
$setting->type,
|
|
|
|
$setting->description,
|
|
|
|
$formattedValue,
|
|
|
|
$formattedDefault,
|
|
|
|
$formattedSet
|
|
|
|
);
|
2014-05-13 14:15:00 +02:00
|
|
|
$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-07-04 18:48:39 +02:00
|
|
|
|
|
|
|
// Trigger Settings Changed Callback
|
2014-07-25 16:28:47 +02:00
|
|
|
if (!$init) {
|
2014-08-13 11:05:52 +02:00
|
|
|
$this->maniaControl->getCallbackManager()->triggerCallback(self::CB_SETTING_CHANGED, $setting);
|
2014-07-04 19:58:59 +02:00
|
|
|
}
|
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
|
|
|
|
2014-05-13 22:54:26 +02: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) {
|
2014-05-13 22:54:26 +02:00
|
|
|
$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
|
2013-12-09 13:45:58 +01:00
|
|
|
* @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);
|
|
|
|
}
|
2014-08-13 11:05:52 +02:00
|
|
|
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
2014-05-13 14:15:00 +02:00
|
|
|
$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;
|
|
|
|
}
|
2014-04-30 02:27:17 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-12 21:07:54 +01:00
|
|
|
* Delete a Setting
|
2014-05-02 17:50:30 +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
|
|
|
|
*/
|
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);
|
|
|
|
}
|
|
|
|
|
2014-08-13 11:05:52 +02:00
|
|
|
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
2014-05-13 14:15:00 +02:00
|
|
|
$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
|
|
|
|
2014-04-30 02:27:17 +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
|
|
|
}
|
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-05-02 17:50:30 +02:00
|
|
|
*
|
2014-02-12 21:07:54 +01:00
|
|
|
* @param mixed $object
|
2014-05-27 23:00:39 +02:00
|
|
|
* @return Setting[]
|
2014-01-18 20:05:50 +01:00
|
|
|
*/
|
2014-02-12 21:07:54 +01:00
|
|
|
public function getSettingsByClass($object) {
|
2014-05-13 14:15:00 +02:00
|
|
|
$className = ClassUtil::getClass($object);
|
2014-08-13 11:05:52 +02:00
|
|
|
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
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-14 00:28:53 +02:00
|
|
|
while ($setting = $result->fetch_object(Setting::CLASS_NAME, array(false, null, null))) {
|
2014-01-18 20:05:50 +01:00
|
|
|
$settings[$setting->index] = $setting;
|
|
|
|
}
|
|
|
|
$result->free();
|
|
|
|
return $settings;
|
|
|
|
}
|
|
|
|
|
2013-11-28 04:48:04 +01:00
|
|
|
/**
|
2014-05-13 14:15:00 +02:00
|
|
|
* Get all Settings
|
2014-05-02 17:50:30 +02:00
|
|
|
*
|
2014-05-27 23:00:39 +02:00
|
|
|
* @return Setting[]
|
2013-11-28 04:48:04 +01:00
|
|
|
*/
|
|
|
|
public function getSettings() {
|
2014-08-13 11:05:52 +02:00
|
|
|
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
2014-05-02 17:50:30 +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-05-14 00:28:53 +02:00
|
|
|
while ($setting = $result->fetch_object(Setting::CLASS_NAME, array(false, null, null))) {
|
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-05-27 23:00:39 +02:00
|
|
|
* @return string[]
|
2014-04-30 03:45:54 +02:00
|
|
|
*/
|
2014-04-30 05:27:23 +02:00
|
|
|
public function getSettingClasses($hidePluginClasses = false) {
|
2014-08-13 11:05:52 +02:00
|
|
|
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
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;
|
|
|
|
}
|
2013-11-10 12:51:41 +01:00
|
|
|
}
|