Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
TaskService.php
1<?php
2
4
5use Bitrix\Disk\Uf\FileUserType;
9use Bitrix\Im\V2\Common\ContextCustomer;
20use Bitrix\Tasks\Slider\Path\PathMaker;
21use Bitrix\Tasks\Slider\Path\TaskPathMaker;
22
24{
25 use ContextCustomer;
26
27 protected const SIGNATURE_SALT = 'task_service_salt';
28 protected const ADD_TASK_EVENT = 'taskAdd';
29 protected const UPDATE_TASK_EVENT = 'taskUpdate';
30 protected const DELETE_TASK_EVENT = 'taskDelete';
31
32 public function registerTask(int $chatId, int $messageId, \Bitrix\Im\V2\Entity\Task\TaskItem $taskItem): Result
33 {
34 $result = new Result();
35
36 $userId = $this->getContext()->getUserId();
37
38 $taskLink = new TaskItem();
39 $taskLink->setEntity($taskItem)->setChatId($chatId)->setAuthorId($userId);
40
41 if ($messageId !== 0)
42 {
43 $taskLink->setMessageId($messageId);
44 }
45
46 $sendMessageResult = $this->sendMessageAboutTask($taskLink, $chatId);
47
48 if (!$sendMessageResult->isSuccess())
49 {
50 $result->addErrors($sendMessageResult->getErrors());
51 }
52
53 $systemMessageId = $sendMessageResult->getResult();
54
55 $taskLink->setMessageId($messageId ?: $systemMessageId);
56 $saveResult = $taskLink->save();
57
58 if (!$saveResult->isSuccess())
59 {
60 return $result->addErrors($saveResult->getErrors());
61 }
62
64 ->setContext($this->context)
65 ->sendFull($taskLink, self::ADD_TASK_EVENT, ['RECIPIENT' => $taskItem->getMembersIds()])
66 ;
67
68 return $result;
69 }
70
71 public function unregisterTaskByEntity(\Bitrix\Im\V2\Entity\Task\TaskItem $taskEntity, bool $saveDelete): Result
72 {
73 $taskItem = TaskItem::getByEntity($taskEntity);
74
75 if ($taskItem === null)
76 {
77 return new Result();
78 }
79
80 return $this->unregisterTask($taskItem, $saveDelete);
81 }
82
83 public function unregisterTask(TaskItem $task, bool $saveDelete): Result
84 {
86 ->setContext($this->context)
87 ->sendIdOnly($task, self::DELETE_TASK_EVENT, ['CHAT_ID' => $task->getChatId()])
88 ;
89 if (!$saveDelete)
90 {
91 $task->delete();
92 }
93
94 return new Result();
95 }
96
97 public function updateTask(\Bitrix\Im\V2\Entity\Task\TaskItem $taskEntity): Result
98 {
99 $taskItem = TaskItem::getByEntity($taskEntity);
100 if ($taskItem === null)
101 {
102 return new Result();
103 }
104
106 ->setContext($this->context)
107 ->sendFull($taskItem, self::UPDATE_TASK_EVENT, ['RECIPIENT' => $taskEntity->getMembersIds()])
108 ;
109
110 return new Result();
111 }
112
113 public function updateTaskLink(TaskItem $taskItem): Result
114 {
115 $result = new Result();
116
117 $saveResult = $taskItem->save();
118
119 if (!$saveResult->isSuccess())
120 {
121 return $result->addErrors($saveResult->getErrors());
122 }
123
125 ->setContext($this->context)
126 ->sendFull($taskItem, self::UPDATE_TASK_EVENT, ['RECIPIENT' => $taskItem->getEntity()->getMembersIds()])
127 ;
128
129 return $result;
130 }
131
132 public function deleteLinkByTaskId(int $taskId): Result
133 {
134 LinkTaskTable::deleteByFilter(['=TASK_ID' => $taskId]);
135
136 return new Result();
137 }
138
139 public function prepareDataForCreateSlider(Chat $chat, ?Message $message = null): Result
140 {
141 $result = new Result();
142
143 if (!Loader::includeModule('tasks'))
144 {
145 return $result->addError(new TaskError(TaskError::TASKS_NOT_INSTALLED));
146 }
147
148 $userId = $this->getContext()->getUserId();
149
150 $chat->setContext($this->context);
151
152 $data = ['PARAMS' => []];
153
154 $taskPath = (new TaskPathMaker(0, PathMaker::EDIT_ACTION, $userId))->makeEntityPath();
155 $link = new Uri($taskPath);
156 $link->addParams([
157 'ta_sec' => 'chat',
158 'ta_el' => 'create_button',
159 ]);
160
161 $data['LINK'] = $link->getUri();
162
163 $data['PARAMS']['RESPONSIBLE_ID'] = $userId;
164 $data['PARAMS']['IM_CHAT_ID'] = $chat->getChatId();
165
166 if ($chat->getEntityType() !== 'SONET_GROUP')
167 {
168 $data['PARAMS']['AUDITORS'] = implode(",", $this->getAuditors($chat));
169 }
170
171 if ($chat->getEntityType() === 'SONET_GROUP')
172 {
173 $data['PARAMS']['GROUP_ID'] = (int)$chat->getEntityId();
174 }
175
176 if ($chat instanceof Chat\OpenLineChat && Loader::includeModule('crm'))
177 {
178 $entityData = explode('|', $chat->getEntityData1() ?? '');
179 if (isset($entityData[0], $entityData[1], $entityData[2]) && $entityData[0] === 'Y')
180 {
181 $crmType = \CCrmOwnerTypeAbbr::ResolveByTypeID(\CCrmOwnerType::ResolveID($entityData[1]));
182 $data['PARAMS']['UF_CRM_TASK'] = $crmType.'_'.$entityData[2];
183 }
184 }
185
186 if (isset($message))
187 {
188 $message->setContext($this->context);
189 $data['PARAMS']['DESCRIPTION'] = \CIMShare::PrepareText([
190 'CHAT_ID' => $chat->getChatId(),
191 'MESSAGE_ID' => $message->getMessageId(),
192 'MESSAGE_TYPE' => $chat->getType(),
193 'MESSAGE' => $message->getMessage(),
194 'AUTHOR_ID' => $message->getAuthorId(),
195 'FILES' => $this->getFilesForPrepareText($message)
196 ]);
197
198 $fileIds = $this->getFilesIdsForTaskFromMessage($message);
199
200 if (!empty($fileIds))
201 {
202 $diskFileUFCode = \Bitrix\Tasks\Integration\Disk\UserField::getMainSysUFCode();
203 $data['PARAMS'][$diskFileUFCode] = $fileIds;
204 $signer = new Signer();
205 $data['PARAMS'][$diskFileUFCode . '_SIGN'] = $signer->sign(Json::encode($fileIds), static::SIGNATURE_SALT);
206 }
207
208 $data['PARAMS']['IM_MESSAGE_ID'] = $message->getMessageId();
209 }
210
211 return $result->setResult($data);
212 }
213
214 protected function sendMessageAboutTask(TaskItem $taskLink, int $chatId): Result
215 {
216 //todo: Replace with new API
217 $dialogId = Dialog::getDialogId($chatId);
218 $authorId = $this->getContext()->getUserId();
219
220 $messageId = \CIMChat::AddMessage([
221 'DIALOG_ID' => $dialogId,
222 'SYSTEM' => 'Y',
223 'MESSAGE' => $this->getMessageText($taskLink),
224 'FROM_USER_ID' => $authorId,
225 'PARAMS' => ['CLASS' => "bx-messenger-content-item-system"],
226 'URL_PREVIEW' => 'N',
227 'SKIP_CONNECTOR' => 'Y',
228 'SKIP_COMMAND' => 'Y',
229 'SILENT_CONNECTOR' => 'Y',
230 'SKIP_URL_INDEX' => 'Y',
231 ]);
232
233 $result = new Result();
234
235 if ($messageId === false)
236 {
237 return $result->addError(new TaskError(TaskError::ADD_TASK_MESSAGE_FAILED));
238 }
239
240 return $result->setResult($messageId);
241 }
242
247 protected function getFilesIdsForTaskFromMessage(Message $message): array
248 {
249 $copies = $message->getFiles()->getCopies();
250 $copies->addToTmp(TemporaryFileService::TASK_SOURCE);
251 $newIds = [];
252
253 foreach ($copies as $copy)
254 {
255 $newIds[] = FileUserType::NEW_FILE_PREFIX . $copy->getId();
256 }
257
258 return $newIds;
259 }
260
261 protected function getAuditors(Chat $chat): array
262 {
263 $userIds = $chat->getRelations(
264 [
265 'SELECT' => ['ID', 'USER_ID', 'CHAT_ID'],
266 'FILTER' => ['ACTIVE' => true, 'ONLY_INTERNAL_TYPE' => true],
267 'LIMIT' => 50,
268 ]
269 )->getUsers()->filterExtranet()->getIds();
270 unset($userIds[$this->getContext()->getUserId()]);
271
272 return $userIds;
273 }
274
275 protected function getFilesForPrepareText(Message $message): array
276 {
277 $files = $message->getFiles();
278 $filesForPrepare = [];
279
280 foreach ($files as $file)
281 {
282 $filesForPrepare[] = ['name' => $file->getDiskFile()->getName()];
283 }
284
285 return $filesForPrepare;
286 }
287
288 protected function getMessageText(TaskItem $task): string
289 {
290 $genderModifier = ($this->getContext()->getUser()->getGender() === 'F') ? '_F' : '';
291
292 if ($task->getMessageId() !== null)
293 {
294 $text = (new Message($task->getMessageId()))->getQuotedMessage() . "\n";
295 $text .= Loc::getMessage(
296 'IM_CHAT_TASK_REGISTER_FROM_MESSAGE_NOTIFICATION' . $genderModifier,
297 [
298 '#LINK#' => $task->getEntity()->getUrl(),
299 '#USER_ID#' => $this->getContext()->getUserId(),
300 '#MESSAGE_ID#' => $task->getMessageId(),
301 '#DIALOG_ID#' => Chat::getInstance($task->getChatId())->getDialogContextId(),
302 ]
303 );
304
305 return $text;
306 }
307 return Loc::getMessage(
308 'IM_CHAT_TASK_REGISTER_FROM_CHAT_NOTIFICATION' . $genderModifier . '_MSGVER_1',
309 [
310 '#LINK#' => $task->getEntity()->getUrl(),
311 '#USER_ID#' => $this->getContext()->getUserId(),
312 '#TASK_TITLE#' => $task->getEntity()->getTitle(),
313 ]
314 );
315 }
316}
static getType($chatData)
Definition chat.php:41
static getUsers($chatId, $options=[])
Definition chat.php:787
static getDialogId(int $chatId, $userId=null)
Definition dialog.php:48
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29