Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
templatestunnel.php
1<?php
2
4
5use Bitrix\Bizproc\Automation\Trigger\Entity\EO_Trigger;
8
10{
13 protected array $availableTriggers = [];
14
16 {
17 $this->srcTemplate = $srcTemplate;
18 $this->dstTemplate = $dstTemplate;
19
20 if (
23 )
24 {
25 $this->dstTemplate = $this->srcTemplate;
26 }
27
28 $documentService = \CBPRuntime::getRuntime()->getDocumentService();
29 $dstTarget = $documentService->createAutomationTarget($this->dstTemplate->getDocumentType());
30
31 foreach ($dstTarget->getAvailableTriggers() as $triggerDescription)
32 {
33 if (is_string($triggerDescription['CODE'] ?? null) && $triggerDescription['CODE'] !== '')
34 {
35 $this->availableTriggers[$triggerDescription['CODE']] = $triggerDescription;
36 }
37 }
38 }
39
40 public function copyRobots(array $robotNames, int $userId): Result
41 {
42 if ($this->srcTemplate->isExternalModified() || $this->dstTemplate->isExternalModified())
43 {
44 $result = new Result();
45 $result->setData([
46 'copied' => [],
47 'denied' => [],
48 ]);
49
50 return $result;
51 }
52
53 $copyingRobots = $this->srcTemplate->getRobotsByNames($robotNames);
54 $partitioned = $this->partitionByDescription($this->dstTemplate->getDocumentType(), $copyingRobots);
55
56 $newRobots = [];
58 foreach ($partitioned['available'] as $robot)
59 {
60 $draftRobot = new Robot([
61 'Name' => Robot::generateName(),
62 'Type' => $robot->getType(),
63 'Activated' => $robot->isActivated() ? 'Y' : 'N',
64 'Properties' => $robot->getProperties(),
65 ]);
66
67 $delayInterval = $robot->getDelayInterval();
68 $condition = $robot->getCondition();
69 if ($delayInterval && !$delayInterval->isNow())
70 {
71 $draftRobot->setDelayInterval($delayInterval);
72 $draftRobot->setDelayName(Robot::generateName());
73 }
74 if ($condition)
75 {
76 $draftRobot->setCondition($robot->getCondition());
77 }
78 if ($robot->isExecuteAfterPrevious())
79 {
80 $draftRobot->setExecuteAfterPrevious();
81 }
82
83 $newRobots[] = $draftRobot;
84 }
85
86 if ($newRobots)
87 {
88 $result = $this->dstTemplate->save(
89 array_merge($this->dstTemplate->getRobots(), $newRobots),
90 $userId
91 );
92 }
93 else
94 {
95 $result = new Result();
96 }
97
98 if ($result->isSuccess())
99 {
100 $result->setData([
101 'copied' => $partitioned['available'],
102 'denied' => $partitioned['unavailable'],
103 ]);
104 }
105
106 return $result;
107 }
108
109 public function moveRobots(array $robotNames, int $userId): Result
110 {
111 if ($this->srcTemplate->isExternalModified() || $this->dstTemplate->isExternalModified())
112 {
113 $result = new Result();
114 $result->setData([
115 'moved' => [],
116 'denied' => [],
117 ]);
118
119 return $result;
120 }
121
122 $result = new Result();
123 $copyingResult = $this->copyRobots($robotNames, $userId);
124
125 if ($copyingResult->isSuccess())
126 {
127 $deletingResult = $this->srcTemplate->deleteRobots($copyingResult->getData()['copied'], $userId);
128
129 if ($deletingResult->isSuccess())
130 {
131 $result->setData([
132 'moved' => $copyingResult->getData()['copied'],
133 'denied' => $copyingResult->getData()['denied'],
134 ]);
135 }
136 else
137 {
138 $result->addErrors($deletingResult->getErrors());
139 }
140 }
141 else
142 {
143 $result->addErrors($copyingResult->getErrors());
144 }
145
146 return $result;
147 }
148
149 public function copyTriggers(array $triggerNames): Result
150 {
151 $documentService = \CBPRuntime::getRuntime()->getDocumentService();
152 $target = $documentService->createAutomationTarget($this->srcTemplate->getDocumentType());
153
155 $triggersToCopy = array_filter(
156 $target->getTriggerObjects([$this->srcTemplate->getDocumentStatus()]),
157 fn ($trigger) => in_array($trigger->getId(), $triggerNames, true),
158 );
159
160 $copiedTriggers = [];
161 $deniedTriggers = [];
162 foreach ($triggersToCopy as $trigger)
163 {
164 if (!array_key_exists($trigger->getCode(), $this->availableTriggers))
165 {
166 $deniedTriggers[] = $trigger;
167 continue;
168 }
169
170 $newTrigger = TriggerTable::createObject();
171
172 $complexDocumentType = $this->dstTemplate->getDocumentType();
173
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());
181
182 $newTrigger->save();
183 $copiedTriggers[] = $newTrigger;
184 }
185
186 $result = new Result();
187 $result->setData([
188 'copied' => $copiedTriggers,
189 'denied' => $deniedTriggers,
190 'original' => $triggersToCopy,
191 ]);
192
193 return $result;
194 }
195
196 public function moveTriggers(array $triggerNames): Result
197 {
198 $copyingResult = $this->copyTriggers($triggerNames);
199
200 $result = new Result();
201 if ($copyingResult->isSuccess())
202 {
203 $deniedTriggers = [];
204 foreach ($copyingResult->getData()['denied'] as $trigger)
205 {
206 $deniedTriggers[$trigger->getId()] = $trigger;
207 }
208
210 foreach ($copyingResult->getData()['original'] as $trigger)
211 {
212 if (!array_key_exists($trigger->getId(), $deniedTriggers))
213 {
214 $trigger->delete();
215 }
216 }
217
218 $result->setData([
219 'moved' => $copyingResult->getData()['copied'],
220 'denied' => $copyingResult->getData()['denied'],
221 'original' => $copyingResult->getData()['original'],
222 ]);
223 }
224 else
225 {
226 $result->addErrors($copyingResult->getErrors());
227 }
228
229 return $result;
230 }
231
237 private function partitionByDescription(array $complexDocumentType, array $robots): array
238 {
239 $runtime = \CBPRuntime::GetRuntime();
240 $partitioned = [
241 'available' => [],
242 'unavailable' => [],
243 ];
244
245 foreach ($robots as $robot)
246 {
247 $type = mb_strtolower($robot->getType());
248 $availableRobots = Template::getAvailableRobots($complexDocumentType);
249 $filter = $robot->getDescription()['FILTER'] ?? [];
250
251 $isRobotAvailable = (
252 isset($availableRobots[$type])
253 && $runtime->checkActivityFilter($filter, $complexDocumentType)
254 );
255 $direction = $isRobotAvailable ? 'available' : 'unavailable';
256
257 $partitioned[$direction][] = $robot;
258 }
259
260 return $partitioned;
261 }
262}
static getAvailableRobots(array $documentType)
Definition template.php:316
__construct(Template $srcTemplate, Template $dstTemplate)