Initial commit

This commit is contained in:
root
2026-01-19 17:44:46 +01:00
commit 823af8b11d
8721 changed files with 1130846 additions and 0 deletions

View File

@@ -0,0 +1,349 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* 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\Tools\DynamicLogic;
use Espo\Core\Field\Date;
use Espo\Core\Field\DateTime;
use Espo\Core\Utils\DateTime\SystemClock;
use Espo\Entities\User;
use Espo\ORM\Entity;
use Espo\Tools\DynamicLogic\ConditionChecker\Options;
use Espo\Tools\DynamicLogic\Exceptions\BadCondition;
use Psr\Clock\ClockInterface;
use Exception;
use RuntimeException;
use DateTimeImmutable;
use DateTimeZone;
/**
* @since 9.1.0
*/
class ConditionChecker
{
/**
* Use `ConditionCheckerFactory` instead.
*/
public function __construct(
private Entity $entity,
private ?User $user = null,
private Options $options = new Options(),
private ClockInterface $clock = new SystemClock(),
) {}
/**
* @throws BadCondition
*/
public function check(Item $item): bool
{
$type = $item->type;
$value = $item->value;
if ($type === Type::And) {
if (!is_array($value)) {
throw new BadCondition();
}
foreach ($value as $subItem) {
if (!$subItem instanceof Item) {
throw new BadCondition();
}
if (!$this->check($subItem)) {
return false;
}
}
return true;
}
if ($type === Type::Or) {
if (!is_array($value)) {
throw new BadCondition();
}
foreach ($value as $subItem) {
if (!$subItem instanceof Item) {
throw new BadCondition();
}
if ($this->check($subItem)) {
return true;
}
}
return false;
}
if ($type === Type::Not) {
if (!$value instanceof Item) {
throw new BadCondition();
}
return !$this->check($value);
}
if (!$item->attribute) {
throw new BadCondition("No attribute.");
}
$setValue = $this->getAttributeValue($item->attribute);
if ($type === Type::Equals) {
return $setValue === $value;
}
if ($type === Type::NotEquals) {
return $setValue !== $value;
}
if ($type === Type::IsEmpty) {
return $setValue === [] ||
$setValue === null ||
$setValue === false ||
$setValue === '';
}
if ($type === Type::IsNotEmpty) {
return !(
$setValue === [] ||
$setValue === null ||
$setValue === false ||
$setValue === ''
);
}
if ($type === Type::IsTrue) {
return (bool) $setValue;
}
if ($type === Type::IsFalse) {
return !$setValue;
}
if ($type === Type::Contains) {
if (is_string($setValue) && is_string($value)) {
return str_contains($setValue, $value);
}
if (is_array($setValue)) {
return in_array($value, $setValue);
}
return false;
}
if ($type === Type::NotContains) {
if (is_string($setValue) && is_string($value)) {
return !str_contains($setValue, $value);
}
if (is_array($setValue)) {
return !in_array($value, $setValue);
}
return true;
}
if ($type === Type::Has) {
if (is_array($setValue)) {
return in_array($value, $setValue);
}
return false;
}
if ($type === Type::NotHas) {
if (is_array($setValue)) {
return !in_array($value, $setValue);
}
return true;
}
if ($type === Type::StartsWith) {
if (is_string($setValue) && is_string($value)) {
return str_starts_with($setValue, $value);
}
return false;
}
if ($type === Type::EndsWith) {
if (is_string($setValue) && is_string($value)) {
return str_ends_with($setValue, $value);
}
return false;
}
if ($type === Type::Matches) {
if (is_string($setValue) && is_string($value)) {
return (bool) preg_match($value, $setValue);
}
return false;
}
if ($type === Type::GreaterThan) {
return $setValue > $value;
}
if ($type === Type::LessThan) {
return $setValue < $value;
}
if ($type === Type::GreaterThanOrEquals) {
return $setValue >= $value;
}
if ($type === Type::LessThanOrEquals) {
return $setValue <= $value;
}
if ($type === Type::In) {
if (is_array($value)) {
return in_array($setValue, $value);
}
return false;
}
if ($type === Type::NotIn) {
if (is_array($value)) {
return !in_array($setValue, $value);
}
return true;
}
if (!$setValue || !is_string($setValue)) {
return false;
}
if ($type === Type::IsToday) {
if (strlen($setValue) > 10) {
$setDateTime = DateTime::fromDateTime($this->createDateTime($setValue));
$todayStart = $this->createNow()
->withTimezone($this->getTimeZone())
->withTime(0, 0);
$todayEnd = $todayStart->addDays(1);
return $todayStart->isLessThanOrEqualTo($setDateTime) && $setDateTime->isLessThan($todayEnd);
}
$setDate = Date::fromDateTime($this->createDateTime($setValue));
$today = $this->createToday();
return $setDate->isEqualTo($today);
}
if ($type === Type::InFuture) {
if (strlen($setValue) > 10) {
$setDateTime = DateTime::fromDateTime($this->createDateTime($setValue));
return $setDateTime->isGreaterThan($this->createNow());
}
$setDate = Date::fromDateTime($this->createDateTime($setValue));
$today = $this->createToday();
return $setDate->isGreaterThan($today);
}
if ($type === Type::InPast) {
if (strlen($setValue) > 10) {
$setDateTime = DateTime::fromDateTime($this->createDateTime($setValue));
return $setDateTime->isLessThan($this->createNow());
}
$setDate = Date::fromDateTime($this->createDateTime($setValue));
$today = $this->createToday();
return $setDate->isLessThan($today);
}
/** @phpstan-ignore-next-line deadCode.unreachable */
throw new BadCondition("Unimplemented type '$type->value'.");
}
private function getAttributeValue(string $attribute): mixed
{
if (str_starts_with($attribute, '$')) {
if (!$this->user) {
return null;
}
if ($attribute === '$user.id') {
return $this->user->getId();
}
if ($attribute === '$user.teamsIds') {
return $this->user->getTeamIdList();
}
}
return $this->entity->get($attribute);
}
private function getTimeZone(): DateTimeZone
{
return $this->options->timezone;
}
private function createDateTime(string $value): DateTimeImmutable
{
try {
$setDateTime = new DateTimeImmutable($value, $this->getTimeZone());
} catch (Exception $e) {
throw new RuntimeException($e->getMessage(), 0, $e);
}
return $setDateTime;
}
private function createNow(): DateTime
{
return DateTime::fromDateTime($this->clock->now());
}
private function createToday(): Date
{
$dateTime = $this->clock
->now()
->setTimezone($this->getTimeZone());
return Date::fromDateTime($dateTime);
}
}

View File

@@ -0,0 +1,39 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* 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\Tools\DynamicLogic\ConditionChecker;
use DateTimeZone;
readonly class Options
{
public function __construct(
public DateTimeZone $timezone = new DateTimeZone('UTC'),
) {}
}

View File

@@ -0,0 +1,70 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* 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\Tools\DynamicLogic;
use DateTimeZone;
use Espo\Core\Utils\Config\ApplicationConfig;
use Espo\Entities\User;
use Espo\ORM\Entity;
use Espo\Tools\DynamicLogic\ConditionChecker\Options;
use Exception;
use RuntimeException;
/**
* @since 9.1.0
* @noinspection PhpUnused
*/
class ConditionCheckerFactory
{
public function __construct(
private User $user,
private ApplicationConfig $applicationConfig,
) {}
/**
* @param Entity $entity An entity to check.
*/
public function create(Entity $entity): ConditionChecker
{
try {
$timezone = new DateTimeZone($this->applicationConfig->getTimeZone());
} catch (Exception $e) {
throw new RuntimeException('', 0, $e);
}
return new ConditionChecker(
entity: $entity,
user: $this->user,
options: new Options(
timezone: $timezone,
),
);
}
}

View File

@@ -0,0 +1,35 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* 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\Tools\DynamicLogic\Exceptions;
use Exception;
class BadCondition extends Exception
{}

View File

@@ -0,0 +1,110 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* 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\Tools\DynamicLogic;
use Espo\Tools\DynamicLogic\Exceptions\BadCondition;
use stdClass;
readonly class Item
{
public function __construct(
public Type $type,
public mixed $value,
public ?string $attribute = null,
) {}
/**
* @param stdClass[] $rawItems
* @throws BadCondition
*/
public static function fromGroupDefinition(array $rawItems): Item
{
return new Item(
type: Type::And,
value: array_map(fn ($it) => self::fromItemDefinition($it), $rawItems),
);
}
/**
* @throws BadCondition
*/
public static function fromItemDefinition(stdClass $rawItem): Item
{
$type = $rawItem->type ?? null;
$attribute = $rawItem->attribute ?? null;
$value = $rawItem->value ?? null;
if (!$type || !is_string($type)) {
throw new BadCondition("No type.");
}
if ($type === 'has') {
$type = 'contains';
}
if ($type === Type::And->value || $type === Type::Or->value) {
if (!is_array($value)) {
throw new BadCondition("Non-array value.");
}
foreach ($value as $it) {
if (!$it instanceof stdClass) {
throw new BadCondition("Bad group item value.");
}
}
return new Item(
type: Type::from($type),
value: array_map(fn ($it) => self::fromItemDefinition($it), $value),
);
}
if ($type === Type::Not->value) {
if (!$value instanceof stdClass) {
throw new BadCondition("Bad not item value.");
}
return new Item(
type: Type::from($type),
value: self::fromItemDefinition($value),
);
}
if ($attribute !== null && !is_string($attribute)) {
throw new BadCondition("No attribute.");
}
return new Item(
type: Type::from($type),
value: $value,
attribute: $attribute,
);
}
}

View File

@@ -0,0 +1,59 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* 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\Tools\DynamicLogic;
enum Type: string
{
case And = 'and';
case Or = 'or';
case Not = 'not';
case Equals = 'equals';
case NotEquals = 'notEquals';
case IsEmpty = 'isEmpty';
case IsNotEmpty = 'isNotEmpty';
case IsTrue = 'isTrue';
case IsFalse = 'isFalse';
case Contains = 'contains';
case Has = 'has';
case NotContains = 'notContains';
case NotHas = 'notHas';
case StartsWith = 'startsWith';
case EndsWith = 'endsWith';
case Matches = 'matches';
case GreaterThan = 'greaterThan';
case LessThan = 'lessThan';
case GreaterThanOrEquals = 'greaterThanOrEquals';
case LessThanOrEquals = 'lessThanOrEquals';
case In = 'in';
case NotIn = 'notIn';
case IsToday = 'isToday';
case InFuture = 'inFuture';
case InPast = 'inPast';
}