29 private const BUTTON_NAME =
'execute';
30 private const FIELD_NAME =
'command';
42 return 'HandleExternalEventActivity';
47 $runtime = \CBPRuntime::getRuntime();
48 if (!$runtime->hasWorkflow($command->workflowId))
56 $externalTask =
new self(TaskData::createFromArray($currentTask), 0);
58 $users = $externalTask->getTaskUsersByStatus();
61 $intersect = array_intersect($users->completed, $command->userIds);
64 $externalTask->markTaskUnCompleted($intersect);
65 $externalTask->addUsersToCompletedUsersParameter($intersect);
68 $allUsers = array_merge($users->completed, $users->waiting);
69 $diff = array_diff($command->userIds, $allUsers);
72 $newUsers = array_merge($allUsers, $command->userIds);
80 $workflow = $runtime->getWorkflow($command->workflowId);
83 workflowId: $command->workflowId,
84 complexDocumentId: $workflow->getDocumentId(),
85 userIds: $command->userIds,
86 activityName: $command->id,
93 workflowId:
$task->workflowId,
94 complexDocumentId:
$task->complexDocumentId,
95 userIds:
$task->userIds,
96 activityName:
$task->activityName,
98 name: Loc::getMessage(
'BIZPROC_LIB_TASK_EXTERNAL_EVENT_TASK_NAME') ??
'',
99 description: Loc::getMessage(
'BIZPROC_LIB_TASK_EXTERNAL_EVENT_TASK_DESCRIPTION') ??
'',
102 parameters: [
'COMPLETED_USERS' => []],
116 return $taskService::getList(
119 'WORKFLOW_ID' => $workflowId,
120 'ACTIVITY' => self::getAssociatedActivity(),
125 [
'ID',
'WORKFLOW_ID',
'PARAMETERS']
131 if ($this->userId <= 0)
133 return Result::createError(
new Error(
'negative userId',
'negative userId'));
136 if (!$this->getEvents())
138 $this->markTaskCompleted([$this->userId]);
142 $this->addUsersToCompletedUsersParameter([$this->userId]);
145 return Result::createOk();
148 private function addUsersToCompletedUsersParameter(
array $userIds): void
150 $completedUsers = $this->task->getCompletedUsersParameter();
151 if ($completedUsers !==
null && $userIds)
156 if (!in_array(
$userId, $completedUsers,
true))
165 $this->task->setCompletedUsersParameter($completedUsers);
166 $this->
update(
new UpdateTaskDto(parameters: $this->task->getParameters()));
173 $users = $this->getTaskUsersByStatus($command->userIds);
176 return Result::createOk();
179 if (!$users->completed && !$users->waiting && !$users->markCompleted)
183 return Result::createOk();
186 if ($users->markCompleted)
188 $this->markTaskCompleted($users->markCompleted);
191 if (!$users->waiting)
195 return Result::createOk();
198 $actualUsers = array_merge($users->completed, $users->waiting, $users->markCompleted);
201 return Result::createOk();
213 $taskUsers = $taskService::getTaskUsers($this->
getId())[$this->
getId()] ?? [];
219 $state = $this->getWorkflowState();
225 return new UsersByStatus($taskUsers, $state, $removeUsers, $this->task->getCompletedUsersParameter());
228 private function markTaskCompleted(
array $userIds): void
233 $taskService->markCompleted($this->
getId(), $userId, self::USER_STATUS);
237 private function markTaskUnCompleted(
array $userIds): void
240 $taskService->markUnCompleted($this->
getId(), $userIds);
245 $field = array_merge(
246 $this->getAllowableCommandFieldProperty(),
248 'Id' => self::FIELD_NAME,
249 'Name' => Loc::getMessage(
'BIZPROC_LIB_TASK_EXTERNAL_EVENT_TASK_FIELD_NAME') ??
'',
257 'TARGET_USER_STATUS' => self::USER_STATUS,
258 'NAME' => self::BUTTON_NAME,
260 'TEXT' => Loc::getMessage(
'BIZPROC_LIB_TASK_EXTERNAL_EVENT_TASK_SEND_BUTTON_NAME') ??
'',
263 'FIELDS' => [$field],
267 private function getAllowableCommandFieldProperty():
array
269 $options = [
'' => Loc::getMessage(
'BIZPROC_LIB_TASK_EXTERNAL_EVENT_TASK_DEFAULT_OPTION_NAME')];
271 $events = $this->getEvents();
281 'Settings' => [
'ShowEmptyValue' =>
false],
289 $command = trim(
$fields[self::FIELD_NAME] ??
'');
297 WorkflowUserTable::syncOnTaskUpdated($this->task->workflowId);
300 return Result::createOk();
303 if (!$this->validateCommand($command))
305 return Result::createError(
new Error(
306 Loc::getMessage(
'BIZPROC_LIB_TASK_EXTERNAL_EVENT_TASK_ERROR_UNKNOWN_COMMAND') ??
'',
311 $eventParameters = [
'Groups' => $this->getUserGroups($this->userId),
'User' =>
$this->userId];
313 \CBPRuntime::sendExternalEvent($this->task->workflowId, $command, $eventParameters);
315 return Result::createOk();
318 private function validateCommand(
string $command): bool
320 return array_key_exists($command, $this->getEvents());
323 private function getEvents():
array
325 if ($this->userId > 0)
327 $state = $this->getWorkflowState();
330 return $this->getAllowableEventsFromState($this->userId, $state);
337 private function getWorkflowState(): ?
array
339 $workflowId = $this->task->workflowId;
340 $documentId = $this->task->getDocumentId();
342 if ($workflowId !==
'' && $documentId)
344 $state = \CBPDocument::getDocumentState($documentId, $workflowId)[$workflowId] ??
null;
356 $allowableEvents = \CBPDocument::getAllowableEvents(
$userId, $this->getUserGroups(
$userId), $state,
true);
358 foreach ($allowableEvents as
$event)
368 $currentUser = \Bitrix\Main\Engine\CurrentUser::get();
370 return (
int)$currentUser->getId() ===
$userId ? $currentUser->getUserGroups() : \CUser::GetUserGroup(
$userId);