41 return 'b_bp_workflow_user';
55 ->configureNullable(
false)
58 ->configureNullable(
false)
61 ->configureNullable(
false)
64 ->configureNullable(
false)
71 $workflowId = $workflow->getInstanceId();
72 $users = static::getTaskUsers($workflowId);
73 $hasUsers = !empty($users) || static::hasStoredUsers($workflowId);
75 if (!$hasUsers && !static::isLiveFeedProcess($workflow->getDocumentId()))
80 $authorId = $workflow->getStartedBy();
81 $workflowStatus = \CBPWorkflowStatus::isFinished($status)
82 ? static::WORKFLOW_STATUS_COMPLETED
83 : static::WORKFLOW_STATUS_ACTIVE
88 $users[$authorId][
'IS_AUTHOR'] ??= 1;
91 foreach ($users as $id => $user)
93 $users[$id][
'WORKFLOW_STATUS'] = $workflowStatus;
96 static::syncUsers($workflowId, $users);
99 private static function isLiveFeedProcess(array $documentId): bool
101 return ($documentId[0] ??
'') ===
'lists' && ($documentId[1] ??
'') ===
'BizprocDocument';
106 $users = static::getTaskUsers($workflowId);
108 return static::syncUsers($workflowId, $users);
111 private static function getTaskUsers(
string $workflowId): array
113 $taskUsers = \CBPTaskService::getWorkflowUsers($workflowId);
116 foreach ($taskUsers as $id => $taskStatus)
119 'TASK_STATUS' => $taskStatus === \CBPTaskUserStatus::Waiting
120 ? static::TASK_STATUS_ACTIVE
121 : static::TASK_STATUS_COMPLETED
126 $authorId = static::getAuthorId($workflowId);
127 if ($authorId && !isset($users[$authorId]))
129 $users[$authorId] = [
130 'TASK_STATUS' => static::TASK_STATUS_NONE,
137 private static function syncUsers(
string $workflowId, array $users): array
139 $stored = static::getStoredUsers($workflowId);
141 $toDelete = array_diff_key($stored, $users);
142 $toAdd = array_diff_key($users, $stored);
143 $toUpdate = array_intersect_key($users, $stored);
145 self::deleteOnSync($workflowId, $toDelete);
146 self::addOnSync($workflowId, $toAdd);
147 self::updateOnSync($workflowId, $toUpdate);
149 return [array_keys($toAdd), array_keys($toUpdate), array_keys($toDelete)];
152 private static function getAuthorId(
string $workflowId): int
154 $result = static::getList([
155 'select' => [
'USER_ID'],
157 '=WORKFLOW_ID' => $workflowId,
162 return (
int)($result[
'USER_ID'] ?? 0);
165 private static function deleteOnSync(
string $workflowId, array $toDelete): void
172 $deleted = array_keys($toDelete);
173 foreach ($deleted as $userId)
176 'USER_ID' => $userId,
177 'WORKFLOW_ID' => $workflowId,
184 private static function addOnSync(
string $workflowId, array $toAdd): void
191 foreach ($toAdd as $userId => $user)
194 'USER_ID' => $userId,
195 'WORKFLOW_ID' => $workflowId,
196 'IS_AUTHOR' => $user[
'IS_AUTHOR'] ?? 0,
197 'WORKFLOW_STATUS' => $user[
'WORKFLOW_STATUS'] ?? static::WORKFLOW_STATUS_ACTIVE,
198 'TASK_STATUS' => $user[
'TASK_STATUS'] ?? static::TASK_STATUS_NONE,
199 'MODIFIED' =>
new DateTime(),
206 private static function updateOnSync(
string $workflowId, array $toUpdate): void
213 $modified =
new DateTime();
215 foreach ($toUpdate as $userId => $user)
217 $user[
'MODIFIED'] = $modified;
221 'USER_ID' => $userId,
222 'WORKFLOW_ID' => $workflowId,
231 private static function getStoredUsers(
string $workflowId): array
233 $result = static::getList([
234 'select' => [
'USER_ID',
'IS_AUTHOR',
'WORKFLOW_STATUS',
'TASK_STATUS'],
235 'filter' => [
'=WORKFLOW_ID' => $workflowId],
240 while ($row = $result->fetch())
242 $users[(int)$row[
'USER_ID']] = [
243 'IS_AUTHOR' => (
int)$row[
'IS_AUTHOR'],
244 'WORKFLOW_STATUS' => (int)$row[
'WORKFLOW_STATUS'],
245 'TASK_STATUS' => (
int)$row[
'TASK_STATUS'],
252 private static function hasStoredUsers(
string $workflowId): bool
254 return static::getCount([
'=WORKFLOW_ID' => $workflowId]) > 0;
259 $stored = static::getStoredUsers($workflowId);
260 self::deleteOnSync($workflowId, $stored);
262 return array_keys($stored);
269 if ($connection->getType() !==
'mysql')
277 DELETE FROM b_bp_workflow_user WHERE USER_ID = {$userId}
284 INSERT INTO b_bp_workflow_user
285 (USER_ID, WORKFLOW_ID, IS_AUTHOR, WORKFLOW_STATUS, MODIFIED)
287 select wt.STARTED_BY, wt.ID, 1,
case when wi.id is
null then 1
else 0 end, wt.MODIFIED
288 from b_bp_workflow_state wt
289 left join b_bp_workflow_instance wi on (wt.id = wi.id)
290 where wt.STARTED_BY = {$userId} and wt.MODULE_ID =
'lists' and wt.ENTITY =
'BizprocDocument'
292 ON DUPLICATE KEY UPDATE IS_AUTHOR = 1, WORKFLOW_STATUS = VALUES(WORKFLOW_STATUS), MODIFIED = VALUES(MODIFIED)
299 INSERT INTO b_bp_workflow_user
300 (USER_ID, WORKFLOW_ID, IS_AUTHOR, WORKFLOW_STATUS, TASK_STATUS, MODIFIED)
302 select tu.USER_ID, t.WORKFLOW_ID, 0, 0, 2,
303 case when tu.DATE_UPDATE is
null then now()
else tu.DATE_UPDATE end
304 from b_bp_task_user tu
305 inner join b_bp_task t on (t.ID = tu.TASK_ID)
306 where tu.USER_ID = {$userId} and tu.STATUS =
'0'
308 ON DUPLICATE KEY UPDATE TASK_STATUS = VALUES(TASK_STATUS), MODIFIED = VALUES(MODIFIED)
313 \Bitrix\Bizproc\Worker\Task\UserToWorkflowStepper::bindUser($userId);