EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
deferred actions, note related in context
Yuri Kuznetsov99902b8c1003
diff --git a/application/Espo/Core/FieldProcessing/Relation/LinkMultipleSaver.php b/application/Espo/Core/FieldProcessing/Relation/LinkMultipleSaver.phpindex 564d212089..74108c2567 100644--- a/application/Espo/Core/FieldProcessing/Relation/LinkMultipleSaver.php+++ b/application/Espo/Core/FieldProcessing/Relation/LinkMultipleSaver.php@@ -33,6 +33,7 @@ use Espo\Core\ORM\Entity as CoreEntity;use Espo\Core\FieldProcessing\Saver\Params;use Espo\Core\ORM\EntityManager;+use Espo\Core\ORM\Repository\Option\SaveContext;use Espo\Core\ORM\Repository\Option\SaveOption;use Espo\ORM\Defs\Params\RelationParam;@@ -202,6 +203,8 @@ class LinkMultipleSaver}}+ $saveContext = $params->getOption(SaveContext::NAME);+foreach ($toCreateIdList as $id) {$data = null;@@ -219,6 +222,7 @@ class LinkMultipleSaverSaveOption::SKIP_HOOKS => $skipHooks,SaveOption::SILENT => $entity->isNew(),self::RELATE_OPTION => $entity->hasLinkMultipleField($name),+ SaveContext::NAME => $saveContext,]);}@@ -226,6 +230,7 @@ class LinkMultipleSaver$repository->getRelation($entity, $name)->unrelateById($id, [SaveOption::SKIP_HOOKS => $skipHooks,self::RELATE_OPTION => $entity->hasLinkMultipleField($name),+ SaveContext::NAME => $saveContext,]);}diff --git a/application/Espo/Core/ORM/Repository/Option/SaveContext.php b/application/Espo/Core/ORM/Repository/Option/SaveContext.phpindex 229cc85718..3ff60b8149 100644--- a/application/Espo/Core/ORM/Repository/Option/SaveContext.php+++ b/application/Espo/Core/ORM/Repository/Option/SaveContext.php@@ -29,7 +29,9 @@namespace Espo\Core\ORM\Repository\Option;+use Closure;use Espo\Core\Utils\Util;+use Espo\ORM\Repository\Option\SaveOptions;/*** @since 9.1.0@@ -41,6 +43,9 @@ class SaveContextprivate string $id;private bool $linkUpdated = false;+ /** @var Closure[] */+ private array $deferredActions = [];+public function __construct(?string $id = null,) {@@ -66,4 +71,63 @@ class SaveContext{return $this->linkUpdated;}++ /**+ * Obtain from save options.+ *+ * @return ?self+ * @since 9.2.0.+ */+ public static function obtainFromOptions(SaveOptions $options): ?self+ {+ $saveContext = $options->get(self::NAME);++ if (!$saveContext instanceof self) {+ return null;+ }++ return $saveContext;+ }++ /**+ * Obtain from raw save options.+ *+ * @param array<string, mixed> $options+ * @return ?self+ * @since 9.2.0.+ */+ public static function obtainFromRawOptions(array $options): ?self+ {+ $saveContext = $options[self::NAME] ?? null;++ if (!$saveContext instanceof self) {+ return null;+ }++ return $saveContext;+ }++ /**+ * Add a deferred action.+ *+ * @param Closure $callback A callback.+ * @since 9.2.0.+ */+ public function addDeferredAction(Closure $callback): void+ {+ $this->deferredActions[] = $callback;+ }++ /**+ * @internal+ * @since 9.2.0.+ */+ public function callDeferredActions(): void+ {+ foreach ($this->deferredActions as $callback) {+ $callback();+ }++ $this->deferredActions = [];+ }}diff --git a/application/Espo/Hooks/Common/CallDeferredActions.php b/application/Espo/Hooks/Common/CallDeferredActions.phpnew file mode 100644index 0000000000..50ee5874e1--- /dev/null+++ b/application/Espo/Hooks/Common/CallDeferredActions.php@@ -0,0 +1,54 @@+<?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\Hooks\Common;++use Espo\Core\Hook\Hook\AfterSave;+use Espo\Core\ORM\Repository\Option\SaveContext;+use Espo\ORM\Entity;+use Espo\ORM\Repository\Option\SaveOptions;++/**+ * @implements AfterSave<Entity>+ */+class CallDeferredActions implements AfterSave+{+ public static int $order = 101;++ public function afterSave(Entity $entity, SaveOptions $options): void+ {+ $saveContext = SaveContext::obtainFromOptions($options);++ if (!$saveContext) {+ return;+ }++ $saveContext->callDeferredActions();+ }+}diff --git a/application/Espo/Modules/Crm/Hooks/Opportunity/Contacts.php b/application/Espo/Modules/Crm/Hooks/Opportunity/Contacts.phpindex c107932900..883e8324f2 100644--- a/application/Espo/Modules/Crm/Hooks/Opportunity/Contacts.php+++ b/application/Espo/Modules/Crm/Hooks/Opportunity/Contacts.php@@ -30,6 +30,7 @@namespace Espo\Modules\Crm\Hooks\Opportunity;use Espo\Core\Hook\Hook\AfterSave;+use Espo\Core\ORM\Repository\Option\SaveContext;use Espo\Modules\Crm\Entities\Contact;use Espo\Modules\Crm\Entities\Opportunity;use Espo\ORM\Entity;@@ -62,8 +63,10 @@ class Contacts implements AfterSave->getRelation($entity, 'contacts');if (!$contactId) {- if ($fetchedContactId) {- $relation->unrelateById($fetchedContactId);+ if ($fetchedContactId && $relation->isRelatedById($fetchedContactId)) {+ $relation->unrelateById($fetchedContactId, [+ SaveContext::NAME => $options->get(SaveContext::NAME),+ ]);}return;@@ -85,6 +88,8 @@ class Contacts implements AfterSavereturn;}- $relation->relateById($contactId);+ $relation->relateById($contactId, [+ SaveContext::NAME => $options->get(SaveContext::NAME),+ ]);}}diff --git a/application/Espo/Tools/Stream/HookProcessor.php b/application/Espo/Tools/Stream/HookProcessor.phpindex 5bb014aa3b..757acfa531 100644--- a/application/Espo/Tools/Stream/HookProcessor.php+++ b/application/Espo/Tools/Stream/HookProcessor.php@@ -31,6 +31,7 @@ namespace Espo\Tools\Stream;use Espo\Core\Field\DateTime;use Espo\Core\Name\Field;+use Espo\Core\ORM\Repository\Option\SaveContext;use Espo\Core\ORM\Repository\Option\SaveOption;use Espo\Core\ORM\Entity as CoreEntity;use Espo\Core\Utils\Metadata;@@ -157,7 +158,7 @@ class HookProcessor$type = $relation->getType();if ($type === Entity::BELONGS_TO) {- $this->handleCreateRelatedBelongsTo($entity, $relation, $notifiedEntityTypeList);+ $this->handleCreateRelatedBelongsTo($entity, $relation, $notifiedEntityTypeList, $options);continue;}@@ -179,11 +180,13 @@ class HookProcessor/*** @param string[] $notifiedEntityTypeList+ * @param array<string, mixed> $options*/private function handleCreateRelatedBelongsTo(Entity $entity,RelationDefs $defs,- array &$notifiedEntityTypeList+ array &$notifiedEntityTypeList,+ array $options,): void {if (@@ -211,7 +214,7 @@ class HookProcessorreturn;}- $this->service->noteCreateRelated($entity, $foreignEntityType, $id);+ $this->service->noteCreateRelated($entity, $foreignEntityType, $id, $options);$notifiedEntityTypeList[] = $foreignEntityType;}@@ -573,12 +576,26 @@ class HookProcessor$audited = $this->metadata->get(['entityDefs', $entityType, 'links', $link, 'audited']);$auditedForeign = $this->metadata->get(['entityDefs', $foreignEntityType, 'links', $foreignLink, 'audited']);+ $saveContext = SaveContext::obtainFromRawOptions($options);+if ($audited) {- $this->service->noteRelate($foreignEntity, $entity);+ if ($saveContext) {+ $saveContext->addDeferredAction(+ fn () => $this->service->noteRelate($foreignEntity, $entity, $options)+ );+ } else {+ $this->service->noteRelate($foreignEntity, $entity, $options);+ }}if ($auditedForeign) {- $this->service->noteRelate($entity, $foreignEntity);+ if ($saveContext) {+ $saveContext->addDeferredAction(+ fn () => $this->service->noteRelate($entity, $foreignEntity, $options)+ );+ } else {+ $this->service->noteRelate($entity, $foreignEntity, $options);+ }}}@@ -606,15 +623,29 @@ class HookProcessor$audited = $this->metadata->get(['entityDefs', $entityType, 'links', $link, 'audited']);$auditedForeign = $this->metadata->get(['entityDefs', $foreignEntityType, 'links', $foreignLink, 'audited']);+ $saveContext = SaveContext::obtainFromRawOptions($options);+if ($audited) {- $this->service->noteUnrelate($foreignEntity, $entity);+ if ($saveContext) {+ $saveContext->addDeferredAction(+ fn () => $this->service->noteUnrelate($foreignEntity, $entity, $options)+ );+ } else {+ $this->service->noteUnrelate($foreignEntity, $entity, $options);+ }// @todo// Add time period (a few minutes). If before, remove RELATE note, don't create 'unrelate' if before.}if ($auditedForeign) {- $this->service->noteUnrelate($entity, $foreignEntity);+ if ($saveContext) {+ $saveContext->addDeferredAction(+ fn () => $this->service->noteUnrelate($entity, $foreignEntity, $options)+ );+ } else {+ $this->service->noteUnrelate($entity, $foreignEntity, $options);+ }// @todo// Add time period (a few minutes). If before, remove RELATE note, don't create 'unrelate' if before.