Skip to content

EspoCRM – Open Source CRM Application

Do not allow removing used stage

Yurii3622ac5e8392
diff --git a/application/Espo/Classes/Record/Hooks/PipelineStage/BeforeDelete.php b/application/Espo/Classes/Record/Hooks/PipelineStage/BeforeDelete.php
new file mode 100644
index 0000000000..1d32b2c3da
--- /dev/null
+++ b/application/Espo/Classes/Record/Hooks/PipelineStage/BeforeDelete.php
@@ -0,0 +1,77 @@
+<?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\Classes\Record\Hooks\PipelineStage;
+
+use Espo\Core\Exceptions\Conflict;
+use Espo\Core\Exceptions\Error\Body;
+use Espo\Core\Name\Field;
+use Espo\Core\Record\DeleteParams;
+use Espo\Core\Record\Hook\DeleteHook;
+use Espo\Entities\PipelineStage;
+use Espo\ORM\Entity;
+use Espo\ORM\EntityManager;
+use Throwable;
+
+/**
+ * @implements DeleteHook<PipelineStage>
+ */
+class BeforeDelete implements DeleteHook
+{
+ public function __construct(
+ private EntityManager $entityManager,
+ ) {}
+
+ public function process(Entity $entity, DeleteParams $params): void
+ {
+ try {
+ $pipeline = $entity->getPipeline();
+ } catch (Throwable) {
+ return;
+ }
+
+ $entityType = $entity->getPipeline()->getTargetEntityType();
+
+ if (!$this->entityManager->hasRepository($entityType)) {
+ return;
+ }
+
+ $one = $this->entityManager
+ ->getRDBRepository($entityType)
+ ->where([Field::PIPELINE_STAGE . 'Id' => $entity->getId()])
+ ->findOne();
+
+ if ($one) {
+ throw Conflict::createWithBody(
+ 'cannotRemoveUsed',
+ Body::create()->withMessageTranslation('cannotRemoveUsed')
+ );
+ }
+ }
+}
diff --git a/application/Espo/Resources/metadata/recordDefs/PipelineStage.json b/application/Espo/Resources/metadata/recordDefs/PipelineStage.json
index 28ae1364f8..428c762d95 100644
--- a/application/Espo/Resources/metadata/recordDefs/PipelineStage.json
+++ b/application/Espo/Resources/metadata/recordDefs/PipelineStage.json
@@ -1,4 +1,7 @@
{
+ "beforeDeleteHookClassNameList": [
+ "Espo\\Classes\\Record\\Hooks\\PipelineStage\\BeforeDelete"
+ ],
"afterCreateHookClassNameList": [
"Espo\\Classes\\Record\\Hooks\\PipelineStage\\AfterSave"
],