snapshot of https://github.com/espocrm/espocrm.git (master, 2026-09-18; blobs > 131072B omitted)
Some checks failed
Test / Test on PHP 8.3 (push) Failing after 5s
Test / Test on PHP 8.4 (push) Failing after 4s
Test / Test on PHP 8.5 (push) Failing after 3s
TypeScript static analysis / TypeScript check (push) Failing after 14s
Test Integration on MySQL / Test on PHP 8.3 (push) Failing after 2s
Test Integration on MySQL / Test on PHP 8.5 (push) Failing after 0s
Test Integration on PostgreSQL / Test on PHP 8.4 fix (push) Failing after 1s
Test Integration on PostgreSQL / Test on PHP 8.4 master (push) Failing after 1s

This commit is contained in:
upstream-mirror
2026-09-18 04:19:15 +05:00
commit 7cc7e6b4f3
10077 changed files with 900756 additions and 0 deletions

View File

@@ -0,0 +1,126 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 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\Pipeline\Api;
use Espo\Core\Acl;
use Espo\Core\Api\Action;
use Espo\Core\Api\Request;
use Espo\Core\Api\Response;
use Espo\Core\Api\ResponseComposer;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Exceptions\NotFound;
use Espo\Core\Record\EntityProvider;
use Espo\Core\Select\SearchParams;
use Espo\Core\Utils\Json;
use Espo\Entities\Pipeline;
use Espo\Tools\Pipeline\MoveService;
/**
* @noinspection PhpUnused
*/
class PostMove implements Action
{
public function __construct(
private EntityProvider $entityProvider,
private Acl $acl,
private MoveService $moveService,
) {}
public function process(Request $request): Response
{
$entity = $this->getEntity($request);
$type = $this->fetchType($request);
$searchParams = $this->fetchSearchParams($request);
$this->moveService->move($entity, $type, $searchParams);
return ResponseComposer::json(true);
}
/**
* @throws BadRequest
* @throws Forbidden
* @throws NotFound
*/
private function getEntity(Request $request): Pipeline
{
$id = $request->getRouteParam('id') ?? throw new BadRequest();
if (!$this->acl->checkScope(Pipeline::ENTITY_TYPE)) {
throw new Forbidden();
}
$entity = $this->entityProvider->getByClass(Pipeline::class, $id);
if (!$this->acl->checkEntityEdit($entity)) {
throw new Forbidden();
}
return $entity;
}
/**
* @return MoveService::TYPE_TOP|MoveService::TYPE_UP|MoveService::TYPE_DOWN|MoveService::TYPE_BOTTOM
* @throws BadRequest
*/
private function fetchType(Request $request): string
{
$type = $request->getRouteParam('type') ?? throw new BadRequest();
if (
!in_array($type, [
MoveService::TYPE_TOP,
MoveService::TYPE_UP,
MoveService::TYPE_DOWN,
MoveService::TYPE_BOTTOM,
])
) {
throw new BadRequest("Bad type.");
}
return $type;
}
private function fetchSearchParams(Request $request): SearchParams
{
$body = $request->getParsedBody();
$searchParams = SearchParams::create();
if ($body->whereGroup ?? null) {
$rawWhere = Json::decode(Json::encode($body->whereGroup), true);
$searchParams = SearchParams::fromRaw(['where' => $rawWhere]);
}
return $searchParams;
}
}

View File

@@ -0,0 +1,111 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 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\Pipeline\Api;
use Espo\Core\Acl;
use Espo\Core\Api\Action;
use Espo\Core\Api\Request;
use Espo\Core\Api\Response;
use Espo\Core\Api\ResponseComposer;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Exceptions\NotFound;
use Espo\Core\Record\EntityProvider;
use Espo\Entities\PipelineStage;
use Espo\Tools\Pipeline\MoveService;
use Espo\Tools\Pipeline\StageMoveService;
/**
* @noinspection PhpUnused
*/
class PostStageMove implements Action
{
public function __construct(
private EntityProvider $entityProvider,
private Acl $acl,
private StageMoveService $moveService,
) {}
public function process(Request $request): Response
{
$entity = $this->getEntity($request);
$type = $this->fetchType($request);
if ($type === MoveService::TYPE_UP) {
$this->moveService->moveUp($entity);
} else {
$this->moveService->moveDown($entity);
}
return ResponseComposer::json(true);
}
/**
* @throws BadRequest
* @throws Forbidden
* @throws NotFound
*/
private function getEntity(Request $request): PipelineStage
{
$id = $request->getRouteParam('id') ?? throw new BadRequest();
if (!$this->acl->checkScope(PipelineStage::ENTITY_TYPE)) {
throw new Forbidden();
}
$entity = $this->entityProvider->getByClass(PipelineStage::class, $id);
if (!$this->acl->checkEntityEdit($entity)) {
throw new Forbidden();
}
return $entity;
}
/**
* @return MoveService::TYPE_UP|MoveService::TYPE_DOWN
* @throws BadRequest
*/
private function fetchType(Request $request): string
{
$type = $request->getRouteParam('type') ?? throw new BadRequest();
if (
!in_array($type, [
MoveService::TYPE_UP,
MoveService::TYPE_DOWN,
])
) {
throw new BadRequest("Not supported type.");
}
return $type;
}
}

View File

@@ -0,0 +1,53 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 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\Pipeline;
use Espo\Core\Utils\DataCache;
use Espo\Core\Utils\Event\EventDispatcher;
use Espo\Core\WebSocket\Submission;
use Espo\Tools\Pipeline\Events\UpdateGlobal;
class CacheClearer
{
private const string CACHE_KEY = 'pipelines';
public function __construct(
private DataCache $dataCache,
private Submission $submission,
private EventDispatcher $eventDispatcher,
) {}
public function clear(): void
{
$this->dataCache->clear(self::CACHE_KEY);
$this->eventDispatcher->dispatch(new UpdateGlobal());
$this->submission->submit('appParamsUpdate');
}
}

View File

@@ -0,0 +1,48 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 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\Pipeline\Data;
readonly class PipelineData
{
/**
* @param string $entityType
* @param StageData[] $stages
* @param string[] $teamIds
*/
public function __construct(
public string $id,
public string $name,
public string $entityType,
public array $stages,
public bool $isAvailableForAll,
public array $teamIds,
public ?int $color,
) {}
}

View File

@@ -0,0 +1,40 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 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\Pipeline\Data;
readonly class StageData
{
public function __construct(
public string $id,
public string $name,
public string $mappedStatus,
public ?string $style,
) {}
}

View File

@@ -0,0 +1,46 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 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\Pipeline\Events;
use Espo\Core\Utils\Event\CrossInstanceEvent;
use stdClass;
class UpdateGlobal implements CrossInstanceEvent
{
public static function fromPayload(stdClass $payload): CrossInstanceEvent
{
return new self();
}
public function toPayload(): stdClass
{
return (object) [];
}
}

View File

@@ -0,0 +1,49 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 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\Pipeline;
use Espo\Core\Utils\Metadata;
class MetadataProvider
{
public function __construct(
private Metadata $metadata,
) {}
public function isEnabled(string $entityType): bool
{
return $this->metadata->get("scopes.$entityType.pipelines") === true;
}
public function getStatusField(string $entityType): ?string
{
return $this->metadata->get("scopes.$entityType.statusField");
}
}

View File

@@ -0,0 +1,259 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 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\Pipeline;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\ORM\Repository\Option\SaveOption;
use Espo\Core\Select\SearchParams;
use Espo\Core\Select\SelectBuilderFactory;
use Espo\Entities\Pipeline;
use Espo\Entities\PipelineStage;
use Espo\ORM\EntityManager;
use Espo\ORM\Name\Attribute;
use Espo\ORM\Query\Part\Order;
use Espo\ORM\Query\SelectBuilder;
class MoveService
{
public const string TYPE_TOP = 'top';
public const string TYPE_BOTTOM = 'bottom';
public const string TYPE_UP = 'up';
public const string TYPE_DOWN = 'down';
public function __construct(
private EntityManager $entityManager,
private SelectBuilderFactory $selectBuilderFactory,
private CacheClearer $cacheClearer,
) {}
/**
* @param self::TYPE_TOP|self::TYPE_BOTTOM|self::TYPE_UP|self::TYPE_DOWN $type
* @throws BadRequest
* @throws Forbidden
*/
public function move(
Pipeline $entity,
string $type,
SearchParams $searchParams,
): void {
$builder = $this->createSelectBuilder($searchParams, $entity);
if ($type === self::TYPE_TOP) {
$this->moveToTop($entity, $builder);
} else if ($type === self::TYPE_BOTTOM) {
$this->moveToBottom($entity, $builder);
} else if ($type === self::TYPE_UP) {
$this->moveUp($entity, $builder);
} else {
$this->moveDown($entity, $builder);
}
$this->reOrder($entity::class);
$this->cacheClearer->clear();
}
/**
* @param class-string<Pipeline|PipelineStage> $className
*/
public function reOrder(string $className, ?string $parentId = null): void
{
$this->entityManager
->getTransactionManager()
->run(fn () => $this->reOrderInternal($className, $parentId));
}
/**
* @param class-string<Pipeline|PipelineStage> $className
*/
private function reOrderInternal(string $className, ?string $parentId = null): void
{
$this->entityManager
->getRDBRepositoryByClass($className)
->select(Attribute::ID)
->forUpdate()
->sth()
->find();
$builder = $this->entityManager
->getRDBRepositoryByClass($className)
->sth()
->order(Pipeline::FIELD_ORDER);
if ($className === PipelineStage::class) {
$builder->where([
PipelineStage::ATTR_PIPELINE_ID => $parentId,
]);
}
$collection = $builder->find();
foreach ($collection as $i => $entity) {
$order = $i + 1;
if ($entity->getOrder() === $order) {
continue;
}
$entity->set(Pipeline::FIELD_ORDER, $order);
$this->entityManager->saveEntity($entity, [SaveOption::SKIP_HOOKS => true]);
}
}
/**
* @throws BadRequest
* @throws Forbidden
*/
private function createSelectBuilder(
SearchParams $searchParams,
Pipeline $entity,
): SelectBuilder {
/** @noinspection PhpRedundantOptionalArgumentInspection */
return $this->selectBuilderFactory
->create()
->from($entity::ENTITY_TYPE)
->withSearchParams($searchParams)
->withStrictAccessControl()
->buildQueryBuilder()
->limit(null, null)
->order([]);
}
private function moveUp(
Pipeline $entity,
SelectBuilder $builder,
): void {
$query = $builder
->where([Pipeline::FIELD_ORDER . '<' => $entity->getOrder()])
->order(Pipeline::FIELD_ORDER, Order::DESC)
->build();
$another = $this->entityManager
->getRDBRepositoryByClass($entity::class)
->clone($query)
->findOne();
if (!$another) {
return;
}
$index = $entity->getOrder();
$entity->set(Pipeline::FIELD_ORDER, $another->getOrder());
$another->set(Pipeline::FIELD_ORDER, $index);
$this->entityManager->saveEntity($entity);
$this->entityManager->saveEntity($another);
}
private function moveDown(
Pipeline $entity,
SelectBuilder $builder,
): void {
$query = $builder
->where([Pipeline::FIELD_ORDER . '>' => $entity->getOrder()])
->order(Pipeline::FIELD_ORDER, Order::ASC)
->build();
$another = $this->entityManager
->getRDBRepositoryByClass($entity::class)
->clone($query)
->findOne();
if (!$another) {
return;
}
$index = $entity->getOrder();
$entity->set(Pipeline::FIELD_ORDER, $another->getOrder());
$another->set(Pipeline::FIELD_ORDER, $index);
$this->entityManager->saveEntity($entity);
$this->entityManager->saveEntity($another);
$this->entityManager->refreshEntity($entity);
$this->entityManager->refreshEntity($another);
}
private function moveToTop(
Pipeline $entity,
SelectBuilder $builder,
): void {
$query = $builder
->where([Pipeline::FIELD_ORDER . '<' => $entity->getOrder()])
->order(Pipeline::FIELD_ORDER, Order::ASC)
->build();
$another = $this->entityManager
->getRDBRepositoryByClass($entity::class)
->clone($query)
->findOne();
if (!$another) {
return;
}
$entity->set(Pipeline::FIELD_ORDER, $another->getOrder() - 1);
$this->entityManager->saveEntity($entity);
}
private function moveToBottom(
Pipeline $entity,
SelectBuilder $builder,
): void {
$query = $builder
->where([Pipeline::FIELD_ORDER . '>' => $entity->getOrder()])
->order(Pipeline::FIELD_ORDER, Order::DESC)
->build();
$another = $this->entityManager
->getRDBRepositoryByClass($entity::class)
->clone($query)
->findOne();
if (!$another) {
return;
}
$entity->set(Pipeline::FIELD_ORDER, $another->getOrder() + 1);
$this->entityManager->saveEntity($entity);
}
}

View File

@@ -0,0 +1,348 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 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\Pipeline;
use Espo\Core\Utils\Config\SystemConfig;
use Espo\Core\Utils\DataCache;
use Espo\Core\Utils\Event\EventDispatcher;
use Espo\Core\Utils\Log;
use Espo\Core\Utils\Metadata;
use Espo\Entities\Pipeline;
use Espo\Entities\PipelineStage;
use Espo\ORM\EntityManager;
use Espo\Tools\Pipeline\Data\PipelineData;
use Espo\Tools\Pipeline\Data\StageData;
use Espo\Tools\Pipeline\Events\UpdateGlobal;
use stdClass;
use Throwable;
class PipelineDataProvider
{
private const int LIMIT = 100;
private const string CACHE_KEY = 'pipelines';
/**
* @var ?array<string, PipelineData[]>
*/
private ?array $data = null;
public function __construct(
private Metadata $metadata,
private EntityManager $entityManager,
private SystemConfig $systemConfig,
private DataCache $dataCache,
private Log $log,
EventDispatcher $eventDispatcher,
) {
$eventDispatcher->subscribe(UpdateGlobal::class, function () {
$this->data = null;
});
}
/**
* @return array<string, PipelineData[]>
*/
public function get(): array
{
if ($this->data !== null) {
return $this->data;
}
$data = null;
$store = false;
if ($this->systemConfig->useCache()) {
$data = $this->getFromCache();
if ($data === null) {
$store = true;
}
}
$data ??= $this->build();
if ($store) {
$this->storeCache($data);
}
$this->data = $data;
return $data;
}
/**
* @return ?array<string, PipelineData[]>
*/
private function getFromCache(): ?array
{
if (!$this->dataCache->has(self::CACHE_KEY)) {
return null;
}
try {
return $this->tryGetFromCache();
} catch (Throwable $e) {
$this->log->warning("Pipeline cache read error.", [
'exception' => $e,
]);
return null;
}
}
/**
* @return array<string, PipelineData[]>
*/
private function build(): array
{
$output = [];
foreach ($this->getEntityTypeList() as $entityType) {
$output[$entityType] = $this->buildForEntityType($entityType);
}
return $output;
}
/**
* @return string[]
*/
private function getEntityTypeList(): array
{
$entityTypeList = [];
/** @var array<string, array<string, mixed>> $scopes */
$scopes = $this->metadata->get('scopes') ?? [];
foreach ($scopes as $scope => $defs) {
if (
!($defs['entity'] ?? false) ||
!($defs['pipelines'] ?? false) ||
($defs['disabled'] ?? false)
) {
continue;
}
$entityTypeList[] = $scope;
}
return $entityTypeList;
}
/**
* @return PipelineData[]
*/
private function buildForEntityType(string $entityType): array
{
$pipelines = $this->entityManager
->getRDBRepositoryByClass(Pipeline::class)
->where([
Pipeline::FIELD_STATUS => Pipeline::STATUS_ACTIVE,
Pipeline::FIELD_ENTITY_TYPE=> $entityType,
])
->order(Pipeline::FIELD_ORDER)
->limit(0, self::LIMIT)
->find();
$output = [];
foreach ($pipelines as $pipeline) {
$output[] = new PipelineData(
id: $pipeline->getId(),
name: $pipeline->getName(),
entityType: $entityType,
stages: $this->getStages($pipeline),
isAvailableForAll: $pipeline->isAvailableForAll(),
teamIds: $pipeline->getTeams()->getIdList(),
color: $pipeline->getColor(),
);
}
return $output;
}
/**
* @return StageData[]
*/
private function getStages(Pipeline $pipeline): array
{
$stages = $this->entityManager
->getRDBRepositoryByClass(PipelineStage::class)
->where([
PipelineStage::ATTR_PIPELINE_ID => $pipeline->getId(),
])
->order(PipelineStage::FIELD_ORDER)
->limit(0, self::LIMIT)
->find();
$output = [];
foreach ($stages as $stage) {
$output[] = new StageData(
id: $stage->getId(),
name: $stage->getName(),
mappedStatus: $stage->getMappedStatus(),
style: $this->getStageStyle($pipeline, $stage),
);
}
return $output;
}
/**
* @param array<string, PipelineData[]> $data
*/
private function storeCache(array $data): void
{
$output = (object) [];
foreach ($data as $entityType => $pipelines) {
$subOutput = [];
foreach ($pipelines as $pipeline) {
$subOutput[] = (object) [
'id' => $pipeline->id,
'name' => $pipeline->name,
'entityType' => $entityType,
'stages' => $this->serializeStages($pipeline->stages),
'isAvailableForAll' => $pipeline->isAvailableForAll,
'teamIds' => $pipeline->teamIds,
'color' => $pipeline->color,
];
}
$output->$entityType = $subOutput;
}
$this->dataCache->store(self::CACHE_KEY, $output);
}
/**
* @param array<string, mixed> $data
*/
private function unserializePipelineData(array $data): PipelineData
{
$data['stages'] = $this->unserializeStages($data['stages'] ?? []);
return new PipelineData(...$data);
}
/**
* @param array<int, mixed> $stages
* @return StageData[]
*/
private function unserializeStages(array $stages): array
{
$output = [];
foreach ($stages as $item) {
if (!$item instanceof stdClass) {
continue;
}
$output[] = new StageData(...get_object_vars($item));
}
return $output;
}
/**
* @param StageData[] $stages
* @return stdClass[]
*/
private function serializeStages(array $stages): array
{
$output = [];
foreach ($stages as $stage) {
$output[] = (object) [
'id' => $stage->id,
'name' => $stage->name,
'mappedStatus' => $stage->mappedStatus,
'style' => $stage->style,
];
}
return $output;
}
/**
* @return ?array<string, PipelineData[]>
*/
private function tryGetFromCache(): ?array
{
$data = $this->dataCache->get(self::CACHE_KEY);
if ($data === null) {
return null;
}
if (!$data instanceof stdClass) {
$this->log->warning("Bad pipeline cache.");
return null;
}
$output = [];
foreach (get_object_vars($data) as $entityType => $list) {
if (!is_array($list)) {
$this->log->warning("Bad pipeline cache, non-array.");
return null;
}
$subOutput = [];
foreach ($list as $item) {
if (!$item instanceof stdClass) {
$this->log->warning("Bad pipeline item cache.");
return null;
}
$subOutput[] = $this->unserializePipelineData(get_object_vars($item));
}
$output[$entityType] = $subOutput;
}
return $output;
}
private function getStageStyle(Pipeline $pipeline, PipelineStage $stage): ?string
{
$entityType = $pipeline->getTargetEntityType();
$field = $pipeline->getTargetField();
$status = $stage->getMappedStatus();
return $this->metadata->get("entityDefs.$entityType.fields.$field.style.$status");
}
}

View File

@@ -0,0 +1,79 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 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\Pipeline;
use Espo\Core\Acl;
use Espo\Entities\User;
use Espo\Tools\Pipeline\Data\PipelineData;
class PipelineDataUserFilter
{
public function __construct(
private Acl $acl,
private User $user,
) {}
/**
* @param array<string, PipelineData[]> $data
* @return array<string, PipelineData[]>
*/
public function filter(array $data): array
{
if ($this->user->isEffectiveAdmin()) {
return $data;
}
foreach ($data as $entityType => $pipelines) {
if (!$this->acl->checkScope($entityType)) {
unset($data[$entityType]);
continue;
}
$filtered = array_filter($pipelines, function ($pipelineData) {
if ($pipelineData->isAvailableForAll) {
return true;
}
if ($this->user->isPortal()) {
return false;
}
return array_intersect($pipelineData->teamIds, $this->user->getTeamIdList()) !== [];
});
$filtered = array_values($filtered);
$data[$entityType] = $filtered;
}
return $data;
}
}

View File

@@ -0,0 +1,132 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 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\Pipeline;
use Espo\Core\Exceptions\Conflict;
use Espo\Entities\PipelineStage;
use Espo\ORM\EntityManager;
use RuntimeException;
class StageMoveService
{
public function __construct(
private EntityManager $entityManager,
private CacheClearer $cacheClearer,
) {}
/**
* @throws Conflict
*/
public function moveUp(PipelineStage $stage): void
{
$columns = $this->getStages($stage);
$index = $this->getIndex($columns, $stage);
$anotherColumn = $columns[$index - 1] ?? null;
if (!$anotherColumn) {
throw new Conflict("Can't move first.");
}
$this->swapStages($anotherColumn, $stage);
$this->cacheClearer->clear();
}
/**
* @throws Conflict
*/
public function moveDown(PipelineStage $column): void
{
$columns = $this->getStages($column);
$index = $this->getIndex($columns, $column);
$anotherColumn = $columns[$index + 1] ?? null;
if (!$anotherColumn) {
throw new Conflict("Can't move last.");
}
$this->swapStages($anotherColumn, $column);
}
/**
* @throws Conflict
*/
private function swapStages(PipelineStage $anotherColumn, PipelineStage $column): void
{
if ($anotherColumn->getMappedStatus() !== $column->getMappedStatus()) {
throw new Conflict("Can't break status order.");
}
$order = $column->getOrder();
$column->setOrder($anotherColumn->getOrder());
$anotherColumn->setOrder($order);
$this->entityManager->saveEntity($column);
$this->entityManager->saveEntity($anotherColumn);
}
/**
* @return PipelineStage[]
*/
private function getStages(PipelineStage $stage): array
{
$collection = $this->entityManager
->getRDBRepositoryByClass(PipelineStage::class)
->where([PipelineStage::ATTR_PIPELINE_ID => $stage->getPipeline()->getId()])
->order(PipelineStage::FIELD_ORDER)
->find();
return iterator_to_array($collection);
}
/**
* @param PipelineStage[] $columns
*/
private function getIndex(array $columns, PipelineStage $column): int
{
$index = -1;
foreach ($columns as $i => $it) {
if ($it->getId() === $column->getId()) {
$index = $i;
break;
}
}
if ($index < 0) {
throw new RuntimeException("Stage not found.");
}
return $index;
}
}

View File

@@ -0,0 +1,61 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 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\Pipeline;
use Espo\Tools\Pipeline\Data\PipelineData;
/**
* @since 10.0.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<string, PipelineData[]>
*/
private function get(): array
{
$pipelines = $this->pipelineDataProvider->get();
return $this->userFilter->filter($pipelines);
}
}