1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
WorkflowService.php
См. документацию.
1<?php
2
3namespace Bitrix\Bizproc\Api\Service;
4
5use Bitrix\Bizproc\Api\Enum\Template\WorkflowTemplateType;
6use Bitrix\Bizproc\Api\Request;
7use Bitrix\Bizproc\Api\Request\WorkflowAccessService\CheckStartWorkflowRequest;
8use Bitrix\Bizproc\Api\Request\WorkflowService\StartWorkflowRequest;
9use Bitrix\Bizproc\Api\Request\WorkflowService\TerminateWorkflowRequest;
10use Bitrix\Bizproc\Api\Request\WorkflowService\TerminateByTemplateRequest;
11use Bitrix\Bizproc\Api\Response\Error;
12use Bitrix\Bizproc\Api\Response\WorkflowService\StartWorkflowResponse;
13use Bitrix\Bizproc\Api\Response\WorkflowService\TerminateWorkflowResponse;
14use Bitrix\Bizproc\Workflow\Entity\EO_WorkflowMetadata;
15use Bitrix\Bizproc\Workflow\Entity\WorkflowInstanceTable;
16use Bitrix\Main\ArgumentException;
17use Bitrix\Main\Localization\Loc;
18
20{
21 private const PREFIX_LOC_ID = 'BIZPROC_LIB_API_WORKFLOW_SERVICE_';
22 private const UNKNOWN_CREATE_WORKFLOW_ERROR = 'UNKNOWN_CREATE_WORKFLOW_ERROR';
23
24 private WorkflowAccessService $accessService;
25
26 public function __construct(?WorkflowAccessService $accessService = null)
27 {
28 $this->accessService = $accessService ?? new WorkflowAccessService();
29 }
30
32 {
34
35 if ($request->checkAccess)
36 {
37 $accessRequest = new CheckStartWorkflowRequest(
38 userId: $request->userId,
39 complexDocumentId: $request->complexDocumentId,
40 parameters: [
41 \CBPDocument::PARAM_TAGRET_USER => 'user_' . $request->targetUserId,
42 'DocumentCategoryId' => $request->documentCategoryId,
43 'WorkflowTemplateId' => $request->templateId,
44 ],
45 );
46
47 $accessResponse = $this->accessService->checkStartWorkflow($accessRequest);
48 if (!$accessResponse->isSuccess())
49 {
50 $response->addErrors($accessResponse->getErrors());
51
52 return $response;
53 }
54 }
55
56 if (isset($request->startDuration) && $request->startDuration < 0)
57 {
58 throw new ArgumentException('Start duration must be non negative');
59 }
60
61 $startWorkflowErrors = [];
62 $instanceId = \CBPDocument::startWorkflow(
63 $request->templateId,
64 $request->complexDocumentId,
65 $request->parameters,
66 $startWorkflowErrors,
67 $request->parentWorkflow,
68 );
69
70 if ($startWorkflowErrors)
71 {
72 foreach ($startWorkflowErrors as $error)
73 {
74 if (is_numeric($error['code']))
75 {
76 $response->addError(new Error($error['message'], (int)$error['code']));
77 }
78 else
79 {
80 $response->addError(new Error($error['message']));
81 }
82 }
83 }
84 elseif (is_null($instanceId))
85 {
86 $response->addError(
87 new Error(Loc::getMessage(static::PREFIX_LOC_ID . static::UNKNOWN_CREATE_WORKFLOW_ERROR))
88 );
89 }
90 else
91 {
92 if (isset($request->startDuration))
93 {
94 $metadata = new EO_WorkflowMetadata();
95
96 $metadata->setWorkflowId($instanceId);
97 $metadata->setStartDuration($request->startDuration);
98 $metadata->save();
99 }
100
101 $response->setWorkflowId($instanceId);
102 }
103
104 return $response;
105 }
106
108 {
110
111 $documentId = \CBPStateService::getStateDocumentId($request->workflowId);
112 if (!$documentId)
113 {
114 return $response->addError(new \Bitrix\Main\Error(
115 Loc::getMessage('BIZPROC_LIB_API_WORKFLOW_SERVICE_COMPLETED')
116 ));
117 }
118
119 $documentStates = \CBPDocument::getActiveStates($documentId);
120
121 if (empty($documentStates[$request->workflowId]))
122 {
123 return $response->addError(new \Bitrix\Main\Error(
124 Loc::getMessage('BIZPROC_LIB_API_WORKFLOW_SERVICE_COMPLETED')
125 ));
126 }
127
128 $canTerminate = \CBPDocument::CanUserOperateDocument(
130 $request->userId,
131 $documentId,
132 ['DocumentStates' => $documentStates]
133 );
134
135 if (!$canTerminate)
136 {
137 $response->addError(new \Bitrix\Main\Error(
138 Loc::getMessage('BIZPROC_LIB_API_WORKFLOW_SERVICE_NO_ACCESS')
139 ));
140
141 return $response;
142 }
143
144 $this->terminateWorkflowById($request->workflowId, $documentId, $response);
145
146 return $response;
147 }
148
150 {
152 $documentStates = \CBPDocument::getActiveStates($request->documentId);
153
154 $canTerminate = \CBPDocument::CanUserOperateDocument(
156 $request->userId,
157 $request->documentId,
158 ['DocumentStates' => $documentStates]
159 );
160
161 if (!$canTerminate)
162 {
163 $response->addError(new \Bitrix\Main\Error(
164 Loc::getMessage('BIZPROC_LIB_API_WORKFLOW_SERVICE_ROBOTS_NO_ACCESS')
165 ));
166
167 return $response;
168 }
169
170 $instanceIds = $this->getWorkflowInstanceIds($request->templateId, $request->documentId);
171
172 if (empty($instanceIds))
173 {
174 $response->addError(new \Bitrix\Main\Error(
175 Loc::getMessage('BIZPROC_LIB_API_WORKFLOW_SERVICE_ROBOTS_NOT_FOUND')
176 ));
177 }
178
179 foreach ($instanceIds as $instanceId)
180 {
181 $this->terminateWorkflowById($instanceId, $request->documentId, $response);
182 }
183
184 return $response;
185 }
186
187 private function getWorkflowInstanceIds(int $templateId, array $documentId): array
188 {
189 $ids = WorkflowInstanceTable::getList([
190 'select' => ['ID'],
191 'filter' => [
192 '=WORKFLOW_TEMPLATE_ID' => $templateId,
193 '=MODULE_ID' => $documentId[0],
194 '=ENTITY' => $documentId[1],
195 '=DOCUMENT_ID' => $documentId[2],
196 '@TEMPLATE.TYPE' => [WorkflowTemplateType::CustomRobots->value, WorkflowTemplateType::Robots->value],
197 ],
198 ])->fetchAll();
199
200 return array_column($ids, 'ID');
201 }
202
203 private function terminateWorkflowById(string $workflowId, array $documentId, TerminateWorkflowResponse $response)
204 {
205 $errors = [];
206 \CBPDocument::TerminateWorkflow($workflowId, $documentId, $errors);
207
208 if (!empty($errors))
209 {
210 foreach ($errors as $error)
211 {
212 $response->addError(new Error($error['message'], $error['code']));
213 }
214 }
215 }
216}
if(!Loader::includeModule('catalog')) if(!AccessController::getCurrent() ->check(ActionDictionary::ACTION_PRICE_EDIT)) if(!check_bitrix_sessid()) $request
Определения catalog_reindex.php:36
terminateWorkflow(TerminateWorkflowRequest $request)
Определения WorkflowService.php:107
startWorkflow(StartWorkflowRequest $request)
Определения WorkflowService.php:31
terminateWorkflowsByTemplate(TerminateByTemplateRequest $request)
Определения WorkflowService.php:149
__construct(?WorkflowAccessService $accessService=null)
Определения WorkflowService.php:26
Определения error.php:15
const StartWorkflow
Определения constants.php:212
const CreateAutomation
Определения constants.php:214
static getStateDocumentId($workflowId)
Определения stateservice.php:75
$templateId
Определения component_props2.php:51
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$errors
Определения iblock_catalog_edit.php:74
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$response
Определения result.php:21
$error
Определения subscription_card_product.php:20