16 private static ?
Session $activeSession =
null;
17 private static bool $isActiveSessionChecked =
false;
19 private const CACHE_TTL = 3600;
21 private static function getList(array $filter = []): ?
Session
24 'select' => [
'*',
'DOCUMENTS',
'WORKFLOW_CONTEXTS'],
33 'filter' => [
'=ACTIVE' =>
'Y'],
34 'cache' => [
'ttl' => self::CACHE_TTL],
40 if (!self::$isActiveSessionChecked)
42 self::setActiveSession(static::getList([
'=ACTIVE' =>
'Y']));
45 return self::$activeSession;
50 if (self::$activeSession && self::$activeSession->getId() === $sessionId)
52 return self::$activeSession;
55 $session = static::getList([
'=ID' => $sessionId]);
56 if ($session && $session->isActive())
58 self::setActiveSession($session);
64 public static function startSession(array $parameterDocumentType,
int $mode,
int $userId,
int $categoryId = 0)
68 return static::getResult(self::ERROR_INCORRECT_MODE);
71 [$module, $entity, $documentType] = \CBPHelper::ParseDocumentId($parameterDocumentType);
73 $session =
new Session();
75 ->setId(uniqid(
'',
true))
76 ->setModuleId($module)
78 ->setDocumentType($documentType)
80 ->setStartedBy($userId)
83 ->setDocumentCategoryId($categoryId)
89 return self::getResult(
90 self::ERROR_DEBUGGER_ALREADY_STARTED,
91 [
'session' => $activeSession]
94 self::clearStaticCache();
96 $result = $session->save();
97 if ($result->isSuccess())
100 $sessionFromResult = $result->getObject();
101 $sessionFromResult->fillWorkflowContexts();
102 $sessionFromResult->fillDocuments();
103 self::setActiveSession($sessionFromResult);
111 $result = $session->
finish();
112 if ($result->isSuccess())
114 self::clearStaticCache();
133 return static::isAvailable($parameterDocumentType) && \CBPDocument::canUserOperateDocumentType(
134 \CBPCanUserOperateOperation::DebugAutomation,
136 $parameterDocumentType
142 if (self::getActiveSession())
150 private static function getResult(
string $errorCode, array $data = []): \
Bitrix\Main\
Result
152 $result = new \Bitrix\Main\Result();
153 $result->setData($data);
155 $error = static::getCustomError($errorCode, $data);
158 $result->addError($error);
164 private static function getCustomError(
string $errorCode, array $data = []): ?\
Bitrix\Main\
Error
167 $session = $data[
'session'];
169 if ($errorCode === self::ERROR_INCORRECT_MODE)
171 return new \Bitrix\Main\Error(
172 Loc::getMessage(
'BIZPROC_DEBUGGER_SESSION_MANAGER_ERROR_INCORRECT_MODE'),
176 elseif ($errorCode === self::ERROR_DEBUGGER_ALREADY_STARTED)
178 return new \Bitrix\Main\Error($session->getShortDescription(), $errorCode);
186 $session = static::getList([
'=ID' => $sessionId]);
191 $errorMessage =
Loc::getMessage(
'BIZPROC_DEBUGGER_SESSION_MANAGER_ERROR_SESSION_NOT_FOUND');
192 $result->addError(
new Error($errorMessage));
194 else if ($session->isActive())
196 $errorMessage =
Loc::getMessage(
'BIZPROC_DEBUGGER_SESSION_MANAGER_ERROR_SESSION_STILL_ACTIVE');
197 $result->addError(
new Error($errorMessage));
201 $deletionResult = $session->deleteAll();
203 $result->setData($deletionResult->getData());
204 $result->addErrors($deletionResult->getErrors());
212 return (
bool)DebuggerSessionWorkflowContextTable::getRow([
213 'filter' => [
'=WORKFLOW_ID' => $workflowId],
219 $session = static::getActiveSession();
231 $session = static::getActiveSession();
235 $session->setDebuggerState($state->
getId());
240 private static function isAvailable(array $documentType): bool
245 $documentType[0] ===
'crm'
246 && $documentType[2] ===
'DEAL'
250 private static function setActiveSession(?Session $activeSession)
252 self::$activeSession = $activeSession;
253 self::$isActiveSessionChecked =
true;
256 private static function clearStaticCache()
258 self::$activeSession =
null;
259 self::$isActiveSessionChecked =
false;
static getMessage($code, $replace=null, $language=null)
static getList(array $parameters=array())