fa7ea9f4f3
* ServerUIPropertiesMenu for Configurator to edit builtin UIProperties of MP * fixed unregister-functions of CallbackManager * Reducing menuItemHeight in Configurator to avoid overlapping of the menu items * Fully rebuild the admins menu after a player rights changed * Added function to FileUtil to improve realpath, so symbolic links can be resolved * Fixed indentation * Update FileUtil.php Fixed error in case of an absolute path on Unix-like systems. * New CallQueue which runs once inbetween the MC-loops * Added important queued call to be executed earlier * ErrorMethod made optional, as in some cases, there might be nothing to fail * Clean up repository from unnecessary files * Added easy installation script for DB on Unix-like systems * Replaced deprecated is_real by is_float * Add milliseconds with dot instead of double colon * Resolved deprecated curly braces error * gitignore all hidden files (except git and gitignore) * Update MC-update-check-interval, so that a restart is not required * Update gitignore to not ignore changes in MCTeam-Plugins * Update gitignore again * And another try * fixed MasterAdmin //delrec, added personal /delrec-command with permission-setting * Increase version number of local records plugin * Add Permission to delete any record * Reworked notifications of locals, removed private only, added private at rank * Fix formatting * Allow AuthenticationManager to store Plugin Permissions * Method to check, if a named function call is already queued * Added command disable feature * Reset timer if DeltaTime updated, so it does not try to catch up missed timings * Added private notification setting * To reduce load of multiple records (especially in rounds), queue call chat notifications * Added internal function to plugin manager to return plugin menu * restore .idea codestyle files * Update MC-Version number to 0.250
200 lines
4.4 KiB
PHP
200 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace ManiaControl\Settings;
|
|
|
|
use ManiaControl\General\UsageInformationAble;
|
|
use ManiaControl\General\UsageInformationTrait;
|
|
use ManiaControl\Utils\ClassUtil;
|
|
|
|
/**
|
|
* ManiaControl Setting Model Class
|
|
*
|
|
* @author ManiaControl Team <mail@maniacontrol.com>
|
|
* @copyright 2014-2020 ManiaControl Team
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
|
*/
|
|
class Setting implements UsageInformationAble {
|
|
use UsageInformationTrait;
|
|
|
|
/*
|
|
* Constants
|
|
*/
|
|
const CLASS_NAME = __CLASS__;
|
|
const TYPE_STRING = 'string';
|
|
const TYPE_INT = 'int';
|
|
const TYPE_REAL = 'real';
|
|
const TYPE_BOOL = 'bool';
|
|
const TYPE_SET = 'set';
|
|
const VALUE_DELIMITER = ';;';
|
|
|
|
/*
|
|
* Public properties
|
|
*/
|
|
public $index = null;
|
|
public $class = null;
|
|
public $setting = null;
|
|
public $type = null;
|
|
public $value = null;
|
|
public $default = null;
|
|
public $set = null;
|
|
public $fetchTime = null;
|
|
|
|
/**
|
|
* Construct a new setting instance
|
|
*
|
|
* @param mixed $object
|
|
* @param string $settingName
|
|
* @param mixed $defaultValue
|
|
*/
|
|
public function __construct($object, $settingName, $defaultValue) {
|
|
if ($object === false) {
|
|
// Fetched from Database
|
|
$this->value = $this->castValue($this->value);
|
|
$this->default = $this->castValue($this->default);
|
|
if ($this->set) {
|
|
$this->set = $this->castValue($this->set, $this->type);
|
|
}
|
|
$this->fetchTime = time();
|
|
} else {
|
|
// Created by Values
|
|
$this->class = ClassUtil::getClass($object);
|
|
$this->setting = (string) $settingName;
|
|
$this->type = self::getValueType($defaultValue);
|
|
if ($this->type === self::TYPE_SET) {
|
|
// Save Set and use first Value as Default
|
|
$this->set = $defaultValue;
|
|
$this->value = reset($defaultValue);
|
|
} else {
|
|
$this->value = $defaultValue;
|
|
}
|
|
$this->default = $this->value;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Cast the Value based on the Setting Type
|
|
*
|
|
* @param mixed $value
|
|
* @param string $type
|
|
* @return mixed
|
|
*/
|
|
private static function castValue($value, $type = null) {
|
|
if ($type === null) {
|
|
$type = self::getValueType($value);
|
|
}
|
|
if ($type === self::TYPE_INT) {
|
|
return (int) $value;
|
|
}
|
|
if ($type === self::TYPE_REAL) {
|
|
return (float) $value;
|
|
}
|
|
if ($type === self::TYPE_BOOL) {
|
|
return (bool) $value;
|
|
}
|
|
if ($type === self::TYPE_STRING) {
|
|
return (string) $value;
|
|
}
|
|
if ($type === self::TYPE_SET) {
|
|
return explode(self::VALUE_DELIMITER, $value);
|
|
}
|
|
trigger_error("Unsupported Setting Value Type: '" . print_r($type, true) . "'!");
|
|
return $value;
|
|
}
|
|
|
|
/**
|
|
* Get Type of a Value Parameter
|
|
*
|
|
* @param mixed $value
|
|
* @return string
|
|
*/
|
|
private static function getValueType($value) {
|
|
if (is_int($value)) {
|
|
return self::TYPE_INT;
|
|
}
|
|
if (is_float($value)) {
|
|
return self::TYPE_REAL;
|
|
}
|
|
if (is_bool($value)) {
|
|
return self::TYPE_BOOL;
|
|
}
|
|
if (is_string($value)) {
|
|
return self::TYPE_STRING;
|
|
}
|
|
if (is_array($value)) {
|
|
return self::TYPE_SET;
|
|
}
|
|
trigger_error("Unsupported Setting Value Type: '" . print_r($value, true) . "'!");
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Get whether the Setting has been persisted at some point
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function isPersisted() {
|
|
return ($this->index > 0);
|
|
}
|
|
|
|
/**
|
|
* Get the Formatted Value of the Setting
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getFormattedValue() {
|
|
return self::formatValue($this->value);
|
|
}
|
|
|
|
/**
|
|
* Format the given Value based on the Type
|
|
*
|
|
* @param mixed $value
|
|
* @param string $type
|
|
* @return string
|
|
*/
|
|
private static function formatValue($value, $type = null) {
|
|
if ($type === null) {
|
|
$type = self::getValueType($value);
|
|
}
|
|
if ($type === self::TYPE_BOOL) {
|
|
return ($value ? 1 : 0);
|
|
}
|
|
if ($type === self::TYPE_SET) {
|
|
return implode(self::VALUE_DELIMITER, $value);
|
|
}
|
|
return $value;
|
|
}
|
|
|
|
/**
|
|
* Get the Formatted Default of the Setting
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getFormattedDefault() {
|
|
return self::formatValue($this->default);
|
|
}
|
|
|
|
/**
|
|
* Get the Formatted Set of the Setting
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getFormattedSet() {
|
|
if ($this->type === self::TYPE_SET) {
|
|
return self::formatValue($this->set);
|
|
}
|
|
return '';
|
|
}
|
|
|
|
/**
|
|
* Check if the Settings belongs to the given Class
|
|
*
|
|
* @param mixed $object
|
|
* @return bool
|
|
*/
|
|
public function belongsToClass($object) {
|
|
$className = ClassUtil::getClass($object);
|
|
return ($className === $this->class);
|
|
}
|
|
}
|