Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
baseactivity.php
1<?php
2
4
13
14abstract class BaseActivity extends \CBPActivity
15{
16 protected static $requiredModules = [];
17 protected $preparedProperties = [];
18
19 public function __get($name)
20 {
21 return $this->preparedProperties[$name] ?? parent::__get($name);
22 }
23
24 public function execute()
25 {
26 if (!static::checkModules())
27 {
28 return \CBPActivityExecutionStatus::Closed;
29 }
30 $this->prepareProperties();
31
32 $errorCollection = $this->checkProperties();
33 if ($errorCollection->isEmpty())
34 {
35 $errorCollection = $this->internalExecute();
36 }
37
38 foreach ($errorCollection as $error)
39 {
40 $this->logError($error->getMessage());
41 }
42
43 return \CBPActivityExecutionStatus::Closed;
44 }
45
46 protected function prepareProperties(): void
47 {
48 $fieldsMap = static::getPropertiesDialogMap();
49
50 foreach (array_keys($this->arProperties) as $propertyId)
51 {
52 $propertyValue = $this->getRawProperty($propertyId);
53
54 $type = '';
55 if (isset($this->arPropertiesTypes) && isset($this->arPropertiesTypes[$propertyId]))
56 {
57 $type = $this->arPropertiesTypes[$propertyId]['Type'] ?? '';
58 }
59 if (!$type && isset($fieldsMap[$propertyId]))
60 {
61 $type = $fieldsMap[$propertyId]['Type'] ?? '';
62 }
63
64 $parsedValue = $this->ParseValue($propertyValue);
65 $this->preparedProperties[$propertyId] = $this->convertPropertyValue($type, $parsedValue);
66 }
67 }
68
69 protected function convertPropertyValue(string $type, $value)
70 {
71 switch ($type)
72 {
73 case FieldType::INT:
74 return (int)$value;
75
76 case FieldType::BOOL:
77 return \CBPHelper::getBool($value);
78
80 return (double)$value;
81
82 default:
83 return $value;
84 }
85 }
86
87 protected function checkProperties(): ErrorCollection
88 {
89 return new ErrorCollection();
90 }
91
92 protected function internalExecute(): ErrorCollection
93 {
94 return new ErrorCollection();
95 }
96
97 protected function logError(string $message = '', int $userId = 0): void
98 {
99 $this->log($message, $userId, \CBPTrackingType::Error);
100 }
101
102 protected function log(string $message = '', int $userId = 0, int $type = -1): void
103 {
104 $this->WriteToTrackingService($message, $userId, $type);
105 }
106
107 public static function getPropertiesDialog(
108 $documentType,
109 $activityName,
110 $workflowTemplate,
111 $workflowParameters,
112 $workflowVariables,
113 $currentValues = null,
114 $formName = '',
115 $popupWindow = null,
116 $siteId = ''
117 )
118 {
119 if (!static::checkModules())
120 {
121 return false;
122 }
123
124 $dialog = new \Bitrix\Bizproc\Activity\PropertiesDialog(static::getFileName(), [
125 'documentType' => $documentType,
126 'activityName' => $activityName,
127 'workflowTemplate' => $workflowTemplate,
128 'workflowParameters' => $workflowParameters,
129 'workflowVariables' => $workflowVariables,
130 'currentValues' => $currentValues,
131 'formName' => $formName,
132 'siteId' => $siteId
133 ]);
134
135 $dialog
136 ->setMapCallback([static::class, 'getPropertiesDialogMap'])
137 ->setRuntimeData(static::getRuntimeData())
138 ;
139
140 if (!static::hasRenderer())
141 {
142 $dialog->setRenderer([static::class, 'renderPropertiesDialog']);
143 }
144
145 return $dialog;
146 }
147
148 private static function hasRenderer(): bool
149 {
150 $dir = Path::getDirectory(Path::normalize(static::getFileName()));
151
152 return
153 File::isFileExists(Path::combine($dir, 'properties_dialog.php'))
154 || File::isFileExists(Path::combine($dir, 'robot_properties_dialog.php'))
155 ;
156 }
157
158 public static function renderPropertiesDialog(PropertiesDialog $dialog)
159 {
160 $propertiesDialogHtml = '';
161 $isRobot = $dialog->getDialogFileName() === 'robot_properties_dialog.php';
162 foreach ($dialog->getMap() as $field)
163 {
164 $propertiesDialogHtml .=
165 $isRobot
166 ? static::renderRobotProperty($dialog, $field)
167 : static::renderBizprocProperty($dialog, $field)
168 ;
169 }
170
171 return $propertiesDialogHtml;
172 }
173
174 protected static function renderBizprocProperty(PropertiesDialog $dialog, array $field): string
175 {
176 $controlHtml = $dialog->renderFieldControl(
177 $field,
178 $dialog->getCurrentValue($field),
179 true,
181 );
182
183 return sprintf(
184 '<tr><td align="right" width="40%%">%s:</td><td width="60%%">%s</td></tr>',
185 htmlspecialcharsbx($field['Name']),
186 $controlHtml
187 );
188 }
189
190 protected static function renderRobotProperty(PropertiesDialog $dialog, array $field): string
191 {
192 $propertyHtml = '
193 <div class="bizproc-automation-popup-settings">
194 <span class="bizproc-automation-popup-settings-title bizproc-automation-popup-settings-title-autocomplete">
195 %s:
196 </span>
197 %s
198 </div>
199 ';
200
201 return sprintf(
202 $propertyHtml,
203 htmlspecialcharsbx($field['Name']),
204 $dialog->renderFieldControl($field, $dialog->getCurrentValue($field))
205 );
206 }
207
208 public static function getPropertiesDialogValues(
209 $documentType,
210 $activityName,
211 &$workflowTemplate,
212 &$workflowParameters,
213 &$workflowVariables,
214 $currentValues,
215 &$errors
216 ): bool
217 {
218 if (!static::checkModules())
219 {
220 return false;
221 }
222
223 $dialog = new PropertiesDialog(static::getFileName(), [
224 'documentType' => $documentType,
225 'activityName' => $activityName,
226 'workflowTemplate' => $workflowTemplate,
227 'workflowParameters' => $workflowParameters,
228 'workflowVariables' => $workflowVariables,
229 'currentValues' => $currentValues,
230 ]);
231
232 $extractingResult = static::extractPropertiesValues($dialog, static::getPropertiesDialogMap($dialog));
233 if (!$extractingResult->isSuccess())
234 {
235 foreach ($extractingResult->getErrors() as $error)
236 {
237 $errors[] = [
238 'code' => $error->getCode(),
239 'message' => $error->getMessage(),
240 'parameter' => $error->getCustomData(),
241 ];
242 }
243 }
244 else
245 {
246 $errors = static::ValidateProperties(
247 $extractingResult->getData(),
248 new \CBPWorkflowTemplateUser(\CBPWorkflowTemplateUser::CurrentUser)
249 );
250 }
251
252 if ($errors)
253 {
254 return false;
255 }
256
257 $currentActivity = &\CBPWorkflowTemplateLoader::FindActivityByName(
258 $workflowTemplate,
259 $activityName
260 );
261 $currentActivity['Properties'] = $extractingResult->getData();
262
263 return true;
264 }
265
266 protected static function extractPropertiesValues(PropertiesDialog $dialog, array $fieldsMap): Result
267 {
268 $result = new Result();
269
270 $properties = [];
271 $errors = [];
272 $currentValues = $dialog->getCurrentValues();
273 $documentService = static::getDocumentService();
274
275 foreach ($fieldsMap as $propertyKey => $fieldProperties)
276 {
277 $field = $documentService->getFieldTypeObject($dialog->getDocumentType(), $fieldProperties);
278 if(!$field)
279 {
280 continue;
281 }
282
283 $properties[$propertyKey] = $field->extractValue(
284 ['Field' => $fieldProperties['FieldName']],
285 $currentValues,
286 $errors
287 );
288 }
289
290 if ($errors)
291 {
292 foreach ($errors as $error)
293 {
294 $result->addError(
295 new Error(
296 $error['message'] ?? '',
297 $error['code'] ?? '',
298 $error['parameter'] ?? ''
299 )
300 );
301 }
302 }
303 else
304 {
305 $result->setData($properties);
306 }
307
308 return $result;
309 }
310
311 abstract protected static function getFileName(): string;
312
313 public static function validateProperties($testProperties = [], \CBPWorkflowTemplateUser $user = null)
314 {
315 $errors = [];
316
317 if (!static::checkModules())
318 {
319 return $errors;
320 }
321
322 foreach (static::getPropertiesDialogMap() as $propertyKey => $fieldProperties)
323 {
324 if(
325 \CBPHelper::getBool($fieldProperties['Required'] ?? null)
326 && \CBPHelper::isEmptyValue($testProperties[$propertyKey] ?? null)
327 )
328 {
329 $errors[] = [
330 'code' => 'NotExist',
331 'parameter' => 'FieldValue',
332 'message' => Loc::getMessage('BIZPROC_BA_EMPTY_PROP', ['#PROPERTY#' => $fieldProperties['Name']]),
333 ];
334 }
335 }
336
337 return array_merge($errors, parent::ValidateProperties($testProperties, $user));
338 }
339
340 protected static function checkModules(): bool
341 {
342 foreach (static::$requiredModules as $module)
343 {
344 if (!Loader::includeModule($module))
345 {
346 return false;
347 }
348 }
349
350 return true;
351 }
352
353 protected static function getDocumentService(): \CBPDocumentService
354 {
355 return \CBPRuntime::GetRuntime(true)->getDocumentService();
356 }
357
358 public static function getPropertiesDialogMap(?PropertiesDialog $dialog = null): array
359 {
360 return [];
361 }
362
363 protected static function getRuntimeData(): array
364 {
365 return [];
366 }
367}
static getPropertiesDialogValues( $documentType, $activityName, &$workflowTemplate, &$workflowParameters, &$workflowVariables, $currentValues, &$errors)
convertPropertyValue(string $type, $value)
static getPropertiesDialog( $documentType, $activityName, $workflowTemplate, $workflowParameters, $workflowVariables, $currentValues=null, $formName='', $popupWindow=null, $siteId='')
static validateProperties($testProperties=[], \CBPWorkflowTemplateUser $user=null)
logError(string $message='', int $userId=0)
static getPropertiesDialogMap(?PropertiesDialog $dialog=null)
log(string $message='', int $userId=0, int $type=-1)
static extractPropertiesValues(PropertiesDialog $dialog, array $fieldsMap)
static renderPropertiesDialog(PropertiesDialog $dialog)
static renderBizprocProperty(PropertiesDialog $dialog, array $field)
static renderRobotProperty(PropertiesDialog $dialog, array $field)
renderFieldControl($field, $value=null, $allowSelection=true, $renderMode=FieldType::RENDER_MODE_PUBLIC)
static isFileExists($path)
Definition file.php:256
static normalize($path)
Definition path.php:26
static getDirectory($path)
Definition path.php:109
static combine()
Definition path.php:221
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29