fix: [UI] Flash error message

pull/8315/head
Jakub Onderka 2022-05-05 22:02:39 +02:00
parent cbf4b02d04
commit 8ccb694b75
4 changed files with 40 additions and 58 deletions

View File

@ -155,15 +155,15 @@ class AttributesController extends AppController
if (!isset($attributes[0])) {
$attributes = array(0 => $attributes);
}
$fails = array();
$fails = [];
$successes = 0;
$attributeCount = count($attributes);
$inserted_ids = array();
$insertedIds = array();
foreach ($attributes as $k => $attribute) {
$validationErrors = array();
$this->Attribute->captureAttribute($attribute, $event['Event']['id'], $this->Auth->user(), false, false, $event, $validationErrors, $this->params['named']);
if (empty($validationErrors)) {
$inserted_ids[] = $this->Attribute->id;
$insertedIds[] = $this->Attribute->id;
$successes++;
} else {
$fails["attribute_" . $k] = $validationErrors;
@ -176,7 +176,7 @@ class AttributesController extends AppController
if ($successes !== 0) {
$attributes = $this->Attribute->find('all', array(
'recursive' => -1,
'conditions' => array('Attribute.id' => $inserted_ids),
'conditions' => array('Attribute.id' => $insertedIds),
'contain' => array(
'AttributeTag' => array(
'Tag' => array('fields' => array('Tag.id', 'Tag.name', 'Tag.colour', 'Tag.numerical_value'))
@ -212,13 +212,23 @@ class AttributesController extends AppController
$message = __('Attributes saved.');
} else {
if ($attributeCount > 1) {
$failKeys = array_keys($fails);
foreach ($failKeys as $k => $v) {
$v = explode('_', $v);
$failKeys[$k] = intval($v[1]);
$flashErrorMessage = [];
foreach ($attributes as $k => $attribute) {
if (isset($fails["attribute_$k"])) {
$reason = '';
foreach ($fails["attribute_" . $k] as $failKey => $failData) {
$reason = $failKey . ': ' . $failData[0];
}
$flashErrorMessage[] = '<span class="red bold">' . h($attribute["value"]) . '</span> (' . h($reason) . ')';
} else {
$flashErrorMessage[] = '<span class="green bold">' . h($attribute["value"]) . '</span>';
}
}
$flashErrorMessage = implode('<br>', $flashErrorMessage);
$this->Session->write('flashErrorMessage', $flashErrorMessage);
if ($successes === 0) {
$message = __('Attributes could not be saved. Click $flashErrorMessage for more info', count($fails));
$message = __('Attributes could not be saved. Click $flashErrorMessage for more info');
} else {
$message = __('Attributes saved, however, %s attributes could not be saved. Click $flashErrorMessage for more info', count($fails));
}
@ -226,25 +236,6 @@ class AttributesController extends AppController
$message = __('Attribute could not be saved.');
}
}
if (!empty($failKeys)) {
$flashErrorMessage = array();
$original_values = trim($this->request->data['Attribute']['value']);
$original_values = explode("\n", $original_values);
foreach ($original_values as $k => $original_value) {
$original_value = trim($original_value);
if (in_array($k, $failKeys)) {
$reason = '';
foreach ($fails["attribute_" . $k] as $failKey => $failData) {
$reason = $failKey . ': ' . $failData[0];
}
$flashErrorMessage[] = '<span class="red bold">' . h($original_value) . '</span> (' . h($reason) . ')';
} else {
$flashErrorMessage[] = '<span class="green bold">' . h($original_value) . '</span>';
}
}
$flashErrorMessage = implode('<br>', $flashErrorMessage);
$this->Session->write('flashErrorMessage', $flashErrorMessage);
}
if ($this->request->is('ajax')) {
if (!empty($successes)) {
$data = ['saved' => true, 'success' => $message];

View File

@ -2,8 +2,9 @@
<button type="button" class="close" data-dismiss="alert">&times;</button>
<?php
$message = h($message);
if (strpos('$flashErrorMessage', $message) >= 0) {
$message = str_replace('$flashErrorMessage', '<span class="useCursorPointer underline bold" onClick="flashErrorPopover();">here</span>', $message);
if (strpos('$flashErrorMessage', $message) >= 0 && $this->Session->read('flashErrorMessage')) {
$toReplace = sprintf('<a href="#" data-content="%s" data-toggle="popover" class="bold">%s</a>', h($this->Session->read('flashErrorMessage')), __("here"));
$message = str_replace('$flashErrorMessage', $toReplace, $message);
}
echo $message;
if (isset($params['url'])) {
@ -13,8 +14,5 @@
echo '<a href="' . h($params['url']) . '">' . h($params['url']) . '</a>';
}
}
if ($this->Session->read('flashErrorMessage')) {
echo sprintf('<div class="hidden" id="flashErrorMessage">%s</div>', $this->element('flashErrorMessage', array('message' => $this->Session->read('flashErrorMessage'))));
}
?>
</div>

View File

@ -1,7 +0,0 @@
<div class="confirmation">
<legend><?php echo __('Errors');?></legend>
<div style="padding-left:5px;padding-right:5px;padding-bottom:5px;">
<div id="flashErrorMessageContent" style="overflow-y: auto;"><?= $message ?></div>
<span role="button" tabindex="0" aria-label="<?php echo __('Cancel');?>" title="<?php echo __('Cancel');?>" class="btn btn-inverse" id="PromptNoButton" onclick="cancelPopoverForm();"><?php echo __('Close');?></span>
</div>
</div>

View File

@ -12,6 +12,15 @@ if (!String.prototype.startsWith) {
};
}
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");
}
function copyToClipboard(element) {
var $temp = $("<input>");
$("body").append($temp);
@ -1307,7 +1316,8 @@ function handleAjaxModalResponse(response, context_id, url, referer, context, co
}
} else {
if (responseArray.errors && typeof responseArray.errors === 'string') {
showMessage("fail", responseArray.errors);
var fullErrors = "full_errors" in responseArray ? responseArray.full_errors : '';
showMessage("fail", responseArray.errors, fullErrors);
} else {
var savedArray = saveValuesForPersistance();
$.ajax({
@ -1412,21 +1422,16 @@ function updateHistogram(selected) {
});
}
function showMessage(success, message, context) {
if (typeof context !== "undefined") {
$("#ajax_" + success, window.parent.document).html(message);
var duration = 1000 + (message.length * 40);
$("#ajax_" + success + "_container", window.parent.document).fadeIn("slow");
$("#ajax_" + success + "_container", window.parent.document).delay(duration).fadeOut("slow");
}
function showMessage(success, message, fullError) {
var duration = 1000 + (message.length * 40);
if (message.indexOf("$flashErrorMessage") >= 0) {
var flashMessageLink = '<a href="#" class="bold" onclick="flashErrorPopover();">here</a>';
var flashMessageLink = '<a href="#" class="bold" data-content="' + escapeHtml(fullError) + '" data-html="true" onclick="event.preventDefault();$(this).popover(\'show\')">here</a>';
message = message.replace("$flashErrorMessage", flashMessageLink);
duration = 5000;
}
$("#ajax_" + success).html(message);
var duration = 1000 + (message.length * 40);
$("#ajax_" + success + "_container").fadeIn("slow").delay(duration).fadeOut("slow");
}
@ -2636,8 +2641,9 @@ function popoverStartup() {
$('[data-toggle="popover"]').not(e.target).popover('hide');
});
$(document).click(function (e) {
if (!$('[data-toggle="popover"]').is(e.target)) {
$('[data-toggle="popover"]').popover('hide');
var $popovers = $('[data-toggle="popover"]');
if (!$popovers.is(e.target)) {
$popovers.popover('hide');
}
});
}
@ -3968,12 +3974,6 @@ $(document).on("click", ".eventViewAttributePopup", function() {
}
});
function flashErrorPopover() {
var $popover = $('#popover_form');
$popover.html($('#flashErrorMessage').html());
openPopup($popover);
}
$(document.body).on('click', function (e) {
$('[data-toggle=popover]').each(function () {
// hide any open popovers when the anywhere else in the body is clicked