add setting priority

This commit is contained in:
Beu 2022-03-17 21:19:31 +01:00
parent d5faaf57ff
commit 8a46edfde6
2 changed files with 24 additions and 9 deletions

View File

@ -39,6 +39,7 @@ class Setting implements UsageInformationAble {
public $set = null; public $set = null;
public $fetchTime = null; public $fetchTime = null;
public $description = null; public $description = null;
public $priority = null;
/** /**
* Construct a new setting instance * Construct a new setting instance
@ -47,8 +48,9 @@ class Setting implements UsageInformationAble {
* @param string $settingName * @param string $settingName
* @param mixed $defaultValue * @param mixed $defaultValue
* @param string|null $description * @param string|null $description
* @param int $priority
*/ */
public function __construct($object, $settingName, $defaultValue, $description = null) { public function __construct($object, $settingName, $defaultValue, $description = null, $priority = 100) {
if ($object === false) { if ($object === false) {
// Fetched from Database // Fetched from Database
$this->value = $this->castValue($this->value); $this->value = $this->castValue($this->value);
@ -71,6 +73,7 @@ class Setting implements UsageInformationAble {
} }
$this->default = $this->value; $this->default = $this->value;
$this->description = $description; $this->description = $description;
$this->priority = $priority;
} }
} }

View File

@ -87,6 +87,15 @@ class SettingManager implements CallbackListener, UsageInformationAble {
trigger_error($mysqli->error, E_USER_ERROR); trigger_error($mysqli->error, E_USER_ERROR);
} }
} }
// Add priority value
$mysqli->query("ALTER TABLE `" . self::TABLE_SETTINGS . "` ADD `priority` INT(5) DEFAULT 100;");
if ($mysqli->error) {
// If not Duplicate
if ($mysqli->errno !== 1060) {
trigger_error($mysqli->error, E_USER_ERROR);
}
}
return $result; return $result;
} }
@ -290,8 +299,8 @@ class SettingManager implements CallbackListener, UsageInformationAble {
* @param string|null $description * @param string|null $description
* @return bool * @return bool
*/ */
public function initSetting($object, $settingName, $defaultValue, $description = null) { public function initSetting($object, $settingName, $defaultValue, $description = null, $priority = 100) {
$setting = new Setting($object, $settingName, $defaultValue, $description); $setting = new Setting($object, $settingName, $defaultValue, $description, $priority);
return $this->saveSetting($setting, true); return $this->saveSetting($setting, true);
} }
@ -318,9 +327,10 @@ class SettingManager implements CallbackListener, UsageInformationAble {
`description`, `description`,
`value`, `value`,
`default`, `default`,
`set` `set`,
`priority`
) VALUES ( ) VALUES (
?, ?, ?, ?, ?, ?, ? ?, ?, ?, ?, ?, ?, ?, ?
) ON DUPLICATE KEY UPDATE ) ON DUPLICATE KEY UPDATE
`index` = LAST_INSERT_ID(`index`), `index` = LAST_INSERT_ID(`index`),
`type` = VALUES(`type`), `type` = VALUES(`type`),
@ -328,6 +338,7 @@ class SettingManager implements CallbackListener, UsageInformationAble {
`default` = VALUES(`default`), `default` = VALUES(`default`),
`set` = VALUES(`set`), `set` = VALUES(`set`),
`description` = VALUES(`description`), `description` = VALUES(`description`),
`priority` = VALUES(`priority`),
`changed` = NOW();"; `changed` = NOW();";
$settingStatement = $mysqli->prepare($settingQuery); $settingStatement = $mysqli->prepare($settingQuery);
if ($mysqli->error) { if ($mysqli->error) {
@ -338,14 +349,15 @@ class SettingManager implements CallbackListener, UsageInformationAble {
$formattedDefault = $setting->getFormattedDefault(); $formattedDefault = $setting->getFormattedDefault();
$formattedSet = $setting->getFormattedSet(); $formattedSet = $setting->getFormattedSet();
$settingStatement->bind_param( $settingStatement->bind_param(
'sssssss', 'sssssssi',
$setting->class, $setting->class,
$setting->setting, $setting->setting,
$setting->type, $setting->type,
$setting->description, $setting->description,
$formattedValue, $formattedValue,
$formattedDefault, $formattedDefault,
$formattedSet $formattedSet,
$setting->priority
); );
$settingStatement->execute(); $settingStatement->execute();
if ($settingStatement->error) { if ($settingStatement->error) {
@ -459,7 +471,7 @@ class SettingManager implements CallbackListener, UsageInformationAble {
$mysqli = $this->maniaControl->getDatabase()->getMysqli(); $mysqli = $this->maniaControl->getDatabase()->getMysqli();
$query = "SELECT * FROM `" . self::TABLE_SETTINGS . "` $query = "SELECT * FROM `" . self::TABLE_SETTINGS . "`
WHERE `class` = '" . $mysqli->escape_string($className) . "' WHERE `class` = '" . $mysqli->escape_string($className) . "'
ORDER BY `setting` ASC;"; ORDER BY `priority` ASC, `setting` ASC;";
$result = $mysqli->query($query); $result = $mysqli->query($query);
if ($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error); trigger_error($mysqli->error);
@ -481,7 +493,7 @@ class SettingManager implements CallbackListener, UsageInformationAble {
public function getSettings() { public function getSettings() {
$mysqli = $this->maniaControl->getDatabase()->getMysqli(); $mysqli = $this->maniaControl->getDatabase()->getMysqli();
$query = "SELECT * FROM `" . self::TABLE_SETTINGS . "` $query = "SELECT * FROM `" . self::TABLE_SETTINGS . "`
ORDER BY `class` ASC, `setting` ASC;"; ORDER BY `class` ASC, `priority` ASC, `setting` ASC;";
$result = $mysqli->query($query); $result = $mysqli->query($query);
if ($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error); trigger_error($mysqli->error);