date filters time zone

This commit is contained in:
Yuri Kuznetsov
2024-01-29 14:47:09 +02:00
parent 1cd914e5a7
commit abbffb9b15
8 changed files with 177 additions and 51 deletions
+11 -5
View File
@@ -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<string, mixed> $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();
@@ -0,0 +1,55 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2024 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* 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;
}
}
@@ -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<string|int, mixed>
* @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<string|int, mixed>
* @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<string|int, mixed>
* @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<string|int, mixed>
* @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<string|int, mixed>
* @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<string|int, mixed>
* @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<string|int, mixed>
* @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<string|int, mixed>
* @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());
}
}
}
@@ -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' => [
@@ -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(),
],
+34 -5
View File
@@ -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;
+2
View File
@@ -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,
};
}
+1
View File
@@ -310,6 +310,7 @@ class DatetimeFieldView extends DateFieldView {
if (data) {
data.dateTime = true;
delete data.date;
}
return data;