18 if (!$this->checkBizprocFeature())
23 $complexDocumentType = $this->getComplexDocumentType();
24 if (!$complexDocumentType)
29 $complexDocumentId =
null;
30 if ($this->getRequest()->
get(
'signedDocumentId'))
32 $complexDocumentId = $this->getComplexDocumentId();
33 if (!$complexDocumentId)
38 if (!$this->checkDocumentTypeMatchDocumentId($complexDocumentType, $complexDocumentId))
46 \CBPDocument::getTemplatesForStart($this->getCurrentUserId(), $complexDocumentType, $complexDocumentId)
53 if (!$this->checkBizprocFeature())
58 $complexDocumentType = $this->getComplexDocumentType();
59 if (!$complexDocumentType)
64 $complexDocumentId = $this->getComplexDocumentId();
65 if (!$complexDocumentId)
70 if (!$this->checkDocumentTypeMatchDocumentId($complexDocumentType, $complexDocumentId))
76 $workflowParameters = $templateService->prepareStartParameters(
79 complexDocumentType: $complexDocumentType,
80 requestParameters: array_merge(
81 $this->getRequest()->toArray(),
82 $this->getRequest()->getFileList()->toArray()
84 targetUserId: $this->getCurrentUserId(),
88 if (!$workflowParameters->isSuccess())
90 $this->
addErrors($workflowParameters->getErrors());
96 $startWorkflow = $workflowService->startWorkflow(
98 userId: $this->getCurrentUserId(),
99 targetUserId: $this->getCurrentUserId(),
101 complexDocumentId: $complexDocumentId,
102 parameters: $workflowParameters->getParameters(),
103 startDuration: $startDuration >= 0 ? $startDuration :
null,
107 if (!$startWorkflow->isSuccess())
109 $this->
addErrors($startWorkflow->getErrors());
114 return [
'workflowId' => $startWorkflow->getWorkflowId()];
119 if (!$this->checkBizprocFeature())
124 if ($autoExecuteType < 0)
127 Loc::getMessage(
'BIZPROC_LIB_API_CONTROLLER_WORKFLOW_STARTER_ERROR_INCORRECT_AUTO_EXECUTE_TYPE') ??
''
133 $parametersDocumentType = $this->getComplexDocumentType();
134 if (!$parametersDocumentType)
140 if ($this->getRequest()->
get(
'signedDocumentId'))
142 $canStart = \CBPDocument::canUserOperateDocument(
144 $this->getCurrentUserId(),
145 $this->getComplexDocumentId(),
150 && !\CBPDocument::canUserOperateDocumentType(
152 $this->getCurrentUserId(),
153 $parametersDocumentType,
158 Loc::getMessage(
'BIZPROC_LIB_API_CONTROLLER_WORKFLOW_STARTER_ERROR_ACCESS_DENIED') ??
''
166 foreach (\CBPWorkflowTemplateLoader::getDocumentTypeStates($parametersDocumentType, $autoExecuteType) as
$template)
171 $this->prepareWorkflowParameters(
173 $parametersDocumentType,
174 "bizproc{$template['TEMPLATE_ID']}_",
178 if ($parameters[
$template[
'TEMPLATE_ID']] ===
null)
190 return [
'parameters' => \CBPDocument::signParameters($parameters)];
195 if (!$this->checkBizprocFeature())
200 $parametersDocumentType = $this->getComplexDocumentType();
201 if (!$parametersDocumentType)
213 requestConstants: array_merge(
$request->toArray(),
$request->getFileList()->toArray()),
214 complexDocumentType: $parametersDocumentType,
215 userId: $this->getCurrentUserId(),
222 return [
'success' =>
true];
230 private function getComplexDocumentType(): ?
array
234 $signedDocumentType =
$request->get(
'signedDocumentType');
235 if (!is_string($signedDocumentType))
238 Loc::getMessage(
'BIZPROC_LIB_API_CONTROLLER_WORKFLOW_STARTER_ERROR_INCORRECT_DOCUMENT_TYPE') ??
''
244 $parametersDocumentType = \CBPDocument::unSignDocumentType($signedDocumentType);
248 \CBPHelper::parseDocumentId($parametersDocumentType);
257 return $parametersDocumentType;
260 private function getComplexDocumentId(): ?
array
264 $signedDocumentId =
$request->get(
'signedDocumentId');
265 if (!is_string($signedDocumentId))
268 Loc::getMessage(
'BIZPROC_LIB_API_CONTROLLER_WORKFLOW_STARTER_ERROR_INCORRECT_DOCUMENT_ID') ??
''
274 $parametersDocumentId = \CBPDocument::unSignDocumentType($signedDocumentId);
278 \CBPHelper::parseDocumentId($parametersDocumentId);
280 catch (\CBPArgumentNullException $e)
287 return $parametersDocumentId;
290 private function checkDocumentTypeMatchDocumentId(
array $parametersDocumentType,
array $parametersDocumentId): bool
293 $parametersDocumentType[0] === $parametersDocumentId[0]
294 && $parametersDocumentType[1] === $parametersDocumentId[1]
301 Loc::getMessage(
'BIZPROC_LIB_API_CONTROLLER_WORKFLOW_STARTER_ERROR_DOC_TYPE_DONT_MATCH_DOC_ID') ??
''
307 private function checkBizprocFeature(): bool
309 if (!\CBPRuntime::isFeatureEnabled())
312 Loc::getMessage(
'BIZPROC_LIB_API_CONTROLLER_WORKFLOW_STARTER_ERROR_BIZPROC_FEATURE_DISABLED') ??
'')
321 private function getCurrentUserId(): int
323 return (
int)($this->getCurrentUser()?->getId());
326 private function prepareWorkflowParameters(
327 array $templateParameters,
328 array $parametersDocumentType,
329 string $keyPrefix =
'',
333 $allRequestParameters = array_merge(
$request->toArray(),
$request->getFileList()->toArray());
335 $requestParameters = [];
336 foreach($templateParameters as
$key => $property)
338 $searchKey = $keyPrefix .
$key;
339 $requestParameters[
$key] = $allRequestParameters[$searchKey] ??
null;
342 $parameters = (
new WorkflowTemplateService())
344 new PrepareParametersRequest(
345 templateParameters: $templateParameters,
346 requestParameters: $requestParameters,
347 complexDocumentType: $parametersDocumentType,
352 if ($parameters->isSuccess())
354 return $parameters->getParameters();
357 $this->
addErrors($parameters->getErrors());
static createFromThrowable(Throwable $exception)