21 $users = (array)$users;
22 $documentUserFields = static::getDocumentFields($documentType,
'user');
23 $documentUserGroups = self::getDocumentUserServiceGroups($documentType);
25 foreach ($users as $user)
27 if (!is_scalar($user))
30 if (mb_substr($user, 0, 5) ===
"user_")
32 $user = intval(mb_substr($user, 5));
33 if (($user > 0) && !in_array($user, $result))
35 $userInfo = self::getUserInfo($user);
39 'name' => htmlspecialcharsBx($userInfo[
'fullName']),
40 'photoSrc' => $userInfo[
'photoSrc'],
41 'url' => $userInfo[
'url'],
42 'entityType' =>
'users',
46 elseif ($user ===
'author' &&
48 isset($documentUserFields[
'ASSIGNED_BY_ID']) ||
49 isset($documentUserFields[
'RESPONSIBLE_ID'])
53 $responsibleKey = isset($documentUserFields[
'ASSIGNED_BY_ID']) ?
'ASSIGNED_BY_ID' :
'RESPONSIBLE_ID';
56 'id' => $documentUserFields[$responsibleKey][
'Expression'],
57 'entityId' => $documentUserFields[$responsibleKey][
'Expression'],
58 'name' => htmlspecialcharsBx($documentUserFields[$responsibleKey][
'Name']),
59 'entityType' =>
'bpuserroles'
62 elseif (isset($documentUserGroups[$user]))
67 'name' => htmlspecialcharsBx($documentUserGroups[$user]),
68 'entityType' =>
'bpuserroles'
74 foreach ($documentUserFields as $field)
76 if ($user === $field[
'Expression'] || $user === $field[
'SystemExpression'])
79 'id' => $field[
'Expression'],
80 'entityId' => $field[
'Expression'],
81 'name' => htmlspecialcharsBx($field[
'Name']),
82 'entityType' =>
'bpuserroles'
88 if (!$found && isset($config[
'additionalFields']))
90 foreach ($config[
'additionalFields'] as $field)
92 if ($user === $field[
'entityId'])
96 'entityId' => $field[
'entityId'],
97 'name' => htmlspecialcharsBx($field[
'name']),
98 'entityType' =>
'bpuserroles'
110 $documentUserFields = static::getDocumentFields($documentType,
'user');
113 if (isset($documentUserFields[
'ASSIGNED_BY_ID']) || isset($documentUserFields[
'RESPONSIBLE_ID']))
115 $responsibleKey = isset($documentUserFields[
'ASSIGNED_BY_ID']) ?
'ASSIGNED_BY_ID' :
'RESPONSIBLE_ID';
116 $result =
'{=Document:'.$responsibleKey.
'}';
118 elseif (isset($documentUserFields[
'CREATED_BY']))
120 $result =
'{=Document:CREATED_BY}';
134 if (!Loader::includeModule(
'disk'))
137 foreach ((array)$attachments as $attachmentId)
139 $attachmentId = (int)$attachmentId;
140 if ($attachmentId <= 0)
145 $file = Disk\File::loadById($attachmentId);
149 'id' => $file->getId(),
150 'name' => $file->getName(),
151 'size' => \CFile::FormatSize($file->getSize()),
169 $files = (array)$files;
170 $documentUserFields = static::getDocumentFields($documentType,
'file');
172 foreach ($files as $file)
174 if (!is_scalar($file))
178 foreach ($documentUserFields as $id => $field)
180 if ($file !== $field[
'Expression'])
186 'expression' => $field[
'Expression'],
187 'name' => $field[
'Name'],
192 if (!$found && mb_strpos($file,
'{') === 0)
196 'expression' => $file,
212 $pattern = \CBPActivity::ValueInlinePattern;
213 [$mapIds, $mapNames, $mapObjectNames] = static::getExpressionsMaps($documentType);
215 $converter =
function ($matches) use ($mapIds, $mapNames, $mapObjectNames, $useTilda)
218 if (isset($matches[
'mod1']))
220 $mods[] = $matches[
'mod1'];
222 if (isset($matches[
'mod2']))
224 $mods[] = $matches[
'mod2'];
226 $modifiers = ($mods ?
' > ' . implode(
',', $mods) :
'');
228 $objectName = $matches[
'object'];
229 $fieldId = $matches[
'field'];
231 if (in_array($objectName, $mapObjectNames))
233 $key = array_search($fieldId, $mapIds[$objectName]);
236 $fieldName = $mapNames[$objectName][$key];
238 return '{{' . $fieldName . $modifiers .
'}}';
241 elseif ($useTilda && $objectName ===
'Template')
243 return '{{~*:' . $fieldId . $modifiers .
'}}';
245 elseif ($useTilda && $objectName ===
'Constant')
247 return '{{~&:' . $fieldId . $modifiers .
'}}';
249 elseif ($useTilda && preg_match(
'/^A[_0-9]+$/', $objectName))
251 return '{{~' . $objectName .
':' . $fieldId . $modifiers .
'}}';
257 return preg_replace_callback($pattern, $converter, $source);
264 $mapObjectNames = [];
266 $objectName = \Bitrix\Bizproc\Workflow\Template\SourceType::DocumentField;
267 [$ids, $names] = static::getFieldsMap($documentType);
268 $mapIds[$objectName] = $ids;
269 $mapNames[$objectName] = $names;
270 $mapObjectNames[] = $objectName;
272 $objectName = \Bitrix\Bizproc\Workflow\Template\SourceType::GlobalVariable;
273 [$ids, $names] = static::getGlobalsMap($objectName, $documentType);
274 $mapIds[$objectName] = $ids;
275 $mapNames[$objectName] = $names;
276 $mapObjectNames[] = $objectName;
278 $objectName = \Bitrix\Bizproc\Workflow\Template\SourceType::GlobalConstant;
279 [$ids, $names] = static::getGlobalsMap($objectName, $documentType);
280 $mapIds[$objectName] = $ids;
281 $mapNames[$objectName] = $names;
282 $mapObjectNames[] = $objectName;
284 return [$mapIds, $mapNames, $mapObjectNames];
289 $pattern =
'/\{\{(?<mixed>[^=].*?)\}\}/is';
290 [$mapIds, $mapNames, $mapObjectNames] = static::getExpressionsMaps($documentType);
292 $converter =
function ($matches) use ($mapIds, $mapNames, $mapObjectNames)
294 $matches[
'mixed'] = htmlspecialcharsback($matches[
'mixed']);
296 if (mb_strpos($matches[
'mixed'],
'~') === 0)
298 $len = mb_strpos($matches[
'mixed'],
'#');
299 $expression = ($len ===
false)
300 ? mb_substr($matches[
'mixed'], 1)
301 : mb_substr($matches[
'mixed'], 1, $len - 1)
304 if (mb_strpos($expression,
'*:') === 0)
306 $expression = ltrim($expression,
'*');
307 $expression =
'Template' . $expression;
310 if (mb_strpos($expression,
'&:') === 0)
312 $expression = ltrim($expression,
'&');
313 $expression =
'Constant' . $expression;
316 return '{=' . trim($expression) .
'}';
319 $pairs = explode(
'>', $matches[
'mixed']);
324 while (($pair = array_shift($pairs)) !==
null)
326 $fieldName .= $fieldName ?
'>' . $pair : $pair;
328 foreach ($mapObjectNames as $object)
330 $key = array_search(trim($fieldName), $mapNames[$object]);
333 $objectName = $object;
334 $fieldId = $mapIds[$object][$key];
345 if (!$fieldId && mb_substr($fieldName, -10) ===
'_printable')
347 $fieldName = mb_substr($fieldName, 0, -10);
348 $key = array_search(trim($fieldName), $mapNames[
'Document']);
351 $objectName =
'Document';
352 $fieldId = $mapIds[
'Document'][$key];
353 $pairs[] =
'printable';
359 $mods = isset($pairs[0]) ? trim($pairs[0]) :
'';
360 $modifiers = $mods ?
' > ' . $mods :
'';
362 return '{=' . $objectName .
':' . $fieldId . $modifiers .
'}';
368 return preg_replace_callback($pattern, $converter, $source);
371 public static function convertProperties(array $properties, array $documentType, $useTilda =
true)
373 foreach ($properties as $code => $property)
375 if (is_array($property))
381 $properties[$code] = static::convertExpressions($property, $documentType, $useTilda);
389 foreach ($properties as $code => $property)
391 if (is_array($property))
397 $properties[$code] = static::unConvertExpressions($property, $documentType);
411 $key = implode(
'@', $documentType);
412 if (!isset(static::$documentFields[$key]))
414 $documentService = \CBPRuntime::getRuntime()->getDocumentService();
417 static::$documentFields[$key] = $documentService->GetDocumentFields($documentType);
419 catch (\Exception $exception)
421 static::$documentFields[$key] = [];
427 if (is_array(static::$documentFields[$key]))
429 foreach (static::$documentFields[$key] as $id => $field)
431 if ($field[
'Type'] ===
'UF:boolean')
434 $field[
'Type'] = $field[
'BaseType'] =
'bool';
437 if ($field[
'Type'] ===
'UF:date')
440 $field[
'Type'] = $field[
'BaseType'] =
'date';
443 if ($typeFilter !==
null && $field[
'Type'] !== $typeFilter)
446 $field[
'Name'] = trim($field[
'Name']);
448 $resultFields[$id] = [
450 'Name' => $field[
'Name'],
451 'Type' => $field[
'Type'],
452 'BaseType' => $field[
'BaseType'] ?? $field[
'Type'],
453 'Expression' =>
'{{' . $field[
'Name'] .
'}}',
454 'SystemExpression' =>
'{=Document:' . $id .
'}',
455 'Options' => $field[
'Options'] ??
null,
456 'Settings' => $field[
'Settings'] ??
null,
457 'Multiple' => $field[
'Multiple'] ??
false,
462 return $resultFields;
468 $globalVariables = Bizproc\Workflow\Type\GlobalVar::getAll($documentType);
471 $visibilityNames = Bizproc\Workflow\Type\GlobalVar::getVisibilityFullNames($documentType);
472 foreach ($globalVariables as $id => $variable)
474 $name = trim($variable[
'Name']);
475 $visibilityName = $visibilityNames[$variable[
'Visibility']];
480 'Type' => $variable[
'Type'],
481 'BaseType' => $variable[
'Type'],
482 'Expression' =>
'{{' . $visibilityName .
': ' . $name .
'}}',
483 'SystemExpression' =>
'{=' . Bizproc\Workflow\Template\SourceType::GlobalVariable .
':' . $id .
'}',
484 'Options' => $variable[
'Options'] ??
null,
485 'Multiple' => $variable[
'Multiple'] ??
false,
486 'Visibility' => $variable[
'Visibility'],
487 'VisibilityName' => $visibilityName,
497 $globalConstants = Bizproc\Workflow\Type\GlobalConst::getAll($documentType);
500 $visibilityNames = Bizproc\Workflow\Type\GlobalConst::getVisibilityFullNames($documentType);
501 foreach ($globalConstants as $id => $constant)
503 $name = trim($constant[
'Name']);
504 $visibilityName = $visibilityNames[$constant[
'Visibility']];
509 'Type' => $constant[
'Type'],
510 'BaseType' => $constant[
'Type'],
511 'Expression' =>
'{{' . $visibilityName .
': ' . $name .
'}}',
512 'SystemExpression' =>
'{=' . Bizproc\Workflow\Template\SourceType::GlobalConstant .
':' . $id .
'}',
513 'Options' => $constant[
'Options'] ??
null,
514 'Multiple' => $constant[
'Multiple'] ??
false,
515 'Visibility' => $constant[
'Visibility'],
516 'VisibilityName' => $visibilityName,
523 private static function getDocumentUserServiceGroups(array $documentType)
525 $documentService = \CBPRuntime::getRuntime()->getDocumentService();
527 return $documentService->GetAllowableUserGroups($documentType);
532 $docGroups = self::getDocumentUserServiceGroups($documentType);
537 foreach ($docGroups as $id => $groupName)
539 if (!$groupName || mb_strpos($id,
'group_') === 0)
545 'id' => preg_match(
'/^[0-9]+$/', $id) ?
'G'.$id : $id,
556 $documentGroups = static::getDocumentUserGroups($documentType);
557 if (empty($documentGroups))
562 foreach ($documentGroups as $group)
564 if ($group[
'id'] === $value)
575 $key = implode(
'@', $documentType);
576 if (!isset(static::$maps[$key]))
581 $fields = static::getDocumentFields($documentType);
582 foreach ($fields as $field)
584 $id[] = $field[
'Id'];
585 $name[] = $field[
'Name'];
588 static::$maps[$key] = [$id, $name];
591 return static::$maps[$key];
598 case \Bitrix\Bizproc\Workflow\Template\SourceType::GlobalConstant:
599 $key =
'globals@const@' . implode(
'@', $documentType);
600 if (isset(static::$maps[$key]))
602 return static::$maps[$key];
604 $globals = static::getGlobalConstants($documentType);
606 case \Bitrix\Bizproc\Workflow\Template\SourceType::GlobalVariable:
607 $key =
'globals@var@' . implode(
'@', $documentType);
608 if (isset(static::$maps[$key]))
610 return static::$maps[$key];
612 $globals = static::getGlobalVariables($documentType);
621 foreach ($globals as $id => $property)
624 $names[] = $property[
'VisibilityName'] .
': ' . trim($property[
'Name']);
627 static::$maps[$key] = [$ids, $names];
629 return static::$maps[$key];
634 $interval = ltrim((
string)$interval,
'=');
647 if (mb_strpos($interval,
'settime(') === 0)
649 $interval = mb_substr($interval, 8, -1);
650 $arguments = explode(
',', $interval);
652 $minute = array_pop($arguments);
653 $hour = array_pop($arguments);
655 $interval = implode(
',', $arguments);
656 $result[
'inTime'] = [(int)$hour, (
int)$minute];
659 if (mb_strpos($interval,
'dateadd(') === 0 || mb_strpos($interval,
'workdateadd(') === 0)
661 if (mb_strpos($interval,
'workdateadd(') === 0)
663 $interval = mb_substr($interval, 12, -1);
664 $result[
'workTime'] =
true;
668 $interval = mb_substr($interval, 8, -1);
671 $arguments = explode(
',', $interval);
672 $result[
'basis'] = trim($arguments[0]);
674 $arguments[1] = trim(($arguments[1] ??
''),
'"\'');
677 preg_match_all(
'/\s*([\d]+)\s*(i|h|d)\s*/i', $arguments[1], $matches);
678 foreach ($matches[0] as $i => $match)
680 $values[$matches[2][$i]] = (int)$matches[1][$i];
683 elseif (\CBPDocument::IsExpression($interval))
685 $result[
'basis'] = $interval;
687 if ($result[
'basis'] !== static::CURRENT_DATETIME_BASIS)
697 $minutes = $values[
'i'] + $values[
'h'] * 60 + $values[
'd'] * 60 * 24;
699 if ($minutes % 1440 === 0)
701 $result[
'value'] = $minutes / 1440;
702 $result[
'valueType'] =
'd';
704 elseif ($minutes % 60 === 0)
706 $result[
'value'] = $minutes / 60;
707 $result[
'valueType'] =
'h';
711 $result[
'value'] = $minutes;
712 $result[
'valueType'] =
'i';
718 $result[
'basis'] !== static::CURRENT_DATETIME_BASIS
721 && \CBPDocument::IsExpression($result[
'basis'])
727 return $result + $values;
732 if (empty($interval[
'basis']) || !\CBPDocument::IsExpression($interval[
'basis']))
734 $interval[
'basis'] = static::CURRENT_DATE_BASIS;
737 $days = isset($interval[
'd']) ? (int)$interval[
'd'] : 0;
738 $hours = isset($interval[
'h']) ? (int)$interval[
'h'] : 0;
739 $minutes = isset($interval[
'i']) ? (int)$interval[
'i'] : 0;
741 if (isset($interval[
'value']) && isset($interval[
'valueType']))
743 switch ($interval[
'valueType'])
746 $minutes = (int)$interval[
'value'];
749 $hours = (int)$interval[
'value'];
752 $days = (int)$interval[
'value'];
764 $add .= $minutes.
'i';
771 $fn = !empty($interval[
'workTime']) ?
'workdateadd' :
'dateadd';
773 if ($fn ===
'workdateadd' && $add ===
'')
779 if ($fn ===
'workdateadd' && isset($interval[
'worker']))
781 $worker = $interval[
'worker'];
784 $result = $interval[
'basis'];
785 $isFunctionInResult =
false;
788 $result = $fn .
'(' . $interval[
'basis'] .
',"' . $add .
'"' . ($worker ?
',' . $worker :
'') .
')';
789 $isFunctionInResult =
true;
792 if (isset($interval[
'inTime']))
795 'settime(%s, %d, %d)',
797 $interval[
'inTime'][0] ?? 0,
798 $interval[
'inTime'][1] ?? 0
800 $isFunctionInResult =
true;
803 return $isFunctionInResult ?
'=' . $result : $result;
808 $pairs = preg_split(
'#[\s:]+#', $time);
809 $pairs[0] = (int)$pairs[0];
810 $pairs[1] = (int)$pairs[1];
812 if (count($pairs) === 3)
814 if ($pairs[2] ==
'pm' && $pairs[0] < 12)
816 if ($pairs[2] ==
'am' && $pairs[0] == 12)
820 return array(
'h' => $pairs[0],
'i' => $pairs[1]);
826 foreach ($statuses as $status)
829 if ($template->getId() > 0)
831 $cnt += count($template->getActivatedRobots());
838 private static function getUserInfo($userID, $format =
'', $htmlEncode =
false)
840 $userID = intval($userID);
846 $format = strval($format);
849 $format = \CSite::GetNameFormat(
false);
852 $dbUser = \CUser::GetList(
855 array(
'ID'=> $userID),
859 'NAME',
'SECOND_NAME',
'LAST_NAME',
860 'LOGIN',
'TITLE',
'EMAIL',
866 $user = $dbUser ? $dbUser->Fetch() :
null;
869 'fullName' => $user ? \CUser::FormatName($format, $user,
true, $htmlEncode) :
'',
870 'photoSrc' => $user ? \CBPViewHelper::getUserPhotoSrc($user) : null,
871 'url' => sprintf(
'/company/personal/user/%s/', $userID),
static getResponsibleUserExpression(array $documentType)
static parseDateTimeInterval($interval)
static convertExpressions($source, array $documentType, $useTilda=true)
static isDocumentUserGroup(string $value, array $documentType)
static countAllRobots(array $documentType, array $statuses)
static getGlobalVariables(array $documentType)
static getDocumentFields(array $documentType, $typeFilter=null)
static parseTimeString($time)
const CURRENT_DATETIME_BASIS
static getGlobalsMap(string $type, array $documentType)
static getFieldsMap(array $documentType)
static getExpressionsMaps($documentType)
static prepareFileAttachments(array $documentType, $files)
static unConvertProperties(array $properties, array $documentType)
static getDateTimeIntervalString($interval)
static getGlobalConstants(array $documentType)
static getDocumentUserGroups(array $documentType)
static prepareDiskAttachments($attachments)
static unConvertExpressions($source, array $documentType)
static convertProperties(array $properties, array $documentType, $useTilda=true)
static prepareUserSelectorEntities(array $documentType, $users, $config=[])