1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
WorkflowService.php
См. документацию.
1<?php
2
3namespace Bitrix\Lists\Api\Service;
4
5use Bitrix\Bizproc\Workflow\Entity\EO_WorkflowMetadata;
6use Bitrix\Lists\Api\Request\WorkflowService\StartWorkflowsRequest;
7use Bitrix\Lists\Api\Response\Response;
8use Bitrix\Lists\Api\Response\WorkflowService\GetParameterValuesResponse;
9use Bitrix\Lists\Api\Response\WorkflowService\StartWorkflowsResponse;
10use Bitrix\Main\Error;
11use Bitrix\Main\Loader;
12use Bitrix\Main\Localization\Loc;
13
14final class WorkflowService
15{
16 private string $iBlockTypeId;
17 private bool $isBpEnabled;
18 private ?array $complexDocumentType;
19
20 public function __construct(array $iBlockInfo)
21 {
22 $this->iBlockTypeId = (string)($iBlockInfo['IBLOCK_TYPE_ID'] ?? '');
23
24 $this->isBpEnabled = (
25 Loader::includeModule('bizproc')
26 && \CLists::isBpFeatureEnabled($this->iBlockTypeId)
27 && (isset($iBlockInfo['BIZPROC']) && $iBlockInfo['BIZPROC'] === 'Y') // $iBlockInfo['BIZPROC'] != 'N'
28 );
29
30 $this->complexDocumentType = (
31 $this->isBpEnabled
33 $this->iBlockTypeId,
34 max((int)($iBlockInfo['ID'] ?? 0), 0)
35 )
36 : null
37 );
38 }
39
40 public function getSignedDocument(int $elementId): ?string
41 {
42 if ($this->isBpEnabled)
43 {
44 return \CBPDocument::signParameters([$this->complexDocumentType, (string)$elementId]);
45 }
46
47 return null;
48 }
49
50 public function canUserWriteDocument(int $elementId, int $userId, array $userGroups): bool
51 {
52 if ($elementId < 0 || $userId <= 0)
53 {
54 return false;
55 }
56
57 $canWrite = true;
58 if ($this->isBpEnabled)
59 {
60 $complexDocumentId = $this->getComplexDocumentId($elementId);
61
63 $parameters = ['AllUserGroups' => $userGroups];
64
65 $canWrite = (
66 $elementId > 0
67 ? \CBPDocument::canUserOperateDocument($operation, $userId, $complexDocumentId, $parameters)
68 : \CBPDocument::canUserOperateDocumentType($operation, $userId, $this->complexDocumentType, $parameters)
69 );
70 }
71
72 return $canWrite;
73 }
74
75 public function canUserStartWorkflow(int $currentUserId, int $elementId, int $sectionId = 0): bool
76 {
77 if ($currentUserId <= 0 || $elementId < 0 || $sectionId < 0)
78 {
79 return false;
80 }
81
82 $canStart = true;
83 if ($this->isBpEnabled)
84 {
85 $documentStates = $this->getDocumentStates($this->getComplexDocumentId($elementId));
86
87 $canStart = \CBPDocument::canUserOperateDocumentType(
89 $currentUserId,
90 $this->complexDocumentType,
91 ['sectionId' => $sectionId, 'DocumentStates' => $documentStates]
92 );
93 }
94
95 return $canStart;
96 }
97
98 public function isConstantsTuned(): bool
99 {
100 $isConstantsTuned = true;
101 if ($this->isBpEnabled)
102 {
103 $templates = array_merge(
104 \CBPWorkflowTemplateLoader::searchTemplatesByDocumentType(
105 $this->complexDocumentType, \CBPDocumentEventType::Create
106 ),
107 \CBPWorkflowTemplateLoader::searchTemplatesByDocumentType(
108 $this->complexDocumentType, \CBPDocumentEventType::Edit
109 ),
110 );
111
112 foreach ($templates as $template)
113 {
114 if (!\CBPWorkflowTemplateLoader::isConstantsTuned($template['ID']))
115 {
116 $isConstantsTuned = false;
117
118 break;
119 }
120 }
121 }
122
123 return $isConstantsTuned;
124 }
125
126 public function hasParameters(int $elementId): bool
127 {
128 $hasParameters = false;
129 if ($this->isBpEnabled)
130 {
131 $states = $this->getNotRunningDocumentStates($elementId);
132 foreach ($states as $state)
133 {
134 $parameters = $state['TEMPLATE_PARAMETERS'] ?? [];
135 if (!empty($parameters) && is_array($parameters))
136 {
137 $hasParameters = true;
138
139 break;
140 }
141 }
142 }
143
144 return $hasParameters;
145 }
146
148 {
150
151 $parameters = [];
152 if ($this->isBpEnabled && $elementId >= 0)
153 {
154 $documentStates = $this->getDocumentStates($this->getComplexDocumentId($elementId));
155 foreach ($documentStates as $state)
156 {
157 if (empty($state['ID']))
158 {
159 $errors = [];
160 $parameters[$state['TEMPLATE_ID']] = \CBPWorkflowTemplateLoader::checkWorkflowParameters(
161 $state['TEMPLATE_PARAMETERS'] ?? [],
162 $request[$state['TEMPLATE_ID']] ?? [],
163 $this->complexDocumentType,
164 $errors
165 );
166
167 foreach ($errors as $error)
168 {
169 $response->addError(new Error(!empty($error['message']) ? $error['message'] : ''));
170 }
171 }
172 }
173 }
174
175 return $response->setParameters($parameters);
176 }
177
179 {
181
182 if ($request->elementId <= 0 || $request->currentUserId <= 0)
183 {
184 $response->addError(
185 new Error(
186 Loc::getMessage('LISTS_LIB_API_WORKFLOW_SERVICE_INCORRECT_START_WORKFLOW_INPUT_DATA') ?? ''
187 )
188 );
189 }
190
191 $workflowIds = [];
192 if ($this->isBpEnabled && $response->isSuccess())
193 {
194 $complexDocumentId = $this->getComplexDocumentId($request->elementId);
195 $documentStates = $this->getDocumentStates($request->isNewElement ? null : $complexDocumentId);
196 foreach ($documentStates as $state)
197 {
198 if (empty($state['ID']))
199 {
200 $errors = [];
201
202 $startWorkflowParameters = [
203 \CBPDocument::PARAM_TAGRET_USER => 'user_' . $request->currentUserId,
204 \CBPDocument::PARAM_MODIFIED_DOCUMENT_FIELDS => $request->changedFields,
205 ];
206
207 $workflowIds[$state['TEMPLATE_ID']] = \CBPDocument::startWorkflow(
208 $state['TEMPLATE_ID'],
209 $complexDocumentId,
210 array_merge($request->parameters[$state['TEMPLATE_ID']] ?? [], $startWorkflowParameters),
211 $errors
212 );
213
214 if (!$errors && isset($request->timeToStart))
215 {
216 $metadata = new EO_WorkflowMetadata();
217 $metadata->setWorkflowId($workflowIds[$state['TEMPLATE_ID']]);
218 $metadata->setStartDuration($request->timeToStart);
219 $metadata->save();
220 }
221
222 foreach ($errors as $error)
223 {
224 $response->addError(new Error(!empty($error['message']) ? $error['message'] : ''));
225 }
226 }
227 }
228 }
229
230 return $response->setWorkflowIds($workflowIds);
231 }
232
233 public function getComplexDocumentId(int $elementId): ?array
234 {
235 if ($this->isBpEnabled)
236 {
237 return ($elementId > 0 ? \BizprocDocument::getDocumentComplexId($this->iBlockTypeId, $elementId) : null);
238 }
239
240 return null;
241 }
242
243 public function getComplexDocumentType(): ?array
244 {
245 return $this->complexDocumentType;
246 }
247
248 public function getDocumentStates(?array $complexDocumentId): array
249 {
250 if ($this->isBpEnabled)
251 {
252 return \CBPDocument::getDocumentStates($this->complexDocumentType, $complexDocumentId);
253 }
254
255 return [];
256 }
257
258 public function getNotRunningDocumentStates(int $elementId)
259 {
260 if (!$this->isBpEnabled)
261 {
262 return [];
263 }
264
265 $autoExecuteType = $elementId > 0 ? \CBPDocumentEventType::Edit: \CBPDocumentEventType::Create;
266
267 return \CBPWorkflowTemplateLoader::getDocumentTypeStates($this->complexDocumentType, $autoExecuteType);
268 }
269
270 public function getDocumentTypeStates(): array
271 {
272 if (!$this->isBpEnabled)
273 {
274 return [];
275 }
276
277 $states = array_merge(
278 \CBPWorkflowTemplateLoader::getDocumentTypeStates($this->complexDocumentType, \CBPDocumentEventType::Create),
279 \CBPWorkflowTemplateLoader::getDocumentTypeStates($this->complexDocumentType, \CBPDocumentEventType::Edit),
280 );
281
282 $result = [];
283 $templateIds = [];
284 foreach ($states as $state)
285 {
286 $templateId = (int)$state['TEMPLATE_ID'];
287 if (isset($templateIds[$templateId]))
288 {
289 continue;
290 }
291 $templateIds[$templateId] = true;
292
293 $state['TEMPLATE_CONSTANTS'] = \CBPWorkflowTemplateLoader::getTemplateConstants($templateId);
294 $result[] = $state;
295 }
296
297 return $result;
298 }
299
300 public function hasTemplatesOnStartup(?array $complexDocumentId = null): bool
301 {
302 if (!$this->isBpEnabled)
303 {
304 return false;
305 }
306
307 $templates = (
308 $complexDocumentId
309 ? \CBPWorkflowTemplateLoader::searchTemplatesByDocumentType(
310 $this->complexDocumentType, \CBPDocumentEventType::Edit
311 )
312 : \CBPWorkflowTemplateLoader::searchTemplatesByDocumentType(
313 $this->complexDocumentType, \CBPDocumentEventType::Create
314 )
315 );
316
317 return !empty($templates);
318 }
319
320 public function getDocumentStatesWithParameters(int $elementId): array
321 {
322 if ($elementId < 0 || !$this->isBpEnabled)
323 {
324 return [];
325 }
326
327 $states = [];
328 foreach ($this->getDocumentStates($this->getComplexDocumentId($elementId)) as $documentState)
329 {
330 $parameters = $documentState['TEMPLATE_PARAMETERS'] ?? [];
331 if (!empty($parameters))
332 {
333 $states[] = $documentState;
334 }
335 }
336
337 return $states;
338 }
339
341 {
342 $notTuned = [];
343 if ($this->isBpEnabled)
344 {
345 foreach ($this->getDocumentTypeStates() as $state)
346 {
347 $templateId = (int)$state['TEMPLATE_ID'];
348 if (\CBPWorkflowTemplateLoader::isConstantsTuned($templateId))
349 {
350 continue;
351 }
352
353 $notTuned[] = $state;
354 }
355 }
356
357 return $notTuned;
358 }
359
361 {
362 $response = new Response();
363
364 $properties = [];
365 if ($this->isBpEnabled)
366 {
367 foreach ($this->getDocumentTypeStates() as $state)
368 {
369 $templateId = (int)$state['TEMPLATE_ID'];
370 if (array_key_exists($templateId, $request) && is_array($state['TEMPLATE_CONSTANTS']))
371 {
372 $errors = [];
373 $values = \CBPWorkflowTemplateLoader::checkWorkflowParameters(
374 $state['TEMPLATE_CONSTANTS'],
375 $request[$templateId] ?? [],
376 $this->complexDocumentType,
377 $errors
378 );
379
380 foreach ($errors as $error)
381 {
382 if (!empty($error['message']))
383 {
384 $response->addError(new Error($error['message']));
385 }
386 }
387
388 if (!$errors)
389 {
390 foreach ($state['TEMPLATE_CONSTANTS'] as $id => $property)
391 {
392 $property['Default'] = $values[$id] ?? null;
393 $properties[$templateId][$id] = $property;
394 }
395 }
396 }
397 }
398
399 if ($response->isSuccess())
400 {
401 foreach ($properties as $templateId => $constants)
402 {
403 \CBPWorkflowTemplateLoader::update($templateId, ['CONSTANTS' => $constants]);
404 }
405 }
406 }
407
408 return $response;
409 }
410}
if(!Loader::includeModule('catalog')) if(!AccessController::getCurrent() ->check(ActionDictionary::ACTION_PRICE_EDIT)) if(!check_bitrix_sessid()) $request
Определения catalog_reindex.php:36
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
getParameterValuesFromRequest(array $request, int $elementId)
Определения WorkflowService.php:147
startWorkflows(StartWorkflowsRequest $request)
Определения WorkflowService.php:178
getDocumentStates(?array $complexDocumentId)
Определения WorkflowService.php:248
setConstants(array $request)
Определения WorkflowService.php:360
canUserStartWorkflow(int $currentUserId, int $elementId, int $sectionId=0)
Определения WorkflowService.php:75
getComplexDocumentId(int $elementId)
Определения WorkflowService.php:233
getDocumentStatesWithParameters(int $elementId)
Определения WorkflowService.php:320
canUserWriteDocument(int $elementId, int $userId, array $userGroups)
Определения WorkflowService.php:50
getNotRunningDocumentStates(int $elementId)
Определения WorkflowService.php:258
hasTemplatesOnStartup(?array $complexDocumentId=null)
Определения WorkflowService.php:300
getSignedDocument(int $elementId)
Определения WorkflowService.php:40
hasParameters(int $elementId)
Определения WorkflowService.php:126
__construct(array $iBlockInfo)
Определения WorkflowService.php:20
Определения error.php:15
static getDocumentComplexId($iblockType, $documentId)
Определения bizprocdocument.php:54
static generateDocumentComplexType($iblockType, $iblockId)
Определения bizprocdocument.php:41
const StartWorkflow
Определения constants.php:212
const WriteDocument
Определения constants.php:215
const Edit
Определения constants.php:154
const Create
Определения constants.php:153
$templateId
Определения component_props2.php:51
$template
Определения file_edit.php:49
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$errors
Определения iblock_catalog_edit.php:74
$response
Определения result.php:21
$canWrite
Определения options.php:10
$error
Определения subscription_card_product.php:20