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