EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
formula where
Yuri Kuznetsov41a0fd48bb8c
diff --git a/application/Espo/Core/Formula/Functions/EntityGroup/CountRelatedType.php b/application/Espo/Core/Formula/Functions/EntityGroup/CountRelatedType.phpindex ac23b8ae83..ffd5350004 100644--- a/application/Espo/Core/Formula/Functions/EntityGroup/CountRelatedType.php+++ b/application/Espo/Core/Formula/Functions/EntityGroup/CountRelatedType.php@@ -32,23 +32,19 @@ namespace Espo\Core\Formula\Functions\EntityGroup;use Espo\Core\Exceptions\BadRequest;use Espo\Core\Exceptions\Forbidden;use Espo\Core\Formula\Exceptions\Error;+use Espo\Core\Formula\Functions\Base;+use Espo\Core\Formula\Functions\RecordGroup\Util\FindQueryUtil;use Espo\ORM\Defs\Params\RelationParam;-use Espo\ORM\EntityManager;use Espo\Core\Di;use stdClass;-class CountRelatedType extends \Espo\Core\Formula\Functions\Base implements+class CountRelatedType extends Base implementsDi\EntityManagerAware,Di\SelectBuilderFactoryAware{use Di\EntityManagerSetter;use Di\SelectBuilderFactorySetter;- /**- * @var EntityManager- */- protected $entityManager;-/*** @return int* @throws Error@@ -86,7 +82,7 @@ class CountRelatedType extends \Espo\Core\Formula\Functions\Base implements->from($foreignEntityType);if ($filter) {- $builder->withPrimaryFilter($filter);+ (new FindQueryUtil())->applyFilter($builder, $filter, 2);}try {diff --git a/application/Espo/Core/Formula/Functions/EntityGroup/IsRelatedType.php b/application/Espo/Core/Formula/Functions/EntityGroup/IsRelatedType.phpindex 0d2d378894..37099cf895 100644--- a/application/Espo/Core/Formula/Functions/EntityGroup/IsRelatedType.php+++ b/application/Espo/Core/Formula/Functions/EntityGroup/IsRelatedType.php@@ -29,25 +29,18 @@namespace Espo\Core\Formula\Functions\EntityGroup;-use Espo\Core\Exceptions\Error;--use Espo\ORM\EntityManager;+use Espo\Core\Formula\Exceptions\Error;+use Espo\Core\Formula\Functions\Base;use Espo\Core\Di;-class IsRelatedType extends \Espo\Core\Formula\Functions\Base implements+class IsRelatedType extends Base implementsDi\EntityManagerAware{use Di\EntityManagerSetter;- /**- * @var EntityManager- */- protected $entityManager;-/*** @return bool- * @throws \Espo\Core\Formula\Exceptions\Error* @throws Error*/public function process(\stdClass $item)diff --git a/application/Espo/Core/Formula/Functions/EntityGroup/SumRelatedType.php b/application/Espo/Core/Formula/Functions/EntityGroup/SumRelatedType.phpindex aadb5a9521..551d149b6d 100644--- a/application/Espo/Core/Formula/Functions/EntityGroup/SumRelatedType.php+++ b/application/Espo/Core/Formula/Functions/EntityGroup/SumRelatedType.php@@ -29,16 +29,18 @@namespace Espo\Core\Formula\Functions\EntityGroup;-use Espo\Core\Exceptions\Error;-+use Espo\Core\Exceptions\BadRequest;+use Espo\Core\Formula\Exceptions\Error;use Espo\Core\Di;-+use Espo\Core\Exceptions\Forbidden;+use Espo\Core\Formula\Functions\Base;+use Espo\Core\Formula\Functions\RecordGroup\Util\FindQueryUtil;use Espo\ORM\Defs\Params\RelationParam;use Espo\ORM\Name\Attribute;use stdClass;use PDO;-class SumRelatedType extends \Espo\Core\Formula\Functions\Base implements+class SumRelatedType extends Base implementsDi\EntityManagerAware,Di\SelectBuilderFactoryAware{@@ -48,7 +50,6 @@ class SumRelatedType extends \Espo\Core\Formula\Functions\Base implements/*** @return float* @throws Error- * @throws \Espo\Core\Formula\Exceptions\Error*/public function process(stdClass $item){@@ -96,10 +97,14 @@ class SumRelatedType extends \Espo\Core\Formula\Functions\Base implements->from($foreignEntityType);if ($filter) {- $builder->withPrimaryFilter($filter);+ (new FindQueryUtil())->applyFilter($builder, $filter, 3);}- $queryBuilder = $builder->buildQueryBuilder();+ try {+ $queryBuilder = $builder->buildQueryBuilder();+ } catch (BadRequest|Forbidden $e) {+ throw new Error($e->getMessage(), $e->getCode(), $e);+ }$queryBuilder->select([[$foreignLinkAlias . '.id', 'foreignId'],diff --git a/application/Espo/Core/Formula/Functions/RecordGroup/CountType.php b/application/Espo/Core/Formula/Functions/RecordGroup/CountType.phpindex 870bb897b7..7969a577e1 100644--- a/application/Espo/Core/Formula/Functions/RecordGroup/CountType.php+++ b/application/Espo/Core/Formula/Functions/RecordGroup/CountType.php@@ -29,11 +29,12 @@namespace Espo\Core\Formula\Functions\RecordGroup;-use Espo\Core\Formula\{- Functions\BaseFunction,- ArgumentList,-};-+use Espo\Core\Exceptions\BadRequest;+use Espo\Core\Exceptions\Forbidden;+use Espo\Core\Formula\ArgumentList;+use Espo\Core\Formula\Exceptions\Error;+use Espo\Core\Formula\Functions\BaseFunction;+use Espo\Core\Formula\Functions\RecordGroup\Util\FindQueryUtil;use Espo\Core\Di;class CountType extends BaseFunction implements@@ -54,7 +55,7 @@ class CountType extends BaseFunction implementsif (count($args) < 3) {$filter = null;- if (count($args) == 2) {+ if (count($args) === 2) {$filter = $this->evaluate($args[1]);}@@ -62,18 +63,16 @@ class CountType extends BaseFunction implements->create()->from($entityType);- if ($filter && !is_string($filter)) {- $this->throwBadArgumentType(2, 'string');- }+ (new FindQueryUtil())->applyFilter($builder, $filter, 2);- if ($filter) {- $builder->withPrimaryFilter($filter);+ try {+ return $this->entityManager+ ->getRDBRepository($entityType)+ ->clone($builder->build())+ ->count();+ } catch (BadRequest|Forbidden $e) {+ throw new Error($e->getMessage(), 0, $e);}-- return $this->entityManager- ->getRDBRepository($entityType)- ->clone($builder->build())- ->count();}$whereClause = [];diff --git a/application/Espo/Core/Formula/Functions/RecordGroup/ExistsType.php b/application/Espo/Core/Formula/Functions/RecordGroup/ExistsType.phpindex ec9126ce36..72872e5980 100644--- a/application/Espo/Core/Formula/Functions/RecordGroup/ExistsType.php+++ b/application/Espo/Core/Formula/Functions/RecordGroup/ExistsType.php@@ -29,40 +29,63 @@namespace Espo\Core\Formula\Functions\RecordGroup;-use Espo\ORM\EntityManager;--use Espo\Core\Formula\{- Functions\BaseFunction,- ArgumentList,-};+use Espo\Core\Exceptions\BadRequest;+use Espo\Core\Exceptions\Forbidden;+use Espo\Core\Formula\ArgumentList;+use Espo\Core\Formula\Exceptions\Error;+use Espo\Core\Formula\Functions\BaseFunction;use Espo\Core\Di;+use Espo\Core\Formula\Functions\RecordGroup\Util\FindQueryUtil;class ExistsType extends BaseFunction implements- Di\EntityManagerAware+ Di\EntityManagerAware,+ Di\SelectBuilderFactoryAware{use Di\EntityManagerSetter;-- /**- * @var EntityManager- */- protected $entityManager;+ use Di\SelectBuilderFactorySetter;public function process(ArgumentList $args){- if (count($args) < 3) {- $this->throwTooFewArguments(3);+ if (count($args) < 1) {+ $this->throwTooFewArguments(1);}$entityType = $this->evaluate($args[0]);+ if (count($args) <= 2) {+ $filter = null;++ if (count($args) === 2) {+ $filter = $this->evaluate($args[1]);+ }++ $builder = $this->selectBuilderFactory+ ->create()+ ->from($entityType);++ (new FindQueryUtil())->applyFilter($builder, $filter, 2);++ try {+ return (bool) $this->entityManager+ ->getRDBRepository($entityType)+ ->clone($builder->build())+ ->findOne();+ } catch (BadRequest|Forbidden $e) {+ throw new Error($e->getMessage(), 0, $e);+ }+ }+$whereClause = [];$i = 1;+while ($i < count($args) - 1) {$key = $this->evaluate($args[$i]);$value = $this->evaluate($args[$i + 1]);+$whereClause[] = [$key => $value];+$i = $i + 2;}diff --git a/application/Espo/Core/Formula/Functions/RecordGroup/FindManyType.php b/application/Espo/Core/Formula/Functions/RecordGroup/FindManyType.phpindex 9d5b2bcc19..3ef1d2f61c 100644--- a/application/Espo/Core/Formula/Functions/RecordGroup/FindManyType.php+++ b/application/Espo/Core/Formula/Functions/RecordGroup/FindManyType.php@@ -33,9 +33,11 @@ use Espo\Core\Exceptions\BadRequest;use Espo\Core\Exceptions\Forbidden;use Espo\Core\Formula\EvaluatedArgumentList;use Espo\Core\Formula\Exceptions\BadArgumentType;+use Espo\Core\Formula\Exceptions\BadArgumentValue;use Espo\Core\Formula\Exceptions\Error as FormulaError;use Espo\Core\Formula\Exceptions\TooFewArguments;use Espo\Core\Formula\Func;+use Espo\Core\Formula\Functions\RecordGroup\Util\FindQueryUtil;use Espo\Core\Select\SelectBuilderFactory;use Espo\ORM\Entity;use Espo\ORM\EntityManager;@@ -76,10 +78,18 @@ class FindManyType implements Functhrow BadArgumentType::create(3, 'string|null');}- if (!is_bool($order) && !is_string($orderBy)) {+ if (!is_bool($order) && !is_string($order)) {throw BadArgumentType::create(4, 'string|bool');}+ if (is_string($order)) {+ $order = strtoupper($order);++ if ($order !== Order::ASC && $order !== Order::DESC) {+ throw BadArgumentValue::create(4, 'Bad order value.');+ }+ }+$builder = $this->selectBuilderFactory->create()->from($entityType);@@ -93,13 +103,7 @@ class FindManyType implements Func$filter = $arguments[4];}- if ($filter && !is_string($filter)) {- throw BadArgumentType::create(5, 'string');- }-- if ($filter) {- $builder->withPrimaryFilter($filter);- }+ (new FindQueryUtil())->applyFilter($builder, $filter, 5);} else {$i = 4;diff --git a/application/Espo/Core/Formula/Functions/RecordGroup/FindOneType.php b/application/Espo/Core/Formula/Functions/RecordGroup/FindOneType.phpindex cd4802bc9b..817e81b318 100644--- a/application/Espo/Core/Formula/Functions/RecordGroup/FindOneType.php+++ b/application/Espo/Core/Formula/Functions/RecordGroup/FindOneType.php@@ -32,11 +32,21 @@ namespace Espo\Core\Formula\Functions\RecordGroup;use Espo\Core\Exceptions\BadRequest;use Espo\Core\Exceptions\Forbidden;use Espo\Core\Formula\ArgumentList;+use Espo\Core\Formula\Exceptions\Error;use Espo\Core\Formula\Exceptions\Error as FormulaError;use Espo\Core\Formula\Functions\BaseFunction;use Espo\Core\Di;+use Espo\Core\Formula\Functions\RecordGroup\Util\FindQueryUtil;+use Espo\Core\Select\Where\Item;+use Espo\Core\Utils\Json;use Espo\ORM\Name\Attribute;+use Espo\ORM\Query\Part\Order;+use InvalidArgumentException;+use stdClass;+/**+ * @noinspection PhpUnused+ */class FindOneType extends BaseFunction implementsDi\EntityManagerAware,Di\SelectBuilderFactoryAware@@ -52,7 +62,7 @@ class FindOneType extends BaseFunction implements$entityType = $this->evaluate($args[0]);$orderBy = $this->evaluate($args[1]);- $order = $this->evaluate($args[2]) ?? 'ASC';+ $order = $this->evaluate($args[2]) ?? Order::ASC;$builder = $this->selectBuilderFactory->create()@@ -63,17 +73,11 @@ class FindOneType extends BaseFunction implementsif (count($args) <= 4) {$filter = null;- if (count($args) == 4) {+ if (count($args) === 4) {$filter = $this->evaluate($args[3]);}- if ($filter && !is_string($filter)) {- $this->throwBadArgumentType(4, 'string');- }-- if ($filter) {- $builder->withPrimaryFilter($filter);- }+ (new FindQueryUtil())->applyFilter($builder, $filter, 4);} else {$i = 3;diff --git a/application/Espo/Core/Formula/Functions/RecordGroup/FindRelatedManyType.php b/application/Espo/Core/Formula/Functions/RecordGroup/FindRelatedManyType.phpindex a31f144212..d4225985d2 100644--- a/application/Espo/Core/Formula/Functions/RecordGroup/FindRelatedManyType.php+++ b/application/Espo/Core/Formula/Functions/RecordGroup/FindRelatedManyType.php@@ -35,6 +35,7 @@ use Espo\Core\Formula\Exceptions\BadArgumentType;use Espo\Core\Formula\Exceptions\Error;use Espo\Core\Formula\Exceptions\ExecutionException;use Espo\Core\Formula\Exceptions\TooFewArguments;+use Espo\Core\Formula\Functions\RecordGroup\Util\FindQueryUtil;use Espo\Core\ORM\Entity as CoreEntity;use Espo\Core\Formula\ArgumentList;use Espo\Core\Formula\Functions\BaseFunction;@@ -42,6 +43,7 @@ use Espo\Core\Di;use Espo\Core\Select\Helpers\RandomStringGenerator;use Espo\ORM\Defs\Params\RelationParam;use Espo\ORM\Name\Attribute;+use Espo\ORM\Type\RelationType;class FindRelatedManyType extends BaseFunction implementsDi\EntityManagerAware,@@ -56,10 +58,8 @@ class FindRelatedManyType extends BaseFunction implements/*** @throws Error- * @throws BadRequest* @throws TooFewArguments* @throws BadArgumentType- * @throws Forbidden* @throws ExecutionException*/public function process(ArgumentList $args)@@ -164,13 +164,7 @@ class FindRelatedManyType extends BaseFunction implements$filter = $args[6];}- if ($filter && !is_string($filter)) {- $this->throwError("Bad filter.");- }-- if ($filter) {- $builder->withPrimaryFilter($filter);- }+ (new FindQueryUtil())->applyFilter($builder, $filter, 7);} else {$i = 6;@@ -184,13 +178,17 @@ class FindRelatedManyType extends BaseFunction implements}}- $queryBuilder = $builder->buildQueryBuilder();+ try {+ $queryBuilder = $builder->buildQueryBuilder();+ } catch (BadRequest|Forbidden $e) {+ throw new Error($e->getMessage(), 0, $e);+ }if (!empty($whereClause)) {$queryBuilder->where($whereClause);}- if ($relationType === 'hasChildren') {+ if ($relationType === RelationType::HAS_CHILDREN) {$queryBuilder->where([$foreignLink . 'Id' => $entity->getId(),$foreignLink . 'Type' => $entity->getEntityType(),diff --git a/application/Espo/Core/Formula/Functions/RecordGroup/FindRelatedOneType.php b/application/Espo/Core/Formula/Functions/RecordGroup/FindRelatedOneType.phpindex 4380ccc2f2..cb7c1c54d0 100644--- a/application/Espo/Core/Formula/Functions/RecordGroup/FindRelatedOneType.php+++ b/application/Espo/Core/Formula/Functions/RecordGroup/FindRelatedOneType.php@@ -29,14 +29,17 @@namespace Espo\Core\Formula\Functions\RecordGroup;+use Espo\Core\Exceptions\BadRequest;+use Espo\Core\Exceptions\Forbidden;use Espo\Core\ORM\Entity as CoreEntity;use Espo\ORM\Defs\Params\RelationParam;use Espo\ORM\Name\Attribute;use Espo\Core\Formula\{+ Exceptions\Error,Functions\BaseFunction,ArgumentList,-};+ Functions\RecordGroup\Util\FindQueryUtil};use Espo\Core\Di;@@ -143,17 +146,11 @@ class FindRelatedOneType extends BaseFunction implementsif (count($args) <= 6) {$filter = null;- if (count($args) == 6) {+ if (count($args) === 6) {$filter = $this->evaluate($args[5]);}- if ($filter && !is_string($filter)) {- $this->throwError("Bad filter.");- }-- if ($filter) {- $builder->withPrimaryFilter($filter);- }+ (new FindQueryUtil())->applyFilter($builder, $filter, 6);} else {$i = 5;@@ -167,7 +164,11 @@ class FindRelatedOneType extends BaseFunction implements}}- $queryBuilder = $builder->buildQueryBuilder();+ try {+ $queryBuilder = $builder->buildQueryBuilder();+ } catch (BadRequest|Forbidden $e) {+ throw new Error($e->getMessage(), 0, $e);+ }if (!empty($whereClause)) {$queryBuilder->where($whereClause);diff --git a/application/Espo/Core/Formula/Functions/RecordGroup/Util/FindQueryUtil.php b/application/Espo/Core/Formula/Functions/RecordGroup/Util/FindQueryUtil.phpnew file mode 100644index 0000000000..2b6d5251c3--- /dev/null+++ b/application/Espo/Core/Formula/Functions/RecordGroup/Util/FindQueryUtil.php@@ -0,0 +1,74 @@+<?php+/************************************************************************+ * This file is part of EspoCRM.+ *+ * EspoCRM – Open Source CRM application.+ * Copyright (C) 2014-2025 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\Formula\Functions\RecordGroup\Util;++use Espo\Core\Formula\Exceptions\BadArgumentType;+use Espo\Core\Formula\Exceptions\Error;+use Espo\Core\Select\SelectBuilder;+use Espo\Core\Select\Where\Item;+use Espo\Core\Utils\Json;+use InvalidArgumentException;+use stdClass;++class FindQueryUtil+{+ public function __construct() {}++ /**+ * @throws Error+ * @throws BadArgumentType+ */+ public function applyFilter(SelectBuilder $builder, mixed $filter, int $position): void+ {+ if (is_string($filter)) {+ $builder->withPrimaryFilter($filter);++ return;+ }++ if ($filter instanceof stdClass) {+ $rawWhere = Json::decode(Json::encode($filter), true);++ try {+ $where = Item::fromRaw($rawWhere);+ } catch (InvalidArgumentException $e) {+ throw new Error($e->getMessage(), 0, $e);+ }++ $builder->withWhere($where);++ return;+ }++ if ($filter) {+ throw BadArgumentType::create($position, 'string|object');+ }+ }+}