Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
script.php
1<?php
2
4
11
12class Script extends Base
13{
14 public const START_STATUS_NOT_PERMITTED = 'NOT_PERMITTED';
15 public const START_STATUS_NOT_EXISTS = 'NOT_EXISTS';
16 public const START_STATUS_NO_DOCUMENTS = 'NO_DOCUMENTS';
17 public const START_STATUS_FILL_PARAMETERS = 'FILL_PARAMETERS';
18 public const START_STATUS_INVALID_PARAMETERS = 'INVALID_PARAMETERS';
19 public const START_STATUS_QUEUED = 'QUEUED';
20
21 public function startAction($scriptId, array $documentIds, array $parameters = [])
22 {
23 $userId = $this->getCurrentUser()->getId();
24 $documentIds = array_unique($documentIds);
25
26 if (!Manager::checkDocumentIdsLimit($documentIds))
27 {
28 return [
29 'status' => static::START_STATUS_NOT_PERMITTED,
30 'error' => Loc::getMessage(
31 'BIZPROC_CONTROLLER_SCRIPT_ERROR_DOCUMENT_ID_LIMIT',
32 [
33 '#LIMIT#' => Manager::getDocumentIdLimit(),
34 '#SELECTED#' => count($documentIds),
35 ]
36 )
37 ];
38 }
39
40 $script = Manager::getById($scriptId);
41 if (!$script)
42 {
43 return [
44 'status' => static::START_STATUS_NOT_EXISTS,
45 'error' => Loc::getMessage('BIZPROC_CONTROLLER_SCRIPT_NOT_EXISTS')
46 ];
47 }
48 if (!$script->getActive())
49 {
50 return [
51 'status' => static::START_STATUS_NOT_PERMITTED,
52 'error' => Loc::getMessage('BIZPROC_CONTROLLER_SCRIPT_CANT_START_INACTIVE')
53 ];
54 }
55
56 if (!Manager::checkQueuesCount($scriptId))
57 {
58 return [
59 'status' => static::START_STATUS_NOT_PERMITTED,
60 'error' => Loc::getMessage(
61 'BIZPROC_CONTROLLER_SCRIPT_ERROR_QUEUES_LIMIT',
62 [
63 '#LIMIT#' => Manager::getQueuesLimit(),
64 '#CNT#' => Manager::getActiveQueueCountByScriptId($scriptId),
65 ]
66 )
67 ];
68 }
69
70 $script->fill('WORKFLOW_TEMPLATE');
71 $tpl = $script->getWorkflowTemplate();
72 if (!$tpl)
73 {
74 return [
75 'status' => static::START_STATUS_NOT_EXISTS,
76 'error' => Loc::getMessage('BIZPROC_CONTROLLER_SCRIPT_NO_TEMPLATE')
77 ];
78 }
79
80 $templateParameters = $tpl->getParameters();
81 if ($templateParameters)
82 {
83 $parameters = $this->grabParameters($templateParameters, $parameters);
84
85 if (empty($parameters))
86 {
87 return [
88 'status' => static::START_STATUS_FILL_PARAMETERS,
89 'parameters' => self::convertTemplateParameters($templateParameters, $tpl->getDocumentComplexType()),
90 'documentType' => $tpl->getDocumentComplexType(),
91 'scriptName' => $script->getName(),
92 ];
93 }
94 }
95
96 $result = Manager::startScript($scriptId, $userId, $documentIds, $parameters);
97 if (!$result->isSuccess())
98 {
99 $error = $result->getErrors()[0];
100 if ($error->getCode() === StartScriptResult::CODE_NOT_ENOUGH_RIGHTS)
101 {
102 return [
103 'status' => static::START_STATUS_NOT_PERMITTED,
104 'error' => $error->getMessage(),
105 ];
106 }
107 if ($error->getCode() === StartScriptResult::CODE_INVALID_PARAMETERS)
108 {
109 return [
110 'status' => static::START_STATUS_INVALID_PARAMETERS,
111 'error' => $error->getMessage(),
112 ];
113 }
114
115 return [
116 'error' => $error->getMessage(),
117 ];
118 }
119
120 return [
121 'status' => static::START_STATUS_QUEUED,
122 'queueId' => $result->getData()['queueId'],
123 ];
124 }
125
126 public function deleteAction($scriptId)
127 {
128 $userId = $this->getCurrentUser()->getId();
129 $script = Manager::getById($scriptId);
130
131 if (!$script)
132 {
133 return [
134 'error' => Loc::getMessage('BIZPROC_CONTROLLER_SCRIPT_NOT_EXISTS')
135 ];
136 }
137
138 if (!Manager::canUserEditScript($script->getId(), $userId))
139 {
140 return [
141 'error' => Loc::getMessage('BIZPROC_CONTROLLER_SCRIPT_CANT_DELETE_SCRIPT')
142 ];
143 }
144
145 $result = Manager::deleteScript($scriptId);
146
147 if (!$result->isSuccess())
148 {
149 return ['error' => Loc::getMessage('BIZPROC_CONTROLLER_SCRIPT_CANT_DELETE_RUNNING_SCRIPT')];
150 }
151
152 return ['status' => 'success'];
153 }
154
155 public function activateAction(int $scriptId)
156 {
157 $script = Manager::getById($scriptId);
158
159 if (!$script)
160 {
161 return [
162 'error' => Loc::getMessage('BIZPROC_CONTROLLER_SCRIPT_NOT_EXISTS')
163 ];
164 }
165
166 $userId = $this->getCurrentUser()->getId();
167 if (!Manager::canUserEditScript($script->getId(), $userId))
168 {
169 return [
170 'error' => Loc::getMessage('BIZPROC_CONTROLLER_SCRIPT_CANT_UPDATE_SCRIPT')
171 ];
172 }
173
174 Manager::activateScript($scriptId);
175
176 return ['status' => 'success'];
177 }
178
179 public function deactivateAction(int $scriptId)
180 {
181 $script = Manager::getById($scriptId);
182
183 if (!$script)
184 {
185 return [
186 'error' => Loc::getMessage('BIZPROC_CONTROLLER_SCRIPT_NOT_EXISTS')
187 ];
188 }
189
190 $userId = $this->getCurrentUser()->getId();
191 if (!Manager::canUserEditScript($script->getId(), $userId))
192 {
193 return [
194 'error' => Loc::getMessage('BIZPROC_CONTROLLER_SCRIPT_CANT_UPDATE_SCRIPT')
195 ];
196 }
197
198 Manager::deactivateScript($scriptId);
199
200 return ['status' => 'success'];
201 }
202
203 public function terminateQueueAction(int $queueId)
204 {
205 $userId = (int)$this->getCurrentUser()->getId();
206 $queue = Manager::getQueueById($queueId);
207
208 if (!$queue)
209 {
210 return [
211 'error' => Loc::getMessage('BIZPROC_CONTROLLER_SCRIPT_NOT_EXISTS')
212 ];
213 }
214
215 if ($userId !== $queue->getStartedBy() && !Manager::canUserStartScript($queue->getScriptId(), $userId))
216 {
217 return [
218 'error' => Loc::getMessage('BIZPROC_CONTROLLER_SCRIPT_CANT_TERMINATE')
219 ];
220 }
221
222 if ($queue->getStatus() > Queue\Status::EXECUTING)
223 {
224 return [
225 'error' => Loc::getMessage('BIZPROC_CONTROLLER_SCRIPT_CANT_TERMINATE_FINISHED')
226 ];
227 }
228
229 Manager::terminateQueue($queueId, $userId);
230 return ['status' => 'success'];
231 }
232
233 public function execQueueAction(int $queueId)
234 {
235 $queue = Manager::getQueueById($queueId);
236
237 if (!$queue)
238 {
239 return [
240 'error' => Loc::getMessage('BIZPROC_CONTROLLER_SCRIPT_NOT_EXISTS')
241 ];
242 }
243
244 //emulate Stepper step
245 $stepper = Queue\Stepper::createInstance();
246 $option = [];
247 $stepper->setOuterParams([$queueId, $queue->getScriptId()]);
248 $result = $stepper->execute($option);
249
250 return ['status' => 'success', 'finished' => ($result === $stepper::FINISH_EXECUTION)];
251 }
252
253 public function deleteQueueAction(int $queueId)
254 {
255 $userId = (int)$this->getCurrentUser()->getId();
256 $queue = Manager::getQueueById($queueId);
257
258 if (!$queue)
259 {
260 return [
261 'error' => Loc::getMessage('BIZPROC_CONTROLLER_SCRIPT_NOT_EXISTS')
262 ];
263 }
264
265 if ($userId !== $queue->getStartedBy() && !Manager::canUserStartScript($queue->getScriptId(), $userId))
266 {
267 return [
268 'error' => Loc::getMessage('BIZPROC_CONTROLLER_SCRIPT_CANT_DELETE_QUEUE')
269 ];
270 }
271
272 Manager::deleteQueue($queueId, $userId);
273 return ['status' => 'success'];
274 }
275
276 private static function convertTemplateParameters(array $parameters, array $documentType): array
277 {
278 $result = [];
279 foreach ($parameters as $id => $parameter)
280 {
281 $parameter = FieldType::normalizeProperty($parameter);
282 $parameter['Id'] = $id;
283
284 if ($parameter['Type'] === 'user')
285 {
286 $parameter['Default'] = \CBPHelper::UsersArrayToString(
287 $parameter['Default'], [], $documentType
288 );
289 }
290
291 $result[] = $parameter;
292 }
293 return $result;
294 }
295
296 private function getFileParameters(): array
297 {
298 $parameters = [];
299
300 foreach ($this->request->getFileList()->getValues() as $key => $value)
301 {
302 if (array_key_exists('name', $value))
303 {
304 if (is_array($value['name']))
305 {
306 $ks = array_keys($value["name"]);
307 for ($i = 0, $cnt = count($ks); $i < $cnt; $i++)
308 {
309 $ar = array();
310 foreach ($value as $k1 => $v1)
311 $ar[$k1] = $v1[$ks[$i]];
312
313 $parameters[$key][] = $ar;
314 }
315 }
316 else
317 {
318 $parameters[$key] = $value;
319 }
320 }
321 }
322
323 return $parameters;
324 }
325
330 private function grabParameters(array $templateParameters, array $parameters): array
331 {
332 $parameters += $this->getFileParameters();
333
334 foreach (array_keys($templateParameters) as $paramId)
335 {
336 if (!array_key_exists($paramId, $parameters) && $this->request->getPost($paramId) !== null)
337 {
338 $parameters[$paramId] = $this->request->getPost($paramId);
339 }
340 }
341
342 return $parameters;
343 }
344}
terminateQueueAction(int $queueId)
Definition script.php:203
startAction($scriptId, array $documentIds, array $parameters=[])
Definition script.php:21
deactivateAction(int $scriptId)
Definition script.php:179
static normalizeProperty($property)
static getQueueById(int $queueId)
Definition manager.php:66
static checkQueuesCount(int $scriptId)
Definition manager.php:623
static checkDocumentIdsLimit(array $documentIds)
Definition manager.php:613
static getActiveQueueCountByScriptId(int $scriptId)
Definition manager.php:429
static canUserStartScript(int $scriptId, int $userId)
Definition manager.php:450
static startScript(int $scriptId, int $userId, array $documentIds, array $parameters=[])
Definition manager.php:344
static deleteQueue(int $queueId, int $userId)
Definition manager.php:444
static canUserEditScript(int $scriptId, int $userId)
Definition manager.php:474
static terminateQueue(int $queueId, int $userId)
Definition manager.php:439
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29