Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
bpt.php
1<?php
2
4
9use CBPDocument;
10use CBPRuntime;
11
12class Bpt extends BasePacker
13{
16
17 protected $useCompression = true;
18
19 public function enableCompression()
20 {
21 $this->useCompression = true;
22 }
23
24 public function disableCompression()
25 {
26 $this->useCompression = false;
27 }
28
29 public function makePackageData(Tpl $tpl)
30 {
31 $documentService = CBPRuntime::GetRuntime(true)->getDocumentService();
32 $documentFieldsTmp = $documentService->GetDocumentFields($tpl->getDocumentComplexType(), true);
33 $documentFieldsAliasesMap = CBPDocument::getDocumentFieldsAliasesMap($documentFieldsTmp);
34
35 $documentFields = [];
36 $len = mb_strlen("_PRINTABLE");
37 foreach ($documentFieldsTmp as $k => $v)
38 {
39 if (mb_strtoupper(mb_substr($k, -$len)) != "_PRINTABLE")
40 {
41 $documentFields[$k] = $v;
42 }
43 }
44
45 $tplData = $tpl->collectValues();
46
47 if ($documentFieldsAliasesMap)
48 {
49 self::replaceTemplateDocumentFieldsAliases($tplData['TEMPLATE'], $documentFieldsAliasesMap);
50 self::replaceVariablesDocumentFieldsAliases($tplData['PARAMETERS'], $documentFieldsAliasesMap);
51 self::replaceVariablesDocumentFieldsAliases($tplData['VARIABLES'], $documentFieldsAliasesMap);
52 self::replaceVariablesDocumentFieldsAliases($tplData['CONSTANTS'], $documentFieldsAliasesMap);
53 }
54
55 return [
56 "VERSION" => 2,
57 "TEMPLATE" => self::ConvertArrayCharset($tplData["TEMPLATE"], static::DIRECTION_EXPORT),
58 "PARAMETERS" => self::ConvertArrayCharset($tplData["PARAMETERS"], static::DIRECTION_EXPORT),
59 "VARIABLES" => self::ConvertArrayCharset($tplData["VARIABLES"], static::DIRECTION_EXPORT),
60 "CONSTANTS" => self::ConvertArrayCharset($tplData["CONSTANTS"], static::DIRECTION_EXPORT),
61 "DOCUMENT_FIELDS" => self::ConvertArrayCharset($documentFields, static::DIRECTION_EXPORT),
62 ];
63 }
64
65 public function pack(Tpl $tpl)
66 {
67 $datum = $this->makePackageData($tpl);
68 $datum = serialize($datum);
69
70 if ($this->useCompression)
71 {
72 $datum = $this->compress($datum);
73 }
74
75 return (new Result\Pack())->setPackage($datum);
76 }
77
78 public function unpack($data)
79 {
80 $result = new Result\Unpack();
81
82 $datumTmp = \CheckSerializedData($data) ? @unserialize($data, ['allowed_classes' => false]) : null;
83
84 if (!is_array($datumTmp) || is_array($datumTmp) && !array_key_exists("TEMPLATE", $datumTmp))
85 {
86 $datumTmp = $this->uncompress($data);
87 $datumTmp = \CheckSerializedData($datumTmp) ? @unserialize($datumTmp, ['allowed_classes' => false]) : null;
88 }
89
90 if (!is_array($datumTmp) || is_array($datumTmp) && !array_key_exists("TEMPLATE", $datumTmp))
91 {
92 $result->addError(new Main\Error(Loc::getMessage("BIZPROC_WF_TEMPLATE_BPT_WRONG_DATA")));
93 return $result;
94 }
95
96 if (array_key_exists("VERSION", $datumTmp) && $datumTmp["VERSION"] == 2)
97 {
98 $datumTmp["TEMPLATE"] = self::ConvertArrayCharset($datumTmp["TEMPLATE"], static::DIRECTION_IMPORT);
99 $datumTmp["PARAMETERS"] = self::ConvertArrayCharset($datumTmp["PARAMETERS"], static::DIRECTION_IMPORT);
100 $datumTmp["VARIABLES"] = self::ConvertArrayCharset($datumTmp["VARIABLES"], static::DIRECTION_IMPORT);
101 $datumTmp["CONSTANTS"] = isset($datumTmp["CONSTANTS"])?
102 self::ConvertArrayCharset($datumTmp["CONSTANTS"], static::DIRECTION_IMPORT)
103 : array();
104 $datumTmp["DOCUMENT_FIELDS"] = self::ConvertArrayCharset($datumTmp["DOCUMENT_FIELDS"], static::DIRECTION_IMPORT);
105 }
106
108 $tpl = WorkflowTemplateTable::createObject();
109 $tpl->set('TEMPLATE', $datumTmp['TEMPLATE']);
110 $tpl->set('PARAMETERS', $datumTmp['PARAMETERS']);
111 $tpl->set('VARIABLES', $datumTmp['VARIABLES']);
112 $tpl->set('CONSTANTS', $datumTmp['CONSTANTS']);
113 $result->setTpl($tpl);
114
115 if (is_array($datumTmp['DOCUMENT_FIELDS'] ?? null))
116 {
117 $result->setDocumentFields($datumTmp['DOCUMENT_FIELDS']);
118 }
119
120 return $result;
121 }
122
123 private static function replaceTemplateDocumentFieldsAliases(&$template, $aliases)
124 {
125 foreach ($template as &$activity)
126 {
127 self::replaceActivityDocumentFieldsAliases($activity, $aliases);
128 if (is_array($activity["Children"]))
129 {
130 self::replaceTemplateDocumentFieldsAliases($activity['Children'], $aliases);
131 }
132 }
133 }
134
135 private static function replaceActivityDocumentFieldsAliases(&$activity, $aliases)
136 {
137 if (!is_array($activity['Properties']))
138 return;
139
140 foreach ($activity['Properties'] as $key => $value)
141 {
142 $activity['Properties'][$key] = self::replaceValueDocumentFieldsAliases($value, $aliases);
143 // Replace field conditions
144 if ($activity['Type'] === 'IfElseBranchActivity' && $key === 'fieldcondition')
145 {
146 $activity['Properties'][$key] = self::replaceFieldConditionsDocumentFieldsAliases(
147 $activity['Properties'][$key],
148 $aliases
149 );
150 }
151 }
152 }
153
154 private static function replaceVariablesDocumentFieldsAliases(&$variables, $aliases)
155 {
156 if (!is_array($variables))
157 return;
158
159 foreach ($variables as $key => &$variable)
160 {
161 $variable['Default'] = self::replaceValueDocumentFieldsAliases($variable['Default'], $aliases);
162 //Type Internalselect use options as link to document field.
163 if (
164 isset($variable['Options'])
165 && is_scalar($variable['Options'])
166 && array_key_exists($variable['Options'], $aliases)
167 )
168 {
169 $variable['Options'] = $aliases[$variable['Options']];
170 }
171 }
172 }
173
174 private static function replaceValueDocumentFieldsAliases($value, $aliases)
175 {
176 if (is_array($value))
177 {
178 $replacesValue = array();
179 foreach ($value as $key => $val)
180 {
181 if (array_key_exists($key, $aliases))
182 $key = $aliases[$key];
183
184 $replacesValue[$key] = self::replaceValueDocumentFieldsAliases($val, $aliases);
185 }
186
187 if (
188 sizeof($replacesValue) == 2
189 && isset($replacesValue[0])
190 && $replacesValue[0] == 'Document'
191 && isset($replacesValue[1])
192 && array_key_exists($replacesValue[1], $aliases)
193 )
194 {
195 $replacesValue[1] = $aliases[$replacesValue[1]];
196 }
197 $value = $replacesValue;
198 }
199 else
200 {
201 foreach ($aliases as $search => $replace)
202 {
203 $value = preg_replace('#(\{=\s*Document\s*\:\s*)'.$search.'#i', '\\1'.$replace, $value);
204 }
205 }
206
207 return $value;
208 }
209
210 private static function replaceFieldConditionsDocumentFieldsAliases($conditions, $aliases)
211 {
212 foreach ($conditions as $key => $condition)
213 {
214 if (array_key_exists($condition[0], $aliases))
215 $conditions[$key][0] = $aliases[$condition[0]];
216 }
217
218 return $conditions;
219 }
220
221 private static function convertValueCharset($s, $direction)
222 {
223 if ("utf-8" == mb_strtolower(LANG_CHARSET))
224 return $s;
225
226 if (is_numeric($s))
227 return $s;
228
229 if ($direction == static::DIRECTION_EXPORT)
230 {
231 $s = Main\Text\Encoding::convertEncoding($s, LANG_CHARSET, "UTF-8");
232 }
233 else
234 {
235 $s = Main\Text\Encoding::convertEncoding($s, "UTF-8", LANG_CHARSET);
236 }
237
238 return $s;
239 }
240
241 private static function convertArrayCharset($value, $direction = self::DIRECTION_EXPORT)
242 {
243 if (is_array($value))
244 {
245 $valueNew = array();
246 foreach ($value as $k => $v)
247 {
248 $k = self::ConvertValueCharset($k, $direction);
249 $v = self::ConvertArrayCharset($v, $direction);
250 $valueNew[$k] = $v;
251 }
252 $value = $valueNew;
253 }
254 else
255 {
256 $value = self::ConvertValueCharset($value, $direction);
257 }
258
259 return $value;
260 }
261}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29