Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
robopackage.php
1<?php
2
4
10use CBPRuntime;
11use Bitrix\Main;
12
14{
15 public function makePackageData(Tpl $tpl)
16 {
17 $robotTemplate = Automation\Engine\Template::createByTpl($tpl);
18
19 if ($robotTemplate->isExternalModified())
20 {
21 return false;
22 }
23
24 $robots = $robotTemplate->toArray()['ROBOTS'];
25
26 return [
27 'DOCUMENT_TYPE' => $tpl->getDocumentComplexType(),
28 'DOCUMENT_STATUS' => $tpl->getDocumentStatus(),
29 'NAME' => $tpl->getName(),
30 'DESCRIPTION' => $tpl->getDescription(),
31 'PARAMETERS' => $tpl->getParameters(),
32 'VARIABLES' => $tpl->getVariables(),
33 'CONSTANTS' => $tpl->getConstants(),
34 'SYSTEM_CODE' => $tpl->getSystemCode(),
35 'ORIGINATOR_ID' => $tpl->getOriginatorId(),
36 'ORIGIN_ID' => $tpl->getOriginId(),
37
38 'ROBOTS' => $robots,
39 'DOCUMENT_FIELDS' => $this->getUsedDocumentFields($tpl),
40 'REQUIRED_APPLICATIONS' => $this->getRequiredApplications($tpl),
41 ];
42 }
43
44 public function pack(Tpl $tpl)
45 {
46 $datum = $this->makePackageData($tpl);
47
48 if (!$datum)
49 {
50 return (new Result\Pack())->addError(new Main\Error(Loc::getMessage("BIZPROC_WF_TEMPLATE_ROBOPACKAGE_EXTERNAL_MODIFIED")));
51 }
52
53 $datum = Main\Web\Json::encode($datum);
54 $datum = $this->compress($datum);
55
56 return (new Result\Pack())->setPackage($datum);
57 }
58
59 public function unpack($data)
60 {
61 $result = new Result\Unpack();
62 $datumTmp = $this->uncompress($data);
63
64 if (is_string($datumTmp))
65 {
66 try
67 {
68 $datumTmp = Main\Web\Json::decode($data);
69 }
70 catch (Main\ArgumentException $e)
71 {
72 //do nothing
73 }
74 }
75
76 if (is_array($datumTmp) && is_array($datumTmp['ROBOTS']))
77 {
78 $robotTemplate = new Automation\Engine\Template($datumTmp['DOCUMENT_TYPE']);
79 try
80 {
81 $robotTemplate->setRobots($datumTmp['ROBOTS']);
82 }
83 catch (Main\ArgumentException $e)
84 {
85 $result->addError(new Main\Error(Loc::getMessage("BIZPROC_WF_TEMPLATE_ROBOPACKAGE_WRONG_DATA")));
86
87 return $result;
88 }
89
91 $tpl = WorkflowTemplateTable::createObject();
92 $tpl->set('MODULE_ID', $datumTmp['DOCUMENT_TYPE'][0]);
93 $tpl->set('ENTITY', $datumTmp['DOCUMENT_TYPE'][1]);
94 $tpl->set('DOCUMENT_TYPE', $datumTmp['DOCUMENT_TYPE'][2]);
95 $tpl->set('DOCUMENT_STATUS', $datumTmp['DOCUMENT_STATUS']);
96 $tpl->set('NAME', $datumTmp['NAME']);
97 $tpl->set('DESCRIPTION', $datumTmp['DESCRIPTION']);
98 $tpl->set('TEMPLATE', $robotTemplate->getActivities());
99 $tpl->set('PARAMETERS', $datumTmp['PARAMETERS']);
100 $tpl->set('VARIABLES', $datumTmp['VARIABLES']);
101 $tpl->set('CONSTANTS', $datumTmp['CONSTANTS']);
102 $tpl->set('SYSTEM_CODE', $datumTmp['SYSTEM_CODE']);
103 $tpl->set('ORIGINATOR_ID', $datumTmp['ORIGINATOR_ID']);
104 $tpl->set('ORIGIN_ID', $datumTmp['ORIGIN_ID']);
105
106 return $result->setTpl($tpl)
107 ->setDocumentFields($datumTmp['DOCUMENT_FIELDS'])
108 ->setRequiredApplications($datumTmp['REQUIRED_APPLICATIONS']);
109 }
110
111 $result->addError(new Main\Error(Loc::getMessage("BIZPROC_WF_TEMPLATE_ROBOPACKAGE_WRONG_DATA")));
112
113 return $result;
114 }
115
116 private function getUsedDocumentFields(Tpl $tpl)
117 {
118 $usedFieldKeys = $tpl->findUsedSourceKeys(SourceType::DocumentField);
119
120 if (!$usedFieldKeys)
121 {
122 return [];
123 }
124
125 $documentService = CBPRuntime::GetRuntime(true)->getDocumentService();
126 $documentFields = $documentService->GetDocumentFields($tpl->getDocumentComplexType(), true);
127
128 $result = [];
129
130 foreach ($usedFieldKeys as $fieldKey)
131 {
132 if (
133 mb_strtoupper(mb_substr($fieldKey, -10)) !== '_PRINTABLE'
134 &&
135 isset($documentFields[$fieldKey])
136 )
137 {
138 $result[$fieldKey] = $documentFields[$fieldKey];
139 }
140 }
141
142 return $result;
143 }
144
145 private function getRequiredApplications(Tpl $tpl)
146 {
147 $types = $tpl->getUsedActivityTypes();
148 $apps = [];
149
150 foreach ($types as $type)
151 {
152 if (mb_strpos($type, 'rest_') === 0)
153 {
154 $apps[] = $type;
155 }
156 }
157
158 //TODO get app external id`s
159
160 return $apps;
161 }
162}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29