Example #1
0
 /**
  * Creates new workflow instance from the specified template.
  *
  * @param int $workflowTemplateId - ID of the workflow template
  * @param string $documentId - ID of the document
  * @param mixed $workflowParameters - Optional parameters of the created workflow instance
  * @param array|null $parentWorkflow - Parent Workflow information.
  * @return CBPWorkflow
  * @throws CBPArgumentNullException
  * @throws CBPArgumentOutOfRangeException
  * @throws Exception
  * @throws \Bitrix\Main\ArgumentNullException
  */
 public function CreateWorkflow($workflowTemplateId, $documentId, $workflowParameters = array(), $parentWorkflow = null)
 {
     $workflowTemplateId = intval($workflowTemplateId);
     if ($workflowTemplateId <= 0) {
         throw new Exception("workflowTemplateId");
     }
     $arDocumentId = CBPHelper::ParseDocumentId($documentId);
     $limit = \Bitrix\Main\Config\Option::get("bizproc", "limit_simultaneous_processes", "0");
     if (intval($limit) > 0) {
         if (CBPStateService::CountDocumentWorkflows($documentId) >= $limit) {
             throw new Exception(GetMessage("BPCGDOC_LIMIT_SIMULTANEOUS_PROCESSES", array("#NUM#" => $limit)));
         }
     }
     if (!$this->isStarted) {
         $this->StartRuntime();
     }
     $workflowId = uniqid("", true);
     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();
     list($rootActivity, $workflowVariablesTypes, $workflowParametersTypes) = $loader->LoadWorkflow($workflowTemplateId);
     if ($rootActivity == null) {
         throw new Exception("EmptyRootActivity");
     }
     //if (!is_a($rootActivity, "IBPRootActivity"))
     //	throw new Exception("RootActivityIsNotAIBPRootActivity");
     foreach (GetModuleEvents("bizproc", "OnCreateWorkflow", true) as $arEvent) {
         ExecuteModuleEventEx($arEvent, array($workflowTemplateId, $documentId, &$workflowParameters));
     }
     $workflow->Initialize($rootActivity, $arDocumentId, $workflowParameters, $workflowVariablesTypes, $workflowParametersTypes, $workflowTemplateId);
     $starterUserId = 0;
     if (isset($workflowParameters[CBPDocument::PARAM_TAGRET_USER])) {
         $starterUserId = intval(substr($workflowParameters[CBPDocument::PARAM_TAGRET_USER], strlen("user_")));
     }
     $this->arServices["StateService"]->AddWorkflow($workflowId, $workflowTemplateId, $arDocumentId, $starterUserId);
     $this->arWorkflows[$workflowId] = $workflow;
     return $workflow;
 }