6if (Loader::includeModule(
'replica'))
8 class MessageHandler
extends \Bitrix\Replica\Client\BaseHandler
10 protected $tasksForumId = 0;
11 protected $messageData = [];
13 protected $tableName =
"b_forum_message";
14 protected $moduleId =
"forum";
16 protected $primary = array(
17 "ID" =>
"auto_increment",
19 protected $predicates = array(
20 "TOPIC_ID" =>
"b_forum_topic.ID",
22 protected $translation = array(
23 "ID" =>
"b_forum_message.ID",
24 "TOPIC_ID" =>
"b_forum_topic.ID",
25 "AUTHOR_ID" =>
"b_user.ID",
27 "EDITOR_ID" =>
"b_user.ID",
29 protected $children = array(
32 protected $fileHandler =
null;
40 $this->fileHandler = new \Bitrix\Forum\Replica\ForumMessageAttachmentHandler();
41 if (\
Bitrix\Main\Loader::includeModule(
'tasks'))
43 $this->tasksForumId = (int)\CTasksTools::GetForumIdForIntranet();
52 public function initDataManagerEvents()
54 parent::initDataManagerEvents();
55 $this->fileHandler->initDataManagerEvents();
67 public function onBeforeMessageAdd($data, $uploadDir): void
69 $this->messageData = $data;
85 public function onAfterMessageAdd(&$id, $message, $topicInfo, $forumInfo, $fields)
87 $op = \Bitrix\Replica\Db\Operation::writeInsert($this->tableName, array(
'ID'), array(
'ID' => $id));
88 $nodes = $op->getTableRecord()->getNodes(
true,
false);
91 $this->fileHandler->onAfterAdd($id, $message);
105 public function onBeforeMessageUpdate(&$id, &$fields, &$uploadDir)
107 $mapper = \Bitrix\Replica\Mapper::getInstance();
108 $map = $mapper->getByPrimaryValue($this->tableName, array(
'ID'), array(
'ID' => $id));
111 $this->fileHandler->onBeforeUpdate($id);
125 public function onAfterMessageUpdate(&$id, &$newMessage, $oldMessage)
127 $op = \Bitrix\Replica\Db\Operation::writeUpdate($this->tableName, array(
'ID'), array(
'ID' => $id));
128 $mapper = \Bitrix\Replica\Mapper::getInstance();
129 $map = $mapper->getByPrimaryValue($this->tableName, array(
'ID'), array(
'ID' => $id));
132 $nodes = current($map);
133 $this->fileHandler->onAfterUpdate($id, $newMessage, $nodes);
145 public function onBeforeMessageDelete($id)
147 $mapper = \Bitrix\Replica\Mapper::getInstance();
148 $map = $mapper->getByPrimaryValue($this->tableName, array(
'ID'), array(
'ID' => $id));
151 $this->fileHandler->onBeforeDelete($id);
164 public function onAfterMessageDelete($id, $message)
166 $this->fileHandler->onAfterDelete($id);
167 $op = \Bitrix\Replica\Db\Operation::writeDelete($this->tableName, array(
'ID'), array(
'ID' => $id));
177 public function afterWriteMissing(array $record)
180 $mapper = \Bitrix\Replica\Mapper::getInstance();
181 $map = $mapper->getByPrimaryValue($this->tableName, array(
'ID'), array(
'ID' => $record[
"ID"]));
184 $this->fileHandler->onAfterAdd($record[
"ID"], $record);
197 public function beforeInsertTrigger(array &$newRecord)
199 if ($this->tasksForumId > 0 && $newRecord[
'FORUM_ID'] ===
'tasks_forum')
201 $mapper = \Bitrix\Replica\Mapper::getInstance();
202 $newRecord[
'FORUM_ID'] = $this->tasksForumId;
204 if (preg_match(
"/^TASK_(.+)\$/", $newRecord[
"XML_ID"], $match))
206 $taskId = $mapper->resolveLogGuid(
false,
'b_tasks.ID', $match[1]);
209 $newRecord[
'XML_ID'] =
'TASK_'.$taskId;
210 $newRecord[
'PARAM2'] = $taskId;
214 if ($newRecord[
"PARAM1"] ===
"TK" && $newRecord[
"PARAM2"])
216 $taskId = $mapper->resolveLogGuid(
false,
'b_tasks.ID', $newRecord[
"PARAM2"]);
219 $newRecord[
'PARAM2'] = $taskId;
223 $this->fileHandler->replaceGuidsWithFiles($newRecord);
225 $fixed = $this->clearUserBbCodes($newRecord[
'POST_MESSAGE']);
228 $newRecord[
"POST_MESSAGE"] = $fixed;
231 $fixed = $this->clearUserBbCodes($newRecord[
'POST_MESSAGE_HTML']);
234 $newRecord[
"POST_MESSAGE_HTML"] = $fixed;
237 $this->messageData = $newRecord;
249 public function afterInsertTrigger(array $newRecord)
252 $this->tasksForumId > 0
253 && $newRecord[
'FORUM_ID'] == $this->tasksForumId
254 && $newRecord[
'PARAM1'] !==
'TK'
255 && $newRecord[
'PARAM2'] > 0
258 $taskId = $newRecord[
'PARAM2'];
261 $fields = [
'AUX',
'AUX_DATA'];
262 foreach ($fields as $key)
264 if (array_key_exists($key, $this->messageData))
266 $newRecord[$key] = $this->messageData[$key];
270 \Bitrix\Tasks\Integration\Forum\Task\Comment::onAfterAdd(
275 "TOPIC_ID" => $newRecord[
"TOPIC_ID"],
276 "MESSAGE_ID" => $newRecord[
"ID"],
278 "POST_MESSAGE" => $newRecord[
"POST_MESSAGE"],
279 "AUTHOR_ID" => $newRecord[
"AUTHOR_ID"],
280 "AUTHOR_NAME" => $newRecord[
"AUTHOR_NAME"],
281 "AUTHOR_EMAIL" => $newRecord[
"AUTHOR_EMAIL"],
282 "USE_SMILES" => $newRecord[
"USE_SMILES"],
284 "AUTHOR_IP" => $newRecord[
"AUTHOR_IP"],
285 "AUTHOR_REAL_IP" => $newRecord[
"AUTHOR_REAL_IP"],
286 "GUEST_ID" => $newRecord[
"GUEST_ID"],
287 "AUX" => ($newRecord[
"AUX"] ??
'N'),
289 "MESSAGE" => $newRecord,
290 "AUX_DATA" => ($newRecord[
"AUX_DATA"] ??
''),
305 public function afterUpdateTrigger(array $oldRecord, array $newRecord)
308 $this->tasksForumId > 0
309 && $newRecord[
'FORUM_ID'] ==
'tasks_forum'
310 && $newRecord[
'PARAM1'] !==
'TK'
314 if (preg_match(
"/^TASK_(.+)\$/", $newRecord[
"XML_ID"], $match))
316 $mapper = \Bitrix\Replica\Mapper::getInstance();
317 $taskId = $mapper->resolveLogGuid(
false,
'b_tasks.ID', $match[1]);
322 \Bitrix\Tasks\Integration\Forum\Task\Comment::onAfterUpdate(
328 "TOPIC_ID" => $newRecord[
"TOPIC_ID"],
329 "MESSAGE_ID" => $newRecord[
"ID"],
331 "POST_MESSAGE" => $newRecord[
"POST_MESSAGE"],
332 "AUTHOR_ID" => $newRecord[
"AUTHOR_ID"],
333 "AUTHOR_NAME" => $newRecord[
"AUTHOR_NAME"],
334 "AUTHOR_EMAIL" => $newRecord[
"AUTHOR_EMAIL"],
335 "USE_SMILES" => $newRecord[
"USE_SMILES"],
337 "AUTHOR_IP" => $newRecord[
"AUTHOR_IP"],
338 "AUTHOR_REAL_IP" => $newRecord[
"AUTHOR_REAL_IP"],
339 "GUEST_ID" => $newRecord[
"GUEST_ID"],
341 "MESSAGE" => $newRecord,
355 public function afterDeleteTrigger(array $oldRecord)
358 $this->tasksForumId > 0
359 && $oldRecord[
'FORUM_ID'] == $this->tasksForumId
363 if (preg_match(
"/^TASK_(.+)\$/", $oldRecord[
"XML_ID"], $match))
365 $taskId = intval($match[1]);
370 \Bitrix\Tasks\Integration\Forum\Task\Comment::onAfterUpdate(
376 "TOPIC_ID" => $oldRecord[
"TOPIC_ID"],
377 "MESSAGE_ID" => $oldRecord[
"ID"],
378 "MESSAGE" => $oldRecord,
393 public function beforeUpdateTrigger(array $oldRecord, array &$newRecord)
395 if ($this->tasksForumId > 0 && $newRecord[
'FORUM_ID'] ===
'tasks_forum')
397 $newRecord[
'FORUM_ID'] = $this->tasksForumId;
398 if (preg_match(
"/^TASK_(.+)\$/", $newRecord[
"XML_ID"], $match))
400 $mapper = \Bitrix\Replica\Mapper::getInstance();
401 $taskId = $mapper->resolveLogGuid(
false,
'b_tasks.ID', $match[1]);
404 $newRecord[
'XML_ID'] =
'TASK_'.$taskId;
405 $newRecord[
'PARAM2'] = $taskId;
409 $fixed = $this->clearUserBbCodes($newRecord[
'POST_MESSAGE']);
412 $newRecord[
"POST_MESSAGE"] = $fixed;
415 $fixed = $this->clearUserBbCodes($newRecord[
'POST_MESSAGE_HTML']);
418 $newRecord[
"POST_MESSAGE_HTML"] = $fixed;
421 $this->fileHandler->replaceGuidsWithFiles($newRecord);
432 public function beforeLogInsert(array $record)
434 if ($this->tasksForumId > 0 && $record[
'FORUM_ID'] == $this->tasksForumId)
451 public function beforeLogFormat(array &$record)
453 if ($this->tasksForumId > 0 && $record[
'FORUM_ID'] == $this->tasksForumId)
455 $mapper = \Bitrix\Replica\Mapper::getInstance();
456 $record[
'FORUM_ID'] =
'tasks_forum';
458 if (preg_match(
"/^TASK_([0-9]+)\$/", $record[
"XML_ID"], $match))
460 $guid = $mapper->getLogGuid(
"b_tasks.ID", $match[1]);
462 $record[
'XML_ID'] =
'TASK_'.$guid;
465 if ($record[
"PARAM1"] ==
"TK" && $record[
"PARAM2"] > 0)
467 $guid = $mapper->getLogGuid(
"b_tasks.ID", $record[
"PARAM2"]);
469 $record[
"PARAM2"] = $guid;
472 $this->fileHandler->replaceFilesWithGuids($record[
"ID"], $record);
474 $fields = [
'AUX',
'AUX_DATA'];
475 foreach ($fields as $key)
477 if (array_key_exists($key, $this->messageData))
479 $record[$key] = $this->messageData[$key];
489 private function clearUserBbCodes($string)
494 return preg_replace(
"/\\[USER=[0-9]+\\](.*?)\\[(\\/|\\\\\\/)USER\\]/",
"\\1", $string);