Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
timeline.php
1
<?php
2
3
namespace
Bitrix\Bizproc\Workflow
;
4
5
use
Bitrix\Bizproc\Workflow\Entity\WorkflowInstanceTable
;
6
use
Bitrix\Bizproc\Workflow\Entity\WorkflowMetadataTable
;
7
use
Bitrix\Bizproc\Workflow\Entity\WorkflowStateTable
;
8
use
Bitrix\Bizproc\Workflow\Task\TimelineTask
;
9
use
Bitrix\Main\Localization\Loc
;
10
use
Bitrix\Main\SystemException
;
11
use
Bitrix\Main\Type\DateTime
;
12
13
class
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
64
public
function
getWorkflowState
():
WorkflowState
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
{
83
$metadata =
WorkflowMetadataTable::query
()
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
}
Bitrix\Bizproc\Workflow\Entity\WorkflowInstanceTable
Definition
workflowinstance.php:25
Bitrix\Bizproc\Workflow\Entity\WorkflowMetadataTable
Definition
workflowmetadatatable.php:28
Bitrix\Bizproc\Workflow\Entity\WorkflowStateTable
Definition
workflowstate.php:27
Bitrix\Bizproc\Workflow\Task\TimelineTask
Definition
timelinetask.php:9
Bitrix\Bizproc\Workflow\Timeline
Definition
timeline.php:14
Bitrix\Bizproc\Workflow\Timeline\__construct
__construct(WorkflowState $workflow)
Definition
timeline.php:52
Bitrix\Bizproc\Workflow\Timeline\getTasks
getTasks()
Definition
timeline.php:97
Bitrix\Bizproc\Workflow\Timeline\getWorkflowState
getWorkflowState()
Definition
timeline.php:64
Bitrix\Bizproc\Workflow\Timeline\getExecutionTime
getExecutionTime()
Definition
timeline.php:69
Bitrix\Bizproc\Workflow\Timeline\createByWorkflowId
static createByWorkflowId(string $workflowId)
Definition
timeline.php:18
Bitrix\Bizproc\Workflow\Timeline\setUserId
setUserId(int $userId)
Definition
timeline.php:57
Bitrix\Bizproc\Workflow\Timeline\getTimeToStart
getTimeToStart()
Definition
timeline.php:81
Bitrix\Bizproc\Workflow\Timeline\jsonSerialize
jsonSerialize()
Definition
timeline.php:116
Bitrix\Bizproc\Workflow\WorkflowState
Definition
workflowstate.php:8
Bitrix\Main\Localization\Loc
Definition
loc.php:11
Bitrix\Main\Localization\Loc\getMessage
static getMessage($code, $replace=null, $language=null)
Definition
loc.php:29
Bitrix\Main\ORM\Data\DataManager\query
static query()
Definition
datamanager.php:559
Bitrix\Main\SystemException
Definition
exception.php:8
Bitrix\Main\Type\DateTime
Definition
datetime.php:9
Bitrix\Bizproc\Workflow
modules
bizproc
lib
workflow
timeline.php
Создано системой
1.10.0