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) ...@@ -56,28 +56,21 @@ function qa_recalc_update(elem, state, noteid)
{ {
if (state) { if (state) {
var recalcCode = elem.form.elements.code_recalc ? elem.form.elements.code_recalc.value : elem.form.elements.code.value; var recalcCode = elem.form.elements.code_recalc ? elem.form.elements.code_recalc.value : elem.form.elements.code.value;
qa_ajax_post( qa_ajax_post(
'recalc', 'recalc',
{state: state, code: recalcCode}, {state: state, code: recalcCode},
function(lines) { function (response) {
if (lines[0] == '1') { if (response.message) {
if (lines[2]) document.getElementById(noteid).innerHTML = response.message;
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);
} else { if (elem.qa_recalc_stopped) {
qa_ajax_error();
qa_recalc_cleanup(elem); qa_recalc_cleanup(elem);
} else {
qa_recalc_update(elem, response.state, noteid);
} }
} }, 1
); );
} else { } else {
qa_recalc_cleanup(elem); qa_recalc_cleanup(elem);
...@@ -93,22 +86,19 @@ function qa_recalc_cleanup(elem) ...@@ -93,22 +86,19 @@ function qa_recalc_cleanup(elem)
function qa_mailing_start(noteid, pauseid) function qa_mailing_start(noteid, pauseid)
{ {
qa_ajax_post('mailing', {}, qa_ajax_post(
function(lines) { 'mailing',
if (lines[0] == '1') { {},
document.getElementById(noteid).innerHTML = lines[1]; function (response) {
window.setTimeout(function() { document.getElementById(noteid).innerHTML = response.message;
if (response.continue) {
window.setTimeout(function () {
qa_mailing_start(noteid, pauseid); qa_mailing_start(noteid, pauseid);
}, 1); // don't recurse }, 1); // don't recurse
} else if (lines[0] == '0') {
document.getElementById(noteid).innerHTML = lines[1];
document.getElementById(pauseid).style.display = 'none';
} else { } else {
qa_ajax_error(); document.getElementById(pauseid).style.display = 'none';
} }
} }, 1
); );
} }
...@@ -159,13 +149,18 @@ function qa_admin_click(target) ...@@ -159,13 +149,18 @@ function qa_admin_click(target)
function qa_version_check(uri, version, elem, isCore) 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, return;
function(lines) { }
if (lines[0] == '1')
document.getElementById(elem).innerHTML = lines[1]; document.getElementById(elem).innerHTML = response.html;
} }, 1
); );
} }
......
...@@ -152,16 +152,16 @@ function qa_favorite_click(elem) ...@@ -152,16 +152,16 @@ function qa_favorite_click(elem)
return false; return false;
} }
function qa_ajax_post(operation, params, callback, version) function qa_ajax_post(operation, params, callback, apiVersion)
{ {
if (typeof(version) === 'undefined') { if (typeof(apiVersion) === 'undefined') {
version = 0; 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) { $.post(qa_root, params, function (response) {
if (version === 0) { if (apiVersion === 0) {
var header = 'QA_AJAX_RESPONSE'; var header = 'QA_AJAX_RESPONSE';
var headerpos = response.indexOf(header); var headerpos = response.indexOf(header);
...@@ -173,7 +173,7 @@ function qa_ajax_post(operation, params, callback, version) ...@@ -173,7 +173,7 @@ function qa_ajax_post(operation, params, callback, version)
callback(response); callback(response);
} }
}, },
version === 0 ? 'text' : 'json' apiVersion === 0 ? 'text' : 'json'
).fail(function (jqXHR) { ).fail(function (jqXHR) {
if (jqXHR.readyState > 0) if (jqXHR.readyState > 0)
qa_ajax_error(); qa_ajax_error();
......
...@@ -30,18 +30,24 @@ if (qa_get_logged_in_level() >= QA_USER_LEVEL_ADMIN) { ...@@ -30,18 +30,24 @@ if (qa_get_logged_in_level() >= QA_USER_LEVEL_ADMIN) {
qa_mailing_perform_step(); qa_mailing_perform_step();
if ($starttime == time()) if ($starttime == time()) {
sleep(1); // make sure at least one second has passed sleep(1); // make sure at least one second has passed
}
$message = qa_mailing_progress_message(); $message = qa_mailing_progress_message();
if (isset($message)) if (isset($message)) {
$continue = true; $continue = true;
else } else {
$message = qa_lang('admin/mailing_complete'); $message = qa_lang('admin/mailing_complete');
}
} else } else {
$message = qa_lang('admin/no_privileges'); $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) { ...@@ -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'))) { if (!qa_check_form_security_code('admin/recalc', qa_post_text('code'))) {
$state = ''; $state = '';
$message = qa_lang('misc/form_security_reload'); $message = qa_lang('misc/form_security_reload');
} else { } else {
$recalc = new \Q2A\Recalc\RecalcMain(qa_post_text('state')); $recalc = new \Q2A\Recalc\RecalcMain(qa_post_text('state'));
$stoptime = time() + 3; $stoptime = time() + 3;
...@@ -37,13 +36,15 @@ if (qa_get_logged_in_level() >= QA_USER_LEVEL_ADMIN) { ...@@ -37,13 +36,15 @@ if (qa_get_logged_in_level() >= QA_USER_LEVEL_ADMIN) {
$message = $recalc->getMessage(); $message = $recalc->getMessage();
$state = $recalc->getState(); $state = $recalc->getState();
} }
} else { } else {
$state = ''; $state = '';
$message = qa_lang('admin/no_privileges'); $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'; ...@@ -23,28 +23,35 @@ require_once QA_INCLUDE_DIR . 'app/admin.php';
require_once QA_INCLUDE_DIR . 'app/users.php'; require_once QA_INCLUDE_DIR . 'app/users.php';
if (qa_get_logged_in_level() < QA_USER_LEVEL_ADMIN) { 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; return;
} }
$uri = qa_post_text('uri'); $uri = qa_post_text('uri');
$version = qa_post_text('version'); $version = qa_post_text('version');
$isCore = qa_post_text('isCore') === "true"; $isCore = qa_post_text('isCore') === 'true';
if ($isCore) { if ($isCore) {
$contents = qa_retrieve_url($uri); $contents = qa_retrieve_url($uri);
if (strlen($contents) > 0) { if (strlen($contents) > 0) {
if (qa_qa_version_below($contents)) { if (qa_qa_version_below($contents)) {
$response = $versionResponse =
'<a href="https://github.com/q2a/question2answer/releases" style="color:#d00;">' . '<a href="https://github.com/q2a/question2answer/releases" style="color:#d00;">' .
qa_lang_html_sub('admin/version_get_x', qa_html('v' . $contents)) . qa_lang_html_sub('admin/version_get_x', qa_html('v' . $contents)) .
'</a>'; '</a>';
} else { } else {
$response = qa_html($contents); // Output the current version number $versionResponse = qa_html($contents); // Output the current version number
} }
} else { } else {
$response = qa_lang_html('admin/version_latest_unknown'); $versionResponse = qa_lang_html('admin/version_latest_unknown');
} }
} else { } else {
$metadataUtil = new Q2A_Util_Metadata(); $metadataUtil = new Q2A_Util_Metadata();
...@@ -53,28 +60,33 @@ if ($isCore) { ...@@ -53,28 +60,33 @@ if ($isCore) {
if (strlen(@$metadata['version']) > 0) { if (strlen(@$metadata['version']) > 0) {
if (strcmp($metadata['version'], $version)) { if (strcmp($metadata['version'], $version)) {
if (qa_qa_version_below(@$metadata['min_q2a'])) { 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']), '^1' => qa_html('v' . $metadata['version']),
'^2' => qa_html($metadata['min_q2a']), '^2' => qa_html($metadata['min_q2a']),
)); ));
} elseif (qa_php_version_below(@$metadata['min_php'])) { } 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']), '^1' => qa_html('v' . $metadata['version']),
'^2' => qa_html($metadata['min_php']), '^2' => qa_html($metadata['min_php']),
)); ));
} else { } 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'])) { 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 { } else {
$response = qa_lang_html('admin/version_latest'); $versionResponse = qa_lang_html('admin/version_latest');
} }
} else { } 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() ...@@ -1621,7 +1621,7 @@ function qa_db_upgrade_tables()
while ($recalc->performStep() && (time() < $stoptime)) 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