Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
TaskService.php
1<?php
2
4
18
20{
21 public function __construct(
22 private TaskAccessService $accessService
23 // private UserService $userService,
24 )
25 {
26 }
27
29 {
30 $delegateResponse = new DelegateTasksResponse();
31
32 $tasksToDelegate = null;
33 try
34 {
35 $tasksToDelegate = TasksToBeDelegated::createFromRequest($request);
36 }
38 {
39 $errorMessage =
40 in_array($e->getParameter(), ['fromUserId', 'toUserId'], true)
41 ? Loc::getMessage('BIZPROC_LIB_API_TASK_SERVICE_DELEGATE_TASK_ERROR_INCORRECT_USER_ID')
42 : $e->getMessage()
43 ;
44 $delegateResponse->addError(new Error($errorMessage));
45 }
46 catch (ArgumentException $e)
47 {
48 $delegateResponse->addError(
49 new Error(Loc::getMessage('BIZPROC_LIB_API_TASK_SERVICE_DELEGATE_TASK_ERROR_NO_TASKS'))
50 );
51 }
52 if (!$tasksToDelegate || !$delegateResponse->isSuccess())
53 {
54 return $delegateResponse;
55 }
56
57 $checkAccessResult = $this->accessService->checkDelegateTask(
58 $tasksToDelegate->getToUserId(),
59 $tasksToDelegate->getFromUserId()
60 );
61 if (!$checkAccessResult->isSuccess())
62 {
63 return $delegateResponse->addErrors($checkAccessResult->getErrors());
64 }
65
66 $allowedDelegationType = $checkAccessResult->getAllowedDelegationTypes();
67
68 $errors = [];
69 $isDelegated = \CBPDocument::delegateTasks(
70 $tasksToDelegate->getFromUserId(),
71 $tasksToDelegate->getToUserId(),
72 $tasksToDelegate->getTaskIds(),
73 $errors,
74 $allowedDelegationType
75 );
76
77 if (!$isDelegated && !$errors)
78 {
79 $errors = [Loc::getMessage('BIZPROC_LIB_API_TASK_SERVICE_DELEGATE_TASK_ERROR_TASKS_NOT_FOUND')];
80 }
81 foreach ($errors as $errorMessage)
82 {
83 $delegateResponse->addError(new Error($errorMessage));
84 }
85
86 if ($delegateResponse->isSuccess())
87 {
88 $delegateResponse->setData([
89 'successMessage' => Loc::getMessage(
90 'BIZPROC_LIB_API_TASK_SERVICE_DELEGATE_TASK_SUCCESS_MESSAGE',
91 ['#USER_NAME#' => \CBPHelper::convertUserToPrintableForm($tasksToDelegate->getToUserId(), '', false)]
92 ),
93 ]);
94 }
95
96 return $delegateResponse;
97 }
98
100 {
101 // $getCurrentUserResult = $this->userService->getCurrentUser();
102 // if (!$getCurrentUserResult->isSuccess())
103 // {
104 // return GetUserTasksResult::createError($getCurrentUserResult->getErrors()[0]);
105 // }
106
107 $getTasksResponse = new GetUserTasksResponse();
108
109 $tasksToGet = null;
110 try
111 {
112 $tasksToGet = TasksToGet::createFromRequest($request);
113 }
114 catch (ArgumentOutOfRangeException $exception)
115 {
116 // todo: user friendly error message
117 // use ::createError?
118 $getTasksResponse->addError(new Error($exception->getMessage()));
119 }
120 if (!$tasksToGet || !$getTasksResponse->isSuccess())
121 {
122 return $getTasksResponse;
123 }
124
125 $checkAccessResult = $this->accessService->checkViewTasks($tasksToGet->getTargetUserId());
126 if (!$checkAccessResult->isSuccess())
127 {
128 return $getTasksResponse->addErrors($checkAccessResult->getErrors());
129 }
130
131 $tasksIterator = \CBPTaskService::getList(
132 $tasksToGet->getSort(),
133 $tasksToGet->getFilter(),
134 false,
135 [
136 'nPageSize' => $tasksToGet->getLimit(),
137 'iNumPage' => (int)($tasksToGet->getOffset() / $tasksToGet->getLimit()) + 1,
138 ],
139 $tasksToGet->buildSelectFields()
140 );
141 $taskList = [];
142 while ($task = $tasksIterator->fetch())
143 {
144 // todo: create DTO Task
145 $task['STATUS'] = (int)$task['STATUS'];
146 $task['MODIFIED'] = FormatDateFromDB($task['MODIFIED']);
147 $documentId =
148 is_array($task['PARAMETERS']['DOCUMENT_ID'] ?? null)
149 ? $task['PARAMETERS']['DOCUMENT_ID']
150 : null
151 ;
152 $task['DOCUMENT_URL'] = $documentId ? \CBPDocument::getDocumentAdminPage($documentId) : '';
153
154 $task['MODULE_ID'] = $documentId ? $documentId[0] : '';
155 $task['ENTITY'] = $documentId ? $documentId[1] : '';
156 $task['DOCUMENT_ID'] = $documentId ? $documentId[2] : '';
157 $task['COMPLEX_DOCUMENT_ID'] = $documentId;
158
159 if (isset($arRecord['WORKFLOW_TEMPLATE_NAME']))
160 {
161 $task['WORKFLOW_NAME'] = $task['WORKFLOW_TEMPLATE_NAME']; // compatibility
162 }
163 if (isset($arRecord['WORKFLOW_STARTED']))
164 {
165 $task['WORKFLOW_STARTED'] = FormatDateFromDB($task['WORKFLOW_STARTED']);
166 }
167
168 $controls = \CBPDocument::getTaskControls($task);
169
170 $task['BUTTONS'] = $controls['BUTTONS'] ?? null;
171 $task['FIELDS'] = $controls['FIELDS'] ?? null;
172
173 $taskList[] = $task;
174 }
175
176 return $getTasksResponse->setData(['tasks' => $taskList]);
177 }
178
179 public function doTask(DoTaskRequest $request)
180 {
182 $task = false;
183
184 $checkAccessResult = $this->accessService->checkViewTasks($request->userId);
185 if (!$checkAccessResult->isSuccess())
186 {
187 return $result->addErrors($checkAccessResult->getErrors());
188 }
189
190 if ($request->taskId > 0)
191 {
192 $task = \CBPTaskService::GetList(
193 [],
194 [
195 "ID" => $request->taskId,
196 "USER_ID" => $request->userId
197 ],
198 false,
199 false,
200 [
201 "ID",
202 "WORKFLOW_ID",
203 "ACTIVITY",
204 "ACTIVITY_NAME",
205 "MODIFIED",
206 "OVERDUE_DATE",
207 "NAME",
208 "DESCRIPTION",
209 "PARAMETERS",
210 "USER_STATUS",
211 ]
212 )->fetch();
213 }
214
215 if (!$task)
216 {
217 return $result->addError(new Error(Loc::getMessage('BIZPROC_LIB_API_TASK_SERVICE_DO_TASK_ERROR_NO_TASK')));
218 }
219
220 if ((int)$task['USER_STATUS'] !== \CBPTaskUserStatus::Waiting)
221 {
222 return $result->addError(new Error(Loc::getMessage('BIZPROC_LIB_API_TASK_SERVICE_DO_TASK_ERROR_ALREADY_DONE')));
223 }
224
225 $task["PARAMETERS"]["DOCUMENT_ID"] = \CBPStateService::GetStateDocumentId($task['WORKFLOW_ID']);
226 $task["MODULE_ID"] = $task["PARAMETERS"]["DOCUMENT_ID"][0];
227 $task["ENTITY"] = $task["PARAMETERS"]["DOCUMENT_ID"][1];
228 $task["DOCUMENT_ID"] = $task["PARAMETERS"]["DOCUMENT_ID"][2];
229
230 if (!\CBPDocument::PostTaskForm($task, $request->userId, $request->taskRequest, $errors))
231 {
232 $error = reset($errors);
233 if ($task['MODULE_ID'] === 'rpa' && $error['code'] === \CBPRuntime::EXCEPTION_CODE_INSTANCE_TERMINATED)
234 {
235 return $result;
236 }
237
238 $result->addError(new Error($error['message']));
239 }
240
241 return $result;
242 }
243
244 public function doInlineTasks(DoInlineTasksRequest $request): Response\TaskService\DoInlineTaskResponse
245 {
246 $taskIds = [];
247 foreach ($request->taskIds as $id)
248 {
249 // TODO - throw argument or return error if $id not integer?
250 if (is_int($id))
251 {
252 $taskIds[] = $id;
253 }
254 }
255
257 if ($taskIds)
258 {
259 $errors = [];
260 \CBPDocument::setTasksUserStatus($request->userId, $request->newTaskStatusId, $taskIds, $errors);
261
262 if ($errors)
263 {
264 foreach ($errors as $errorMessage)
265 {
266 $result->addError(new \Bitrix\Bizproc\Error($errorMessage));
267 }
268 }
269 }
270
271 return $result;
272 }
273}
delegateTasks(DelegateTasksRequest $request)
__construct(private TaskAccessService $accessService)
doInlineTasks(DoInlineTasksRequest $request)
getTasks(GetUserTasksRequest $request)
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29