From b03d86221b2cabaee803c2f2b356ba207591226c Mon Sep 17 00:00:00 2001 From: mokaddem Date: Tue, 28 Jan 2020 14:38:58 +0100 Subject: [PATCH] chg: [attribute:type] Datetime value is forced to be a valid ISO format - It is converted into UTC in the server - /attribute/add Form includes javascript validation part --- app/Model/Attribute.php | 7 +++++++ app/View/Attributes/add.ctp | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/app/Model/Attribute.php b/app/Model/Attribute.php index 18a6105be..f73503181 100644 --- a/app/Model/Attribute.php +++ b/app/Model/Attribute.php @@ -1594,6 +1594,13 @@ class Attribute extends AppModel } $value = ($value) ? '1' : '0'; break; + case 'datetime': + try { + $value = (new DateTime($value))->setTimezone(new DateTimeZone('GMT'))->format('Y-m-d\TH:i:s.uO'); // ISO8601 formating with microseconds + } catch (Exception $e) { + // silently skip. Rejection will be done in runValidation() + } + break; } return $value; } diff --git a/app/View/Attributes/add.ctp b/app/View/Attributes/add.ctp index 188650dbd..930671141 100644 --- a/app/View/Attributes/add.ctp +++ b/app/View/Attributes/add.ctp @@ -141,6 +141,31 @@ endif; ?> checkSharingGroup('Attribute'); + + var $form = $('#AttributeType').closest('form').submit(function( event ) { + if ($('#AttributeType').val() === 'datetime') { + // add timezone of the browser if not set + var allowLocalTZ = true; + var $valueInput = $('#AttributeValue') + var dateValue = moment($valueInput.val()) + if (dateValue.isValid()) { + if (dateValue.creationData().format !== "YYYY-MM-DDTHH:mm:ssZ" && dateValue.creationData().format !== "YYYY-MM-DDTHH:mm:ss.SSSSZ") { + // Missing timezone data + var confirm_message = '' + dateValue.format('Z') + confirm_message += '' + dateValue.toISOString(allowLocalTZ) + if (confirm(confirm_message)) { + $valueInput.val(dateValue.toISOString(allowLocalTZ)); + } else { + return false; + } + } + } else { + textStatus = '' + showMessage('fail', + ": " + '???'); + return false; + } + } + }); }); element('form_seen_input'); ?>