mirror of
https://github.com/slawkens/myaac.git
synced 2025-04-29 18:59:21 +02:00
Add new possibility: to deny saving setting if condition is not met
This commit is contained in:
parent
3236f1aebb
commit
20dd49b1c5
@ -25,4 +25,10 @@ $settings = Settings::getInstance();
|
||||
|
||||
$settings->save($_REQUEST['plugin'], $_POST['settings']);
|
||||
|
||||
$errors = $settings->getErrors();
|
||||
if (count($errors) > 0) {
|
||||
http_response_code(500);
|
||||
die(implode('<br/>', $errors));
|
||||
}
|
||||
|
||||
echo 'Saved at ' . date('H:i');
|
||||
|
@ -15,6 +15,7 @@ class Settings implements ArrayAccess
|
||||
private $settingsDatabase = [];
|
||||
private $cache = [];
|
||||
private $valuesAsked = [];
|
||||
private $errors = [];
|
||||
|
||||
/**
|
||||
* @return Settings
|
||||
@ -67,12 +68,19 @@ class Settings implements ArrayAccess
|
||||
}
|
||||
}
|
||||
|
||||
$this->errors = [];
|
||||
$db->query('DELETE FROM `' . TABLE_PREFIX . 'settings` WHERE `name` = ' . $db->quote($pluginName) . ';');
|
||||
foreach ($values as $key => $value) {
|
||||
$errorMessage = '';
|
||||
if (isset($settings['settings'][$key]['callbacks']['beforeSave']) && !$settings['settings'][$key]['callbacks']['beforeSave']($key, $value, $errorMessage)) {
|
||||
$this->errors[] = $errorMessage;
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
$db->insert(TABLE_PREFIX . 'settings', ['name' => $pluginName, 'key' => $key, 'value' => $value]);
|
||||
} catch (PDOException $error) {
|
||||
warning('Error while saving setting (' . $pluginName . ' - ' . $key . '): ' . $error->getMessage());
|
||||
$this->errors[] = 'Error while saving setting (' . $pluginName . ' - ' . $key . '): ' . $error->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
@ -587,4 +595,8 @@ class Settings implements ArrayAccess
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getErrors() {
|
||||
return $this->errors;
|
||||
}
|
||||
}
|
||||
|
@ -1301,6 +1301,16 @@ Sent by MyAAC,<br/>
|
||||
'desc' => 'What to give to player after donation - what column in accounts table to use.',
|
||||
'options' => ['premium_points' => 'Premium Points', 'coins' => 'Coins'],
|
||||
'default' => 'premium_points',
|
||||
'callbacks' => [
|
||||
'beforeSave' => function($key, $value, &$errorMessage) {
|
||||
global $db;
|
||||
if ($value == 'coins' && !$db->hasColumn('accounts', 'coins')) {
|
||||
$errorMessage = "Shop: Donate Column: Cannot set column to coins, because it doesn't exist in database.";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
]
|
||||
],
|
||||
'account_generate_new_reckey' => [
|
||||
'name' => 'Allow Generate New Key',
|
||||
|
@ -80,11 +80,20 @@
|
||||
Toastify({
|
||||
position: 'center',
|
||||
text: response,
|
||||
duration: 3000
|
||||
duration: 3000,
|
||||
escapeMarkup: false,
|
||||
}).showToast();
|
||||
},
|
||||
error : function(response) {
|
||||
alert(response.responseText);
|
||||
Toastify({
|
||||
position: 'center',
|
||||
text: response.responseText,
|
||||
duration: 3000,
|
||||
style: {
|
||||
background: 'red',
|
||||
},
|
||||
escapeMarkup: false,
|
||||
}).showToast();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user