• Модуль: bizproc
  • Путь к файлу: ~/bitrix/modules/bizproc/classes/general/runtime.php
  • Класс: CBP
  • Вызов: CBP::createWorkflow
public function createWorkflow($workflowTemplateId, $documentId, $workflowParameters = array(), $parentWorkflow = null)
{
	$workflowTemplateId = intval($workflowTemplateId);
	if ($workflowTemplateId <= 0)
	{
		throw new Exception("workflowTemplateId");
	}

	$arDocumentId = CBPHelper::ParseDocumentId($documentId);

	$limit = BitrixMainConfigOption::get("bizproc", "limit_simultaneous_processes", "0");
	$ignoreLimits = !empty($workflowParameters[CBPDocument::PARAM_IGNORE_SIMULTANEOUS_PROCESSES_LIMIT]);
	if (!$ignoreLimits && intval($limit) > 0)
	{
		if (CBPStateService::CountDocumentWorkflows($documentId) >= $limit)
		{
			throw new Exception(GetMessage("BPCGDOC_LIMIT_SIMULTANEOUS_PROCESSES", ["#NUM#" => $limit]));
		}
	}
	unset($workflowParameters[CBPDocument::PARAM_IGNORE_SIMULTANEOUS_PROCESSES_LIMIT]);

	if (!$this->isStarted)
	{
		$this->StartRuntime();
	}

	$workflowId = $workflowParameters[CBPDocument::PARAM_PRE_GENERATED_WORKFLOW_ID] ?? static::generateWorkflowId();

	if ($parentWorkflow)
	{
		$this->addWorkflowToChain($workflowId, $parentWorkflow);
		if ($this->checkWorkflowRecursion($workflowId, $workflowTemplateId))
		{
			throw new Exception(GetMessage("BPCGDOC_WORKFLOW_RECURSION_LOCK"));
		}
	}

	$workflow = new CBPWorkflow($workflowId, $this);

	$loader = CBPWorkflowTemplateLoader::GetLoader();

	/** @var CBPCompositeActivity $rootActivity */
	[$rootActivity, $workflowVariablesTypes, $workflowParametersTypes] = $loader->LoadWorkflow($workflowTemplateId);
	foreach ($workflowParametersTypes as $parameterName => $parametersProperty)
	{
		if (!array_key_exists($parameterName, $workflowParameters))
		{
			$workflowParameters[$parameterName] = $parametersProperty['Default'];
		}
	}

	if ($rootActivity == null)
	{
		throw new Exception("EmptyRootActivity");
	}

	foreach(GetModuleEvents("bizproc", "OnCreateWorkflow", true)  as $arEvent)
	{
		ExecuteModuleEventEx($arEvent, [$workflowTemplateId, $documentId, &$workflowParameters, $workflowId]);
	}

	$workflow->Initialize($rootActivity, $arDocumentId, $workflowParameters, $workflowVariablesTypes, $workflowParametersTypes, $workflowTemplateId);

	$starterUserId = 0;
	if (isset($workflowParameters[CBPDocument::PARAM_TAGRET_USER]))
	{
		$starterUserId = intval(mb_substr($workflowParameters[CBPDocument::PARAM_TAGRET_USER], mb_strlen("user_")));
	}

	$this->GetService("StateService")->AddWorkflow($workflowId, $workflowTemplateId, $arDocumentId, $starterUserId);

	$this->workflows[$workflowId] = $workflow;

	return $workflow;
}