WIP: Download wait message

This commit is contained in:
Calvo 2022-12-02 09:42:24 +01:00
parent 14745a9625
commit d525cb594c
11 changed files with 60 additions and 49 deletions

View File

@ -1055,7 +1055,7 @@ switch ($action) {
] ]
); );
$data[2] .= '</a>'; $data[2] .= '</a>';
$data[3] = '<a href="'.ui_get_full_url(false, false, false, false).'ajax.php?page='.$config['homedir'].'/operation/reporting/reporting_xml&id='.$report['id_report'].'">'; $data[3] = '<a onclick="blockResubmit($(this))" href="'.ui_get_full_url(false, false, false, false).'ajax.php?page='.$config['homedir'].'/operation/reporting/reporting_xml&id='.$report['id_report'].'">';
$data[3] .= html_print_image( $data[3] .= html_print_image(
'images/xml.png', 'images/xml.png',
true, true,

View File

@ -506,6 +506,7 @@ class ModuleTemplates extends HTML
header('Content-Disposition: attachment; filename='.preg_replace('/\s/', '_', $fileName).'.csv'); header('Content-Disposition: attachment; filename='.preg_replace('/\s/', '_', $fileName).'.csv');
header('Pragma: no-cache'); header('Pragma: no-cache');
header('Expires: 0'); header('Expires: 0');
setDownloadCookieToken();
// Clean up output buffering // Clean up output buffering
while (@ob_end_clean()) { while (@ob_end_clean()) {
@ -931,7 +932,7 @@ class ModuleTemplates extends HTML
] ]
); );
$data[3] = '<a href="'.$this->baseUrl.'&action=delete&id_np='.$row['id_np'].'" onclick="if (!confirm(\''.__('Are you sure?').'\')) return false;">'.html_print_image('images/cross.png', true, ['title' => __('Delete'), 'class' => 'invert_filter']).'</a>'; $data[3] = '<a href="'.$this->baseUrl.'&action=delete&id_np='.$row['id_np'].'" onclick="if (!confirm(\''.__('Are you sure?').'\')) return false;">'.html_print_image('images/cross.png', true, ['title' => __('Delete'), 'class' => 'invert_filter']).'</a>';
$data[3] .= '<a href="'.$this->baseUrl.'&action=export&id_np='.$row['id_np'].'">'.html_print_image('images/csv.png', true, ['title' => __('Export to CSV'), 'class' => 'invert_filter']).'</a>'; $data[3] .= '<a href="'.$this->baseUrl.'&action=export&id_np='.$row['id_np'].'" onclick="blockResubmit($(this))">'.html_print_image('images/csv.png', true, ['title' => __('Export to CSV'), 'class' => 'invert_filter']).'</a>';
array_push($table->data, $data); array_push($table->data, $data);
} }

View File

@ -6271,31 +6271,22 @@ function arrayOutputSorting($sort, $sortField)
} }
function setCookieToken($cookieName, $cookieValue, $httpOnly=true, $secure=false) /**
{ * Get dowload started cookie from js and set ready cokkie for download ready comntrol.
// See: http://stackoverflow.com/a/1459794/59087 *
// See: http://shiflett.org/blog/2006/mar/server-name-versus-http-host * @return
// See: http://stackoverflow.com/a/3290474/59087 */
setcookie(
$cookieName,
$cookieValue,
2147483647,
// expires January 1, 2038
'/',
// your path
$_SERVER['HTTP_HOST'],
// your domain
$secure,
// Use true over HTTPS
$httpOnly
// Set true for $AUTH_COOKIE_NAME
);
}
function setDownloadCookieToken() function setDownloadCookieToken()
{ {
$token = 'downloadToken'; $download_cookie = get_cookie('downloadToken', false);
if ($download_cookie === false) {
setCookieToken($token, $_GET[$token], false, false); return;
} else {
setcookie(
'downloadReady',
$download_cookie,
(time() + 15),
'/'
);
}
} }

View File

@ -46,6 +46,8 @@ function reporting_xml_get_report($report, $filename, $return=false)
// Download if marked to download. // Download if marked to download.
if ($filename !== false) { if ($filename !== false) {
// Cookie for download control.
setDownloadCookieToken();
header('Content-Type: application/xml; charset=UTF-8'); header('Content-Type: application/xml; charset=UTF-8');
header('Content-Disposition: attachment; filename="'.$filename.'.xml"'); header('Content-Disposition: attachment; filename="'.$filename.'.xml"');
} }

View File

@ -3540,6 +3540,9 @@ function ui_print_datatable(array $parameters)
titleAttr: "'.__('Export current page to CSV').'", titleAttr: "'.__('Export current page to CSV').'",
title: "export_'.$parameters['id'].'_current_page_'.date('Y-m-d').'", title: "export_'.$parameters['id'].'_current_page_'.date('Y-m-d').'",
fieldSeparator: "'.$config['csv_divider'].'", fieldSeparator: "'.$config['csv_divider'].'",
action: function ( e, dt, node, config ) {
blockResubmit(node);
},
exportOptions : { exportOptions : {
modifier : { modifier : {
// DataTables core // DataTables core
@ -3547,7 +3550,7 @@ function ui_print_datatable(array $parameters)
page : "All", page : "All",
search : "applied" search : "applied"
}'.$export_columns.' }'.$export_columns.'
} },
} }
] : [], ] : [],
lengthMenu: '.json_encode($pagination_options).', lengthMenu: '.json_encode($pagination_options).',

View File

@ -51,6 +51,9 @@ $default_filename = 'data_exported - '.date($config['date_format']);
$filename = (string) get_parameter('filename', $default_filename); $filename = (string) get_parameter('filename', $default_filename);
$filename = io_safe_output($filename); $filename = io_safe_output($filename);
// Set cookie for download control.
setDownloadCookieToken();
/* /*
* $data = array( * $data = array(
* 'head' => array(<column>,<column>,...,<column>), * 'head' => array(<column>,<column>,...,<column>),

View File

@ -2716,6 +2716,7 @@ function pandoraFlotArea(
$("#menu_export_csv_" + graph_id).click(function(e) { $("#menu_export_csv_" + graph_id).click(function(e) {
e.preventDefault(); e.preventDefault();
blockResubmit($(this));
plot.exportDataCSV(); plot.exportDataCSV();
var es_firefox = var es_firefox =
navigator.userAgent.toLowerCase().indexOf("firefox") > -1; navigator.userAgent.toLowerCase().indexOf("firefox") > -1;

View File

@ -456,8 +456,7 @@ function load_modal(settings) {
// Function that shows a dialog box to confirm closures of generic manners. // Function that shows a dialog box to confirm closures of generic manners.
// The modal id is random. // The modal id is random.
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
function confirmDialog(settings) { function confirmDialog(settings, idDialog = uniqId()) {
var randomStr = uniqId();
var hideOkButton = ""; var hideOkButton = "";
var hideCancelButton = ""; var hideCancelButton = "";
@ -487,11 +486,11 @@ function confirmDialog(settings) {
if (typeof settings.message == "function") { if (typeof settings.message == "function") {
$("body").append( $("body").append(
'<div id="confirm_' + randomStr + '">' + settings.message() + "</div>" '<div id="confirm_' + idDialog + '">' + settings.message() + "</div>"
); );
} else { } else {
$("body").append( $("body").append(
'<div id="confirm_' + randomStr + '">' + settings.message + "</div>" '<div id="confirm_' + idDialog + '">' + settings.message + "</div>"
); );
} }
@ -542,8 +541,8 @@ function confirmDialog(settings) {
buttons.unshift(newButton); buttons.unshift(newButton);
} }
$("#confirm_" + randomStr); $("#confirm_" + idDialog);
$("#confirm_" + randomStr) $("#confirm_" + idDialog)
.dialog({ .dialog({
open: settings.open, open: settings.open,
title: settings.title, title: settings.title,
@ -785,10 +784,11 @@ function setCursor(buttonStyle, button) {
button.css("cursor", buttonStyle); button.css("cursor", buttonStyle);
} }
function setFormToken(button) { function setToken() {
var downloadToken = new Date().getTime(); var downloadToken = new Date().getTime();
button.append("<input type='hidden' id='downloadToken'"); document.cookie =
$("#downloadToken").val(downloadToken); "downloadToken" + "=" + downloadToken + ";" + "-1" + ";path=/";
return downloadToken; return downloadToken;
} }
@ -797,24 +797,28 @@ var attempts = 30;
// Prevents double-submits by waiting for a cookie from the server. // Prevents double-submits by waiting for a cookie from the server.
function blockResubmit(button) { function blockResubmit(button) {
var downloadToken = setFormToken(button); var downloadToken = setToken();
setCursor("wait", button); setCursor("wait", button);
// Disable butoon to prevent clicking until download is ready. // Disable butoon to prevent clicking until download is ready.
button.disable(); button.disable();
button.click(false);
//Show dialog. //Show dialog.
confirmDialog({ confirmDialog(
title: get_php_value("prepareDownloadTitle"), {
message: get_php_value("prepareDownloadMsg"), title: get_php_value("prepareDownloadTitle"),
hideCancelButton: true message: get_php_value("prepareDownloadMsg"),
}); hideCancelButton: true
},
"downloadDialog"
);
downloadTimer = window.setInterval(function() { downloadTimer = setInterval(function() {
var token = getCookie("downloadToken"); var downloadReady = getCookie("downloadReady");
if (token == downloadToken || attempts == 0) { if (downloadToken == downloadReady || attempts == 0) {
unblockSubmit(); unblockSubmit(button);
} }
attempts--; attempts--;
@ -824,7 +828,10 @@ function blockResubmit(button) {
function unblockSubmit(button) { function unblockSubmit(button) {
setCursor("pointer", button); setCursor("pointer", button);
button.enable(); button.enable();
window.clearInterval(downloadTimer); button.on("click");
clearInterval(downloadTimer);
$("#confirm_downloadDialog").dialog("close");
expireCookie("downloadToken"); expireCookie("downloadToken");
expireCookie("downloadReady");
attempts = 30; attempts = 30;
} }

View File

@ -1428,7 +1428,7 @@ if ($pure) {
// CSV. // CSV.
$csv['active'] = false; $csv['active'] = false;
$csv['text'] = '<a class="events_link" href="'.ui_get_full_url(false, false, false, false).'operation/events/export_csv.php?'.($filter_b64 ?? '').'">'.html_print_image( $csv['text'] = '<a class="events_link" onclick="blockResubmit($(this))" href="'.ui_get_full_url(false, false, false, false).'operation/events/export_csv.php?'.($filter_b64 ?? '').'">'.html_print_image(
'images/csv.png', 'images/csv.png',
true, true,
[ [

View File

@ -150,6 +150,7 @@ $now = date('Y-m-d');
// Download header. // Download header.
header('Content-type: text/txt'); header('Content-type: text/txt');
header('Content-Disposition: attachment; filename="export_events_'.$now.'.csv"'); header('Content-Disposition: attachment; filename="export_events_'.$now.'.csv"');
setDownloadCookieToken();
try { try {
$fb64 = get_parameter('fb64', null); $fb64 = get_parameter('fb64', null);

View File

@ -9478,6 +9478,8 @@ class Mpdf implements \Psr\Log\LoggerAwareInterface
function Output($name = '', $dest = '') function Output($name = '', $dest = '')
{ {
$this->logger->debug(sprintf('PDF generated in %.6F seconds', microtime(true) - $this->time0), ['context' => LogContext::STATISTICS]); $this->logger->debug(sprintf('PDF generated in %.6F seconds', microtime(true) - $this->time0), ['context' => LogContext::STATISTICS]);
// Set cokie token to indicate download is ready.
setDownloadCookieToken();
// Finish document if necessary // Finish document if necessary
if ($this->state < 3) { if ($this->state < 3) {