aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/adm/style/ajax.js
diff options
context:
space:
mode:
authorMarc Alexander <admin@m-a-styles.de>2016-02-21 22:14:58 +0100
committerMarc Alexander <admin@m-a-styles.de>2016-12-03 14:23:34 +0100
commit89fef2ce13a183c7b963032274f455a09a05f325 (patch)
tree9a6d7d5306328c5f11e709e7bda33d0a65f069a6 /phpBB/adm/style/ajax.js
parent80a63a9a94af042da37227ed747eba0feeac1049 (diff)
downloadforums-89fef2ce13a183c7b963032274f455a09a05f325.tar
forums-89fef2ce13a183c7b963032274f455a09a05f325.tar.gz
forums-89fef2ce13a183c7b963032274f455a09a05f325.tar.bz2
forums-89fef2ce13a183c7b963032274f455a09a05f325.tar.xz
forums-89fef2ce13a183c7b963032274f455a09a05f325.zip
[ticket/14492] Send statistics via ajax request
Flooding ajax requests will try to be prevented and sending stats without JS will also properly work. PHPBB3-14492
Diffstat (limited to 'phpBB/adm/style/ajax.js')
-rw-r--r--phpBB/adm/style/ajax.js99
1 files changed, 99 insertions, 0 deletions
diff --git a/phpBB/adm/style/ajax.js b/phpBB/adm/style/ajax.js
index 3c1c57505b..1d4e00dfa4 100644
--- a/phpBB/adm/style/ajax.js
+++ b/phpBB/adm/style/ajax.js
@@ -4,6 +4,101 @@
'use strict';
+
+phpbb.prepareSendStats = function () {
+ var $form = $('#acp_help_phpbb');
+ var $dark = $('#darkenwrapper');
+ var $loadingIndicator;
+
+ $form.on('submit', function () {
+ var $this = $(this),
+ currentTime = Math.floor(new Date().getTime() / 1000),
+ statsTime = parseInt($this.find('input[name=help_send_statistics_time]').val(), 10);
+
+ event.preventDefault();
+ $this.unbind('submit');
+
+ // Skip ajax request if form is submitted too early or send stats
+ // checkbox is not checked
+ if (!$this.find('input[name=help_send_statistics]').is(':checked') ||
+ statsTime > currentTime) {
+ $form.find('input[type=submit]').click();
+ setTimeout(function () {
+ $form.find('input[type=submit]').click();
+ }, 300);
+ return;
+ }
+
+ /**
+ * Handler for AJAX errors
+ */
+ function errorHandler(jqXHR, textStatus, errorThrown) {
+ if (typeof console !== 'undefined' && console.log) {
+ console.log('AJAX error. status: ' + textStatus + ', message: ' + errorThrown);
+ }
+ phpbb.clearLoadingTimeout();
+ var errorText = '';
+
+ if (typeof errorThrown === 'string' && errorThrown.length > 0) {
+ errorText = errorThrown;
+ } else {
+ errorText = $dark.attr('data-ajax-error-text-' + textStatus);
+ if (typeof errorText !== 'string' || !errorText.length) {
+ errorText = $dark.attr('data-ajax-error-text');
+ }
+ }
+ phpbb.alert($dark.attr('data-ajax-error-title'), errorText);
+ }
+
+ /**
+ * This is a private function used to handle the callbacks, refreshes
+ * and alert. It calls the callback, refreshes the page if necessary, and
+ * displays an alert to the user and removes it after an amount of time.
+ *
+ * It cannot be called from outside this function, and is purely here to
+ * avoid repetition of code.
+ *
+ * @param {object} res The object sent back by the server.
+ */
+ function returnHandler(res) {
+ phpbb.clearLoadingTimeout();
+
+ // If a confirmation is not required, display an alert and call the
+ // callbacks.
+ $dark.fadeOut(phpbb.alertTime);
+
+ if ($loadingIndicator) {
+ $loadingIndicator.fadeOut(phpbb.alertTime);
+ }
+
+ var $sendStatisticsSuccess = $('<input />', {
+ type: 'hidden',
+ name: 'send_statistics_response',
+ value: res
+ });
+ $sendStatisticsSuccess.appendTo('p.submit-buttons');
+
+ // Finish actual form submission
+ $form.find('input[type=submit]').click();
+ }
+
+ $loadingIndicator = phpbb.loadingIndicator();
+
+ $.ajax({
+ url: $this.attr('data-ajax-action').replace('&amp;', '&'),
+ type: 'POST',
+ data: 'systemdata=' + $this.find('input[name=systemdata]').val(),
+ success: returnHandler,
+ error: errorHandler,
+ cache: false
+ }).always(function() {
+ if ($loadingIndicator && $loadingIndicator.is(':visible')) {
+ $loadingIndicator.fadeOut(phpbb.alertTime);
+ }
+ });
+ });
+};
+
/**
* The following callbacks are for reording items. row_down
* is triggered when an item is moved down, and row_up is triggered when
@@ -225,6 +320,10 @@ $(function() {
$(this).attr('data-clicked', true);
});
}
+
+ if ($('#acp_help_phpbb')) {
+ phpbb.prepareSendStats();
+ }
});