Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
timeline.php
1<?php
2
4
12
13class Timeline implements \JsonSerializable
14{
15 private ?int $userId = null;
16 private WorkflowState $workflow;
17
18 public static function createByWorkflowId(string $workflowId): ?static
19 {
20 $workflow = WorkflowStateTable::query()
21 ->setSelect([
22 'ID',
23 'MODULE_ID',
24 'ENTITY',
25 'DOCUMENT_ID',
26 'MODIFIED',
27 'STARTED_BY',
28 'STARTED',
29 'STATE',
30 'WORKFLOW_TEMPLATE_ID',
31 'TASKS.ID',
32 'TASKS.ACTIVITY',
33 'TASKS.ACTIVITY_NAME',
34 'TASKS.NAME',
35 'TASKS.STATUS',
36 'TASKS.CREATED_DATE',
37 'TASKS.MODIFIED',
38 'TASKS.PARAMETERS',
39 'TASKS.TASK_USERS.USER_ID',
40 'TASKS.TASK_USERS.STATUS',
41 'TASKS.TASK_USERS.DATE_UPDATE',
42 ])
43 ->setOrder(['TASKS.ID' => 'ASC'])
44 ->setFilter(['=ID' => $workflowId])
45 ->exec()
46 ->fetchObject()
47 ;
48
49 return $workflow ? new static($workflow) : null;
50 }
51
52 public function __construct(WorkflowState $workflow)
53 {
54 $this->workflow = $workflow;
55 }
56
57 public function setUserId(int $userId): static
58 {
59 $this->userId = $userId;
60
61 return $this;
62 }
63
65 {
66 return $this->workflow;
67 }
68
69 public function getExecutionTime(): int
70 {
71 if ($this->isWorkflowRunning())
72 {
73 return (new DateTime())->getTimestamp() - $this->workflow->getStarted()->getTimestamp();
74 }
75 else
76 {
77 return $this->workflow->getModified()->getTimestamp() - $this->workflow->getStarted()->getTimestamp();
78 }
79 }
80
81 public function getTimeToStart(): ?int
82 {
84 ->setSelect(['START_DURATION'])
85 ->setFilter(['=WORKFLOW_ID' => $this->workflow->getId()])
86 ->setLimit(1)
87 ->exec()
88 ->fetchObject()
89 ;
90
91 return $metadata?->getStartDuration();
92 }
93
97 public function getTasks(): array
98 {
99 $tasks = $this->workflow->getTasks();
100
101 $timelineTasks = [];
102 foreach ($tasks as $task)
103 {
104 $timelineTask = new TimelineTask($task);
105 if (isset($this->userId))
106 {
107 $timelineTask->setUserId($this->userId);
108 }
109
110 $timelineTasks[] = $timelineTask;
111 }
112
113 return $timelineTasks;
114 }
115
116 public function jsonSerialize(): array
117 {
118 $documentService = \CBPRuntime::getRuntime()->getDocumentService();
119 $complexDocumentId = $this->workflow->getComplexDocumentId();
120 try
121 {
122 $complexDocumentType = $documentService->getDocumentType($complexDocumentId);
123 }
124 catch (SystemException | \Exception $exception)
125 {
126 $complexDocumentType = null;
127 }
128
129 $entityName =
130 isset($complexDocumentType)
131 ? $documentService->getDocumentTypeCaption($complexDocumentType)
132 : null
133 ;
134
135 return [
136 'documentType' => $complexDocumentType,
137 'moduleName' => $this->getModuleName($complexDocumentId[0] ?? ''),
138 'entityName' => $entityName ?? '',
139 'documentName' => $documentService->getDocumentName($complexDocumentId) ?? '',
140 'isWorkflowRunning' => $this->isWorkflowRunning(),
141 'timeToStart' => $this->getTimeToStart(),
142 'executionTime' => $this->getExecutionTime(),
143 'started' => $this->workflow->getStarted()->getTimestamp(),
144 'startedBy' => $this->workflow->getStartedBy(),
145 'tasks' => $this->getTasks(),
146 ];
147 }
148
149 private function isWorkflowRunning(): bool
150 {
151 return WorkflowInstanceTable::exists($this->workflow->getId());
152 }
153
154 private function getModuleName(string $moduleId): string
155 {
156 return match ($moduleId) {
157 'crm' => 'CRM',
158 'disk' => Loc::getMessage('BIZPROC_TIMELINE_DISK_MODULE_NAME'),
159 'lists' => Loc::getMessage('BIZPROC_TIMELINE_LISTS_MODULE_NAME'),
160 'rpa' => 'RPA',
161 default => '',
162 };
163 }
164}
__construct(WorkflowState $workflow)
Definition timeline.php:52
static createByWorkflowId(string $workflowId)
Definition timeline.php:18
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29