Initial javascript API for handling user configuration

There ar now configGet/configSet functions which can be used to
manipulate user settings.

Issue #13466

Signed-off-by: Michal Čihař <michal@cihar.com>
This commit is contained in:
Michal Čihař 2017-08-02 09:54:22 +02:00
parent a5afef2208
commit 241f93c4be
2 changed files with 105 additions and 3 deletions

View File

@ -9,6 +9,9 @@
use PhpMyAdmin\Response;
use PhpMyAdmin\Util;
use PhpMyAdmin\Core;
$_GET['ajax_request'] = 'true';
require_once 'libraries/common.inc.php';
$response = Response::getInstance();
@ -22,14 +25,26 @@ switch ($_POST['type']) {
$response->addJSON('databases', $GLOBALS['dblist']->databases);
break;
case 'list-tables':
Util::checkParameters(array('db'));
Util::checkParameters(array('db'), true);
$response->addJSON('tables', $GLOBALS['dbi']->getTables($_REQUEST['db']));
break;
case 'list-columns':
Util::checkParameters(array('db', 'table'));
Util::checkParameters(array('db', 'table'), true);
$response->addJSON('columns', $GLOBALS['dbi']->getColumnNames($_REQUEST['db'], $_REQUEST['table']));
break;
case 'config-get':
Util::checkParameters(array('key'), true);
$response->addJSON('value', $GLOBALS['PMA_Config']->get($_REQUEST['key']));
break;
case 'config-set':
Util::checkParameters(array('key', 'value'), true);
$result = $GLOBALS['PMA_Config']->setUserValue(null, $_REQUEST['key'], json_decode($_REQUEST['value']));
if ($result !== true) {
$response = Response::getInstance();
$response->setRequestStatus(false);
$response->addJSON('message', $result);
}
break;
default:
Core::fatalError(__('Bad type!'));
}

View File

@ -4992,3 +4992,90 @@ function PMA_getImage(image, alternate, attributes) {
return retval;
}
/**
* Sets a configuration value.
*
* A configuration value may be set in both browser's local storage and
* remotely in server's configuration table.
*
* If the `only_local` argument is `true`, the value is store is stored only in
* browser's local storage and may be lost if the user resets his browser's
* settings.
*
* NOTE: Depending on server's configuration, the configuration table may be or
* not persistent.
*
* @param {string} key Configuration key.
* @param {object} value Configuration value.
* @param {boolean} only_local Configuration type.
*/
function configSet(key, value, only_local=false)
{
let serialized = JSON.stringify(value);
localStorage.setItem(key, serialized);
$.ajax({
url: "ajax.php",
type: "POST",
dataType: "json",
data: {
key: key,
type: "config-set",
value: serialized,
},
success: function (data) {
// Updating value in local storage.
if (! data.success) {
PMA_ajaxShowMessage(data.message);
}
// Eventually, call callback.
}
});
}
/**
* Gets a configuration value. A configuration value will be searched in
* browser's local storage first and if not found, a call to the server will be
* made.
*
* If value should not be cached and the up-to-date configuration value from
* right from the server is required, the third parameter should be `false`.
*
* @param {string} key Configuration key.
* @param {boolean} only_local Configuration type.
*
* @return {object} Configuration value.
*/
function configGet(key, cached=true)
{
let value = localStorage.getItem(key);
if (cached && value !== undefined && value !== null) {
return JSON.parse(value);
}
// Result not found in local storage or ignored.
// Hitting the server.
$.ajax({
// TODO: This is ugly, but usually when a configuration is needed,
// processing cannot continue until that value is found.
// Another solution is to provide a callback as a parameter.
async: false,
url: "ajax.php",
type: "POST",
dataType: "json",
data: {
type: "config-get",
key: key
},
success: function (data) {
// Updating value in local storage.
if (data.success) {
localStorage.setItem(key, JSON.stringify(data.value));
} else {
PMA_ajaxShowMessage(data.message);
}
// Eventually, call callback.
}
});
return JSON.parse(localStorage.getItem(key));
}