Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
WorkflowStateService.php
1<?php
2
4
13use Bitrix\Bizproc\Workflow\Entity\EO_WorkflowState_Collection;
19
21{
22 private const CONVERTER_VERSION = 2;
23
25 {
26 $this->convertProcesses($toGet->getFilterUserId());
27
28 $response = new GetListResponse();
29 $responseCollection = new EO_WorkflowState_Collection();
30
31 $queryResult = WorkflowUserTable::query()
32 ->addSelect('WORKFLOW_ID')
33 ->setFilter($toGet->getOrmFilter())
34 ->setOrder($toGet->getOrder())
35 ->setLimit($toGet->getLimit())
36 ->setOffset($toGet->getOffset())
37 ->countTotal($toGet->isCountingTotal())
38 ->exec()
39 ;
40
41 if ($toGet->isCountingTotal())
42 {
43 $response->setTotalCount($queryResult->getCount());
44 }
45 $workflowStates = $queryResult->fetchAll();
46 $ids = array_column($workflowStates, 'WORKFLOW_ID');
47 if ($ids)
48 {
49 $query = WorkflowStateTable::query()->setSelect($toGet->getSelect());
50 if (count($ids) === 1)
51 {
52 $query->where('ID', '=', $ids[0]);
53 }
54 else
55 {
56 $query->whereIn('ID', $ids);
57 }
58
59 $collection = $query->exec()->fetchCollection();
60
61 foreach ($ids as $id)
62 {
63 $workflowState = $collection->getByPrimary($id);
64 if ($workflowState)
65 {
66 $responseCollection->add($workflowState);
67 }
68 }
69 }
70
71 return $response->setWorkflowStatesCollection($responseCollection);
72 }
73
75 {
76 $response = new GetFullFilledListResponse();
77
78 $toGet->setSelectAllFields();
79 $getListResult = $this->getList($toGet);
80 $collection = $getListResult->getWorkflowStatesCollection();
81 if (!$collection)
82 {
83 return $response->setWorkflowStatesList([]);
84 }
85
86 $fullFilledList = [];
87 $userIds = [];
88 foreach ($collection as $stateElement)
89 {
90 $tasksInfo = $stateElement->getTasksInfo();
91 foreach ($tasksInfo as $task)
92 {
93 if (!empty($task['TASK_USERS']))
94 {
95 foreach ($task['TASK_USERS'] as $row)
96 {
97 $userIds[$row['USER_ID']] = true;
98 $userIds[$row['ORIGINAL_USER_ID']] = true;
99 }
100 }
101 }
102 $fullFilledList[] = [
103 'ID' => $stateElement->getId(),
104 'STATE_INFO' => $stateElement->getStateInfo(),
105 'DOCUMENT_INFO' => $this->getDocumentInfo($stateElement->getComplexDocumentId()),
106 'STARTED_USER_INFO' => [
107 'ID' => $stateElement->getStartedBy(),
108 ],
109 'TASKS_INFO' => $tasksInfo,
110 'TEMPLATE_NAME' => $stateElement->getTemplate()?->getName(),
111 ];
112
113 $userIds[$stateElement->getStartedBy()] = true;
114 }
115
116 return (
117 $response
118 ->setWorkflowStatesList($fullFilledList)
119 ->setMembersInfo($this->getMembersInfo(array_keys($userIds)))
120 );
121 }
122
124 {
125 $timeline = Timeline::createByWorkflowId($request->workflowId);
126
127 if (!$timeline)
128 {
130 }
131
132 return GetTimelineResponse::createOk(['timeline' => $timeline->setUserId($request->userId)]);
133 }
134
135 private function getDocumentInfo(array $complexDocumentId): array
136 {
137 $documentService = \CBPRuntime::getRuntime()->getDocumentService();
138
139 $key = null;
140 try
141 {
142 $complexDocumentType = $documentService->getDocumentType($complexDocumentId);
143 $key = $complexDocumentId[0] . '@' . $complexDocumentId[1] . '@' . $complexDocumentType[2];
144 }
145 catch (SystemException | \Exception $exception)
146 {
147 $complexDocumentType = null;
148 }
149
150 static $cache = [];
151 if ($key && !isset($cache[$key]))
152 {
153 $cache[$key] = $documentService->getDocumentTypeCaption($complexDocumentType);
154 }
155 $typeCaption = $key ? $cache[$key] : '';
156
157 return [
158 'COMPLEX_ID' => $complexDocumentId,
159 'COMPLEX_TYPE' => $complexDocumentType,
160 'NAME' => $documentService->getDocumentName($complexDocumentId),
161 'TYPE_CAPTION' => $typeCaption,
162 ];
163 }
164
165 private function getMembersInfo(array $ids): array
166 {
167 if (empty($ids))
168 {
169 return [];
170 }
171
172 $userFields = ['ID', 'NAME', 'SECOND_NAME', 'LAST_NAME', 'LOGIN', 'TITLE', 'EMAIL', 'PERSONAL_PHOTO'];
173
174 $users = \CUser::GetList(
175 'id',
176 'asc',
177 ['ID' => implode('|', $ids)],
178 ['FIELDS' => $userFields]
179 );
180
181 $info = [];
182 while ($user = $users->Fetch())
183 {
184 $fullName = \CUser::FormatName(\CSite::GetNameFormat(false), $user, true, false);
185 $personalPhoto = (int)$user['PERSONAL_PHOTO'];
186
187 $info[] = [
188 'ID' => (int)($user['ID'] ?? 0),
189 'FULL_NAME' => $fullName,
190 'PERSONAL_PHOTO' => $personalPhoto,
191 ];
192 }
193
194 return $info;
195 }
196
197 private function convertProcesses(int $userId)
198 {
199 if (empty($userId))
200 {
201 return;
202 }
203
204 $converterVersion = \CUserOptions::getOption(
205 'bizproc',
206 'processes_converted',
207 0,
208 $userId
209 );
210
211 if ($converterVersion === self::CONVERTER_VERSION)
212 {
213 return;
214 }
215
216 WorkflowUserTable::convertUserProcesses($userId);
217
218 \CUserOptions::setOption(
219 'bizproc',
220 'processes_converted',
221 self::CONVERTER_VERSION,
222 false,
223 $userId
224 );
225 }
226
230 {
231 // rights? canUserOperateDocumentType(CBPCanUserOperateOperation::ReadDocument, ...)
232
233 $response = new GetAverageWorkflowDurationResponse();
234 if ($request->templateId <= 0)
235 {
236 $response->addError(new Error('incorrect template id'));
237 }
238
239 if ($response->isSuccess())
240 {
241 $averageDuration = WorkflowDurationStatTable::getAverageDurationByTemplateId($request->templateId);
242 if ($averageDuration !== null)
243 {
244 $response->setAverageDuration($averageDuration);
245 }
246 }
247
248 return $response;
249 }
250}
getAverageWorkflowDuration(GetAverageWorkflowDurationRequest $request)
static fromCode(string $code, $customData=null)
Definition error.php:13
static createError(Error $error)
Definition result.php:12
static createOk(?array $data=null)
Definition result.php:20
static createByWorkflowId(string $workflowId)
Definition timeline.php:18