From abbffb9b1554868073602fe5ae6ceaf962c0c088 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Mon, 29 Jan 2024 14:47:09 +0200 Subject: [PATCH] date filters time zone --- application/Espo/Core/Select/Where/Item.php | 16 ++- .../Espo/Core/Select/Where/Item/Data/Date.php | 55 ++++++++++ .../Select/Where/ItemGeneralConverter.php | 102 +++++++++++------- .../Meeting/Where/DateTimeItemTransformer.php | 6 ++ .../Crm/Tools/Activities/UpcomingService.php | 7 +- client/src/search-manager.js | 39 ++++++- client/src/views/fields/date.js | 2 + client/src/views/fields/datetime.js | 1 + 8 files changed, 177 insertions(+), 51 deletions(-) create mode 100644 application/Espo/Core/Select/Where/Item/Data/Date.php diff --git a/application/Espo/Core/Select/Where/Item.php b/application/Espo/Core/Select/Where/Item.php index 279412a4d1..951c63f6bb 100644 --- a/application/Espo/Core/Select/Where/Item.php +++ b/application/Espo/Core/Select/Where/Item.php @@ -30,7 +30,6 @@ namespace Espo\Core\Select\Where; use Espo\Core\Select\Where\Item\Data; -use Espo\Core\Select\Where\Item\Data\DateTime as DateTimeData; use InvalidArgumentException; use RuntimeException; @@ -69,7 +68,7 @@ class Item /** * @param array $params - * {@internal} + * @internal */ public static function fromRaw(array $params): self { @@ -85,7 +84,12 @@ class Item $obj->value = $params['value'] ?? null; if ($params['dateTime'] ?? false) { - $obj->data = DateTimeData + $obj->data = Data\DateTime + ::create() + ->withTimeZone($params['timeZone'] ?? null); + } + else if ($params['timeZone'] ?? null) { + $obj->data = Data\Date ::create() ->withTimeZone($params['timeZone'] ?? null); } @@ -156,8 +160,10 @@ class Item $raw['attribute'] = $this->attribute; } - if ($this->data instanceof DateTimeData) { - $raw['dateTime'] = true; + if ($this->data instanceof Data\DateTime || $this->data instanceof Data\Date) { + if ($this->data instanceof Data\DateTime) { + $raw['dateTime'] = true; + } $timeZone = $this->data->getTimeZone(); diff --git a/application/Espo/Core/Select/Where/Item/Data/Date.php b/application/Espo/Core/Select/Where/Item/Data/Date.php new file mode 100644 index 0000000000..5067e15bd9 --- /dev/null +++ b/application/Espo/Core/Select/Where/Item/Data/Date.php @@ -0,0 +1,55 @@ +. + * + * The interactive user interfaces in modified source and object code versions + * of this program must display Appropriate Legal Notices, as required under + * Section 5 of the GNU Affero General Public License version 3. + * + * In accordance with Section 7(b) of the GNU Affero General Public License version 3, + * these Appropriate Legal Notices must retain the display of the "EspoCRM" word. + ************************************************************************/ + +namespace Espo\Core\Select\Where\Item\Data; + +use Espo\Core\Select\Where\Item\Data; + +class Date implements Data +{ + private ?string $timeZone = null; + + public static function create(): self + { + return new self(); + } + + public function withTimeZone(?string $timeZone): self + { + $obj = clone $this; + $obj->timeZone = $timeZone; + + return $obj; + } + + public function getTimeZone(): ?string + { + return $this->timeZone; + } +} diff --git a/application/Espo/Core/Select/Where/ItemGeneralConverter.php b/application/Espo/Core/Select/Where/ItemGeneralConverter.php index 5a4fb990be..78700c6065 100644 --- a/application/Espo/Core/Select/Where/ItemGeneralConverter.php +++ b/application/Espo/Core/Select/Where/ItemGeneralConverter.php @@ -32,6 +32,7 @@ namespace Espo\Core\Select\Where; use DateTimeZone; use Espo\Core\Exceptions\BadRequest; use Espo\Core\Field\DateTime; +use Espo\Core\Select\Where\Item\Data; use Espo\Core\Select\Where\Item\Type; use Espo\Core\Select\Helpers\RandomStringGenerator; use Espo\Core\Utils\Config; @@ -190,35 +191,35 @@ class ItemGeneralConverter implements ItemConverter } if ($type === Type::TODAY) { - return WhereClause::fromRaw($this->processToday($attribute)); + return WhereClause::fromRaw($this->processToday($attribute, $item->getData())); } if ($type === Type::PAST) { - return WhereClause::fromRaw($this->processPast($attribute)); + return WhereClause::fromRaw($this->processPast($attribute, $item->getData())); } if ($type === Type::FUTURE) { - return WhereClause::fromRaw($this->processFuture($attribute)); + return WhereClause::fromRaw($this->processFuture($attribute, $item->getData())); } if ($type === Type::LAST_SEVEN_DAYS) { - return WhereClause::fromRaw($this->processLastSevenDays($attribute)); + return WhereClause::fromRaw($this->processLastSevenDays($attribute, $item->getData())); } if ($type === Type::LAST_X_DAYS) { - return WhereClause::fromRaw($this->processLastXDays($attribute, $value)); + return WhereClause::fromRaw($this->processLastXDays($attribute, $value, $item->getData())); } if ($type === Type::NEXT_X_DAYS) { - return WhereClause::fromRaw($this->processNextXDays($attribute, $value)); + return WhereClause::fromRaw($this->processNextXDays($attribute, $value, $item->getData())); } if ($type === Type::OLDER_THAN_X_DAYS) { - return WhereClause::fromRaw($this->processOlderThanXDays($attribute, $value)); + return WhereClause::fromRaw($this->processOlderThanXDays($attribute, $value, $item->getData())); } if ($type === Type::AFTER_X_DAYS) { - return WhereClause::fromRaw($this->processAfterXDays($attribute, $value)); + return WhereClause::fromRaw($this->processAfterXDays($attribute, $value, $item->getData())); } if ($type === Type::CURRENT_MONTH) { @@ -952,11 +953,12 @@ class ItemGeneralConverter implements ItemConverter /** * @return array + * @throws BadRequest */ - private function processToday(string $attribute): array + private function processToday(string $attribute, ?Data $data): array { - $today = DateTime::createNow() - ->withTimezone($this->getSystemTimeZone()); + $timeZone = $this->getTimeZone($data); + $today = DateTime::createNow()->withTimezone($timeZone); return [ $attribute . '=' => $today->toDateTime()->format(DateTimeUtil::SYSTEM_DATE_FORMAT), @@ -965,11 +967,12 @@ class ItemGeneralConverter implements ItemConverter /** * @return array + * @throws BadRequest */ - private function processPast(string $attribute): array + private function processPast(string $attribute, ?Data $data): array { - $today = DateTime::createNow() - ->withTimezone($this->getSystemTimeZone()); + $timeZone = $this->getTimeZone($data); + $today = DateTime::createNow()->withTimezone($timeZone); return [ $attribute . '<' => $today->toDateTime()->format(DateTimeUtil::SYSTEM_DATE_FORMAT), @@ -978,11 +981,12 @@ class ItemGeneralConverter implements ItemConverter /** * @return array + * @throws BadRequest */ - private function processFuture(string $attribute): array + private function processFuture(string $attribute, ?Data $data): array { - $today = DateTime::createNow() - ->withTimezone($this->getSystemTimeZone()); + $timeZone = $this->getTimeZone($data); + $today = DateTime::createNow()->withTimezone($timeZone); return [ $attribute . '>' => $today->toDateTime()->format(DateTimeUtil::SYSTEM_DATE_FORMAT), @@ -991,11 +995,12 @@ class ItemGeneralConverter implements ItemConverter /** * @return array + * @throws BadRequest */ - private function processLastSevenDays(string $attribute): array + private function processLastSevenDays(string $attribute, ?Data $data): array { - $today = DateTime::createNow() - ->withTimezone($this->getSystemTimeZone()); + $timeZone = $this->getTimeZone($data); + $today = DateTime::createNow()->withTimezone($timeZone); $from = $today->addDays(-7); @@ -1008,13 +1013,13 @@ class ItemGeneralConverter implements ItemConverter } /** - * @param mixed $value * @return array + * @throws BadRequest */ - private function processLastXDays(string $attribute, $value): array + private function processLastXDays(string $attribute, mixed $value, ?Data $data): array { - $today = DateTime::createNow() - ->withTimezone($this->getSystemTimeZone()); + $timeZone = $this->getTimeZone($data); + $today = DateTime::createNow()->withTimezone($timeZone); $number = intval($value); @@ -1029,13 +1034,13 @@ class ItemGeneralConverter implements ItemConverter } /** - * @param mixed $value * @return array + * @throws BadRequest */ - private function processNextXDays(string $attribute, $value): array + private function processNextXDays(string $attribute, mixed $value, ?Data $data): array { - $today = DateTime::createNow() - ->withTimezone($this->getSystemTimeZone()); + $timeZone = $this->getTimeZone($data); + $today = DateTime::createNow()->withTimezone($timeZone); $number = intval($value); @@ -1050,16 +1055,17 @@ class ItemGeneralConverter implements ItemConverter } /** - * @param mixed $value * @return array + * @throws BadRequest */ - private function processOlderThanXDays(string $attribute, $value): array + private function processOlderThanXDays(string $attribute, mixed $value, ?Data $data): array { + $timeZone = $this->getTimeZone($data); + $today = DateTime::createNow()->withTimezone($timeZone); + $number = intval($value); - $date = DateTime::createNow() - ->withTimezone($this->getSystemTimeZone()) - ->addDays(- $number); + $date = $today->addDays(- $number); return [ $attribute . '<' => $date->toDateTime()->format(DateTimeUtil::SYSTEM_DATE_FORMAT), @@ -1067,16 +1073,17 @@ class ItemGeneralConverter implements ItemConverter } /** - * @param mixed $value * @return array + * @throws BadRequest */ - private function processAfterXDays(string $attribute, $value): array + private function processAfterXDays(string $attribute, mixed $value, ?Data $data): array { + $timeZone = $this->getTimeZone($data); + $today = DateTime::createNow()->withTimezone($timeZone); + $number = intval($value); - $date = DateTime::createNow() - ->withTimezone($this->getSystemTimeZone()) - ->addDays($number); + $date = $today->addDays($number); return [ $attribute . '>' => $date->toDateTime()->format(DateTimeUtil::SYSTEM_DATE_FORMAT), @@ -1676,4 +1683,23 @@ class ItemGeneralConverter implements ItemConverter throw new RuntimeException($e->getMessage()); } } + + /** + * @throws BadRequest + */ + private function getTimeZone(?Data $data): DateTimeZone + { + $timeZone = $data instanceof Data\Date ? $data->getTimeZone() : null; + + if (!$timeZone) { + return $this->getSystemTimeZone(); + } + + try { + return new DateTimeZone($timeZone); + } + catch (Exception $e) { + throw new BadRequest($e->getMessage()); + } + } } diff --git a/application/Espo/Modules/Crm/Classes/Select/Meeting/Where/DateTimeItemTransformer.php b/application/Espo/Modules/Crm/Classes/Select/Meeting/Where/DateTimeItemTransformer.php index 098bd41b97..169e26b043 100644 --- a/application/Espo/Modules/Crm/Classes/Select/Meeting/Where/DateTimeItemTransformer.php +++ b/application/Espo/Modules/Crm/Classes/Select/Meeting/Where/DateTimeItemTransformer.php @@ -84,6 +84,12 @@ class DateTimeItemTransformer implements DateTimeItemTransformerInterface 'value' => $value, ]; + $data = $item->getData(); + + if ($data instanceof Item\Data\DateTime) { + $datePartRaw['timeZone'] = $data->getTimeZone(); + } + $raw = [ 'type' => Item::TYPE_OR, 'value' => [ diff --git a/application/Espo/Modules/Crm/Tools/Activities/UpcomingService.php b/application/Espo/Modules/Crm/Tools/Activities/UpcomingService.php index 8c005c8a03..1ded8e44de 100644 --- a/application/Espo/Modules/Crm/Tools/Activities/UpcomingService.php +++ b/application/Espo/Modules/Crm/Tools/Activities/UpcomingService.php @@ -229,7 +229,6 @@ class UpcomingService 'type' => 'before', 'attribute' => 'dateEnd', 'value' => $taskBeforeString, - 'timeZone' => $timeZone, ]) )->getRaw() ] @@ -242,6 +241,7 @@ class UpcomingService WhereItem::fromRaw([ 'type' => 'past', 'attribute' => 'dateStart', + 'dateTime' => true, 'timeZone' => $timeZone, ]) )->getRaw(), @@ -250,6 +250,7 @@ class UpcomingService WhereItem::fromRaw([ 'type' => 'today', 'attribute' => 'dateStart', + 'dateTime' => true, 'timeZone' => $timeZone, ]) )->getRaw(), @@ -259,7 +260,6 @@ class UpcomingService 'type' => 'before', 'attribute' => 'dateStart', 'value' => $beforeString, - 'timeZone' => $timeZone, ]) )->getRaw(), ] @@ -275,6 +275,7 @@ class UpcomingService WhereItem::fromRaw([ 'type' => 'today', 'attribute' => 'dateStart', + 'dateTime' => true, 'timeZone' => $timeZone, ]) )->getRaw(), @@ -284,6 +285,7 @@ class UpcomingService WhereItem::fromRaw([ 'type' => 'future', 'attribute' => 'dateEnd', + 'dateTime' => true, 'timeZone' => $timeZone, ]) )->getRaw(), @@ -293,7 +295,6 @@ class UpcomingService 'type' => 'before', 'attribute' => 'dateStart', 'value' => $beforeString, - 'timeZone' => $timeZone, ]) )->getRaw(), ], diff --git a/client/src/search-manager.js b/client/src/search-manager.js index 3d01c45609..ba7130b36d 100644 --- a/client/src/search-manager.js +++ b/client/src/search-manager.js @@ -50,7 +50,7 @@ * @property {string} [attribute] An attribute (field). * @property {module:search-manager~whereItem[]|string|number|boolean|null} [value] A value. * @property {boolean} [dateTime] Is a date-time item. - * @property {string} [timeZone] A time-zone (for date-time items). + * @property {string} [timeZone] A time-zone. */ /** @@ -69,6 +69,12 @@ */ class SearchManager { + /** + * @type {string|null} + * @private + */ + timeZone + /** * @param {module:collection} collection A collection. * @param {string|null} type A type. Used for a storage key. @@ -268,14 +274,26 @@ class SearchManager { attribute = defs.attribute; } - if (defs.dateTime) { - return { + if (defs.dateTime || defs.date) { + const timeZone = this.timeZone !== undefined ? + this.timeZone : + this.dateTime.getTimeZone(); + + const data = { type: type, attribute: attribute, value: defs.value, - dateTime: true, - timeZone: this.dateTime.getTimeZone(), }; + + if (defs.dateTime) { + data.dateTime = true; + } + + if (timeZone) { + data.timeZone = timeZone; + } + + return data; } value = defs.value; @@ -389,6 +407,17 @@ class SearchManager { this.storage.clear(this.type + 'Search', this.scope); } } + + // noinspection JSUnusedGlobalSymbols + /** + * Set a time zone. Null will not add a time zone. + * + * @type {string|null} + * @internal Is used. Do not remove. + */ + setTimeZone(timeZone) { + this.timeZone = timeZone; + } } export default SearchManager; diff --git a/client/src/views/fields/date.js b/client/src/views/fields/date.js index 52d95a9702..c42fabec26 100644 --- a/client/src/views/fields/date.js +++ b/client/src/views/fields/date.js @@ -397,6 +397,7 @@ class DateFieldView extends BaseFieldView { data = { type: type, value: number, + date: true, }; } else if (['on', 'notOn', 'after', 'before'].includes(type)) { @@ -423,6 +424,7 @@ class DateFieldView extends BaseFieldView { else { data = { type: type, + date: true, }; } diff --git a/client/src/views/fields/datetime.js b/client/src/views/fields/datetime.js index 7dc5365c8c..d5d66522e6 100644 --- a/client/src/views/fields/datetime.js +++ b/client/src/views/fields/datetime.js @@ -310,6 +310,7 @@ class DatetimeFieldView extends DateFieldView { if (data) { data.dateTime = true; + delete data.date; } return data;