1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
basetrigger.php
См. документацию.
1<?php
3
8
10{
11 protected $target;
12 protected $returnValues;
13
17 public static function className()
18 {
19 return get_called_class();
20 }
21
22 public static function isEnabled()
23 {
24 return true;
25 }
26
31 public function setTarget(BaseTarget $target)
32 {
33 $this->target = $target;
34 return $this;
35 }
36
41 public function getTarget()
42 {
43 if ($this->target === null)
44 {
45 throw new Main\InvalidOperationException('Target must be set by setTarget method.');
46 }
47 return $this->target;
48 }
49
53 public static function getCode()
54 {
55 return 'BASE';
56 }
57
61 public static function getName()
62 {
63 return 'Base trigger';
64 }
65
66 public function send()
67 {
68 $this->runWorkflowTriggers();
69
70 $applied = false;
71 $triggers = $this->getPotentialTriggers();
72 if ($triggers)
73 {
74 foreach ($triggers as $trigger)
75 {
76 if ($this->checkApplyRules($trigger))
77 {
78 $this->applyTrigger($trigger);
79 $applied = true;
80
81 break;
82 }
83 }
84 }
85
86 return $applied;
87 }
88
89 protected function runWorkflowTriggers(): void
90 {
91 if (
92 !\CBPRuntime::isFeatureEnabled()
93 || !\Bitrix\Main\Config\Option::get('bizproc', 'bp_triggers', false)
94 )
95 {
96 return;
97 }
98
99 try
100 {
101 [$module, $entity, $documentType] = \CBPHelper::parseDocumentId($this->target->getDocumentType());
102 }
103 catch (\CBPArgumentNullException $exception)
104 {
105 return;
106 }
107
108 $code = static::getCode();
109 $triggers =
110 WorkflowTemplateSettingsTable::query()
111 ->setSelect(['TEMPLATE', 'TEMPLATE_ID'])
112 ->where('NAME', "TRIGGER_$code")
113 ->where('VALUE', 'Y')
114 ->where('TEMPLATE.MODULE_ID', $module)
115 ->where('TEMPLATE.ENTITY', $entity)
116 ->where('TEMPLATE.DOCUMENT_TYPE', $documentType)
117 ->exec()
118 ->fetchCollection()
119 ;
120
121 if ($triggers)
122 {
123 $complexDocumentId = $this->target->getComplexDocumentId();
124 foreach ($triggers as $trigger)
125 {
126 $errors = [];
127 \CBPDocument::startWorkflow($trigger->getTemplateId(), $complexDocumentId, [], $errors);
128 }
129 }
130 }
131
132 protected function applyTrigger(array $trigger)
133 {
134 $statusId = $trigger['DOCUMENT_STATUS'];
135
136 $target = $this->getTarget();
137
138 $target->setAppliedTrigger($trigger);
139 $target->setDocumentStatus($statusId);
140 $target->getRuntime()->onDocumentStatusChanged();
141
142 return true;
143 }
144
145 protected function getPotentialTriggers()
146 {
147 $triggers = [];
148
149 $currentStatus = $this->getTarget()->getDocumentStatus();
150 $allStatuses = array_keys($this->getTarget()->getDocumentStatusList());
151
152 $needleKey = array_search($currentStatus, $allStatuses, false);
153
154 if ($needleKey === false)
155 {
156 return $triggers;
157 }
158
159 $forwardStatuses = array_slice($allStatuses, $needleKey + 1);
160 unset($allStatuses[$needleKey]);
161
162 $code = static::getCode();
163 $rows = [];
164 $targetTriggers = $this->getTarget()->getTriggers($allStatuses);
165
166 foreach ($targetTriggers as $row)
167 {
168 if ($row['CODE'] !== $code)
169 {
170 continue;
171 }
172
173 if (!in_array($row['DOCUMENT_STATUS'], $forwardStatuses, false))
174 {
175 if (
176 !isset($row['APPLY_RULES']['ALLOW_BACKWARDS'])
177 ||
178 $row['APPLY_RULES']['ALLOW_BACKWARDS'] !== 'Y'
179 )
180 {
181 continue;
182 }
183 }
184
185 $rows[$row['DOCUMENT_STATUS']][] = $row;
186 }
187
188 if ($rows)
189 {
190 foreach ($allStatuses as $needleStatus)
191 {
192 if (isset($rows[$needleStatus]))
193 {
194 $triggers[] = $rows[$needleStatus];
195 }
196 }
197 }
198
199 return array_merge(...$triggers);
200 }
201
202 public function checkApplyRules(array $trigger)
203 {
204 $conditionRules = $trigger['APPLY_RULES']['Condition'] ?? null;
205
206 if ($conditionRules)
207 {
208 $conditionGroup = new ConditionGroup($conditionRules);
209 $target = $this->getTarget();
210 $result = $conditionGroup->evaluate($target);
211
212 if ($result)
213 {
214 $target->setAppliedTriggerConditionResults($conditionGroup->getEvaluateResults());
215 }
216
217 return $result;
218 }
219
220 return true;
221 }
222
223 public function getReturnValues(): ?array
224 {
225 return $this->returnValues;
226 }
227
232 public function setReturnValues(array $values)
233 {
234 $this->returnValues = $values;
235 return $this;
236 }
237
238 public static function getReturnProperties(): array
239 {
240 return [];
241 }
242
243 public static function toArray()
244 {
245 return [
246 'NAME' => static::getName(),
247 'CODE' => static::getCode(),
248 'RETURN' => static::getReturnProperties(),
249 'DESCRIPTION' => static::getDescription(),
250 'GROUP' => static::getGroup(),
251 'SETTINGS' => static::getSettings(),
252 ];
253 }
254
255 public static function getDescription(): string
256 {
257 return '';
258 }
259
260 public static function getGroup(): array
261 {
262 return [];
263 }
264
265 protected static function getSettings(): ?array
266 {
267 $map = static::getPropertiesMap();
268 if ($map)
269 {
270 return ['Properties' => array_values($map)];
271 }
272
273 return null;
274 }
275
276 protected static function getPropertiesMap(): array
277 {
278 return [];
279 }
280}
setTarget(BaseTarget $target)
Определения basetrigger.php:31
checkApplyRules(array $trigger)
Определения basetrigger.php:202
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$entity
$errors
Определения iblock_catalog_edit.php:74
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
$map
Определения config.php:5
$rows
Определения options.php:264