Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
WorkflowService.php
1<?php
2
4
5use Bitrix\Bizproc\Workflow\Entity\EO_WorkflowMetadata;
11
12final class WorkflowService
13{
14 private string $iBlockTypeId;
15 private bool $isBpEnabled;
16 private array $complexDocumentType;
17
18 public function __construct(array $iBlockInfo)
19 {
20 $this->iBlockTypeId = (string)($iBlockInfo['IBLOCK_TYPE_ID'] ?? '');
21
22 $this->isBpEnabled = (
23 Loader::includeModule('bizproc')
24 && \CLists::isBpFeatureEnabled($this->iBlockTypeId)
25 && (isset($iBlockInfo['BIZPROC']) && $iBlockInfo['BIZPROC'] === 'Y') // $iBlockInfo['BIZPROC'] != 'N'
26 );
27
28 $this->complexDocumentType = \BizprocDocument::generateDocumentComplexType(
29 $this->iBlockTypeId,
30 max((int)($iBlockInfo['ID'] ?? 0), 0)
31 );
32 }
33
34 public function isBpEnabled(): bool
35 {
36 return $this->isBpEnabled;
37 }
38
39 public function canUserWriteDocument(int $elementId, int $userId, array $userGroups): bool
40 {
41 if ($elementId < 0 || $userId <= 0)
42 {
43 return false;
44 }
45
46 $canWrite = true;
47 if ($this->isBpEnabled)
48 {
49 $complexDocumentId = $this->getComplexDocumentId($elementId);
50
51 $operation = \CBPCanUserOperateOperation::WriteDocument;
52 $parameters = [
53 'AllUserGroups' => $userGroups,
54 'DocumentStates' => $this->getDocumentStates($complexDocumentId)
55 ];
56
57 $canWrite = (
58 $elementId > 0
59 ? \CBPDocument::canUserOperateDocument($operation, $userId, $complexDocumentId, $parameters)
60 : \CBPDocument::canUserOperateDocumentType($operation, $userId, $this->complexDocumentType, $parameters)
61 );
62 }
63
64 return $canWrite;
65 }
66
67 public function canUserStartWorkflow(int $currentUserId, int $elementId, int $sectionId = 0): bool
68 {
69 if ($currentUserId <= 0 || $elementId < 0 || $sectionId < 0)
70 {
71 return false;
72 }
73
74 $canStart = true;
75 if ($this->isBpEnabled)
76 {
77 $documentStates = $this->getDocumentStates($this->getComplexDocumentId($elementId));
78
79 $canStart = \CBPDocument::canUserOperateDocumentType(
80 \CBPCanUserOperateOperation::StartWorkflow,
81 $currentUserId,
82 $this->complexDocumentType,
83 ['sectionId' => $sectionId, 'DocumentStates' => $documentStates]
84 );
85 }
86
87 return $canStart;
88 }
89
90 public function isConstantsTuned(): bool
91 {
92 $isConstantsTuned = true;
93 if ($this->isBpEnabled)
94 {
95 $templates = array_merge(
96 \CBPWorkflowTemplateLoader::searchTemplatesByDocumentType(
97 $this->complexDocumentType, \CBPDocumentEventType::Create
98 ),
99 \CBPWorkflowTemplateLoader::searchTemplatesByDocumentType(
100 $this->complexDocumentType, \CBPDocumentEventType::Edit
101 ),
102 );
103
104 foreach ($templates as $template)
105 {
106 if (!\CBPWorkflowTemplateLoader::isConstantsTuned($template['ID']))
107 {
108 $isConstantsTuned = false;
109
110 break;
111 }
112 }
113 }
114
115 return $isConstantsTuned;
116 }
117
118 public function hasParameters(int $elementId): bool
119 {
120 $hasParameters = false;
121 if ($this->isBpEnabled)
122 {
123 $states = $this->getDocumentStates($this->getComplexDocumentId($elementId));
124 foreach ($states as $state)
125 {
126 $parameters = $state['TEMPLATE_PARAMETERS'] ?? [];
127 if (!empty($parameters) && is_array($parameters))
128 {
129 $hasParameters = true;
130
131 break;
132 }
133 }
134 }
135
136 return $hasParameters;
137 }
138
139 public function getParameterValuesFromRequest(array $request, int $elementId): GetParameterValuesResponse
140 {
141 $response = new GetParameterValuesResponse();
142
143 $parameters = [];
144 if ($this->isBpEnabled && $elementId >= 0)
145 {
146 $documentStates = $this->getDocumentStates($this->getComplexDocumentId($elementId));
147 foreach ($documentStates as $state)
148 {
149 if (empty($state['ID']))
150 {
151 $errors = [];
152 $parameters[$state['TEMPLATE_ID']] = \CBPWorkflowTemplateLoader::checkWorkflowParameters(
153 $state['TEMPLATE_PARAMETERS'] ?? [],
154 $request[$state['TEMPLATE_ID']] ?? [],
155 $this->complexDocumentType,
156 $errors
157 );
158
159 foreach ($errors as $error)
160 {
161 $response->addError(new Error($error['message']));
162 }
163 }
164 }
165 }
166
167 return $response->setParameters($parameters);
168 }
169
171 {
172 $response = new StartWorkflowsResponse();
173
174 $workflowIds = [];
175 if ($request->elementId <= 0 || $request->currentUserId <= 0)
176 {
177 // todo: loc
178 $response->addError(new Error('incorrect input data'));
179 }
180
181 if ($this->isBpEnabled && $response->isSuccess())
182 {
183 $complexDocumentId = $this->getComplexDocumentId($request->elementId);
184 $documentStates = $this->getDocumentStates($request->isNewElement ? null : $complexDocumentId);
185 foreach ($documentStates as $state)
186 {
187 if (empty($state['ID']))
188 {
189 $errors = [];
190
191 $startWorkflowParameters = [
192 \CBPDocument::PARAM_TAGRET_USER => 'user_' . $request->currentUserId,
193 \CBPDocument::PARAM_MODIFIED_DOCUMENT_FIELDS => $request->changedFields,
194 ];
195
196 $workflowIds[$state['TEMPLATE_ID']] = \CBPDocument::startWorkflow(
197 $state['TEMPLATE_ID'],
198 $complexDocumentId,
199 array_merge($request->parameters[$state['TEMPLATE_ID']] ?? [], $startWorkflowParameters),
200 $errors
201 );
202
203 if (!$errors && isset($request->timeToStart))
204 {
205 $metadata = new EO_WorkflowMetadata();
206 $metadata->setWorkflowId($workflowIds[$state['TEMPLATE_ID']]);
207 $metadata->setStartDuration($request->timeToStart);
208 $metadata->save();
209 }
210
211 foreach ($errors as $error)
212 {
213 $response->addError(new Error($error['message']));
214 }
215 }
216 }
217 }
218
219 return $response->setWorkflowIds($workflowIds);
220 }
221
222 public function getComplexDocumentId(int $elementId): ?array
223 {
224 return ($elementId > 0 ? \BizprocDocument::getDocumentComplexId($this->iBlockTypeId, $elementId) : null);
225 }
226
227 public function getDocumentStates(?array $complexDocumentId): array
228 {
229 if ($this->isBpEnabled)
230 {
231 return \CBPDocument::getDocumentStates($this->complexDocumentType, $complexDocumentId);
232 }
233
234 return [];
235 }
236}
getParameterValuesFromRequest(array $request, int $elementId)
startWorkflows(StartWorkflowsRequest $request)
getDocumentStates(?array $complexDocumentId)
canUserStartWorkflow(int $currentUserId, int $elementId, int $sectionId=0)
canUserWriteDocument(int $elementId, int $userId, array $userGroups)
static getDocumentComplexId($iblockType, $documentId)