entityManager ->getRDBRepositoryByClass(Workflow::class) ->where([ 'flowchartId' => $entity->getId(), 'isInternal' => true, ]) ->find(); $workflowsToRecreate = false; if ( !$entity->isNew() && json_encode($entity->get('data')) !== json_encode($entity->getFetched('data')) ) { $workflowsToRecreate = true; } if ($entity->isNew() || $workflowsToRecreate) { $this->removeWorkflows($entity); $data = $entity->get('data'); if (isset($data->list) && is_array($data->list)) { foreach ($data->list as $item) { if (!is_object($item)) { continue; } $itId = $item->id ?? null; $itType = $item->type ?? null; $itTriggerType = $item->triggerType ?? null; $itSignal = $item->signal ?? null; if ( $itType === 'eventStartConditional' && in_array($itTriggerType, [ 'afterRecordCreated', 'afterRecordSaved', 'afterRecordUpdated', ]) ) { $workflow = $this->entityManager->getNewEntity(Workflow::ENTITY_TYPE); $conditionsAll = []; if (isset($item->conditionsAll)) { $conditionsAll = $item->conditionsAll; } $conditionsAny = []; if (isset($item->conditionsAny)) { $conditionsAny = $item->conditionsAny; } $conditionsFormula = null; if (isset($item->conditionsFormula)) { $conditionsFormula = $item->conditionsFormula; } $workflow->set([ 'type' => $itTriggerType, 'entityType' => $entity->get('targetType'), 'isInternal' => true, 'flowchartId' => $entity->getId(), 'isActive' => $entity->get('isActive'), 'conditionsAll' => $conditionsAll, 'conditionsAny' => $conditionsAny, 'conditionsFormula' => $conditionsFormula, 'actions' => [ (object) [ 'type' => 'startBpmnProcess', 'flowchartId' => $entity->getId(), 'elementId' => $itId, 'cid' => 0, ] ], 'processOrder' => 100, ]); $this->entityManager->saveEntity($workflow); } if ( $itType === 'eventStartSignal' && $itSignal ) { $workflow = $this->entityManager->getNewEntity(Workflow::ENTITY_TYPE); $workflow->set([ 'type' => Workflow::TYPE_SIGNAL, 'signalName' => $itSignal, 'entityType' => $entity->get('targetType'), 'isInternal' => true, 'flowchartId' => $entity->get('id'), 'isActive' => $entity->get('isActive'), 'actions' => [ (object) [ 'type' => 'startBpmnProcess', 'flowchartId' => $entity->getId(), 'elementId' => $itId, 'cid' => 0, ] ], 'processOrder' => 150, ]); $this->entityManager->saveEntity($workflow); } if ( $itType === 'eventStartTimer' && !empty($item->targetReportId) && !empty($item->scheduling) ) { $workflow = $this->entityManager->getNewEntity(Workflow::ENTITY_TYPE); $workflow->set([ 'type' => Workflow::TYPE_SCHEDULED, 'entityType' => $entity->get('targetType'), 'isInternal' => true, 'flowchartId' => $entity->getId(), 'isActive' => $entity->get('isActive'), 'scheduling' => $item->scheduling, 'schedulingApplyTimezone' => $item->schedulingApplyTimezone ?? false, 'targetReportId' => $item->targetReportId, 'targetReportName' => $item->targetReportId, 'actions' => [ (object) [ 'type' => 'startBpmnProcess', 'flowchartId' => $entity->getId(), 'elementId' => $itId, 'cid' => 0, ] ], 'processOrder' => 100, ]); $this->entityManager->saveEntity($workflow); } } } } if ( $entity->isAttributeChanged('isActive') && !$entity->isNew() && !$workflowsToRecreate ) { foreach ($workflowList as $workflow) { if ($workflow->get('isActive') !== $entity->get('isActive')) { $workflow->set('isActive', $entity->get('isActive')); $this->entityManager->saveEntity($workflow); } } } } private function removeWorkflows(Entity $entity): void { $workflowList = $this->entityManager ->getRDBRepository(Workflow::ENTITY_TYPE) ->where([ 'flowchartId' => $entity->getId(), 'isInternal' => true, ]) ->find(); foreach ($workflowList as $workflow) { $this->entityManager->removeEntity($workflow); $this->entityManager ->getRDBRepository(Workflow::ENTITY_TYPE) ->deleteFromDb($workflow->getId()); } } }