Unverified Commit 4ac3db2a by Scott Committed by GitHub

Merge pull request #672 from pupi1985/patch-118

Turn all remaining AJAX calls in admin into JSON
parents 1b3b746f 3699ec82
......@@ -56,28 +56,21 @@ function qa_recalc_update(elem, state, noteid)
{
if (state) {
var recalcCode = elem.form.elements.code_recalc ? elem.form.elements.code_recalc.value : elem.form.elements.code.value;
qa_ajax_post(
'recalc',
{state: state, code: recalcCode},
function(lines) {
if (lines[0] == '1') {
if (lines[2])
document.getElementById(noteid).innerHTML = lines[2];
if (elem.qa_recalc_stopped)
qa_recalc_cleanup(elem);
else
qa_recalc_update(elem, lines[1], noteid);
} else if (lines[0] == '0') {
document.getElementById(noteid).innerHTML = lines[1];
qa_recalc_cleanup(elem);
function (response) {
if (response.message) {
document.getElementById(noteid).innerHTML = response.message;
}
} else {
qa_ajax_error();
if (elem.qa_recalc_stopped) {
qa_recalc_cleanup(elem);
} else {
qa_recalc_update(elem, response.state, noteid);
}
}
}, 1
);
} else {
qa_recalc_cleanup(elem);
......@@ -93,22 +86,19 @@ function qa_recalc_cleanup(elem)
function qa_mailing_start(noteid, pauseid)
{
qa_ajax_post('mailing', {},
function(lines) {
if (lines[0] == '1') {
document.getElementById(noteid).innerHTML = lines[1];
window.setTimeout(function() {
qa_ajax_post(
'mailing',
{},
function (response) {
document.getElementById(noteid).innerHTML = response.message;
if (response.continue) {
window.setTimeout(function () {
qa_mailing_start(noteid, pauseid);
}, 1); // don't recurse
} else if (lines[0] == '0') {
document.getElementById(noteid).innerHTML = lines[1];
document.getElementById(pauseid).style.display = 'none';
} else {
qa_ajax_error();
document.getElementById(pauseid).style.display = 'none';
}
}
}, 1
);
}
......@@ -159,13 +149,18 @@ function qa_admin_click(target)
function qa_version_check(uri, version, elem, isCore)
{
var params = {uri: uri, version: version, isCore: isCore};
qa_ajax_post(
'version',
{uri: uri, version: version, isCore: isCore},
function (response) {
if (response.result === 'error') {
alert(response.error.message);
qa_ajax_post('version', params,
function(lines) {
if (lines[0] == '1')
document.getElementById(elem).innerHTML = lines[1];
}
return;
}
document.getElementById(elem).innerHTML = response.html;
}, 1
);
}
......
......@@ -152,16 +152,16 @@ function qa_favorite_click(elem)
return false;
}
function qa_ajax_post(operation, params, callback, version)
function qa_ajax_post(operation, params, callback, apiVersion)
{
if (typeof(version) === 'undefined') {
version = 0;
if (typeof(apiVersion) === 'undefined') {
apiVersion = 0;
}
$.extend(params, {qa: 'ajax', qa_operation: operation, qa_root: qa_root, qa_request: qa_request, version: version});
$.extend(params, {qa: 'ajax', qa_operation: operation, qa_root: qa_root, qa_request: qa_request, apiVersion: apiVersion});
$.post(qa_root, params, function (response) {
if (version === 0) {
if (apiVersion === 0) {
var header = 'QA_AJAX_RESPONSE';
var headerpos = response.indexOf(header);
......@@ -173,7 +173,7 @@ function qa_ajax_post(operation, params, callback, version)
callback(response);
}
},
version === 0 ? 'text' : 'json'
apiVersion === 0 ? 'text' : 'json'
).fail(function (jqXHR) {
if (jqXHR.readyState > 0)
qa_ajax_error();
......
......@@ -30,18 +30,24 @@ if (qa_get_logged_in_level() >= QA_USER_LEVEL_ADMIN) {
qa_mailing_perform_step();
if ($starttime == time())
if ($starttime == time()) {
sleep(1); // make sure at least one second has passed
}
$message = qa_mailing_progress_message();
if (isset($message))
if (isset($message)) {
$continue = true;
else
} else {
$message = qa_lang('admin/mailing_complete');
} else
}
} else {
$message = qa_lang('admin/no_privileges');
}
$response = array(
'continue' => $continue,
'message' => qa_html($message),
);
echo "QA_AJAX_RESPONSE\n" . (int)$continue . "\n" . qa_html($message);
echo json_encode($response);
......@@ -26,7 +26,6 @@ if (qa_get_logged_in_level() >= QA_USER_LEVEL_ADMIN) {
if (!qa_check_form_security_code('admin/recalc', qa_post_text('code'))) {
$state = '';
$message = qa_lang('misc/form_security_reload');
} else {
$recalc = new \Q2A\Recalc\RecalcMain(qa_post_text('state'));
$stoptime = time() + 3;
......@@ -37,13 +36,15 @@ if (qa_get_logged_in_level() >= QA_USER_LEVEL_ADMIN) {
$message = $recalc->getMessage();
$state = $recalc->getState();
}
} else {
$state = '';
$message = qa_lang('admin/no_privileges');
}
$response = array(
'state' => $state,
'message' => qa_html($message),
);
echo "QA_AJAX_RESPONSE\n1\n" . $state . "\n" . qa_html($message);
echo json_encode($response);
......@@ -23,28 +23,35 @@ require_once QA_INCLUDE_DIR . 'app/admin.php';
require_once QA_INCLUDE_DIR . 'app/users.php';
if (qa_get_logged_in_level() < QA_USER_LEVEL_ADMIN) {
echo "QA_AJAX_RESPONSE\n0\n" . qa_lang_html('admin/no_privileges');
$response = array(
'result' => 'error',
'error' => array(
'message' => qa_lang_html('admin/no_privileges'),
),
);
echo json_encode($response);
return;
}
$uri = qa_post_text('uri');
$version = qa_post_text('version');
$isCore = qa_post_text('isCore') === "true";
$isCore = qa_post_text('isCore') === 'true';
if ($isCore) {
$contents = qa_retrieve_url($uri);
if (strlen($contents) > 0) {
if (qa_qa_version_below($contents)) {
$response =
$versionResponse =
'<a href="https://github.com/q2a/question2answer/releases" style="color:#d00;">' .
qa_lang_html_sub('admin/version_get_x', qa_html('v' . $contents)) .
'</a>';
} else {
$response = qa_html($contents); // Output the current version number
$versionResponse = qa_html($contents); // Output the current version number
}
} else {
$response = qa_lang_html('admin/version_latest_unknown');
$versionResponse = qa_lang_html('admin/version_latest_unknown');
}
} else {
$metadataUtil = new Q2A_Util_Metadata();
......@@ -53,28 +60,33 @@ if ($isCore) {
if (strlen(@$metadata['version']) > 0) {
if (strcmp($metadata['version'], $version)) {
if (qa_qa_version_below(@$metadata['min_q2a'])) {
$response = strtr(qa_lang_html('admin/version_requires_q2a'), array(
$versionResponse = strtr(qa_lang_html('admin/version_requires_q2a'), array(
'^1' => qa_html('v' . $metadata['version']),
'^2' => qa_html($metadata['min_q2a']),
));
} elseif (qa_php_version_below(@$metadata['min_php'])) {
$response = strtr(qa_lang_html('admin/version_requires_php'), array(
$versionResponse = strtr(qa_lang_html('admin/version_requires_php'), array(
'^1' => qa_html('v' . $metadata['version']),
'^2' => qa_html($metadata['min_php']),
));
} else {
$response = qa_lang_html_sub('admin/version_get_x', qa_html('v' . $metadata['version']));
$versionResponse = qa_lang_html_sub('admin/version_get_x', qa_html('v' . $metadata['version']));
if (strlen(@$metadata['uri'])) {
$response = '<a href="' . qa_html($metadata['uri']) . '" style="color:#d00;">' . $response . '</a>';
$versionResponse = '<a href="' . qa_html($metadata['uri']) . '" style="color:#d00;">' . $versionResponse . '</a>';
}
}
} else {
$response = qa_lang_html('admin/version_latest');
$versionResponse = qa_lang_html('admin/version_latest');
}
} else {
$response = qa_lang_html('admin/version_latest_unknown');
$versionResponse = qa_lang_html('admin/version_latest_unknown');
}
}
echo "QA_AJAX_RESPONSE\n1\n" . $response;
$response = array(
'result' => 'success',
'html' => $versionResponse,
);
echo json_encode($response);
......@@ -1621,7 +1621,7 @@ function qa_db_upgrade_tables()
while ($recalc->performStep() && (time() < $stoptime))
;
qa_db_upgrade_progress($recalc->getMmessage());
qa_db_upgrade_progress($recalc->getMessage());
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment