1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
conditiongroup.php
См. документацию.
1<?php
2namespace Bitrix\Bizproc\Automation\Engine;
3
4use Bitrix\Bizproc;
5use Bitrix\Bizproc\Automation\Target\BaseTarget;
6use Bitrix\Main\Localization\Loc;
7use Bitrix\Bizproc\Automation\Helper;
8use Bitrix\Main\NotSupportedException;
9
10Loc::loadMessages(__FILE__);
11
13{
14 public const TYPE_FIELD = 'field';
15 public const TYPE_MIXED = 'mixed';
16
17 public const JOINER_AND = 'AND';// 0
18 public const JOINER_OR = 'OR';// 1
19
20 private $type;
21 private $items = [];
22 private array $activityNames = [];
23 protected array $evaluateResults = [];
24 protected bool $activated = true;
25 protected bool $internalized = false;
26
27 public function __construct(array $params = null)
28 {
29 $this->setType(static::TYPE_FIELD);
30 if ($params)
31 {
32 if (isset($params['type']))
33 {
34 $this->setType($params['type']);
35 }
36 if (isset($params['items']) && is_array($params['items']))
37 {
38 foreach ($params['items'] as [$item, $joiner])
39 {
40 if (!empty($item['field']))
41 {
42 $condition = new Condition($item);
43 $this->addItem($condition, $joiner);
44 }
45 }
46 }
47 }
48 }
49
54 public function evaluate(BaseTarget $target)
55 {
56 $documentType = $target->getDocumentType();
57 $documentId = $documentType;
58 $documentId[2] = $target->getDocumentId();
59
60 return $this->evaluateByDocument($documentType, $documentId);
61 }
62
69 public function evaluateByDocument(array $documentType, array $documentId, array $document = null): bool
70 {
71 if (empty($this->items))
72 {
73 return true;
74 }
75
76 if ($this->getType() === static::TYPE_MIXED)
77 {
78 throw new NotSupportedException('Mixed conditions can`t be evaluated by document only');
79 }
80
81 $documentService = \CBPRuntime::getRuntime(true)->getDocumentService();
82
83 if ($document === null)
84 {
85 $select = [];
86 foreach ($this->items as $item)
87 {
88 $condition = $item[0];
89 $select[] = $condition->getField();
90 }
91
92 $document = $documentService->getDocument($documentId, $documentType, $select);
93 }
94
95 $result = [0 => true];
96 $i = 0;
97 $joiner = static::JOINER_AND;
98
99 $this->evaluateResults = [];
100
101 foreach ($this->items as $item)
102 {
104 $condition = $item[0];
105 $conditionField = $condition->getField();
106
107 $conditionResult = true;
108
109 $fld = $document[$conditionField] ?? null;
110 $fieldType = $this->getFieldTypeObject($documentService, $documentType, $conditionField);
111
112 if (!$condition->checkValue($fld, $fieldType, $documentId))
113 {
114 $conditionResult = false;
115 }
116
117 if ($joiner == static::JOINER_OR)
118 {
119 ++$i;
120 $result[$i] = $conditionResult;
121 }
122 elseif (!$conditionResult)
123 {
124 $result[$i] = false;
125 }
126
127 $this->evaluateResults[] = [
128 'condition' => $condition->toArray(),
129 'joiner' => $joiner,
130 'fieldValue' => $fld ? $fieldType->formatValue($fld) : null,
131 'result' => $conditionResult ? 'Y' : 'N',
132 ];
133
134 $joiner = ($item[1] === static::JOINER_OR) ? static::JOINER_OR : static::JOINER_AND;
135 }
136
137 return (count(array_filter($result)) > 0);
138 }
139
144 public function setType($type)
145 {
146 if ($type === static::TYPE_FIELD || $type === static::TYPE_MIXED)
147 {
148 $this->type = $type;
149 }
150 return $this;
151 }
152
156 public function getType()
157 {
158 return $this->type;
159 }
160
166 public function addItem(Condition $condition, $joiner = self::JOINER_AND)
167 {
168 $this->items[] = [$condition, $joiner];
169 return $this;
170 }
171
175 public function getItems()
176 {
177 return $this->items;
178 }
179
181 {
182 $this->activityNames = [
183 'Activity' => $activity['Name'],
184 'Branch1' => $activity['Children'][0]['Name'],
185 'Branch2' => $activity['Children'][1]['Name'],
186 ];
187 }
188
189 public function getActivityNames(): array
190 {
191 if (isset($this->activityNames))
192 {
193 return $this->activityNames;
194 }
195
196 return [];
197 }
198
199 public function setActivated(bool $isActivated): void
200 {
201 $this->activated = $isActivated;
202 }
203
204 public function isActivated(): bool
205 {
206 return $this->activated;
207 }
208
212 public function toArray()
213 {
214 $itemsArray = [];
215
217 foreach ($this->getItems() as [$condition, $joiner])
218 {
219 $itemsArray[] = [$condition->toArray(), $joiner];
220 }
221
222 return [
223 'type' => $this->getType(),
224 'items' => $itemsArray,
225 'activityNames' => $this->getActivityNames(),
226 ];
227 }
228
235 public function createBizprocActivity(array $childActivity, array $documentType, Template $template)
236 {
237 $mixedCondition = [];
238 $bizprocJoiner = 0;
239
241 foreach ($this->getItems() as [$condition, $joiner])
242 {
243 $object = $condition->getObject();
244 $field = $condition->getField();
245 $value = $condition->getValue();
246 $property = $template->getProperty($object, $field);
247
248 $operator = $condition->getOperator();
249 $isOperatorWithValue = !in_array(
250 $operator,
251 [Bizproc\Activity\Operator\EmptyOperator::getCode(), Bizproc\Activity\Operator\NotEmptyOperator::getCode()],
252 true
253 );
254 if ($property && $isOperatorWithValue)
255 {
256 $fieldInputValueResult = $this->getFieldInputValue($property, $documentType, $condition);
257 if ($fieldInputValueResult->isSuccess())
258 {
259 $value = $fieldInputValueResult->getData()['value'];
260 }
261 }
262
263 $mixedCondition[] = [
264 'object' => $object,
265 'field' => $field,
266 'operator' => $condition->getOperator(),
267 'value' => self::unConvertExpressions($value, $documentType),
268 'joiner' => $bizprocJoiner,
269 ];
270 $bizprocJoiner = ($joiner === static::JOINER_OR) ? 1 : 0;
271 }
272
273 $title = Loc::getMessage('BIZPROC_AUTOMATION_CONDITION_TITLE');
274 $activated = $childActivity['Activated'] === 'N' ? 'N' : 'Y';
275 $activity = [
276 'Type' => 'IfElseActivity',
277 'Name' => Robot::generateName(),
278 'Activated' => $activated,
279 'Properties' => ['Title' => $title],
280 'Children' => [
281 [
282 'Type' => 'IfElseBranchActivity',
283 'Name' => Robot::generateName(),
284 'Properties' => [
285 'Title' => $title,
286 'mixedcondition' => $mixedCondition
287 ],
288 'Children' => [$childActivity],
289 'Activated' => $activated,
290 ],
291 [
292 'Type' => 'IfElseBranchActivity',
293 'Name' => Robot::generateName(),
294 'Properties' => [
295 'Title' => $title,
296 'truecondition' => '1',
297 ],
298 'Children' => [],
299 'Activated' => $activated,
300 ]
301 ]
302 ];
303
304 return $activity;
305 }
306
313 public static function convertBizprocActivity(array &$activity, array $documentType, Template $template)
314 {
315 $conditionGroup = false;
316
317 if (
318 count($activity['Children']) === 2
319 && $activity['Children'][0]['Type'] === 'IfElseBranchActivity'
320 && $activity['Children'][1]['Type'] === 'IfElseBranchActivity'
321 && (
322 !empty($activity['Children'][0]['Properties']['fieldcondition'])
323 ||
324 !empty($activity['Children'][0]['Properties']['mixedcondition'])
325 )
326 && !empty($activity['Children'][1]['Properties']['truecondition'])
327 && count($activity['Children'][0]['Children']) === 1
328 )
329 {
330 $conditionGroup = new static();
331 $conditionGroup->setType(static::TYPE_MIXED);
332 $conditionGroup->setActivityNames($activity);
333 $conditionGroup->setActivated(\CBPHelper::getBool($activity['Activated'] ?? true));
334
335 $isMixed = isset($activity['Children'][0]['Properties']['mixedcondition']);
336 $bizprocConditions = $activity['Children'][0]['Properties'][$isMixed?'mixedcondition':'fieldcondition'];
337
338 foreach ($bizprocConditions as $index => $condition)
339 {
340 if (!$isMixed)
341 {
342 $condition = self::convertDocumentCondition($condition);
343 }
344
345 $property = $template->getProperty($condition['object'], $condition['field']);
346 if ($property && $property['Type'] === 'user')
347 {
348 $condition['value'] = \CBPHelper::UsersArrayToString(
349 $condition['value'],
350 null,
351 $documentType
352 );
353 }
354
355 if ($property && $property['Type'] === 'time')
356 {
357 $offset = \CTimeZone::GetOffset();
358 $condition['value'] = array_map(
359 static fn($value) => Bizproc\BaseType\Value\Time::tryMakeCorrectFormat($value, $offset),
360 (array)$condition['value']
361 );
362 }
363
364 $conditionItem = new Condition(array(
365 'object' => $condition['object'],
366 'field' => $condition['field'],
367 'operator' => $condition['operator'],
368 'value' => self::convertExpressions($condition['value'], $documentType),
369 ));
370
371 $nextCondition = isset($bizprocConditions[$index + 1]) ? $bizprocConditions[$index + 1] : null;
372 $joiner = ($nextCondition && (!empty($nextCondition[3]) || !empty($nextCondition['joiner'])))
373 ? static::JOINER_OR : static::JOINER_AND;
374
375 $conditionGroup->addItem($conditionItem, $joiner);
376 }
377
378 $activity = $activity['Children'][0]['Children'][0];
379 }
380
381 return $conditionGroup;
382 }
383
384 private static function convertDocumentCondition(array $condition): array
385 {
386 return [
387 'object' => 'Document',
388 'field' => $condition[0],
389 'operator' => $condition[1],
390 'value' => $condition[2],
391 'joiner' => $condition[3],
392 ];
393 }
394
400 public function internalizeValues(array $documentType): self
401 {
402 $documentService = \CBPRuntime::GetRuntime(true)->getDocumentService();
403 $documentFields = $documentService->GetDocumentFields($documentType);
404
406 foreach ($this->getItems() as [$condition, $joiner])
407 {
408 $field = $condition->getField();
409 $value = $condition->getValue();
410 $property = $documentFields[$field] ?? null;
411 if ($property && !in_array($condition->getOperator(), ['empty', '!empty']))
412 {
413 $condition->setValue(self::unConvertExpressions($value, $documentType));
414 $fieldInputValueResult = $this->getFieldInputValue($property, $documentType, $condition);
415
416 $condition->setValue(
417 $fieldInputValueResult->isSuccess() ? $fieldInputValueResult->getData()['value'] : $value
418 );
419 }
420 }
421
422 $this->internalized = true;
423
424 return $this;
425 }
426
427 public function isInternalized(): bool
428 {
429 return $this->internalized;
430 }
431
437 public function externalizeValues(array $documentType): self
438 {
439 $documentService = \CBPRuntime::GetRuntime(true)->getDocumentService();
440 $documentFields = $documentService->GetDocumentFields($documentType);
441
443 foreach ($this->getItems() as [$condition, $joiner])
444 {
445 $field = $condition->getField();
446 $value = $condition->getValue();
447 $property = isset($documentFields[$field]) ? $documentFields[$field] : null;
448 if ($property && !in_array($condition->getOperator(), ['empty', '!empty']))
449 {
450 $value = self::convertExpressions($value, $documentType);
451 if ($property['Type'] === 'user')
452 {
453 $value = \CBPHelper::UsersArrayToString(
454 $value,
455 null,
456 $documentType
457 );
458 }
459 if ($property['Type'] === 'time')
460 {
461 $offset = \CTimeZone::GetOffset();
462 $value = array_map(
463 static fn($value) => Bizproc\BaseType\Value\Time::tryMakeCorrectFormat($value, $offset),
464 (array)$value
465 );
466 }
467
468 $condition->setValue($value);
469 }
470 }
471
472 $this->internalized = false;
473
474 return $this;
475 }
476
477 private static function convertExpressions($value, array $documentType)
478 {
479 if (is_array($value))
480 {
481 foreach ($value as $k => $v)
482 {
483 $value[$k] = self::convertExpressions($v, $documentType);
484 }
485 }
486 else
487 {
488 $value = Helper::convertExpressions($value, $documentType);
489 }
490 return $value;
491 }
492
493 private static function unConvertExpressions($value, array $documentType)
494 {
495 if (is_array($value))
496 {
497 foreach ($value as $k => $v)
498 {
499 $value[$k] = self::unConvertExpressions($v, $documentType);
500 }
501 }
502 else
503 {
504 $value = Helper::unConvertExpressions($value, $documentType);
505 }
506 return $value;
507 }
508
509 private function getFieldTypeObject(\CBPDocumentService $documentService, array $documentType, $conditionField): ?\Bitrix\Bizproc\FieldType
510 {
511 $documentFields = $documentService->getDocumentFields($documentType);
512
513 $fieldType = null;
514
515 if (isset($documentFields[$conditionField]))
516 {
517 $fieldType = $documentService->getFieldTypeObject($documentType, $documentFields[$conditionField]);
518 }
519
520 if (!$fieldType)
521 {
522 $fieldType = $documentService->getFieldTypeObject($documentType, ['Type' => 'string']);
523 }
524
525 return $fieldType;
526 }
527
528 public function getEvaluateResults(): array
529 {
531 }
532
533 private function getFieldInputValue(array $property, array $documentType, Condition $condition): \Bitrix\Main\Result
534 {
535 $documentService = \CBPRuntime::getRuntime()->getDocumentService();
536 $conditionValue = $condition->getValue();
537 $currentValues = ['field' => $conditionValue];
538 $errors = [];
539
540 $isBetweenOperator = $condition->getOperator() === Bizproc\Activity\Operator\BetweenOperator::getCode();
541 $valueInternal =
542 $isBetweenOperator
543 ? []
544 : $documentService->getFieldInputValue($documentType, $property, 'field', $currentValues,$errors)
545 ;
546 if ($isBetweenOperator)
547 {
548 $currentValues['field_greater_then'] =
549 is_array($conditionValue) && isset($conditionValue[0])
550 ? $conditionValue[0]
551 : $conditionValue
552 ;
553 $currentValues['field_less_then'] =
554 is_array($conditionValue) && isset($conditionValue[1])
555 ? $conditionValue[1]
556 : ''
557 ;
558 $property['Multiple'] = false;
559 $valueInternal1 = $documentService->getFieldInputValue(
560 $documentType,
561 $property,
562 'field_greater_then',
564 $errors
565 );
566 $valueInternal2 = $documentService->getFieldInputValue(
567 $documentType,
568 $property,
569 'field_less_then',
571 $errors
572 );
573
574 $valueInternal = [$valueInternal1 ?? '', $valueInternal2 ?? ''];
575 }
576
577 $result = new \Bitrix\Main\Result();
578 $result->setData(['value' => $valueInternal]);
579
580 if ($errors)
581 {
582 foreach ($errors as $error)
583 {
584 if (isset($error['message'], $error['code']))
585 {
586 $result->addError(new \Bitrix\Main\Error($error['message'], $error['code']));
587 }
588 }
589 }
590
591 return $result;
592 }
593}
$type
Определения options.php:106
__construct(array $params=null)
Определения conditiongroup.php:27
static convertBizprocActivity(array &$activity, array $documentType, Template $template)
Определения conditiongroup.php:313
addItem(Condition $condition, $joiner=self::JOINER_AND)
Определения conditiongroup.php:166
static generateName()
Определения robot.php:193
Определения error.php:15
getFieldTypeObject(array $parameterDocumentType, array $property)
Определения documentservice.php:1219
getFieldInputValue($parameterDocumentType, $fieldType, $fieldName, $arRequest, &$arErrors)
Определения documentservice.php:1033
getDocumentFields($parameterDocumentType, $importExportMode=false)
Определения documentservice.php:363
$template
Определения file_edit.php:49
</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
if($request->isPost() && $currentAction !==null &&check_bitrix_sessid()) $currentValues
Определения options.php:198
$activity
Определения options.php:214
$errors
Определения iblock_catalog_edit.php:74
$select
Определения iblock_catalog_list.php:194
Определения base.php:3
$value
Определения Param.php:39
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$i
Определения factura.php:643
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$items
Определения template.php:224
$title
Определения pdf.php:123
$error
Определения subscription_card_product.php:20
$k
Определения template_pdf.php:567