1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
starter.php
См. документацию.
1<?php
2
3namespace Bitrix\Bizproc\Controller\Workflow;
4
5use Bitrix\Bizproc\Api\Request\WorkflowTemplateService\PrepareParametersRequest;
6use Bitrix\Bizproc\Api\Request\WorkflowTemplateService\PrepareStartParametersRequest;
7use Bitrix\Bizproc\Api\Request\WorkflowTemplateService\SetConstantsRequest;
8use Bitrix\Bizproc\Api\Request\WorkflowService\StartWorkflowRequest;
9use Bitrix\Bizproc\Api\Service\WorkflowService;
10use Bitrix\Bizproc\Api\Service\WorkflowTemplateService;
11use Bitrix\Bizproc\Error;
12use Bitrix\Main\Localization\Loc;
13
15{
16 public function getTemplatesAction(): ?array
17 {
18 if (!$this->checkBizprocFeature())
19 {
20 return null;
21 }
22
23 $complexDocumentType = $this->getComplexDocumentType();
24 if (!$complexDocumentType)
25 {
26 return null;
27 }
28
29 $complexDocumentId = null;
30 if ($this->getRequest()->get('signedDocumentId'))
31 {
32 $complexDocumentId = $this->getComplexDocumentId();
33 if (!$complexDocumentId)
34 {
35 return null;
36 }
37
38 if (!$this->checkDocumentTypeMatchDocumentId($complexDocumentType, $complexDocumentId))
39 {
40 return null;
41 }
42 }
43
44 return [
45 'templates' => (
46 \CBPDocument::getTemplatesForStart($this->getCurrentUserId(), $complexDocumentType, $complexDocumentId)
47 ),
48 ];
49 }
50
51 public function startWorkflowAction(int $templateId, ?int $startDuration = null): ?array
52 {
53 if (!$this->checkBizprocFeature())
54 {
55 return null;
56 }
57
58 $complexDocumentType = $this->getComplexDocumentType();
59 if (!$complexDocumentType)
60 {
61 return null;
62 }
63
64 $complexDocumentId = $this->getComplexDocumentId();
65 if (!$complexDocumentId)
66 {
67 return null;
68 }
69
70 if (!$this->checkDocumentTypeMatchDocumentId($complexDocumentType, $complexDocumentId))
71 {
72 return null;
73 }
74
75 $templateService = new WorkflowTemplateService();
76 $workflowParameters = $templateService->prepareStartParameters(
78 templateId: $templateId,
79 complexDocumentType: $complexDocumentType,
80 requestParameters: array_merge(
81 $this->getRequest()->toArray(),
82 $this->getRequest()->getFileList()->toArray()
83 ),
84 targetUserId: $this->getCurrentUserId(),
85 )
86 );
87
88 if (!$workflowParameters->isSuccess())
89 {
90 $this->addErrors($workflowParameters->getErrors());
91
92 return null;
93 }
94
95 $workflowService = new WorkflowService();
96 $startWorkflow = $workflowService->startWorkflow(
98 userId: $this->getCurrentUserId(),
99 targetUserId: $this->getCurrentUserId(),
100 templateId: $templateId,
101 complexDocumentId: $complexDocumentId,
102 parameters: $workflowParameters->getParameters(),
103 startDuration: $startDuration >= 0 ? $startDuration : null,
104 )
105 );
106
107 if (!$startWorkflow->isSuccess())
108 {
109 $this->addErrors($startWorkflow->getErrors());
110
111 return null;
112 }
113
114 return ['workflowId' => $startWorkflow->getWorkflowId()];
115 }
116
117 public function checkParametersAction(int $autoExecuteType): ?array
118 {
119 if (!$this->checkBizprocFeature())
120 {
121 return null;
122 }
123
124 if ($autoExecuteType < 0)
125 {
126 $this->addError(new Error(
127 Loc::getMessage('BIZPROC_LIB_API_CONTROLLER_WORKFLOW_STARTER_ERROR_INCORRECT_AUTO_EXECUTE_TYPE') ?? ''
128 ));
129
130 return null;
131 }
132
133 $parametersDocumentType = $this->getComplexDocumentType();
134 if (!$parametersDocumentType)
135 {
136 return null;
137 }
138
139 $canStart = false;
140 if ($this->getRequest()->get('signedDocumentId'))
141 {
142 $canStart = \CBPDocument::canUserOperateDocument(
144 $this->getCurrentUserId(),
145 $this->getComplexDocumentId(),
146 );
147 }
148
149 if (!$canStart
150 && !\CBPDocument::canUserOperateDocumentType(
152 $this->getCurrentUserId(),
153 $parametersDocumentType,
154 )
155 )
156 {
157 $this->addError(new Error(
158 Loc::getMessage('BIZPROC_LIB_API_CONTROLLER_WORKFLOW_STARTER_ERROR_ACCESS_DENIED') ?? ''
159 ));
160
161 return null;
162 }
163
164 $parameters = [];
165 $hasErrors = false;
166 foreach (\CBPWorkflowTemplateLoader::getDocumentTypeStates($parametersDocumentType, $autoExecuteType) as $template)
167 {
168 if (is_array($template['TEMPLATE_PARAMETERS']) && $template['TEMPLATE_PARAMETERS'])
169 {
170 $parameters[$template['TEMPLATE_ID']] =
171 $this->prepareWorkflowParameters(
172 $template['TEMPLATE_PARAMETERS'],
173 $parametersDocumentType,
174 "bizproc{$template['TEMPLATE_ID']}_",
175 )
176 ;
177
178 if ($parameters[$template['TEMPLATE_ID']] === null)
179 {
180 $hasErrors = true;
181 }
182 }
183 }
184
185 if ($hasErrors)
186 {
187 return null;
188 }
189
190 return ['parameters' => \CBPDocument::signParameters($parameters)];
191 }
192
193 public function setConstantsAction(int $templateId): ?array
194 {
195 if (!$this->checkBizprocFeature())
196 {
197 return null;
198 }
199
200 $parametersDocumentType = $this->getComplexDocumentType();
201 if (!$parametersDocumentType)
202 {
203 return null;
204 }
205
206 $request = $this->getRequest();
207
208 $response =
210 ->setConstants(
212 templateId: $templateId,
213 requestConstants: array_merge($request->toArray(), $request->getFileList()->toArray()),
214 complexDocumentType: $parametersDocumentType,
215 userId: $this->getCurrentUserId(),
216 )
217 )
218 ;
219
220 if ($response->isSuccess())
221 {
222 return ['success' => true];
223 }
224
225 $this->addErrors($response->getErrors());
226
227 return null;
228 }
229
230 private function getComplexDocumentType(): ?array
231 {
232 $request = $this->getRequest();
233
234 $signedDocumentType = $request->get('signedDocumentType');
235 if (!is_string($signedDocumentType))
236 {
237 $this->addError(new Error(
238 Loc::getMessage('BIZPROC_LIB_API_CONTROLLER_WORKFLOW_STARTER_ERROR_INCORRECT_DOCUMENT_TYPE') ?? ''
239 ));
240
241 return null;
242 }
243
244 $parametersDocumentType = \CBPDocument::unSignDocumentType($signedDocumentType);
245
246 try
247 {
248 \CBPHelper::parseDocumentId($parametersDocumentType);
249 }
250 catch (\CBPArgumentNullException $e)
251 {
253
254 return null;
255 }
256
257 return $parametersDocumentType;
258 }
259
260 private function getComplexDocumentId(): ?array
261 {
262 $request = $this->getRequest();
263
264 $signedDocumentId = $request->get('signedDocumentId');
265 if (!is_string($signedDocumentId))
266 {
267 $this->addError(new Error(
268 Loc::getMessage('BIZPROC_LIB_API_CONTROLLER_WORKFLOW_STARTER_ERROR_INCORRECT_DOCUMENT_ID') ?? ''
269 ));
270
271 return null;
272 }
273
274 $parametersDocumentId = \CBPDocument::unSignDocumentType($signedDocumentId);
275
276 try
277 {
278 \CBPHelper::parseDocumentId($parametersDocumentId);
279 }
280 catch (\CBPArgumentNullException $e)
281 {
283
284 return null;
285 }
286
287 return $parametersDocumentId;
288 }
289
290 private function checkDocumentTypeMatchDocumentId(array $parametersDocumentType, array $parametersDocumentId): bool
291 {
292 if (
293 $parametersDocumentType[0] === $parametersDocumentId[0]
294 && $parametersDocumentType[1] === $parametersDocumentId[1]
295 )
296 {
297 return true;
298 }
299
300 $this->addError(new Error(
301 Loc::getMessage('BIZPROC_LIB_API_CONTROLLER_WORKFLOW_STARTER_ERROR_DOC_TYPE_DONT_MATCH_DOC_ID') ?? ''
302 ));
303
304 return false;
305 }
306
307 private function checkBizprocFeature(): bool
308 {
309 if (!\CBPRuntime::isFeatureEnabled())
310 {
311 $this->addError(new Error(
312 Loc::getMessage('BIZPROC_LIB_API_CONTROLLER_WORKFLOW_STARTER_ERROR_BIZPROC_FEATURE_DISABLED') ?? '')
313 );
314
315 return false;
316 }
317
318 return true;
319 }
320
321 private function getCurrentUserId(): int
322 {
323 return (int)($this->getCurrentUser()?->getId());
324 }
325
326 private function prepareWorkflowParameters(
327 array $templateParameters,
328 array $parametersDocumentType,
329 string $keyPrefix = '',
330 ): ?array
331 {
332 $request = $this->getRequest();
333 $allRequestParameters = array_merge($request->toArray(), $request->getFileList()->toArray());
334
335 $requestParameters = [];
336 foreach($templateParameters as $key => $property)
337 {
338 $searchKey = $keyPrefix . $key;
339 $requestParameters[$key] = $allRequestParameters[$searchKey] ?? null;
340 }
341
342 $parameters = (new WorkflowTemplateService())
343 ->prepareParameters(
344 new PrepareParametersRequest(
345 templateParameters: $templateParameters,
346 requestParameters: $requestParameters,
347 complexDocumentType: $parametersDocumentType,
348 )
349 )
350 ;
351
352 if ($parameters->isSuccess())
353 {
354 return $parameters->getParameters();
355 }
356
357 $this->addErrors($parameters->getErrors());
358
359 return null;
360 }
361}
if(!Loader::includeModule('catalog')) if(!AccessController::getCurrent() ->check(ActionDictionary::ACTION_PRICE_EDIT)) if(!check_bitrix_sessid()) $request
Определения catalog_reindex.php:36
static addError($error)
Определения base.php:278
static addErrors(array $errors)
Определения base.php:287
setConstantsAction(int $templateId)
Определения starter.php:193
checkParametersAction(int $autoExecuteType)
Определения starter.php:117
startWorkflowAction(int $templateId, ?int $startDuration=null)
Определения starter.php:51
Определения error.php:15
static createFromThrowable(Throwable $exception)
Определения error.php:45
const StartWorkflow
Определения constants.php:212
$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
trait Error
Определения error.php:11
if(empty($signedUserToken)) $key
Определения quickway.php:257
$response
Определения result.php:21