1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
TaskService.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Link\Task;
4
5use Bitrix\Disk\Uf\FileUserType;
6use Bitrix\Im\Dialog;
7use Bitrix\Im\Model\LinkTaskTable;
8use Bitrix\Im\V2\Chat;
9use Bitrix\Im\V2\Common\ContextCustomer;
10use Bitrix\Im\V2\Link\File\TemporaryFileService;
11use Bitrix\Im\V2\Link\Push;
12use Bitrix\Im\V2\Message;
13use Bitrix\Im\V2\Entity\Task\TaskError;
14use Bitrix\Im\V2\RelationCollection;
15use Bitrix\Im\V2\Result;
16use Bitrix\Main\Loader;
17use Bitrix\Main\Localization\Loc;
18use Bitrix\Main\Security\Sign\Signer;
19use Bitrix\Main\Web\Json;
20use Bitrix\Main\Web\Uri;
21use Bitrix\Tasks\Slider\Path\PathMaker;
22use Bitrix\Tasks\Slider\Path\TaskPathMaker;
23
25{
26 use ContextCustomer;
27
28 protected const SIGNATURE_SALT = 'task_service_salt';
29 protected const ADD_TASK_EVENT = 'taskAdd';
30 protected const UPDATE_TASK_EVENT = 'taskUpdate';
31 protected const DELETE_TASK_EVENT = 'taskDelete';
32
33 public function registerTask(int $chatId, int $messageId, \Bitrix\Im\V2\Entity\Task\TaskItem $taskItem): Result
34 {
35 $result = new Result();
36
37 $userId = $this->getContext()->getUserId();
38
39 $taskLink = new TaskItem();
40 $taskLink->setEntity($taskItem)->setChatId($chatId)->setAuthorId($userId);
41
42 if ($messageId !== 0)
43 {
44 $taskLink->setMessageId($messageId);
45 }
46
47 $sendMessageResult = $this->sendMessageAboutTask($taskLink, $chatId);
48
49 if (!$sendMessageResult->isSuccess())
50 {
51 $result->addErrors($sendMessageResult->getErrors());
52 }
53
54 $systemMessageId = $sendMessageResult->getResult();
55
56 $taskLink->setMessageId($messageId ?: $systemMessageId);
57 $saveResult = $taskLink->save();
58
59 if (!$saveResult->isSuccess())
60 {
61 return $result->addErrors($saveResult->getErrors());
62 }
63
65 ->setContext($this->context)
66 ->sendFull($taskLink, self::ADD_TASK_EVENT, ['RECIPIENT' => $taskItem->getMembersIds()])
67 ;
68
69 return $result;
70 }
71
72 public function unregisterTaskByEntity(\Bitrix\Im\V2\Entity\Task\TaskItem $taskEntity, bool $saveDelete): Result
73 {
74 $taskItem = TaskItem::getByEntity($taskEntity);
75
76 if ($taskItem === null)
77 {
78 return new Result();
79 }
80
81 return $this->unregisterTask($taskItem, $saveDelete);
82 }
83
84 public function unregisterTask(TaskItem $task, bool $saveDelete): Result
85 {
87 ->setContext($this->context)
88 ->sendIdOnly($task, self::DELETE_TASK_EVENT, ['CHAT_ID' => $task->getChatId()])
89 ;
90 if (!$saveDelete)
91 {
92 $task->delete();
93 TaskItem::cleanCache($task->getEntityId() ?? 0);
94 }
95
96 return new Result();
97 }
98
99 public function updateTask(\Bitrix\Im\V2\Entity\Task\TaskItem $taskEntity): Result
100 {
101 $taskItem = TaskItem::getByEntity($taskEntity);
102 if ($taskItem === null)
103 {
104 return new Result();
105 }
106
108 ->setContext($this->context)
109 ->sendFull($taskItem, self::UPDATE_TASK_EVENT, ['RECIPIENT' => $taskEntity->getMembersIds()])
110 ;
111
112 return new Result();
113 }
114
115 public function updateTaskLink(TaskItem $taskItem): Result
116 {
117 $result = new Result();
118
119 $saveResult = $taskItem->save();
120
121 if (!$saveResult->isSuccess())
122 {
123 return $result->addErrors($saveResult->getErrors());
124 }
125
127 ->setContext($this->context)
128 ->sendFull($taskItem, self::UPDATE_TASK_EVENT, ['RECIPIENT' => $taskItem->getEntity()->getMembersIds()])
129 ;
130
131 return $result;
132 }
133
134 public function updateTaskLinks(TaskCollection $taskCollection): Result
135 {
136 $result = new Result();
137
138 if ($taskCollection->count() === 0)
139 {
140 return $result;
141 }
142
143 $saveResult = $taskCollection->save();
144
145 if (!$saveResult->isSuccess())
146 {
147 return $result->addErrors($saveResult->getErrors());
148 }
149
150 $taskCollection->fillEntities();
151
152 foreach ($taskCollection as $task)
153 {
154 $pushRecipient = ['RECIPIENT' => $task->getEntity()->getMembersIds()];
156 ->setContext($this->context)
157 ->sendFull($task, self::UPDATE_TASK_EVENT, $pushRecipient)
158 ;
159 }
160
161 return $result;
162 }
163
164 public function deleteLinkByTaskId(int $taskId): Result
165 {
166 LinkTaskTable::deleteByFilter(['=TASK_ID' => $taskId]);
167
168 return new Result();
169 }
170
171 public function prepareDataForCreateSlider(Chat $chat, ?Message $message = null): Result
172 {
173 $result = new Result();
174
175 if (!Loader::includeModule('tasks'))
176 {
177 return $result->addError(new TaskError(TaskError::TASKS_NOT_INSTALLED));
178 }
179
180 $userId = $this->getContext()->getUserId();
181
182 $chat->setContext($this->context);
183
184 $data = ['PARAMS' => []];
185
186 $taskPath = (new TaskPathMaker(0, PathMaker::EDIT_ACTION, $userId))->makeEntityPath();
187 $link = new Uri($taskPath);
188 $link->addParams([
189 'ta_sec' => 'chat',
190 'ta_el' => 'comment_context_menu',
191 ]);
192
193 $data['LINK'] = $link->getUri();
194
195 $data['PARAMS']['RESPONSIBLE_ID'] = $userId;
196 $data['PARAMS']['IM_CHAT_ID'] = $chat->getChatId();
197
198 if ($chat->getEntityType() !== 'SONET_GROUP')
199 {
200 $data['PARAMS']['AUDITORS'] = implode(",", $this->getAuditors($chat));
201 }
202
203 if ($chat->getEntityType() === 'SONET_GROUP')
204 {
205 $data['PARAMS']['GROUP_ID'] = (int)$chat->getEntityId();
206 }
207
208 if ($chat instanceof Chat\OpenLineChat && Loader::includeModule('crm'))
209 {
210 $entityData = explode('|', $chat->getEntityData1() ?? '');
211 if (isset($entityData[0], $entityData[1], $entityData[2]) && $entityData[0] === 'Y')
212 {
213 $crmType = \CCrmOwnerTypeAbbr::ResolveByTypeID(\CCrmOwnerType::ResolveID($entityData[1]));
214 $data['PARAMS']['UF_CRM_TASK'] = $crmType.'_'.$entityData[2];
215 }
216 }
217
218 if (isset($message))
219 {
220 $message->setContext($this->context);
221 $data['PARAMS']['DESCRIPTION'] = \CIMShare::PrepareText([
222 'CHAT_ID' => $chat->getChatId(),
223 'MESSAGE_ID' => $message->getMessageId(),
224 'MESSAGE_TYPE' => $chat->getType(),
225 'MESSAGE' => $message->getMessage(),
226 'AUTHOR_ID' => $message->getAuthorId(),
227 'FILES' => $this->getFilesForPrepareText($message)
228 ]);
229
231
232 if (!empty($files))
233 {
234 $fileIds = $this->getFilesIds($files);
235
236 $diskFileUFCode = \Bitrix\Tasks\Integration\Disk\UserField::getMainSysUFCode();
237 $data['PARAMS'][$diskFileUFCode] = $fileIds;
238 $signer = new Signer();
239 $data['PARAMS'][$diskFileUFCode . '_SIGN'] = $signer->sign(Json::encode($fileIds), static::SIGNATURE_SALT);
240 $data['PARAMS'][$diskFileUFCode . '_DATA'] = $files;
241 }
242
243 $data['PARAMS']['IM_MESSAGE_ID'] = $message->getMessageId();
244 }
245
246 return $result->setResult($data);
247 }
248
249 protected function sendMessageAboutTask(TaskItem $taskLink, int $chatId): Result
250 {
251 //todo: Replace with new API
252 $dialogId = Dialog::getDialogId($chatId);
253 $authorId = $this->getContext()->getUserId();
254
255 $messageId = \CIMChat::AddMessage([
256 'DIALOG_ID' => $dialogId,
257 'SYSTEM' => 'Y',
258 'MESSAGE' => $this->getMessageText($taskLink),
259 'FROM_USER_ID' => $authorId,
260 'PARAMS' => ['CLASS' => "bx-messenger-content-item-system"],
261 'URL_PREVIEW' => 'N',
262 'SKIP_CONNECTOR' => 'Y',
263 'SKIP_COMMAND' => 'Y',
264 'SILENT_CONNECTOR' => 'Y',
265 'SKIP_URL_INDEX' => 'Y',
266 ]);
267
268 $result = new Result();
269
270 if ($messageId === false)
271 {
273 }
274
275 return $result->setResult($messageId);
276 }
277
283 {
284 $copies = $message->getFiles()->copyToOwnUploadedFiles()->getResult();
285 if (!isset($copies))
286 {
287 return [];
288 }
289
290 $copies->addToTmp(TemporaryFileService::TASK_SOURCE);
291 $files = [];
292
293 foreach ($copies as $file)
294 {
295 $fileData = $file->toRestFormat();
296 $files[] = [
297 'id' => FileUserType::NEW_FILE_PREFIX . $file->getId(),
298 'objectId' => $fileData['viewerAttrs']['objectId'],
299 'name' => $fileData['name'],
300 'type' => $fileData['extension'],
301 'url' => $fileData['urlShow'],
302 'height' => $fileData['image']['height'] ?? 0,
303 'width' => $fileData['image']['width'] ?? 0,
304 'preview_url' => $fileData['urlPreview'],
305 ];
306 }
307
308 return $files;
309 }
310
311 protected function getFilesIds(array $files): array
312 {
313 $fileIds = [];
314 foreach ($files as $file)
315 {
316 if (isset($file['id']))
317 {
318 $fileIds[] = $file['id'];
319 }
320 }
321
322 return $fileIds;
323 }
324
325 protected function getAuditors(Chat $chat): array
326 {
328 [
329 'ACTIVE' => true,
330 'ONLY_INTERNAL_TYPE' => true,
331 'CHAT_ID' => $chat->getId(),
332 'IS_HIDDEN' => false,
333 'ONLY_INTRANET' => true,
334 '!USER_ID' => $this->getContext()->getUserId()
335 ],
336 limit: 50,
337 select: ['ID', 'USER_ID', 'CHAT_ID']
338 )->getUserIds();
339 }
340
342 {
343 $files = $message->getFiles();
344 $filesForPrepare = [];
345
346 foreach ($files as $file)
347 {
348 $filesForPrepare[] = ['name' => $file->getDiskFile()->getName()];
349 }
350
351 return $filesForPrepare;
352 }
353
354 protected function getMessageText(TaskItem $task): string
355 {
356 $genderModifier = ($this->getContext()->getUser()->getGender() === 'F') ? '_F' : '';
357
358 if ($task->getMessageId() !== null)
359 {
360 $text = (new Message($task->getMessageId()))->getQuotedMessage() . "\n";
361 $text .= Loc::getMessage(
362 'IM_CHAT_TASK_REGISTER_FROM_MESSAGE_NOTIFICATION' . $genderModifier . '_MSGVER_1',
363 [
364 '#LINK#' => $task->getEntity()->getUrl(),
365 '#USER_ID#' => $this->getContext()->getUserId(),
366 '#MESSAGE_ID#' => $task->getMessageId(),
367 '#DIALOG_ID#' => Chat::getInstance($task->getChatId())->getDialogContextId(),
368 ]
369 );
370
371 return $text;
372 }
373 return Loc::getMessage(
374 'IM_CHAT_TASK_REGISTER_FROM_CHAT_NOTIFICATION' . $genderModifier . '_MSGVER_1',
375 [
376 '#LINK#' => $task->getEntity()->getUrl(),
377 '#USER_ID#' => $this->getContext()->getUserId(),
378 '#TASK_TITLE#' => $task->getEntity()->getTitle(),
379 ]
380 );
381 }
382}
return select
Определения access_edit.php:440
if(! $messageFields||!isset($messageFields['message_id'])||!isset($messageFields['status'])||!CModule::IncludeModule("messageservice")) $messageId
Определения callback_ismscenter.php:26
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static getType($chatData, bool $camelCase=true)
Определения chat.php:45
static getDialogId(int $chatId, $userId=null)
Определения dialog.php:50
const TASKS_NOT_INSTALLED
Определения TaskError.php:10
const ADD_TASK_MESSAGE_FAILED
Определения TaskError.php:12
static find(array $filter, array $order=[], ?int $limit=null, ?Context $context=null, array $select=self::COMMON_FIELDS)
Определения RelationCollection.php:50
static getInstance()
Определения application.php:98
Определения result.php:20
Определения uri.php:17
static PrepareText($quoteMessage)
Определения im_share.php:512
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
Определения Uuid.php:3
Определения ActionUuid.php:3
Определения ufield.php:9
$files
Определения mysql_to_pgsql.php:30
$message
Определения payment.php:8
$text
Определения template_pdf.php:79