1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
WorkflowUserView.php
См. документацию.
1<?php
2
3namespace Bitrix\Bizproc\UI;
4
5use Bitrix\Bizproc\Workflow\Entity\WorkflowUserCommentTable;
6use Bitrix\Bizproc\Workflow\WorkflowState;
7use Bitrix\Bizproc\WorkflowInstanceTable;
8use Bitrix\Main\EO_User;
9use Bitrix\Main\SystemException;
10use Bitrix\Main\Type\Collection;
11use Bitrix\Main\Type\DateTime;
12use Bitrix\Main\UserTable;
13
14class WorkflowUserView implements \JsonSerializable
15{
17 private array $tasks;
18 private array $myRunningTasks;
19 private array $myCompletedTasks;
20 protected int $userId;
21
22 protected ?WorkflowFacesView $faces = null;
23 protected ?bool $isCompleted = null;
24
26 {
27 $this->workflow = $workflow;
28 $this->userId = $userId;
29
30 $this->tasks = \CBPViewHelper::getWorkflowTasks($workflow['ID'], true, true);
31 $this->myRunningTasks = $this->getMyWaitingTasks();
32 $this->myCompletedTasks = $this->getMyCompletedTasks();
33 }
34
35 public function toArray(): array
36 {
37 return $this->jsonSerialize();
38 }
39
40 public function jsonSerialize(): array
41 {
42 return [
43 'id' => $this->getId(),
44 'data' => [
45 'id' => $this->getId(),
46 'typeName' => $this->getTypeName(),
47 'itemName' => $this->getName(),
48 'itemDescription' => $this->getDescription(),
49 'itemTime' => $this->getTime(),
50 'statusText' => $this->getStatusText(),
51 'faces' => $this->getFaces(),
52 'tasks' => $this->getTasks(),
53 'authorId' => $this->getAuthorId(),
54 'newCommentsCounter' => $this->getCommentCounter(),
55 'result' => $this->getIsCompleted() ? $this->getWorkflowResult() : null,
56 ],
57 ];
58 }
59
60 public function getId(): string
61 {
62 return $this->workflow->getId();
63 }
64
65 public function getName(): mixed
66 {
67 if ($this->getTasks())
68 {
69 return current($this->getTasks())['name'];
70 }
71
72 if (!$this->isWorkflowAuthorView())
73 {
74 $task = current($this->getCompletedTasks());
75 if ($task)
76 {
77 return $task['name'];
78 }
79 }
80
81 $documentService = \CBPRuntime::getRuntime()->getDocumentService();
82
83 return html_entity_decode(
84 $documentService->getDocumentName($this->workflow->getComplexDocumentId()) ?? ''
85 );
86 }
87
88 public function getDescription(): ?string
89 {
90 $task = false;
91 if ($this->getTasks())
92 {
93 $task = current($this->getTasks());
94 }
95 elseif (!$this->isWorkflowAuthorView() && $this->getCompletedTasks())
96 {
97 $task = current($this->getCompletedTasks());
98 }
99
100 if ($task)
101 {
102 return trim($task['description']);
103 }
104
105 return null;
106 }
107
108 public function getStatusText(): mixed
109 {
110 return $this->workflow->getStateTitle();
111 }
112
113 public function getIsCompleted(): bool
114 {
115 if ($this->isCompleted === null)
116 {
117 $this->isCompleted = !WorkflowInstanceTable::exists($this->getId());
118 }
119
120 return $this->isCompleted;
121 }
122
123 public function getCommentCounter(): int
124 {
125 $row = WorkflowUserCommentTable::getList([
126 'filter' => [
127 '=WORKFLOW_ID' => $this->getId(),
128 '=USER_ID' => $this->userId,
129 ],
130 'select' => ['UNREAD_CNT'],
131 ])->fetch();
132
133 return $row ? (int)$row['UNREAD_CNT'] : 0;
134 }
135
136 public function getTasks(): array
137 {
138 return $this->myRunningTasks;
139 }
140
141 public function getTaskById(int $taskId): ?array
142 {
143 foreach ($this->getTasks() as $task)
144 {
145 if ($task['id'] === $taskId)
146 {
147 return $task;
148 }
149 }
150
151 foreach ($this->getCompletedTasks() as $task)
152 {
153 if ($task['id'] === $taskId)
154 {
155 return $task;
156 }
157 }
158
160 [],
161 ['ID' => $taskId, 'USER_ID' => $this->userId],
162 false,
163 false,
164 [
165 'ID',
166 'MODIFIED',
167 'NAME',
168 'DESCRIPTION',
169 'PARAMETERS',
170 'STATUS',
171 'IS_INLINE',
172 'ACTIVITY',
173 'ACTIVITY_NAME',
174 'CREATED_DATE',
175 'DELEGATION_TYPE',
176 'OVERDUE_DATE',
177 ],
178 )->getNext();
179
180 if ($task)
181 {
182 $task['USERS'] = \CBPTaskService::getTaskUsers($taskId)[$taskId] ?? [];
183 $preparedTasks = $this->prepareTasks([$task]);
184
185 return $preparedTasks[0] ?? null;
186 }
187
188 return null;
189 }
190
191 public function getCompletedTasks(): array
192 {
193 return $this->myCompletedTasks;
194 }
195
196 public function getAuthorId(): mixed
197 {
198 return $this->workflow->getStartedBy();
199 }
200
201 protected function getTime(): string
202 {
203 return \CBPViewHelper::formatDateTime($this->workflow->getModified());
204 }
205
206 public function getOverdueDate(): ?DateTime
207 {
208 $task = $this->getTasks()[0] ?? null;
209 if (!$task || !isset($task['overdueDate']))
210 {
211 return null;
212 }
213
214 return DateTime::createFromUserTime($task['overdueDate']);
215 }
216
217 public function getStartedBy(): ?EO_User
218 {
220 $currentUserId = $currentUser->getId();
221 if (!is_null($currentUserId) && (int)$currentUserId === $this->workflow->getStartedBy())
222 {
223 $rows = [
224 'ID' => $currentUser->getId(),
225 'LOGIN' => $currentUser->getLogin(),
226 'NAME' => $currentUser->getFirstName(),
227 'SECOND_NAME' => $currentUser->getSecondName(),
228 'LAST_NAME' => $currentUser->getLastName(),
229 ];
230
231 return UserTable::wakeUpObject($rows);
232 }
233
234 return \Bitrix\Main\UserTable::getByPrimary(
235 $this->workflow->getStartedBy(),
236 ['select' => ['ID', 'LOGIN', 'NAME', 'SECOND_NAME', 'LAST_NAME']]
237 )->fetchObject();
238 }
239
240 public function getFaces(): WorkflowFacesView
241 {
242 if (!$this->faces)
243 {
244 $task = $this->getFirstRunningTask();
245 $this->faces = new WorkflowFacesView($this->getId(), $task ? $task['id'] : null);
246 }
247
248 return $this->faces;
249 }
250
251 private function getMyWaitingTasks(): array
252 {
254
255 $myTasks = array_filter(
256 $this->tasks['RUNNING'],
257 static function($task) use ($userId) {
258 $waitingUsers = array_filter(
259 $task['USERS'],
260 static fn ($user) => ((int)$user['STATUS'] === \CBPTaskUserStatus::Waiting),
261 );
262
263 return in_array($userId, array_column($waitingUsers, 'USER_ID'));
264 },
265 );
266
267 return $this->prepareTasks(array_values($myTasks));
268 }
269
270 private function getMyCompletedTasks(): array
271 {
273
274 $completedRunningTasks = array_filter(
275 $this->tasks['RUNNING'],
276 static function ($task) use ($userId) {
277 $completedUsers = array_filter(
278 $task['USERS'],
279 static fn ($user) => ((int)$user['STATUS'] !== \CBPTaskUserStatus::Waiting),
280 );
281
282 $taskUserIds = array_column($completedUsers, 'USER_ID');
283 Collection::normalizeArrayValuesByInt($taskUserIds);
284
285 return in_array($userId, $taskUserIds, true);
286 }
287 );
288
289 $completedTasks = array_filter(
290 $this->tasks['COMPLETED'],
291 static function ($task) use ($userId) {
292 $taskUserIds = array_column($task['USERS'], 'USER_ID');
293 Collection::normalizeArrayValuesByInt($taskUserIds);
294
295 return in_array($userId, $taskUserIds, true);
296 }
297 );
298
299 return $this->prepareTasks(array_values(array_merge($completedRunningTasks, $completedTasks)));
300 }
301
302 protected function prepareTasks(array $myTasks): array
303 {
304 $isRpa = $this->workflow->getModuleId() === 'rpa';
306
307 $tasks = [];
308 foreach ($myTasks as $task)
309 {
310 $isRunning = (int)$task['STATUS'] === \CBPTaskStatus::Running;
311 if ($isRunning)
312 {
313 $users = array_filter(
314 $task['USERS'] ?? [],
315 static function ($user) use ($userId) {
316 return (int)$user['USER_ID'] === $userId;
317 },
318 );
319 if ($users)
320 {
321 $user = current($users);
322 $isRunning = (int)$user['STATUS'] === \CBPTaskUserStatus::Waiting;
323 }
324 }
325
326 $controls = $isRunning ? \CBPDocument::getTaskControls($task, $userId) : [];
327 $buttons = $controls['BUTTONS'] ?? null;
328 if (!empty($buttons))
329 {
330 foreach ($buttons as &$button)
331 {
332 if (!empty($button['TEXT']))
333 {
334 $button['TEXT'] = html_entity_decode(htmlspecialcharsback($button['TEXT']));
335 }
336 }
337
338 unset($button);
339 }
340
341 $taskId = (int)$task['ID'];
342 $tasks[] = [
343 'id' => $taskId,
344 'name' => html_entity_decode($task['~NAME']),
345 'description' => $task['~DESCRIPTION'],
346 'isInline' => \CBPHelper::getBool($task['IS_INLINE']),
347 'controls' => [
348 'buttons' => $buttons,
349 'fields' => $controls['FIELDS'] ?? null,
350 ],
351 'createdDate' => $task['~CREATED_DATE'] ?? null,
352 'delegationType' => $task['~DELEGATION_TYPE'] ?? null,
353 'overdueDate' => $task['~OVERDUE_DATE'] ?? null,
354 'url' => $isRpa
355 ? "/rpa/task/id/$taskId/"
356 : sprintf(
357 '/company/personal/bizproc/%s/?USER_ID=%d',
358 $taskId,
359 $this->userId
360 )
361 ,
362 'userId' => $userId,
363 'isRunning' => $isRunning,
364 ];
365 }
366
367 return $tasks;
368 }
369
370 public function getTypeName(): mixed
371 {
372 $this->workflow->fillTemplate();
373 if (
374 $this->workflow->getModuleId() !== 'lists'
375 && !empty($this->workflow->getTemplate()?->getName())
376 )
377 {
378 return $this->workflow->getTemplate()?->getName();
379 }
380
381 $documentService = \CBPRuntime::getRuntime()->getDocumentService();
382
383 $complexDocumentType = null;
384 try
385 {
386 $complexDocumentType = $documentService->getDocumentType($this->workflow->getComplexDocumentId());
387 }
388 catch (SystemException | \Exception $exception)
389 {}
390
391 return $complexDocumentType ? $documentService->getDocumentTypeCaption($complexDocumentType) : null;
392 }
393
394 public function getWorkflowResult(): ?array
395 {
396 return \CBPViewHelper::getWorkflowResult($this->getId(), $this->userId);
397 }
398
399 protected function isWorkflowAuthorView(): bool
400 {
401 return $this->getAuthorId() === $this->userId;
402 }
403
404 private function formatDateTime(string $format, $datetime): ?string
405 {
406 if ($datetime instanceof DateTime)
407 {
408 $datetime = (string)$datetime;
409 }
410
411 if (is_string($datetime) && DateTime::isCorrect($datetime))
412 {
413 $timestamp = (new DateTime($datetime))->getTimestamp();
414
415 return FormatDate($format, $timestamp);
416 }
417
418 return null;
419 }
420
421 protected function getAuthorView(): ?UserView
422 {
423 return \Bitrix\Bizproc\UI\UserView::createFromId($this->getAuthorId());
424 }
425
426 protected function getDocumentUrl(): ?string
427 {
428 $complexDocumentId = $this->workflow->getComplexDocumentId();
429
430 return \CBPDocument::getDocumentAdminPage($complexDocumentId);
431 }
432
433 public function getFirstRunningTask(): ?array
434 {
435 return $this->getTasks()[0] ?? null;
436 }
437
438 protected function isRunningTaskUser(array $task): bool
439 {
440 $taskUsers = $task['USERS'] ?? [];
441
442 foreach ($taskUsers as $user)
443 {
444 if ((int)$user['USER_ID'] === $this->userId)
445 {
446 return (int)$user['STATUS'] === \CBPTaskUserStatus::Waiting;
447 }
448 }
449
450 return false;
451 }
452
453 protected function getTaskControls(array $task): array
454 {
455 $controls = \CBPDocument::getTaskControls($task, $this->userId);
456 $buttons = $controls['BUTTONS'] ?? null;
457 if (!empty($buttons))
458 {
459 foreach ($buttons as &$button)
460 {
461 if (!empty($button['TEXT']))
462 {
463 $button['TEXT'] = html_entity_decode(htmlspecialcharsback($button['TEXT']));
464 }
465 }
466
467 unset($button);
468 }
469
470 return [
471 'buttons' => $buttons,
472 'fields' => $controls['FIELDS'] ?? null,
473 ];
474 }
475}
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
WorkflowState $workflow
Определения WorkflowUserView.php:16
prepareTasks(array $myTasks)
Определения WorkflowUserView.php:302
getTaskControls(array $task)
Определения WorkflowUserView.php:453
__construct(WorkflowState $workflow, int $userId)
Определения WorkflowUserView.php:25
WorkflowFacesView $faces
Определения WorkflowUserView.php:22
getTaskById(int $taskId)
Определения WorkflowUserView.php:141
isRunningTaskUser(array $task)
Определения WorkflowUserView.php:438
Определения orm.php:16144
static get()
Определения currentuser.php:33
static getTaskUsers($taskId)
Определения taskservice.php:99
static getList($arOrder=array("ID"=> "DESC"), $arFilter=array(), $arGroupBy=false, $arNavStartParams=false, $arSelectFields=array())
Определения taskservice.php:831
const Running
Определения constants.php:258
const Waiting
Определения constants.php:273
static getWorkflowTasks($workflowId, $withUsers=false, $extendUserInfo=false)
Определения viewhelper.php:66
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
FormatDate($format="", $timestamp=false, $now=false, ?string $languageId=null)
Определения tools.php:871
htmlspecialcharsback($str)
Определения tools.php:2693
$user
Определения mysql_to_pgsql.php:33
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$rows
Определения options.php:264