From 4cb8df2d0c41eaca6e3918372a79f9cb25e665b3 Mon Sep 17 00:00:00 2001 From: Yurii Date: Sun, 5 Apr 2026 13:18:15 +0300 Subject: [PATCH] populate default pipeline --- .../Core/Record/Defaults/DefaultPopulator.php | 77 +++++++++++++++++++ .../Pipeline/UserPipelineDataProvider.php | 61 +++++++++++++++ .../Espo/Tools/Pipeline/PipelineTest.php | 11 +++ 3 files changed, 149 insertions(+) create mode 100644 application/Espo/Tools/Pipeline/UserPipelineDataProvider.php diff --git a/application/Espo/Core/Record/Defaults/DefaultPopulator.php b/application/Espo/Core/Record/Defaults/DefaultPopulator.php index fa383c836c..1763c6b3ee 100644 --- a/application/Espo/Core/Record/Defaults/DefaultPopulator.php +++ b/application/Espo/Core/Record/Defaults/DefaultPopulator.php @@ -47,6 +47,10 @@ use Espo\Modules\Crm\Entities\Contact; use Espo\ORM\Defs\Params\FieldParam; use Espo\ORM\Entity; use Espo\ORM\EntityManager; +use Espo\Tools\Pipeline\Data\PipelineData; +use Espo\Tools\Pipeline\MetadataProvider as PipelineMetadata; +use Espo\Tools\Pipeline\UserPipelineDataProvider; +use LogicException; use RuntimeException; /** @@ -61,6 +65,8 @@ class DefaultPopulator implements Populator private EntityManager $entityManager, private CurrencyConfigDataProvider $currencyConfig, private Metadata $metadata, + private PipelineMetadata $pipelineMetadata, + private UserPipelineDataProvider $userPipelineDataProvider, ) {} public function populate(Entity $entity): void @@ -69,6 +75,7 @@ class DefaultPopulator implements Populator $this->processDefaultTeam($entity); $this->processCurrency($entity); $this->processPortal($entity); + $this->processPipeline($entity); } /** @@ -275,4 +282,74 @@ class DefaultPopulator implements Populator $entity->setValueObject($link, $linkMultiple); } } + + private function processPipeline(Entity $entity): void + { + $entityType = $entity->getEntityType(); + + if (!$this->pipelineMetadata->isEnabled($entityType)) { + return; + } + + $pipelineIdAttr = Field::PIPELINE . 'Id'; + $stageIdAttr = Field::PIPELINE_STAGE . 'Id'; + + if ( + $entity->get($pipelineIdAttr) && + $entity->get($stageIdAttr) + ) { + return; + } + + if (!$entity->get($pipelineIdAttr)) { + $pipeline = $this->userPipelineDataProvider->getForEntityType($entityType)[0] ?? null; + + if (!$pipeline) { + return; + } + + $entity->setMultiple([ + $pipelineIdAttr => $pipeline->id, + Field::PIPELINE . 'Name' => $pipeline->name, + ]); + } + + $id = $entity->get($pipelineIdAttr); + + if (!$id) { + throw new LogicException(); + } + + if ($entity->get($stageIdAttr)) { + return; + } + + $pipeline = $this->getPipeline($entityType, $id); + + if (!$pipeline) { + return; + } + + $stage = $pipeline->stages[0] ?? null; + + if (!$stage) { + return; + } + + $entity->setMultiple([ + $stageIdAttr => $stage->id, + Field::PIPELINE_STAGE . 'Name' => $stage->name, + ]); + } + + private function getPipeline(string $entityType, mixed $id): ?PipelineData + { + foreach ($this->userPipelineDataProvider->getForEntityType($entityType) as $it) { + if ($id === $it->id) { + return $it; + } + } + + return null; + } } diff --git a/application/Espo/Tools/Pipeline/UserPipelineDataProvider.php b/application/Espo/Tools/Pipeline/UserPipelineDataProvider.php new file mode 100644 index 0000000000..25dbdd6d8d --- /dev/null +++ b/application/Espo/Tools/Pipeline/UserPipelineDataProvider.php @@ -0,0 +1,61 @@ +. + * + * 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\Pipeline; + +use Espo\Tools\Pipeline\Data\PipelineData; + +/** + * @since 9.4.0 + */ +class UserPipelineDataProvider +{ + public function __construct( + private PipelineDataProvider $pipelineDataProvider, + private PipelineDataUserFilter $userFilter, + ) {} + + /** + * @return PipelineData[] + */ + public function getForEntityType(string $entityType): array + { + return $this->get()[$entityType] ?? []; + } + + /** + * @return array + */ + private function get(): array + { + $pipelines = $this->pipelineDataProvider->get(); + + return $this->userFilter->filter($pipelines); + } +} diff --git a/tests/integration/Espo/Tools/Pipeline/PipelineTest.php b/tests/integration/Espo/Tools/Pipeline/PipelineTest.php index 51653d2c05..5fed1613c2 100644 --- a/tests/integration/Espo/Tools/Pipeline/PipelineTest.php +++ b/tests/integration/Espo/Tools/Pipeline/PipelineTest.php @@ -180,6 +180,17 @@ class PipelineTest extends BaseTestCase // + $opp = $oppService->create((object) [ + Field::NAME => 'Opp 3', + Opportunity::FIELD_CLOSED_DATE => Date::fromString('2026-01-01')->toString(), + Opportunity::FIELD_AMOUNT => 1000, + ], CreateParams::create()); + + $this->assertEquals($pipeline1->getId(), $opp->get(Field::PIPELINE . 'Id')); + $this->assertEquals($pipeline1->getStages()[0]->getId(), $opp->get(Field::PIPELINE_STAGE . 'Id')); + + // + $pipelinesParam = $this->getInjectableFactory()->create(Pipelines::class); $pipelines = $pipelinesParam->get();