1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
timeline.php
См. документацию.
1<?php
2
3namespace Bitrix\Bizproc\Workflow;
4
5use Bitrix\Bizproc\Api\Service\WorkflowStateService;
6use Bitrix\Bizproc\UI\Helpers\DurationFormatter;
7use Bitrix\Bizproc\Workflow\Entity\WorkflowInstanceTable;
8use Bitrix\Bizproc\Workflow\Entity\WorkflowMetadataTable;
9use Bitrix\Bizproc\Workflow\Entity\WorkflowStateTable;
10use Bitrix\Bizproc\Workflow\Task\TimelineTask;
11use Bitrix\Main\Localization\Loc;
12use Bitrix\Main\SystemException;
13use Bitrix\Bizproc\Api\Request\WorkflowStateService\GetExecutionTimeRequest;
14
15class Timeline implements \JsonSerializable
16{
17 private ?int $userId = null;
18 private WorkflowState $workflow;
19
20 public static function createByWorkflowId(string $workflowId): ?static
21 {
22 $workflow = WorkflowStateTable::query()
23 ->setSelect([
24 'ID',
25 'MODULE_ID',
26 'ENTITY',
27 'DOCUMENT_ID',
28 'MODIFIED',
29 'STARTED_BY',
30 'STARTED',
31 'STATE',
32 'WORKFLOW_TEMPLATE_ID',
33 'TASKS.ID',
34 'TASKS.WORKFLOW_ID',
35 'TASKS.ACTIVITY',
36 'TASKS.ACTIVITY_NAME',
37 'TASKS.NAME',
38 'TASKS.STATUS',
39 'TASKS.CREATED_DATE',
40 'TASKS.MODIFIED',
41 'TASKS.PARAMETERS',
42 'TASKS.TASK_USERS.USER_ID',
43 'TASKS.TASK_USERS.STATUS',
44 'TASKS.TASK_USERS.DATE_UPDATE',
45 ])
46 ->setOrder(['TASKS.ID' => 'ASC'])
47 ->setFilter(['=ID' => $workflowId])
48 ->exec()
49 ->fetchObject()
50 ;
51
52 return $workflow ? new static($workflow) : null;
53 }
54
55 public function __construct(WorkflowState $workflow)
56 {
57 $this->workflow = $workflow;
58 }
59
60 public function setUserId(int $userId): static
61 {
62 $this->userId = $userId;
63
64 return $this;
65 }
66
68 {
69 return $this->workflow;
70 }
71
72 public function getExecutionTime(): ?int
73 {
74 $workflowStateService = new WorkflowStateService();
75 return $workflowStateService->getExecutionTime(
77 workflowId: $this->workflow->getId(),
78 workflowStarted: $this->workflow->getStarted(),
79 workflowModified: $this->workflow->getModified(),
80 )
81 )->getRoundedExecutionTime();
82 }
83
84 public function getTimeToStart(): ?int
85 {
87 ->setSelect(['START_DURATION'])
88 ->setFilter(['=WORKFLOW_ID' => $this->workflow->getId()])
89 ->setLimit(1)
90 ->exec()
91 ->fetchObject()
92 ;
93
94 $startDuration = $metadata?->getStartDuration();
95 if ($startDuration)
96 {
97 return DurationFormatter::roundTimeInSeconds($startDuration, 2);
98 }
99
100 return null;
101 }
102
106 public function getTasks(): array
107 {
108 $tasks = $this->workflow->getTasks();
109
110 $timelineTasks = [];
111 foreach ($tasks as $task)
112 {
113 $timelineTask = new TimelineTask($task);
114 if (isset($this->userId))
115 {
116 $timelineTask->setUserId($this->userId);
117 }
118
119 $timelineTasks[] = $timelineTask;
120 }
121
122 return $timelineTasks;
123 }
124
125 public function jsonSerialize(): array
126 {
127 $documentService = \CBPRuntime::getRuntime()->getDocumentService();
128 $complexDocumentId = $this->workflow->getComplexDocumentId();
129 try
130 {
131 $complexDocumentType = $documentService->getDocumentType($complexDocumentId);
132 }
133 catch (SystemException | \Exception $exception)
134 {
135 $complexDocumentType = null;
136 }
137
138 $entityName =
139 isset($complexDocumentType)
140 ? $documentService->getDocumentTypeCaption($complexDocumentType)
141 : null
142 ;
143
144 $documentUrl = $documentService->getDocumentDetailUrl($complexDocumentId);
145
146 return [
147 'documentId' => $complexDocumentId,
148 'documentType' => $complexDocumentType,
149 'moduleName' => $this->getModuleName($complexDocumentId[0] ?? ''),
150 'entityName' => $entityName ?? '',
151 'documentUrl' => empty($documentUrl) ? null : $documentUrl,
152 'documentName' => $documentService->getDocumentName($complexDocumentId) ?? '',
153 'isWorkflowRunning' => $this->isWorkflowRunning(),
154 'timeToStart' => $this->getTimeToStart(),
155 'executionTime' => $this->getExecutionTime(),
156 'workflowModifiedDate' => $this->workflow->getModified()->getTimestamp(),
157 'started' => $this->workflow->getStarted()?->getTimestamp(),
158 'startedBy' => $this->workflow->getStartedBy(),
159 'tasks' => $this->getTasks(),
160 ];
161 }
162
163 private function isWorkflowRunning(): bool
164 {
165 return WorkflowInstanceTable::exists($this->workflow->getId());
166 }
167
168 private function getModuleName(string $moduleId): string
169 {
170 return match ($moduleId) {
171 'crm' => 'CRM',
172 'disk' => Loc::getMessage('BIZPROC_TIMELINE_DISK_MODULE_NAME'),
173 'lists' => Loc::getMessage('BIZPROC_TIMELINE_LISTS_MODULE_NAME'),
174 'rpa' => 'RPA',
175 default => '',
176 };
177 }
178}
__construct(WorkflowState $workflow)
Определения timeline.php:55
static createByWorkflowId(string $workflowId)
Определения timeline.php:20
setUserId(int $userId)
Определения timeline.php:60
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$moduleId