23 'finishDebugSession' => [
31 public function fillAutomationViewAction(
string $sessionId): ?array
33 $session = $this->getSession($sessionId);
40 $isBeforeDebuggerStartState = $session->isBeforeDebuggerStartState();
42 if ($isBeforeDebuggerStartState)
45 $documentType = $session->getParameterDocumentType();
49 [$documentId, $documentType] = $this->getActiveDocument($session);
57 $documentService = \CBPRuntime::GetRuntime(
true)->getDocumentService();
59 $target = $documentService->createAutomationTarget($documentType);
61 $target->setDocumentId($documentId);
62 [$status, $statusList] = $this->getStatus($target, $session);
64 $template =
new Template($documentType, $status);
65 $documentFields = $isBeforeDebuggerStartState ? [] : $this->getDocumentFields($documentType);
67 $triggers = $target->getTriggers(array_keys($statusList));
68 $target->prepareTriggersToShow($triggers);
70 $workflowId = $target->getRuntime()->getCurrentWorkflowId();
71 $hasInstance = $workflowId && WorkflowInstanceTable::exists($workflowId);
73 $workflowStatus = $hasInstance ? \CBPWorkflowStatus::Suspended : \CBPWorkflowStatus::Completed;
75 $debuggerState = Manager::getDebuggerState()->getId();
80 $workflow = \CBPRuntime::GetRuntime(
true)->getWorkflow($workflowId,
true);
81 foreach ($workflow->getDebugEventIds() as $eventName)
83 foreach ($template->getRobots() as $robot)
85 if ($robot->getDelayName() === $eventName)
89 'sourceId' => $robot->getName(),
98 $isBeforeDebuggerStartState
99 ? $session->getDocumentCategoryId()
100 : $target->getDocumentCategory()
104 $isBeforeDebuggerStartState
106 : $this->getDocumentValues($documentType, $target->getComplexDocumentId(), $documentFields)
110 'triggers' => $triggers,
111 'template' => Automation\Component\Base::getTemplateViewData($template->toArray(), $documentType),
112 'documentId' => $documentId,
113 'documentStatus' => $status,
114 'statusList' => array_values($statusList),
115 'documentCategoryId' => $documentCategoryId,
116 'documentFields' => array_values($documentFields),
117 'documentValues' => $documentValues,
118 'workflowId' => $workflowId,
119 'workflowStatus' => $workflowStatus,
120 'workflowEvents' => $workflowEvents,
121 'debuggerState' => $debuggerState,
122 'track' => $this->getTrack($workflowId, $workflowStatus),
123 'globalVariables' => array_values(Automation\Helper::getGlobalVariables($documentType)),
124 'globalConstants' => array_values(Automation\Helper::getGlobalConstants($documentType)),
128 private function getDocumentFields(array $documentType): array
130 $fields = Automation\Helper::getDocumentFields($documentType);
132 foreach ($fields as $id => $field)
136 && strpos($field[
'Id'],
'.') ===
false
137 && strpos($field[
'Id'],
'_PRINTABLE') ===
false
138 && strpos($field[
'Id'],
'_IDS') ===
false
141 $fields[$id][
'Watchable'] =
true;
148 private function getDocumentValues(array $documentType, array $documentId, array $fields): array
150 $documentService = \CBPRuntime::GetRuntime(
true)->getDocumentService();
151 $lazyList = $documentService->GetDocument($documentId);
155 foreach ($fields as $fieldId => $property)
157 if (empty($property[
'Watchable']))
162 $fieldType = $documentService->getFieldTypeObject($documentType, $property);
163 $fieldType->setDocumentId($documentId);
165 $values[$fieldId] = $fieldType->formatValue($lazyList[$fieldId]);
171 public function setDocumentStatusAction($statusId): ?array
173 $session = $this->getSession();
180 [$documentId, $documentType] = $this->getActiveDocument($session);
187 $documentService = \CBPRuntime::GetRuntime(
true)->getDocumentService();
189 $target = $documentService->createAutomationTarget($documentType);
191 $target->setDocumentId($documentId);
193 if ($target->getDocumentStatus() === $statusId)
198 $target->setDocumentStatus($statusId);
199 $target->getRuntime()->onDocumentStatusChanged();
201 $template =
new Template($documentType, $statusId);
204 'newStatus' => $statusId,
205 'template' => Automation\Component\Base::getTemplateViewData($template->toArray(), $documentType),
209 public function resumeAutomationTemplateAction(
string $sessionId)
211 $session = $this->getSession($sessionId);
218 [$documentId, $documentType] = $this->getActiveDocument($session);
225 $currentState = Manager::getDebuggerState();
227 if ($currentState->is(DebuggerState::RUN))
229 $newState = DebuggerState::pause();
233 $newState = DebuggerState::run();
236 Manager::setDebuggerState($newState);
238 $documentService = \CBPRuntime::GetRuntime(
true)->getDocumentService();
239 $automationTarget = $documentService->createAutomationTarget($documentType);
240 $automationTarget->setDocumentId($documentId);
242 $workflowId = $automationTarget->getRuntime()->getCurrentWorkflowId();
243 $hasInstance = $workflowId && WorkflowInstanceTable::exists($workflowId);
245 if ($hasInstance && $newState->is(DebuggerState::RUN))
248 $workflow = \CBPRuntime::GetRuntime(
true)->getWorkflow($workflowId);
253 'workflowId' => $workflowId,
254 'debuggerState' => $newState->getId(),
260 $session = $this->getSession();
267 if (!$session->hasWorkflow($workflowId) || !WorkflowInstanceTable::exists($workflowId))
274 $runtime = \CBPRuntime::GetRuntime(
true);
275 $workflow = $runtime->getWorkflow($workflowId);
284 $workflow->sendDebugEvent($eventId);
292 $session = $this->getSession($sessionId);
299 if (!$session->canUserFinish($this->getCurrentUser()->getId()))
306 $toDeleteDocument = $session->isExperimentalMode() && $deleteDocument ? $session->getFixedDocument() :
null;
308 $result = Manager::finishSession($session);
309 if (!$result->isSuccess())
317 if ($toDeleteDocument)
319 $documentService = \CBPRuntime::GetRuntime(
true)->getDocumentService();
320 $documentService->deleteDocument($toDeleteDocument->getParameterDocumentId());
327 public function loadAllLogAction(
string $sessionId): ?array
329 $userId = (int)$this->getCurrentUser()->getId();
331 $session = \Bitrix\Bizproc\Debugger\Session\Manager::getSessionById($sessionId);
339 if (!$session->canUserDebug($userId))
348 $trackingResult = new \CBPTrackingServiceResult();
349 $trackingResult->InitFromArray($session->getLogs());
351 while ($log = $trackingResult->fetch())
354 $values = $log->collectValues();
355 $values[
'MODIFIED'] = (string)($values[
'MODIFIED']);
361 'workflowRobots' => $session->getRobots(),
365 private function getTrack(?
string $workflowId,
int $workflowStatus): array
371 $trackResult = \CBPTrackingService::GetList([
'ID' =>
'ASC'], [
'WORKFLOW_ID' => $workflowId]);
373 while ($row = $trackResult->fetch())
375 $row[
'WORKFLOW_STATUS'] = $workflowStatus;
383 private function getSession(
string $id =
null): ?Session
385 $userId = $this->getCurrentUser()->getId();
387 $session = $id ? Manager::getSessionById($id) : Manager::getActiveSession();
395 if (!$session->canUserDebug($userId))
405 private function getActiveDocument(Session $session): array
407 $document = $session->getFixedDocument();
409 if (!$document || !$this->isDocumentExists($document->getParameterDocumentId()))
416 return [$document->getDocumentId(), $session->getParameterDocumentType()];
419 private function getStatus(Automation\Target\BaseTarget $target, Session $session): array
421 $isBeforeDebuggerStartState = $session->isBeforeDebuggerStartState();
423 if ($isBeforeDebuggerStartState)
425 $statusList = $target->getDocumentStatusList($session->getDocumentCategoryId());
426 $status = array_key_first($statusList);
428 return [$status, $statusList];
431 return [$target->getDocumentStatus(), $target->getDocumentStatusList($target->getDocumentCategory())];
434 private function isDocumentExists(array $documentId): bool
436 $documentService = \CBPRuntime::GetRuntime(
true)->getDocumentService();
440 $documentService->getDocumentType($documentId);
442 catch (\Exception $e)
451 public function startSessionAction(
string $documentSigned,
int $mode): ?array
453 [$documentType, $documentCategoryId] = \CBPDocument::unSignParameters($documentSigned);
454 $userId = (int)$this->getCurrentUser()->getId();
456 if (!Manager::canUserDebugAutomation($userId, $documentType))
463 $result = Manager::startSession($documentType, $mode, $userId, $documentCategoryId);
464 if (!$result->isSuccess())
466 $this->
addErrors($result->getErrorCollection()->toArray());
472 $session = $result->getObject();
473 if ($session->getMode() === \
Bitrix\Bizproc\Debugger\Session\Mode::EXPERIMENTAL)
475 $documentId = $this->createExperimentalDocument($documentType, $documentCategoryId);
478 Manager::finishSession($session);
484 return $this->fixateSessionDocumentAction($documentId);
488 'documentSigned' => \CBPDocument::signParameters([$documentType]),
489 'session' => $session->toArray(),
493 private function createExperimentalDocument(array $documentType,
string $documentCategoryId): ?string
495 $documentService = \CBPRuntime::GetRuntime(
true)->getDocumentService();
498 'TITLE' => \Bitrix\Main\Localization\Loc::getMessage(
499 'BIZPROC_CONTROLLER_DEBUGGER_DOCUMENT_TITLE',
500 [
'#ENTITY#' => $documentService->getDocumentTypeName($documentType)]
502 'CATEGORY_ID' => (int)$documentCategoryId
505 return $documentService->createTestDocument(
508 (
int)(\Bitrix\Main\Engine\CurrentUser::get()->getId())
512 public function fixateSessionDocumentAction(
string $documentId): ?array
514 $session = $this->getSession();
520 $result = $session->fixateDocument($documentId);
521 if (!$result->isSuccess())
523 $this->
addErrors($result->getErrorCollection()->toArray());
528 $documentService = \CBPRuntime::GetRuntime(
true)->getDocumentService();
530 $target = $documentService->createAutomationTarget($session->getParameterDocumentType());
531 $target->setDocumentId($documentId);
532 $target->getRuntime()->onDocumentAdd();
535 $updatedSession = $result->getObject();
538 'documentSigned' => $updatedSession->getFixedDocument()->getSignedDocument(),
539 'session' => $updatedSession->toArray(),
545 $session = $this->getSession();
552 $documents = clone($session->getDocuments());
554 foreach ($documents as $document)
556 if (in_array($document->getDocumentId(), $documentIds,
true))
558 $session->removeFromDocuments($document);
563 'session' => $session,
569 $userId = (int)$this->getCurrentUser()->getId();
571 $session = \Bitrix\Bizproc\Debugger\Session\Manager::getSessionById($sessionId);
579 if (!$session->canUserDebug($userId))
586 if (!$session->hasWorkflow($workflowId))
595 foreach ($session->getWorkflowContexts() as $context)
597 if ($context->getWorkflowId() !== $workflowId)
602 $templateShards = $context->fillTemplateShards();
603 $robots = $templateShards ? $templateShards->getRobotData() : [];
608 'workflowRobots' => $robots