22 private array $activityNames = [];
29 $this->
setType(static::TYPE_FIELD);
32 if (isset($params[
'type']))
34 $this->
setType($params[
'type']);
36 if (isset($params[
'items']) && is_array($params[
'items']))
38 foreach ($params[
'items'] as [$item, $joiner])
40 if (!empty($item[
'field']))
43 $this->
addItem($condition, $joiner);
57 $documentId = $documentType;
60 return $this->evaluateByDocument($documentType, $documentId);
69 public function evaluateByDocument(array $documentType, array $documentId, array $document =
null): bool
71 if (empty($this->items))
76 if ($this->
getType() === static::TYPE_MIXED)
81 $documentService = \CBPRuntime::getRuntime(
true)->getDocumentService();
83 if ($document ===
null)
85 $document = $documentService->getDocument($documentId, $documentType);
88 $result = [0 =>
true];
90 $joiner = static::JOINER_AND;
92 $this->evaluateResults = [];
94 foreach ($this->items as $item)
97 $condition = $item[0];
98 $conditionField = $condition->getField();
100 $conditionResult =
true;
102 $fld = $document[$conditionField] ??
null;
103 $fieldType = $this->getFieldTypeObject($documentService, $documentType, $conditionField);
105 if (!$condition->checkValue($fld, $fieldType, $documentId))
107 $conditionResult =
false;
110 if ($joiner == static::JOINER_OR)
113 $result[$i] = $conditionResult;
115 elseif (!$conditionResult)
120 $this->evaluateResults[] = [
121 'condition' => $condition->toArray(),
123 'fieldValue' => $fld ? $fieldType->formatValue($fld) :
null,
124 'result' => $conditionResult ?
'Y' :
'N',
127 $joiner = ($item[1] === static::JOINER_OR) ? static::JOINER_OR : static::
JOINER_AND;
130 return (count(array_filter($result)) > 0);
139 if ($type === static::TYPE_FIELD || $type === static::TYPE_MIXED)
161 $this->items[] = [$condition, $joiner];
175 $this->activityNames = [
176 'Activity' => $activity[
'Name'],
177 'Branch1' => $activity[
'Children'][0][
'Name'],
178 'Branch2' => $activity[
'Children'][1][
'Name'],
184 if (isset($this->activityNames))
186 return $this->activityNames;
194 $this->activated = $isActivated;
205 public function toArray()
210 foreach ($this->
getItems() as [$condition, $joiner])
212 $itemsArray[] = [$condition->toArray(), $joiner];
217 'items' => $itemsArray,
228 public function createBizprocActivity(array $childActivity, array $documentType, Template $template)
230 $mixedCondition = [];
234 foreach ($this->
getItems() as [$condition, $joiner])
236 $object = $condition->getObject();
237 $field = $condition->getField();
238 $value = $condition->getValue();
239 $property = $template->getProperty($object, $field);
241 $operator = $condition->getOperator();
242 $isOperatorWithValue = !in_array(
244 [Bizproc\Activity\Operator\EmptyOperator::getCode(), Bizproc\Activity\Operator\NotEmptyOperator::getCode()],
247 if ($property && $isOperatorWithValue)
249 $fieldInputValueResult = $this->getFieldInputValue($property, $documentType, $condition);
250 if ($fieldInputValueResult->isSuccess())
252 $value = $fieldInputValueResult->getData()[
'value'];
256 $mixedCondition[] = [
259 'operator' => $condition->getOperator(),
260 'value' => self::unConvertExpressions($value, $documentType),
261 'joiner' => $bizprocJoiner,
263 $bizprocJoiner = ($joiner === static::JOINER_OR) ? 1 : 0;
267 $activated = $childActivity[
'Activated'] ===
'N' ?
'N' :
'Y';
269 'Type' =>
'IfElseActivity',
272 'Properties' => [
'Title' =>
$title],
275 'Type' =>
'IfElseBranchActivity',
279 'mixedcondition' => $mixedCondition
281 'Children' => [$childActivity],
285 'Type' =>
'IfElseBranchActivity',
289 'truecondition' =>
'1',
308 $conditionGroup =
false;
311 count($activity[
'Children']) === 2
312 && $activity[
'Children'][0][
'Type'] ===
'IfElseBranchActivity'
313 && $activity[
'Children'][1][
'Type'] ===
'IfElseBranchActivity'
315 !empty($activity[
'Children'][0][
'Properties'][
'fieldcondition'])
317 !empty($activity[
'Children'][0][
'Properties'][
'mixedcondition'])
319 && !empty($activity[
'Children'][1][
'Properties'][
'truecondition'])
320 && count($activity[
'Children'][0][
'Children']) === 1
323 $conditionGroup =
new static();
324 $conditionGroup->setType(static::TYPE_MIXED);
325 $conditionGroup->setActivityNames($activity);
326 $conditionGroup->setActivated(\CBPHelper::getBool($activity[
'Activated'] ??
true));
328 $isMixed = isset($activity[
'Children'][0][
'Properties'][
'mixedcondition']);
329 $bizprocConditions = $activity[
'Children'][0][
'Properties'][$isMixed?
'mixedcondition':
'fieldcondition'];
331 foreach ($bizprocConditions as $index => $condition)
335 $condition = self::convertDocumentCondition($condition);
338 $property = $template->
getProperty($condition[
'object'], $condition[
'field']);
339 if ($property && $property[
'Type'] ===
'user')
341 $condition[
'value'] = \CBPHelper::UsersArrayToString(
348 if ($property && $property[
'Type'] ===
'time')
350 $offset = \CTimeZone::GetOffset();
351 $condition[
'value'] = array_map(
352 static fn($value) => Bizproc\BaseType\Value\Time::tryMakeCorrectFormat($value, $offset),
353 (array)$condition[
'value']
358 'object' => $condition[
'object'],
359 'field' => $condition[
'field'],
360 'operator' => $condition[
'operator'],
361 'value' => self::convertExpressions($condition[
'value'], $documentType),
364 $nextCondition = isset($bizprocConditions[$index + 1]) ? $bizprocConditions[$index + 1] :
null;
365 $joiner = ($nextCondition && (!empty($nextCondition[3]) || !empty($nextCondition[
'joiner'])))
366 ? static::JOINER_OR : static::JOINER_AND;
368 $conditionGroup->addItem($conditionItem, $joiner);
371 $activity = $activity[
'Children'][0][
'Children'][0];
374 return $conditionGroup;
377 private static function convertDocumentCondition(array $condition): array
380 'object' =>
'Document',
381 'field' => $condition[0],
382 'operator' => $condition[1],
383 'value' => $condition[2],
384 'joiner' => $condition[3],
393 public function internalizeValues(array $documentType): self
395 $documentService = \CBPRuntime::GetRuntime(
true)->getDocumentService();
396 $documentFields = $documentService->GetDocumentFields($documentType);
399 foreach ($this->
getItems() as [$condition, $joiner])
401 $field = $condition->getField();
402 $value = $condition->getValue();
403 $property = $documentFields[$field] ??
null;
404 if ($property && !in_array($condition->getOperator(), [
'empty',
'!empty']))
406 $condition->setValue(self::unConvertExpressions($value, $documentType));
407 $fieldInputValueResult = $this->getFieldInputValue($property, $documentType, $condition);
409 $condition->setValue(
410 $fieldInputValueResult->isSuccess() ? $fieldInputValueResult->getData()[
'value'] : $value
415 $this->internalized =
true;
430 public function externalizeValues(array $documentType): self
432 $documentService = \CBPRuntime::GetRuntime(
true)->getDocumentService();
433 $documentFields = $documentService->GetDocumentFields($documentType);
436 foreach ($this->
getItems() as [$condition, $joiner])
438 $field = $condition->getField();
439 $value = $condition->getValue();
440 $property = isset($documentFields[$field]) ? $documentFields[$field] :
null;
441 if ($property && !in_array($condition->getOperator(), [
'empty',
'!empty']))
443 $value = self::convertExpressions($value, $documentType);
444 if ($property[
'Type'] ===
'user')
446 $value = \CBPHelper::UsersArrayToString(
452 if ($property[
'Type'] ===
'time')
454 $offset = \CTimeZone::GetOffset();
456 static fn($value) => Bizproc\BaseType\Value\Time::tryMakeCorrectFormat($value, $offset),
461 $condition->setValue($value);
465 $this->internalized =
false;
470 private static function convertExpressions($value, array $documentType)
472 if (is_array($value))
474 foreach ($value as $k => $v)
476 $value[$k] = self::convertExpressions($v, $documentType);
486 private static function unConvertExpressions($value, array $documentType)
488 if (is_array($value))
490 foreach ($value as $k => $v)
492 $value[$k] = self::unConvertExpressions($v, $documentType);
502 private function getFieldTypeObject(\CBPDocumentService $documentService, array $documentType, $conditionField): ?\
Bitrix\Bizproc\FieldType
504 $documentFields = $documentService->getDocumentFields($documentType);
508 if (isset($documentFields[$conditionField]))
510 $fieldType = $documentService->getFieldTypeObject($documentType, $documentFields[$conditionField]);
515 $fieldType = $documentService->getFieldTypeObject($documentType, [
'Type' =>
'string']);
526 private function getFieldInputValue(array $property, array $documentType,
Condition $condition): \
Bitrix\Main\
Result
528 $documentService = \CBPRuntime::getRuntime()->getDocumentService();
529 $conditionValue = $condition->
getValue();
530 $currentValues = [
'field' => $conditionValue];
533 $isBetweenOperator = $condition->
getOperator() === Bizproc\Activity\Operator\BetweenOperator::getCode();
537 : $documentService->getFieldInputValue($documentType, $property,
'field', $currentValues,$errors)
539 if ($isBetweenOperator)
541 $currentValues[
'field_greater_then'] =
542 is_array($conditionValue) && isset($conditionValue[0])
546 $currentValues[
'field_less_then'] =
547 is_array($conditionValue) && isset($conditionValue[1])
551 $property[
'Multiple'] =
false;
552 $valueInternal1 = $documentService->getFieldInputValue(
555 'field_greater_then',
559 $valueInternal2 = $documentService->getFieldInputValue(
567 $valueInternal = [$valueInternal1 ??
'', $valueInternal2 ??
''];
570 $result = new \Bitrix\Main\Result();
571 $result->setData([
'value' => $valueInternal]);
575 foreach ($errors as $error)
577 if (isset($error[
'message'], $error[
'code']))
579 $result->addError(
new \
Bitrix\Main\
Error($error[
'message'], $error[
'code']));