Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
basetarget.php
1<?php
3
7use Bitrix\Bizproc\Automation\Trigger\Entity\EO_Trigger;
9
10abstract class BaseTarget
11{
12 private const CACHE_TTL = 7200;
13
14 protected $runtime;
15 protected $appliedTrigger;
16 protected $documentId;
17 protected $documentType;
18 protected array $appliedTriggerConditionResults = [];
19
20 public function isAvailable()
21 {
22 return true;
23 }
24
30 public function setAppliedTrigger(array $trigger)
31 {
32 $this->appliedTrigger = $trigger;
33
34 return $this;
35 }
36
41 public function getAppliedTrigger()
42 {
44 }
45
49 public function getRuntime()
50 {
51 if ($this->runtime === null)
52 {
53 $this->runtime = new Runtime();
54 $this->runtime->setTarget($this);
55 }
56
57 return $this->runtime;
58 }
59
60 abstract public function getDocumentStatus();
61
62 public function getDocumentCategory(): int
63 {
64 return 0;
65 }
66
67 abstract public function setDocumentStatus($statusId);
68
69 abstract public function getDocumentStatusList($categoryId = 0);
70
71 public function getTriggers(array $statuses)
72 {
73 $result = [];
75
76 $iterator = TriggerTable::getList(array(
77 'filter' => array(
78 '=MODULE_ID' => $documentType[0],
79 '=ENTITY' => $documentType[1],
80 '=DOCUMENT_TYPE' => $documentType[2],
81 '@DOCUMENT_STATUS' => $statuses
82 ),
83 'cache' => [
84 'ttl' => self::CACHE_TTL
85 ]
86 ));
87
88 while ($row = $iterator->fetch())
89 {
90 $row['DOCUMENT_TYPE'] = $documentType;
91 $result[] = $row;
92 }
93
94 return $result;
95 }
96
101 public function getTriggerObjects(array $statuses): array
102 {
104
105 $iterator = TriggerTable::getList([
106 'filter' => [
107 '=MODULE_ID' => $documentType[0],
108 '=ENTITY' => $documentType[1],
109 '=DOCUMENT_TYPE' => $documentType[2],
110 '@DOCUMENT_STATUS' => $statuses,
111 ],
112 ]);
113
114 return $iterator->fetchCollection()->getAll();
115 }
116
117 public function prepareTriggersToSave(array &$triggers)
118 {
119 foreach ($triggers as $i => $trigger)
120 {
121 if (isset($trigger['DELETED']) && $trigger['DELETED'] === 'Y')
122 {
123 continue;
124 }
125
126 $triggers[$i]['APPLY_RULES'] = $this->prepareApplyRules($trigger['APPLY_RULES']);
127 }
128 }
129
130 public function prepareTriggersToShow(array &$triggers)
131 {
132 foreach ($triggers as $i => $trigger)
133 {
134 $triggers[$i]['APPLY_RULES'] = $this->prepareApplyRules($trigger['APPLY_RULES'], true);
135 }
136 }
137
138 public function setTriggers(array $triggers)
139 {
140 $updatedTriggers = [];
141 foreach ($triggers as $trigger)
142 {
143 $triggerId = isset($trigger['ID']) ? (int)$trigger['ID'] : 0;
144
145 if (isset($trigger['DELETED']) && $trigger['DELETED'] === 'Y')
146 {
147 if ($triggerId > 0)
148 {
149 //TODO: check document type
150 TriggerTable::delete($triggerId);
151 }
152 continue;
153 }
154
155 if ($triggerId > 0)
156 {
157 TriggerTable::update($triggerId, array(
158 'NAME' => $trigger['NAME'],
159 'DOCUMENT_STATUS' => $trigger['DOCUMENT_STATUS'],
160 'APPLY_RULES' => is_array($trigger['APPLY_RULES']) ? $trigger['APPLY_RULES'] : null
161 ));
162 }
163 elseif (isset($trigger['CODE']) && isset($trigger['DOCUMENT_STATUS']))
164 {
166 $addResult = TriggerTable::add(array(
167 'NAME' => $trigger['NAME'],
168 'MODULE_ID' => $documentType[0],
169 'ENTITY' => $documentType[1],
170 'DOCUMENT_TYPE' => $documentType[2],
171 'DOCUMENT_STATUS' => $trigger['DOCUMENT_STATUS'],
172 'CODE' => $trigger['CODE'],
173 'APPLY_RULES' => is_array($trigger['APPLY_RULES']) ? $trigger['APPLY_RULES'] : null
174 ));
175
176 if ($addResult->isSuccess())
177 {
178 $trigger['ID'] = $addResult->getId();
179 }
180 }
181 $updatedTriggers[] = $trigger;
182 }
183
184 return $updatedTriggers;
185 }
186
187 public function extractTemplateParameters(array $triggers): array
188 {
189 $params = [];
190 foreach ($triggers as $trigger)
191 {
192 $triggerDescription = $this->getAvailableTriggerByCode($trigger['CODE']);
193 $status = $trigger['DOCUMENT_STATUS'];
194
195 if ($triggerDescription && isset($triggerDescription['RETURN']))
196 {
197 if (!isset($params[$status]))
198 {
199 $params[$status] = [];
200 }
201 foreach ($triggerDescription['RETURN'] as $property)
202 {
203 $params[$status][$property['Id']] = $property;
204 }
205 }
206 }
207 return $params;
208 }
209
210 private function prepareApplyRules($rules, $external = false): ?array
211 {
212 if (!is_array($rules))
213 {
214 return null;
215 }
216
217 if (isset($rules['Condition']))
218 {
219 $condition = new ConditionGroup($rules['Condition']);
220 if ($external)
221 {
222 $condition->externalizeValues($this->getDocumentType());
223 }
224 else
225 {
226 $condition->internalizeValues($this->getDocumentType());
227 }
228 $rules['Condition'] = $condition->toArray();
229 }
230
231 if (isset($rules['ExecuteBy']))
232 {
233 if ($external)
234 {
235 $rules['ExecuteBy'] = \CBPHelper::UsersArrayToString(
236 $rules['ExecuteBy'],
237 null,
238 $this->getDocumentType()
239 );
240 }
241 else
242 {
243 $rules['ExecuteBy'] = \CBPHelper::UsersStringToArray(
244 $rules['ExecuteBy'],
245 $this->getDocumentType(),
246 $errors
247 );
248 }
249 }
250
251 return $rules;
252 }
253
257 public function getAvailableTriggers()
258 {
259 return [];
260 }
261
262 public function canTriggerSetExecuteBy(): bool
263 {
264 return false;
265 }
266
271 public function getAvailableTriggerByCode($code): ?array
272 {
273 foreach ($this->getAvailableTriggers() as $availableTrigger)
274 {
275 if ($code === $availableTrigger['CODE'])
276 {
277 return $availableTrigger;
278 }
279 }
280 return null;
281 }
282
283 public function setDocumentType(array $documentType)
284 {
285 return $this->documentType = $documentType;
286 }
287
288 public function getDocumentType()
289 {
290 return $this->documentType;
291 }
292
293 public function getDocumentId()
294 {
295 return $this->documentId;
296 }
297
298 public function setDocumentId($documentId)
299 {
300 $this->documentId = $documentId;
301 return $this;
302 }
303
304 public function getComplexDocumentId(): array
305 {
306 $type = $this->getDocumentType();
307
308 return [$type[0], $type[1], $this->getDocumentId()];
309 }
310
312 {
313 return null;
314 }
315
316 public function setAppliedTriggerConditionResults(array $log)
317 {
318 $this->appliedTriggerConditionResults = $log;
319 }
320
321 public function getAppliedTriggerConditionResults(): array
322 {
324 }
325
326 public function getDocumentCategoryCode(): string
327 {
328 return '';
329 }
330}