From 4c4428102d4e6bb811469b83b93e51ffc9a36386 Mon Sep 17 00:00:00 2001 From: MightyMCoder <130976036+MightyMCoder@users.noreply.github.com> Date: Wed, 15 Jan 2025 18:41:26 +0100 Subject: [PATCH] bugfix: import datetime in correct format --- classes/items.php | 36 +++++++++++++++++---------------- import/import_items.php | 44 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 17 deletions(-) diff --git a/classes/items.php b/classes/items.php index 4cc0844..1dfb75e 100644 --- a/classes/items.php +++ b/classes/items.php @@ -660,24 +660,26 @@ public function setValue($fieldNameIntern, $newValue) // format of date will be local but database has stored Y-m-d format must be changed for compare if ($this->mItemFields[$fieldNameIntern]->getValue('imf_type') === 'DATE') { - if ($this->pPreferences->config['Optionen']['field_date_time_format'] === 'datetime') { - //check if date is datetime or only date - if (strpos($newValue, ' ') === false) { - $newValue .= ' 00:00'; - } - $date = \DateTime::createFromFormat('Y-m-d H:i', $newValue); - if ($date !== false) { - $newValue = $date->format('Y-m-d H:i'); - } - } - else { - // check if date is date or datetime - if (strpos($newValue, ' ') !== false) { - $newValue = substr($newValue, 0, 10); + if ($newValue !== '') { + if ($this->pPreferences->config['Optionen']['field_date_time_format'] === 'datetime') { + //check if date is datetime or only date + if (strpos($newValue, ' ') === false) { + $newValue .= ' 00:00'; + } + $date = \DateTime::createFromFormat('Y-m-d H:i', $newValue); + if ($date !== false) { + $newValue = $date->format('Y-m-d H:i'); + } } - $date = \DateTime::createFromFormat('Y-m-d', $newValue); - if ($date !== false) { - $newValue = $date->format('Y-m-d'); + else { + // check if date is date or datetime + if (strpos($newValue, ' ') !== false) { + $newValue = substr($newValue, 0, 10); + } + $date = \DateTime::createFromFormat('Y-m-d', $newValue); + if ($date !== false) { + $newValue = $date->format('Y-m-d'); + } } } } diff --git a/import/import_items.php b/import/import_items.php index a7cf840..2f79723 100644 --- a/import/import_items.php +++ b/import/import_items.php @@ -14,6 +14,7 @@ require_once(__DIR__ . '/../../../adm_program/system/common.php'); require_once(__DIR__ . '/../common_function.php'); require_once(__DIR__ . '/../classes/items.php'); +require_once(__DIR__ . '/../classes/configtable.php'); // Access only with valid login require_once(__DIR__ . '/../../../adm_program/system/login_valid.php'); @@ -34,6 +35,9 @@ $startRow = 0; $importedFields = array(); +$pPreferences = new CConfigTablePIM(); +$pPreferences->read(); + // create array with all profile fields that where assigned to columns of the import file foreach ($_POST as $formFieldId => $importFileColumn) { if ($importFileColumn !== '' && $formFieldId !== 'admidio-csrf-token' && $formFieldId !== 'first_row') { @@ -176,6 +180,46 @@ } } } + elseif($imfNameIntern === 'RECEIVED_ON' || $imfNameIntern === 'RECEIVED_BACK_ON') { + $val = $values[$imfNameIntern]; + if ($val !== '') { + // date must be formatted + if ($pPreferences->config['Optionen']['field_date_time_format'] === 'datetime') { + //check if date is datetime or only date + if (strpos($val, ' ') === false) { + $val .= '00:00'; + } + // check if date is wrong formatted + $dateObject = DateTime::createFromFormat('d.m.Y H:i', $val); + if ($dateObject instanceof DateTime) { + // convert date to correct format + $val = $dateObject->format('Y-m-d H:i'); + } + // check if date is right formatted + $date = DateTime::createFromFormat('Y-m-d H:i', $val); + if ($date instanceof DateTime) { + $val = $date->format($gSettingsManager->getString('system_date').' '.$gSettingsManager->getString('system_time')); + } + } + else { + // check if date is date or datetime + if (strpos($val, ' ') !== false) { + $val = substr($val, 0, 10); + } + // check if date is wrong formatted + $dateObject = DateTime::createFromFormat('d.m.Y', $val); + if ($dateObject instanceof DateTime) { + // convert date to correct format + $val = $dateObject->format('Y-m-d'); + } + // check if date is right formatted + $date = DateTime::createFromFormat('Y-m-d', $val); + if ($date instanceof DateTime) { + $htmlValue = $date->format($gSettingsManager->getString('system_date')); + } + } + } + } else { $val = $values[$imfNameIntern]; }