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