Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
template.php
1<?php
3
6use Bitrix\Bizproc\WorkflowTemplateTable;
14
15Loc::loadMessages(__FILE__);
16
22{
23 protected static $parallelActivityType = 'ParallelActivity';
24 protected static $sequenceActivityType = 'SequenceActivity';
26 protected static $delayActivityType = 'DelayActivity';
27 protected static $robotDelayActivityType = 'RobotDelayActivity';
28 protected static $conditionActivityType = 'IfElseActivity';
29 protected static $availableActivities = [];
30 protected static $availableActivityClasses = [];
31
32 protected $template;
33 protected $autoExecuteType = \CBPDocumentEventType::Automation;
35 protected $robots;
37 protected $isConverted = false;
38
47 public function __construct(array $documentType, $documentStatus = null)
48 {
49 $this->template = array(
50 'ID' => 0,
51 'MODULE_ID' => $documentType[0],
52 'ENTITY' => $documentType[1],
53 'DOCUMENT_TYPE' => $documentType[2],
54 'DOCUMENT_STATUS' => $documentStatus,
55 'AUTO_EXECUTE' => $this->autoExecuteType,
56 'TEMPLATE' => [],
57 'PARAMETERS' => [],
58 'CONSTANTS' => [],
59 'VARIABLES' => [],
60 );
61
62 if ($documentStatus)
63 {
64 $row = WorkflowTemplateTable::getList([
65 'filter' => [
66 '=MODULE_ID' => $documentType[0],
67 '=ENTITY' => $documentType[1],
68 '=DOCUMENT_TYPE' => $documentType[2],
69 '=DOCUMENT_STATUS' => $documentStatus,
70 //'=AUTO_EXECUTE' => $this->autoExecuteType
71 ],
72 ])->fetch();
73 if ($row)
74 {
75 $this->template = $row;
76 $this->autoExecuteType = (int) $this->template['AUTO_EXECUTE'];
77 }
78 }
79 }
80
81 public static function createByTpl(Tpl $tpl)
82 {
83 $instance = new static($tpl->getDocumentComplexType());
84 $instance->template = $tpl->collectValues();
85 $instance->autoExecuteType = (int) $instance->template['AUTO_EXECUTE'];
86
87 return $instance;
88 }
89
90 public function deleteRobots(array $robots, int $userId): Result
91 {
92 $isSameRobot = function ($lhsRobot, $rhsRobot) {
93 return strcmp($lhsRobot->getName(), $rhsRobot->getName());
94 };
95
96 $remainingRobots = array_udiff($this->getRobots(), $robots, $isSameRobot);
97
98 return $this->save($remainingRobots, $userId);
99 }
100
101 public function getDocumentStatus()
102 {
103 return isset($this->template['DOCUMENT_STATUS']) ? $this->template['DOCUMENT_STATUS'] : null;
104 }
105
106 public function setDocumentStatus($status)
107 {
108 $this->template['DOCUMENT_STATUS'] = (string) $status;
109 return $this;
110 }
111
112 public function setName(string $name)
113 {
114 $this->template['NAME'] = $name;
115 return $this;
116 }
117
119 {
121 }
122
124 {
125 if (\CBPDocumentEventType::Out($autoExecuteType) === '')
126 {
127 throw new ArgumentException('Incorrect DocumentEventType');
128 }
129
130 $this->autoExecuteType = $autoExecuteType;
131 }
132
133 public function getId()
134 {
135 return isset($this->template['ID']) ? (int)$this->template['ID'] : 0;
136 }
137
138 public function getParameters(): array
139 {
140 return $this->template['PARAMETERS'] ?? [];
141 }
142
143 public function getRobotSettingsDialog(array $robot, $request = null)
144 {
145 if (isset($robot['Properties']) && is_array($robot['Properties']))
146 {
147 $robot['Properties'] = Automation\Helper::convertProperties($robot['Properties'], $this->getDocumentType());
148 }
149
150 unset($robot['Delay'], $robot['Condition']);
151
152 $copy = clone $this;
153 $copy->setRobots([$robot]);
154
155 return \CBPActivity::callStaticMethod(
156 $robot['Type'],
157 "GetPropertiesDialog",
158 array(
159 $this->getDocumentType(), //documentType
160 $robot['Name'] ?? null, //activityName
161 $copy->template['TEMPLATE'], //arWorkflowTemplate
162 [], //arWorkflowParameters
163 [], //arWorkflowVariables
164 $request, //arCurrentValues = null
165 'bizproc_automation_robot_dialog', //formName = ""
166 null, //popupWindow = null
167 SITE_ID //siteId = ''
168 )
169 );
170 }
171
172 public function saveRobotSettings(array $robot, array $request)
173 {
174 $saveResult = new Result();
175 $documentType = $this->getDocumentType();
176
177 if (isset($robot['Properties']) && is_array($robot['Properties']))
178 {
179 $robot['Properties'] = Automation\Helper::unConvertProperties($robot['Properties'], $documentType);
180 }
181
182 $request = Automation\Helper::unConvertProperties($request, $documentType);
183
184 $copy = clone $this;
185 $copy->setRobots([$robot]);
186 $raw = $copy->template['TEMPLATE'];
187
188 $robotErrors = $v = $p = array();
189 $result = \CBPActivity::callStaticMethod(
190 $robot['Type'],
191 "GetPropertiesDialogValues",
192 [
193 $documentType,
194 $robot['Name'] ?? null,
195 &$raw,
196 &$v,
197 &$p,
198 $request,
199 &$robotErrors,
200 ]
201 );
202
203 if ($result || (isset($robot['Activated']) && $robot['Activated'] === 'N'))
204 {
205 $templateActivity = \CBPWorkflowTemplateLoader::findActivityByName($raw, $robot['Name'] ?? null);
206
207 $robotTitle = $robot['Properties']['Title'] ?? null;
208 $robot['Properties'] = $templateActivity['Properties'];
209 $robot['Properties']['Title'] = $robotTitle;
210
211 $saveResult->setData(['robot' => $robot]);
212 }
213 else
214 {
215 foreach ($robotErrors as $i => $error)
216 {
217 $errorMessage = $error['message'] ?? null;
218 $errorCode = $error['code'] ?? null;
219 $errorParameter = $error['parameter'] ?? null;
220 $saveResult->addError(new Error($errorMessage, $errorCode, ['parameter' => $errorParameter]));
221 }
222 }
223
224 return $saveResult;
225 }
226
227 public function save(array $robots, $userId, array $additional = [])
228 {
229 $userId = (int)$userId;
230 $result = new Result();
231 $templateId = !empty($this->template['ID']) ? $this->template['ID'] : 0;
232
233 if (isset($additional['PARAMETERS']) && is_array($additional['PARAMETERS']))
234 {
235 $this->template['PARAMETERS'] = $additional['PARAMETERS'];
236 }
237 if (isset($additional['CONSTANTS']) && is_array($additional['CONSTANTS']))
238 {
239 $this->template['CONSTANTS'] = $additional['CONSTANTS'];
240 }
241
242 if ($templateId)
243 {
244 $templateResult = $this->updateTemplateRobots($robots, $userId);
245 }
246 else
247 {
248 $this->setRobots($robots);
249 $templateResult = $this->addBizprocTemplate($userId);
250 }
251
252 if ($templateResult->isSuccess())
253 {
254 $resultData = $templateResult->getData();
255 if (isset($resultData['ID']))
256 {
257 $this->template['ID'] = $resultData['ID'];
258 }
259 }
260 else
261 {
262 $result->addErrors($templateResult->getErrors());
263 }
264
265 return $result;
266 }
267
268 public function setRobots(array $robots)
269 {
270 $this->robots = array();
271 $this->isExternalModified = null;
272 foreach ($robots as $robot)
273 {
274 if (is_array($robot))
275 $robot = new Robot($robot);
276
277 if (!($robot instanceof Robot))
278 {
279 throw new ArgumentException('Robots array is incorrect', 'robots');
280 }
281
282 $this->robots[] = $robot;
283 }
284
285 $this->unConvertTemplate();// make bizproc template
286
287 return $this;
288 }
289
294 public function toArray()
295 {
296 $result = [
297 'ID' => $this->getId(),
298 'DOCUMENT_TYPE' => $this->getDocumentType(),
299 'DOCUMENT_STATUS' => $this->template['DOCUMENT_STATUS'],
300 'PARAMETERS' => $this->template['PARAMETERS'],
301 'CONSTANTS' => $this->template['CONSTANTS'],
302 'VARIABLES' => $this->template['VARIABLES'] ?? [],
303 ];
304
305 $result['IS_EXTERNAL_MODIFIED'] = $this->isExternalModified();
306 $result['ROBOTS'] = array();
307
308 foreach ($this->getRobots() as $robot)
309 {
310 $result['ROBOTS'][] = $robot->toArray();
311 }
312
313 return $result;
314 }
315
316 public static function getAvailableRobots(array $documentType)
317 {
318 $key = implode('@', $documentType);
319 if (!isset(static::$availableActivities[$key]))
320 {
321 static::$availableActivities[$key] = \CBPRuntime::getRuntime()
322 ->searchActivitiesByType('robot_activity', $documentType);
323 }
324 return static::$availableActivities[$key];
325 }
326
327 protected static function getAvailableRobotClasses(array $documentType)
328 {
329 $key = implode('@', $documentType);
330 if (!isset(static::$availableActivityClasses[$key]))
331 {
332 static::$availableActivityClasses[$key] = array();
333 $activities = static::getAvailableRobots($documentType);
334 foreach ($activities as $activity)
335 {
336 static::$availableActivityClasses[$key][] = $activity['CLASS'];
337 }
338 }
339 return static::$availableActivityClasses[$key];
340 }
341
342 protected function addBizprocTemplate($userId)
343 {
344 $userId = (int)$userId;
345 $documentType = $this->getDocumentType();
346
347 $raw = $this->template;
348 $raw['DOCUMENT_TYPE'] = $documentType;
349 $raw['NAME'] = $raw['NAME'] ?? $this->makeTemplateName();
350 $raw['USER_ID'] = $userId;
351 $raw['MODIFIER_USER'] = new \CBPWorkflowTemplateUser($userId);
352
353 $result = new Result();
354 try
355 {
356 $raw['ID'] = \CBPWorkflowTemplateLoader::add($raw, $userId === 1);
357 $result->setData(array('ID' => $raw['ID']));
358
359 $raw['MODULE_ID'] = $documentType[0];
360 $raw['ENTITY'] = $documentType[1];
361 $raw['DOCUMENT_TYPE'] = $documentType[2];
362 $raw['PARAMETERS'] = [];
363 $raw['CONSTANTS'] = [];
364 $this->template = $raw;
365 }
366 catch (\Exception $e)
367 {
368 $result->addError(new Error($e->getMessage()));
369 }
370
371 return $result;
372 }
373
374 protected function makeTemplateName()
375 {
376 $msg = Loc::getMessage('BIZPROC_AUTOMATION_TEMPLATE_NAME', [
377 '#STATUS#' => $this->template['DOCUMENT_STATUS']
378 ]);
379
380 if ($this->autoExecuteType === \CBPDocumentEventType::Script)
381 {
382 $msg = Loc::getMessage('BIZPROC_AUTOMATION_TEMPLATE_SCRIPT_NAME');
383 }
384
385 return $msg;
386 }
387
388 private function updateTemplateRobots(array $robots, int $userId): Result
389 {
390 $templateId = $this->template['ID'];
391 $result = new Result();
392
393 $errors = $this->validateUpdatedRobots($robots, new \CBPWorkflowTemplateUser($userId));
394 if (!$errors->isEmpty())
395 {
396 $result->addErrors($errors->getValues());
397
398 return $result;
399 }
400
401 $this->setRobots($robots);
402 $updateFields = [
403 'TEMPLATE' => $this->template['TEMPLATE'],
404 'PARAMETERS' => $this->template['PARAMETERS'],
405 'VARIABLES' => [],
406 'CONSTANTS' => $this->template['CONSTANTS'],
407 'USER_ID' => $userId,
408 'MODIFIER_USER' => new \CBPWorkflowTemplateUser($userId),
409 ];
410
411 if (isset($this->template['NAME']))
412 {
413 $updateFields['NAME'] = $this->template['NAME'];
414 }
415
416 try
417 {
418 \CBPWorkflowTemplateLoader::update($templateId, $updateFields, false, false);
419 }
420 catch (\Exception $e)
421 {
422 $result->addError(new Error($e->getMessage()));
423 }
424
425 return $result;
426 }
427
428 private function validateUpdatedRobots(array $robots, \CBPWorkflowTemplateUser $user): ErrorCollection
429 {
430 $errors = new ErrorCollection();
431 $loader = \CBPWorkflowTemplateLoader::GetLoader();
432 $originalRobots = $this->getRobots();
433
434 $isSameRobot = function ($lhsRobot, $rhsRobot) {
435 return $lhsRobot->getName() === $rhsRobot->getName();
436 };
437
439 foreach ($robots as $robot)
440 {
441 if (is_array($robot))
442 {
443 $robot = new Robot($robot);
444 }
445 if (!($robot instanceof Robot))
446 {
447 $errors->setError(new Error('Robots array is incorrect'));
448 }
449 if (!$errors->isEmpty())
450 {
451 break;
452 }
453
454 $indexOfFoundRobot = -1;
455 foreach ($originalRobots as $index => $originalRobot)
456 {
457 if ($isSameRobot($robot, $originalRobot))
458 {
459 $indexOfFoundRobot = $index;
460 break;
461 }
462 }
463
464 if ($indexOfFoundRobot < 0 || !$this->areRobotsEqual($robot, $originalRobots[$indexOfFoundRobot]))
465 {
466 if ($robot->isActivated())
467 {
468 $sequence = $this->convertRobotToSequenceActivity($robot);
469 foreach ($loader->ValidateTemplate($sequence, $user) as $rawError)
470 {
471 $errors->setError(new Error(trim($rawError['message'])));
472 }
473 }
474 unset($originalRobots[$indexOfFoundRobot]);
475 }
476 }
477
478 return $errors;
479 }
480
481 private function areRobotsEqual(Robot $lhsRobot, Robot $rhsRobot): bool
482 {
483 $lhsCondition = $lhsRobot->getCondition() ?? new ConditionGroup();
484 $rhsCondition = $rhsRobot->getCondition() ?? new ConditionGroup();
485
486 $lhsDelay = $lhsRobot->getDelayInterval();
487 $rhsDelay = $rhsRobot->getDelayInterval();
488 if (!isset($lhsDelay) || $lhsDelay->isNow())
489 {
490 $lhsDelay = new DelayInterval();
491 }
492 if (!isset($rhsDelay) || $rhsDelay->isNow())
493 {
494 $rhsDelay = new DelayInterval();
495 }
496
497 return
498 $lhsCondition->toArray()['items'] === $rhsCondition->toArray()['items']
499 && $lhsDelay->toArray() === $rhsDelay->toArray()
500 && $lhsRobot->getBizprocActivity() === $rhsRobot->getBizprocActivity()
501 ;
502 }
503
504 protected function updateBizprocTemplate($id, $userId)
505 {
506 $raw = $this->template;
507 $result = new Result();
508
509 $updateFields = [
510 'TEMPLATE' => $raw['TEMPLATE'],
511 'PARAMETERS' => $raw['PARAMETERS'],
512 'VARIABLES' => [],
513 'CONSTANTS' => $raw['CONSTANTS'],
514 'USER_ID' => $userId,
515 'MODIFIER_USER' => new \CBPWorkflowTemplateUser($userId),
516 ];
517
518 if (isset($raw['NAME']))
519 {
520 $updateFields['NAME'] = $raw['NAME'];
521 }
522
523 try
524 {
525 \CBPWorkflowTemplateLoader::update($id, $updateFields);
526 }
527 catch (\Exception $e)
528 {
529 $result->addError(new Error($e->getMessage()));
530 }
531
532 return $result;
533 }
534
535 protected function convertTemplate()
536 {
537 $this->robots = array();
538
539 $raw = $this->template;
540 if (!is_array($raw) || !isset($raw['TEMPLATE']))
541 {
542 return false; // BP template is lost.
543 }
544
545 /*if (!empty($raw['PARAMETERS']) || !empty($raw['VARIABLES']) || !empty($raw['CONSTANTS']))
546 {
547 $this->isExternalModified = true;
548 return false; // modified or incorrect.
549 }*/
550
551 if (empty($raw['TEMPLATE'][0]['Children']) || !is_array($raw['TEMPLATE'][0]['Children']))
552 return true;
553
554 if (count($raw['TEMPLATE'][0]['Children']) > 1)
555 {
556 $this->isExternalModified = true;
557 return false; // modified or incorrect.
558 }
559
560 $parallelActivity = $raw['TEMPLATE'][0]['Children'][0];
561 if (
562 !$parallelActivity
563 || $parallelActivity['Type'] !== static::$parallelActivityType
564 || (isset($parallelActivity['Activated']) && $parallelActivity['Activated'] === 'N')
565 )
566 {
567 $this->isExternalModified = true;
568 return false; // modified or incorrect.
569 }
570
571 foreach ($parallelActivity['Children'] as $sequence)
572 {
573 $delay = $condition = null;
574 $robotsCnt = 0;
575 foreach ($sequence['Children'] as $activity)
576 {
577 if (
578 $activity['Type'] === static::$delayActivityType
579 || $activity['Type'] === static::$robotDelayActivityType)
580 {
581 $delay = $activity;
582 continue;
583 }
584
585 if ($activity['Type'] === static::$conditionActivityType)
586 {
587 $condition = ConditionGroup::convertBizprocActivity($activity, $this->getDocumentType(), $this);
588 if ($condition === false)
589 {
590 $this->isExternalModified = true;
591 $this->robots = array();
592 return false; // modified or incorrect.
593 }
594 }
595
596 if (!$this->isRobot($activity))
597 {
598 $this->isExternalModified = true;
599 $this->robots = array();
600 return false; // modified or incorrect.
601 }
602
603 $robotActivity = new Robot($activity);
604 if ($delay !== null)
605 {
606 $delayInterval = DelayInterval::createFromActivityProperties($delay['Properties']);
607 $delayInterval->setActivated(\CBPHelper::getBool($delay['Activated'] ?? true));
608
609 $robotActivity->setDelayInterval($delayInterval);
610 $robotActivity->setDelayName($delay['Name']);
611
612 if($delayInterval->isActivated() !== $robotActivity->isActivated())
613 {
614 $this->isExternalModified = true;
615 $this->robots = [];
616
617 return false; // modified
618 }
619
620 $delay = null;
621 }
622
623 if ($condition !== null)
624 {
625 if ($condition->isActivated() !== $robotActivity->isActivated())
626 {
627 $this->isExternalModified = true;
628 $this->robots = [];
629
630 return false; // modified
631 }
632
633 $robotActivity->setCondition($condition);
634 $condition = null;
635 }
636
637 if ($robotsCnt > 0)
638 {
639 $robotActivity->setExecuteAfterPrevious();
640 }
641
642 ++$robotsCnt;
643 $this->robots[] = $robotActivity;
644 }
645 }
646
647 $this->isConverted = true;
648 return $this->robots;
649 }
650
651 protected function unConvertTemplate()
652 {
653 $documentType = $this->getDocumentType();
654 $this->template = [
655 'ID' => $this->getId(),
656 'MODULE_ID' => $documentType[0],
657 'ENTITY' => $documentType[1],
658 'DOCUMENT_TYPE' => $documentType[2],
659 'DOCUMENT_STATUS' => $this->template['DOCUMENT_STATUS'],
660 'NAME' => $this->template['NAME'] ?? $this->makeTemplateName(),
661 'AUTO_EXECUTE' => $this->autoExecuteType,
662 'TEMPLATE' => [[
663 'Type' => 'SequentialWorkflowActivity',
664 'Name' => 'Template',
665 'Properties' => ['Title' => 'Bizproc Automation template'],
666 'Children' => [],
667 ]],
668 'PARAMETERS' => $this->template['PARAMETERS'],
669 'CONSTANTS' => $this->template['CONSTANTS'],
670 'SYSTEM_CODE' => 'bitrix_bizproc_automation',
671 ];
672
673 if ($this->robots)
674 {
675 $parallelActivity = $this->createParallelActivity();
676 $sequence = $this->createSequenceActivity();
677
678 foreach ($this->robots as $i => $robot)
679 {
680 if ($i !== 0 && !$robot->isExecuteAfterPrevious())
681 {
682 $parallelActivity['Children'][] = $sequence;
683 $sequence = $this->convertRobotToSequenceActivity($robot);
684 }
685 else
686 {
687 $sequence['Children'] = array_merge(
688 $sequence['Children'],
689 $this->convertRobotToSequenceActivity($robot)['Children']
690 );
691 }
692 }
693
694 $parallelActivity['Children'][] = $sequence;
695
696 if (count($parallelActivity['Children']) < 2)
697 {
698 $parallelActivity['Children'][] = $this->createSequenceActivity();
699 }
700
701 $this->template['TEMPLATE'][0]['Children'][] = $parallelActivity;
702 }
703 $this->robots = null;
704 $this->isConverted = false;
705 }
706
707 private function convertRobotToSequenceActivity(Robot $robot): array
708 {
709 $sequence = $this->createSequenceActivity();
710
711 $delayInterval = $robot->getDelayInterval();
712 if ($delayInterval && !$delayInterval->isNow())
713 {
714 $delayName = $robot->getDelayName();
715 if (!$delayName)
716 {
717 $delayName = Robot::generateName();
718 $robot->setDelayName($delayName);
719 }
720
721 $delayIntervalProperties = $delayInterval->toActivityProperties($this->getDocumentType());
722
723 $sequence['Children'][] = $this->createDelayActivity(
724 $delayIntervalProperties,
725 $delayName,
726 $robot->isActivated()
727 );
728 }
729
730 $activity = $robot->getBizprocActivity();
731 $condition = $robot->getCondition();
732
733 if ($condition && $condition->getItems())
734 {
735 $activity = $condition->createBizprocActivity($activity, $this->getDocumentType(), $this);
736 }
737
738 $sequence['Children'][] = $activity;
739
740 return $sequence;
741 }
742
743 protected function isRobot(array $activity)
744 {
745 if (!in_array($activity['Type'], static::getAvailableRobotClasses($this->getDocumentType())))
746 {
747 if ($this->isRestRobot($activity))
748 {
749 return true;
750 }
751
752 return false;
753 }
754
755 if (!empty($activity['Children']))
756 {
757 return false;
758 }
759
760 return true;
761 }
762
763 private function isRestRobot(array $activity): bool
764 {
765 if (!(mb_strpos($activity['Type'], \CBPRuntime::REST_ACTIVITY_PREFIX) === 0))
766 {
767 return false;
768 }
769
770 if (!empty($activity['Children']))
771 {
772 return false;
773 }
774
775 return true;
776 }
777
781 public function getRobots()
782 {
783 if ($this->robots === null)
784 {
785 $this->convertTemplate();
786 }
787
788 return $this->robots;
789 }
790
791 public function getActivatedRobots(): array
792 {
793 if ($this->robots === null)
794 {
795 $this->convertTemplate();
796 }
797
798 $activatedRobots = [];
799 foreach ($this->robots as $robot)
800 {
801 if ($robot->isActivated())
802 {
803 $activatedRobots[] = $robot;
804 }
805 }
806
807 return $activatedRobots;
808 }
809
815 public function getRobotByName(string $name): ?Robot
816 {
817 foreach ($this->getRobots() as $robot)
818 {
819 if ($name === $robot->getName())
820 {
821 return $robot;
822 }
823 }
824
825 return null;
826 }
827
828 public function getRobotsByNames(array $names): array
829 {
830 return array_uintersect($this->getRobots(), $names, function ($lhs, $rhs) {
831 $lhsName = is_string($lhs) ? $lhs : $lhs->getName();
832 $rhsName = is_string($rhs) ? $rhs : $rhs->getName();
833
834 return strcmp($lhsName, $rhsName);
835 });
836 }
837
841 public function getActivities()
842 {
843 return $this->template['TEMPLATE'];
844 }
845
846 public function getModified(): ?DateTime
847 {
848 return $this->template['MODIFIED'] ?? null;
849 }
850
855 public function isExternalModified()
856 {
857 if ($this->isExternalModified === null)
858 $this->getRobots();
859
860 return ($this->isExternalModified === true);
861 }
862
863 public function getDocumentType(): array
864 {
865 return [$this->template['MODULE_ID'], $this->template['ENTITY'], $this->template['DOCUMENT_TYPE']];
866 }
867
868 public function getProperty($object, $field): ?array
869 {
870 switch ($object)
871 {
872 case 'Template':
873 return $this->template['PARAMETERS'][$field] ?? null;
874 case 'Variable':
875 return $this->template['VARIABLES'][$field] ?? null;
876 case 'Constant':
877 return $this->template['CONSTANTS'][$field] ?? null;
878 case 'GlobalConst':
879 return Bizproc\Workflow\Type\GlobalConst::getVisibleById($field, $this->getDocumentType());
880 case 'GlobalVar':
881 return Bizproc\Workflow\Type\GlobalVar::getVisibleById($field, $this->getDocumentType());
882 case 'Document':
883 static $fields;
884 if (!$fields)
885 {
886 $documentService = \CBPRuntime::GetRuntime(true)->getDocumentService();
887 $fields = $documentService->GetDocumentFields($this->getDocumentType());
888 }
889
890 return $fields[$field] ?? null;
891 default:
892 if ($this->isConverted)
893 {
894 return $this->findRobotProperty($object, $field);
895 }
896 else
897 {
898 return $this->findActivityProperty($object, $field);
899 }
900 }
901 }
902
903 private function findRobotProperty($object, $field): ?array
904 {
905 $robot = $this->getRobotByName($object);
906 return $robot ? $robot->getReturnProperty($field) : null;
907 }
908
909 private function findActivityProperty($object, $field): ?array
910 {
911 $activity = self::findTemplateActivity($this->template['TEMPLATE'], $object);
912 if (!$activity)
913 {
914 return null;
915 }
916
917 $props = \CBPRuntime::GetRuntime(true)->getActivityReturnProperties($activity['Type']);
918 return $props[$field] ?? null;
919 }
920
921 private static function findTemplateActivity(array $template, $id)
922 {
923 foreach ($template as $activity)
924 {
925 if ($activity['Name'] === $id)
926 {
927 return $activity;
928 }
929 if (is_array($activity['Children']))
930 {
931 $found = self::findTemplateActivity($activity['Children'], $id);
932 if ($found)
933 {
934 return $found;
935 }
936 }
937 }
938 return null;
939 }
940
941 private function createSequenceActivity()
942 {
943 return [
944 'Type' => static::$sequenceActivityType,
945 'Name' => Robot::generateName(),
946 'Properties' => [
947 'Title' => 'Automation sequence',
948 ],
949 'Children' => [],
950 'Activated' => 'Y',
951 ];
952 }
953
954 private function createParallelActivity()
955 {
956 return [
957 'Type' => static::$parallelActivityType,
958 'Name' => Robot::generateName(),
959 'Properties' => [
960 'Title' => Loc::getMessage('BIZPROC_AUTOMATION_PARALLEL_ACTIVITY'),
961 ],
962 'Children' => [],
963 'Activated' => 'Y',
964 ];
965 }
966
967 private function createDelayActivity(array $delayProperties, $delayName, $delayActived)
968 {
969 if (!isset($delayProperties['Title']))
970 {
971 $delayProperties['Title'] = Loc::getMessage('BIZPROC_AUTOMATION_ROBOT_DELAY_ACTIVITY');
972 }
973
974 return [
975 'Type' => static::$robotDelayActivityType,
976 'Name' => $delayName,
977 'Activated' => $delayActived ? 'Y' : 'N',
978 'Properties' => $delayProperties,
979 'Children' => [],
980 ];
981 }
982}
static convertBizprocActivity(array &$activity, array $documentType, Template $template)
static createFromActivityProperties(array $properties)
deleteRobots(array $robots, int $userId)
Definition template.php:90
static getAvailableRobotClasses(array $documentType)
Definition template.php:327
static getAvailableRobots(array $documentType)
Definition template.php:316
__construct(array $documentType, $documentStatus=null)
Definition template.php:47
getRobotSettingsDialog(array $robot, $request=null)
Definition template.php:143
save(array $robots, $userId, array $additional=[])
Definition template.php:227
saveRobotSettings(array $robot, array $request)
Definition template.php:172
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29