16 private const CACHE_TTL = 3600;
17 private const LIMIT_DOCUMENT_ID = 1000;
18 private const LIMIT_QUEUES = 1;
24 '=MODULE_ID' => $documentType[0],
25 '=ENTITY' => $documentType[1],
26 '=DOCUMENT_TYPE' => $documentType[2],
31 $filter[
'=ACTIVE'] =
'Y';
34 $list = Entity\ScriptTable::getList(
37 'ID',
'NAME',
'ORIGINATOR_ID',
'ORIGIN_ID',
38 'MODULE_ID',
'ENTITY',
'DOCUMENT_TYPE',
'NAME',
'DESCRIPTION',
45 'cache' => [
'ttl' => self::CACHE_TTL],
54 $script = Entity\ScriptTable::getList(
68 return Entity\ScriptQueueTable::getById($queueId)->fetchObject();
71 public static function saveScript(
int $id, array $documentType, array $fields, $userId =
null)
75 $result = self::updateScript($id, $fields[
'script'], $fields[
'robotsTemplate'], $userId);
79 $result = self::addScript($documentType, $fields[
'script'], $fields[
'robotsTemplate'], $userId);
82 if ($result->isSuccess())
84 self::clearMenuCache();
94 'MODULE_ID' => $documentType[0],
95 'ENTITY' => $documentType[1],
96 'DOCUMENT_TYPE' => $documentType[2],
98 'DESCRIPTION' =>
Loc::getMessage(
"BIZPROC_SCRIPT_MANAGER_NEW_DESCRIPTION"),
104 private static function updateScript($id, array $scriptFields, array $templateFields, $userId): Main\
Result
106 $script = static::getById($id);
111 $result->addError(
new Main\
Error(
'Script not found'));
116 $wl = array_fill_keys([
'NAME',
'DESCRIPTION'],
true);
118 $scriptFields = array_intersect_key($scriptFields, $wl);
119 $scriptFields[
'MODIFIED_BY'] = $userId;
122 $scriptUpdateResult = Entity\ScriptTable::update($script[
'ID'], $scriptFields);
124 if (!$scriptUpdateResult->isSuccess())
126 return $scriptUpdateResult;
129 $documentType = [$script[
'MODULE_ID'], $script[
'ENTITY'], $script[
'DOCUMENT_TYPE']];
132 $tpl = WorkflowTemplateTable::getById($script[
'WORKFLOW_TEMPLATE_ID'])->fetchObject();
135 $template = \Bitrix\Bizproc\Automation\Engine\Template::createByTpl($tpl);
140 $template = new \Bitrix\Bizproc\Automation\Engine\Template($documentType);
143 $template->setName($scriptFields[
'NAME'] ?? $script->getName());
144 $robots = isset($templateFields[
'ROBOTS']) && is_array($templateFields[
'ROBOTS']) ? $templateFields[
'ROBOTS'] : [];
146 $result = $template->save($robots, $userId);
148 if ($result->isSuccess())
150 self::saveTemplateConfigs($template->getId(), $templateFields);
156 private static function addScript(array $documentType, array $scriptFields, array $templateFields, $userId): Main\Result
158 $templateFields[
'NAME'] = $scriptFields[
'NAME'];
159 $result = self::addWorkflowTemplate($documentType, $templateFields, $userId);
161 if (!$result->isSuccess())
166 return self::addScriptRecord(
169 'WORKFLOW_TEMPLATE_ID' => $result->getData()[
'ID'],
170 'NAME' => $scriptFields[
'NAME'],
171 'DESCRIPTION' => $scriptFields[
'DESCRIPTION'],
177 private static function addWorkflowTemplate(array $documentType, array $templateFields,
int $userId, $extractParameters =
true)
179 $template = new \Bitrix\Bizproc\Automation\Engine\Template($documentType);
180 $robots = isset($templateFields[
'ROBOTS']) && is_array($templateFields[
'ROBOTS']) ? $templateFields[
'ROBOTS'] : [];
181 $template->setDocumentStatus(
'SCRIPT');
182 $template->setName($templateFields[
'NAME']);
183 $template->setExecuteType(\CBPDocumentEventType::Script);
185 $result = $template->save($robots, $userId);
187 if (!$result->isSuccess())
192 self::saveTemplateConfigs($template->getId(), $templateFields, $extractParameters);
194 $result->setData([
'ID' => $template->getId()]);
199 private static function addScriptRecord(array $documentType, array $scriptFields,
int $userId)
202 'MODULE_ID' => $documentType[0],
203 'ENTITY' => $documentType[1],
204 'DOCUMENT_TYPE' => $documentType[2],
205 'WORKFLOW_TEMPLATE_ID' => $scriptFields[
'WORKFLOW_TEMPLATE_ID'],
206 'NAME' => $scriptFields[
'NAME'],
207 'DESCRIPTION' => $scriptFields[
'DESCRIPTION'],
209 'CREATED_DATE' =>
new Main\Type\DateTime(),
211 'MODIFIED_DATE' =>
new Main\Type\DateTime(),
214 if (isset($scriptFields[
'ORIGINATOR_ID']) && isset($scriptFields[
'ORIGIN_ID']))
216 $addFields[
'ORIGINATOR_ID'] = $scriptFields[
'ORIGINATOR_ID'];
217 $addFields[
'ORIGIN_ID'] = $scriptFields[
'ORIGIN_ID'];
220 return Entity\ScriptTable::add($addFields);
223 private static function saveTemplateConfigs(
int $tplId, array $templateFields, $extractParameters =
true)
225 $tpl = WorkflowTemplateTable::getById($tplId)->fetchObject();
231 $constants = $templateFields[
'CONSTANTS'] ?? [];
232 $parameters = $templateFields[
'PARAMETERS'] ?? [];
234 $usages = $tpl->collectUsages();
236 $usedConstants = $usages->getValuesBySourceType(SourceType::Constant);
237 $usedParameters = $usages->getValuesBySourceType(SourceType::Parameter);
239 $constants = array_intersect_key($constants, array_fill_keys($usedConstants,
true));
240 $parameters = array_intersect_key($parameters, array_fill_keys($usedParameters,
true));
242 if ($extractParameters)
244 $constants = self::extractProperties($constants, $tpl->getDocumentComplexType());
245 $parameters = self::extractProperties($parameters, $tpl->getDocumentComplexType());
248 \CBPWorkflowTemplateLoader::update(
250 [
'CONSTANTS' => $constants,
'PARAMETERS' => $parameters],
255 private static function extractProperties(array $properties, array $documentType): array
257 $documentService = \CBPRuntime::GetRuntime(
true)->getDocumentService();
259 foreach ($properties as $i => $property)
261 $properties[$i][
'Default'] = $documentService->GetFieldInputValue(
265 [
'field' => $property[
'Default']],
275 $script = static::getById($id);
280 $result->addError(
new Main\
Error(
'Script not found'));
287 \CBPWorkflowTemplateLoader::getLoader()->deleteTemplate($script[
'WORKFLOW_TEMPLATE_ID']);
288 Entity\ScriptTable::delete($id);
289 Entity\ScriptQueueTable::deleteByScript($id);
291 self::clearMenuCache();
293 catch (\Exception $e)
295 $result->addError(
new Main\
Error($e->getMessage()));
303 $script = static::getById($id);
308 $result->addError(
new Main\
Error(
'Script not found'));
313 $script->setActive(
'Y');
316 \CBPWorkflowTemplateLoader::update($script->getWorkflowTemplateId(), [
'ACTIVE' =>
'Y'],
true);
318 self::clearMenuCache();
325 $script = static::getById($id);
330 $result->addError(
new Main\
Error(
'Script not found'));
335 \CBPWorkflowTemplateLoader::update($script->getWorkflowTemplateId(), [
'ACTIVE' =>
'N'],
true);
337 $script->setActive(
'N');
339 self::clearMenuCache();
348 array $parameters = []
356 return $result->addScriptNotExistError();
358 if (!$script->getActive())
360 return $result->addInactiveScriptError();
363 $script->fill(
'WORKFLOW_TEMPLATE');
364 $template = $script->getWorkflowTemplate();
367 return $result->addTemplateNotExistError();
370 $templateParameters = $template->getParameters();
371 if ($templateParameters)
373 if (empty($parameters))
375 return $result->addEmptyTemplateParameterError();
379 $parameters = \CBPWorkflowTemplateLoader::checkWorkflowParameters(
382 $template->getDocumentComplexType(),
387 return $result->addInvalidParameterErrors($errors);
391 $documentIds = array_unique($documentIds);
393 $addResult = ScriptQueueTable::add([
394 'SCRIPT_ID' => $scriptId,
396 'STARTED_BY' => $userId,
397 'STATUS' => Queue\Status::QUEUED,
399 'WORKFLOW_PARAMETERS' => $parameters,
403 if ($addResult->isSuccess())
405 $queueId = $addResult->getId();
406 $documentRows = array_map(
407 function ($id) use ($queueId)
410 'QUEUE_ID' => $queueId,
411 'DOCUMENT_ID' => $id,
412 'STATUS' => Queue\Status::QUEUED,
417 ScriptQueueDocumentTable::addMulti($documentRows,
true);
420 Queue\Stepper::bind(1, [$queueId, $scriptId]);
423 'queueId' => $queueId,
433 return \Bitrix\Bizproc\Script\Entity\ScriptTable::getActiveQueueCount($scriptId);
446 ScriptQueueTable::delete($queueId);
452 $user = new \CBPWorkflowTemplateUser($userId);
454 if ($user->isAdmin())
459 $script = static::getById($scriptId);
465 $documentType = [$script[
'MODULE_ID'], $script[
'ENTITY'], $script[
'DOCUMENT_TYPE']];
467 return \CBPDocument::canUserOperateDocumentType(
468 \CBPCanUserOperateOperation::ViewWorkflow,
476 $script = static::getById($scriptId);
482 $documentType = [$script[
'MODULE_ID'], $script[
'ENTITY'], $script[
'DOCUMENT_TYPE']];
484 return static::canUserCreateScript($documentType, $userId);
489 $user = new \CBPWorkflowTemplateUser($userId);
491 if ($user->isAdmin())
496 return \CBPDocument::canUserOperateDocumentType(
497 \CBPCanUserOperateOperation::CreateWorkflow,
505 $script = static::getById($scriptId);
512 'MODULE_ID' => $script->getModuleId(),
513 'ENTITY' => $script->getEntity(),
514 'DOCUMENT_TYPE' => $script->getDocumentType(),
515 'NAME' => $script->getName(),
516 'DESCRIPTION' => $script->getDescription(),
517 'ORIGINATOR_ID' => $script->getOriginatorId(),
518 'ORIGIN_ID' => $script->getOriginId(),
521 $script->fillWorkflowTemplate();
523 $packageData = $roboPackage->makePackageData($script->getWorkflowTemplate());
524 $exportData[
'WORKFLOW_TEMPLATE'] = [
525 'PARAMETERS' => $packageData[
'PARAMETERS'],
526 'CONSTANTS' => $packageData[
'CONSTANTS'],
527 'ROBOTS' => $packageData[
'ROBOTS'],
528 'DOCUMENT_FIELDS' => $packageData[
'DOCUMENT_FIELDS'],
529 'REQUIRED_APPLICATIONS' => $packageData[
'REQUIRED_APPLICATIONS'],
537 $documentType = [$data[
'MODULE_ID'], $data[
'ENTITY'], $data[
'DOCUMENT_TYPE']];
538 $templateFields = $data[
'WORKFLOW_TEMPLATE'];
539 $templateFields[
'DOCUMENT_TYPE'] = $documentType;
540 $templateFields[
'NAME'] = $data[
'NAME'];
541 $templateFields[
'DESCRIPTION'] = $data[
'DESCRIPTION'];
542 $templateFields[
'ORIGINATOR_ID'] = $data[
'ORIGINATOR_ID'];
543 $templateFields[
'ORIGIN_ID'] = $data[
'ORIGIN_ID'];
545 $result = self::importWorkflowTemplate($templateFields, $userId);
547 if (!$result->isSuccess())
552 $result = self::addScriptRecord(
555 'WORKFLOW_TEMPLATE_ID' => $result->getData()[
'ID'],
556 'NAME' => $data[
'NAME'],
557 'DESCRIPTION' => $data[
'DESCRIPTION'],
558 'ORIGINATOR_ID' => $data[
'ORIGINATOR_ID'],
559 'ORIGIN_ID' => $data[
'ORIGIN_ID'],
564 if ($result->isSuccess())
566 self::clearMenuCache();
572 private static function importWorkflowTemplate(array $data,
int $userId)
575 $result = $roboPackage->unpack($data);
577 if ($result->isSuccess())
579 $tpl = $result->getTpl();
580 $tpl->setUserId($userId);
581 $tpl->setDocumentStatus(
'SCRIPT');
582 $tpl->setAutoExecute(\CBPDocumentEventType::Script);
584 $saveResult = $tpl->save();
586 if ($saveResult->isSuccess())
588 $result->setData([
'ID' => $saveResult->getId()]);
590 \CBPWorkflowTemplateLoader::importDocumentFields(
591 $tpl->getDocumentComplexType(),
592 $result->getDocumentFields()
597 $result->addErrors($saveResult->getErrors());
604 private static function clearMenuCache(): void
606 if (defined(
'BX_COMP_MANAGED_CACHE'))
608 $cache = Main\Application::getInstance()->getTaggedCache();
609 $cache->clearByTag(
'intranet_menu_binding');
620 return self::LIMIT_DOCUMENT_ID;
632 return self::LIMIT_QUEUES;
static getMessage($code, $replace=null, $language=null)