28 $documentService = \CBPRuntime::getRuntime()->getDocumentService();
29 $dstTarget = $documentService->createAutomationTarget($this->dstTemplate->getDocumentType());
31 foreach ($dstTarget->getAvailableTriggers() as $triggerDescription)
33 if (is_string($triggerDescription[
'CODE'] ??
null) && $triggerDescription[
'CODE'] !==
'')
35 $this->availableTriggers[$triggerDescription[
'CODE']] = $triggerDescription;
40 public function copyRobots(array $robotNames,
int $userId):
Result
42 if ($this->srcTemplate->isExternalModified() || $this->dstTemplate->isExternalModified())
53 $copyingRobots = $this->srcTemplate->getRobotsByNames($robotNames);
54 $partitioned = $this->partitionByDescription($this->dstTemplate->getDocumentType(), $copyingRobots);
58 foreach ($partitioned[
'available'] as $robot)
60 $draftRobot =
new Robot([
62 'Type' => $robot->getType(),
63 'Activated' => $robot->isActivated() ?
'Y' :
'N',
64 'Properties' => $robot->getProperties(),
67 $delayInterval = $robot->getDelayInterval();
68 $condition = $robot->getCondition();
69 if ($delayInterval && !$delayInterval->isNow())
71 $draftRobot->setDelayInterval($delayInterval);
76 $draftRobot->setCondition($robot->getCondition());
78 if ($robot->isExecuteAfterPrevious())
80 $draftRobot->setExecuteAfterPrevious();
83 $newRobots[] = $draftRobot;
88 $result = $this->dstTemplate->save(
89 array_merge($this->dstTemplate->getRobots(), $newRobots),
95 $result =
new Result();
98 if ($result->isSuccess())
101 'copied' => $partitioned[
'available'],
102 'denied' => $partitioned[
'unavailable'],
111 if ($this->srcTemplate->isExternalModified() || $this->dstTemplate->isExternalModified())
123 $copyingResult = $this->copyRobots($robotNames, $userId);
125 if ($copyingResult->isSuccess())
127 $deletingResult = $this->srcTemplate->deleteRobots($copyingResult->getData()[
'copied'], $userId);
129 if ($deletingResult->isSuccess())
132 'moved' => $copyingResult->getData()[
'copied'],
133 'denied' => $copyingResult->getData()[
'denied'],
138 $result->addErrors($deletingResult->getErrors());
143 $result->addErrors($copyingResult->getErrors());
149 public function copyTriggers(array $triggerNames):
Result
151 $documentService = \CBPRuntime::getRuntime()->getDocumentService();
152 $target = $documentService->createAutomationTarget($this->srcTemplate->getDocumentType());
155 $triggersToCopy = array_filter(
156 $target->getTriggerObjects([$this->srcTemplate->getDocumentStatus()]),
157 fn ($trigger) => in_array($trigger->getId(), $triggerNames,
true),
160 $copiedTriggers = [];
161 $deniedTriggers = [];
162 foreach ($triggersToCopy as $trigger)
164 if (!array_key_exists($trigger->getCode(), $this->availableTriggers))
166 $deniedTriggers[] = $trigger;
170 $newTrigger = TriggerTable::createObject();
172 $complexDocumentType = $this->dstTemplate->getDocumentType();
174 $newTrigger->setName($trigger->getName());
175 $newTrigger->setCode($trigger->getCode());
176 $newTrigger->setModuleId($complexDocumentType[0]);
177 $newTrigger->setEntity($complexDocumentType[1]);
178 $newTrigger->setDocumentType($complexDocumentType[2]);
179 $newTrigger->setDocumentStatus($this->dstTemplate->getDocumentStatus());
180 $newTrigger->setApplyRules($trigger->getApplyRules());
183 $copiedTriggers[] = $newTrigger;
188 'copied' => $copiedTriggers,
189 'denied' => $deniedTriggers,
190 'original' => $triggersToCopy,
196 public function moveTriggers(array $triggerNames): Result
198 $copyingResult = $this->copyTriggers($triggerNames);
200 $result =
new Result();
201 if ($copyingResult->isSuccess())
203 $deniedTriggers = [];
204 foreach ($copyingResult->getData()[
'denied'] as $trigger)
206 $deniedTriggers[$trigger->getId()] = $trigger;
210 foreach ($copyingResult->getData()[
'original'] as $trigger)
212 if (!array_key_exists($trigger->getId(), $deniedTriggers))
219 'moved' => $copyingResult->getData()[
'copied'],
220 'denied' => $copyingResult->getData()[
'denied'],
221 'original' => $copyingResult->getData()[
'original'],
226 $result->addErrors($copyingResult->getErrors());
237 private function partitionByDescription(array $complexDocumentType, array $robots): array
239 $runtime = \CBPRuntime::GetRuntime();
245 foreach ($robots as $robot)
247 $type = mb_strtolower($robot->getType());
249 $filter = $robot->getDescription()[
'FILTER'] ?? [];
251 $isRobotAvailable = (
252 isset($availableRobots[$type])
253 && $runtime->checkActivityFilter($filter, $complexDocumentType)
255 $direction = $isRobotAvailable ?
'available' :
'unavailable';
257 $partitioned[$direction][] = $robot;