Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
messagehandler.php
1<?php
3
5
6if (Loader::includeModule('replica'))
7{
8 class MessageHandler extends \Bitrix\Replica\Client\BaseHandler
9 {
10 protected $tasksForumId = 0;
11 protected $messageData = [];
12
13 protected $tableName = "b_forum_message";
14 protected $moduleId = "forum";
15
16 protected $primary = array(
17 "ID" => "auto_increment",
18 );
19 protected $predicates = array(
20 "TOPIC_ID" => "b_forum_topic.ID",
21 );
22 protected $translation = array(
23 "ID" => "b_forum_message.ID",
24 "TOPIC_ID" => "b_forum_topic.ID",
25 "AUTHOR_ID" => "b_user.ID",
26 //TODO GUEST_ID
27 "EDITOR_ID" => "b_user.ID",
28 );
29 protected $children = array(
30 );
31
32 protected $fileHandler = null;
33
38 public function __construct()
39 {
40 $this->fileHandler = new \Bitrix\Forum\Replica\ForumMessageAttachmentHandler();
41 if (\Bitrix\Main\Loader::includeModule('tasks'))
42 {
43 $this->tasksForumId = (int)\CTasksTools::GetForumIdForIntranet();
44 }
45 }
46
52 public function initDataManagerEvents()
53 {
54 parent::initDataManagerEvents();
55 $this->fileHandler->initDataManagerEvents();
56 }
57
67 public function onBeforeMessageAdd($data, $uploadDir): void
68 {
69 $this->messageData = $data;
70 }
71
85 public function onAfterMessageAdd(&$id, $message, $topicInfo, $forumInfo, $fields)
86 {
87 $op = \Bitrix\Replica\Db\Operation::writeInsert($this->tableName, array('ID'), array('ID' => $id));
88 $nodes = $op->getTableRecord()->getNodes(true, false);
89 if ($nodes)
90 {
91 $this->fileHandler->onAfterAdd($id, $message);
92 }
93 }
94
105 public function onBeforeMessageUpdate(&$id, &$fields, &$uploadDir)
106 {
107 $mapper = \Bitrix\Replica\Mapper::getInstance();
108 $map = $mapper->getByPrimaryValue($this->tableName, array('ID'), array('ID' => $id));
109 if ($map)
110 {
111 $this->fileHandler->onBeforeUpdate($id);
112 }
113 }
114
125 public function onAfterMessageUpdate(&$id, &$newMessage, $oldMessage)
126 {
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));
130 if ($map)
131 {
132 $nodes = current($map);
133 $this->fileHandler->onAfterUpdate($id, $newMessage, $nodes);
134 }
135 }
136
145 public function onBeforeMessageDelete($id)
146 {
147 $mapper = \Bitrix\Replica\Mapper::getInstance();
148 $map = $mapper->getByPrimaryValue($this->tableName, array('ID'), array('ID' => $id));
149 if ($map)
150 {
151 $this->fileHandler->onBeforeDelete($id);
152 }
153 }
154
164 public function onAfterMessageDelete($id, $message)
165 {
166 $this->fileHandler->onAfterDelete($id);
167 $op = \Bitrix\Replica\Db\Operation::writeDelete($this->tableName, array('ID'), array('ID' => $id));
168 }
169
177 public function afterWriteMissing(array $record)
178 {
179 //AddMessage2Log($record);
180 $mapper = \Bitrix\Replica\Mapper::getInstance();
181 $map = $mapper->getByPrimaryValue($this->tableName, array('ID'), array('ID' => $record["ID"]));
182 if ($map)
183 {
184 $this->fileHandler->onAfterAdd($record["ID"], $record);
185 }
186 }
187
197 public function beforeInsertTrigger(array &$newRecord)
198 {
199 if ($this->tasksForumId > 0 && $newRecord['FORUM_ID'] === 'tasks_forum')
200 {
201 $mapper = \Bitrix\Replica\Mapper::getInstance();
202 $newRecord['FORUM_ID'] = $this->tasksForumId;
203
204 if (preg_match("/^TASK_(.+)\$/", $newRecord["XML_ID"], $match))
205 {
206 $taskId = $mapper->resolveLogGuid(false, 'b_tasks.ID', $match[1]);
207 if ($taskId)
208 {
209 $newRecord['XML_ID'] = 'TASK_'.$taskId;
210 $newRecord['PARAM2'] = $taskId;
211 }
212 }
213
214 if ($newRecord["PARAM1"] === "TK" && $newRecord["PARAM2"])
215 {
216 $taskId = $mapper->resolveLogGuid(false, 'b_tasks.ID', $newRecord["PARAM2"]);
217 if ($taskId)
218 {
219 $newRecord['PARAM2'] = $taskId;
220 }
221 }
222
223 $this->fileHandler->replaceGuidsWithFiles($newRecord);
224
225 $fixed = $this->clearUserBbCodes($newRecord['POST_MESSAGE']);
226 if ($fixed != null)
227 {
228 $newRecord["POST_MESSAGE"] = $fixed;
229 }
230
231 $fixed = $this->clearUserBbCodes($newRecord['POST_MESSAGE_HTML']);
232 if ($fixed != null)
233 {
234 $newRecord["POST_MESSAGE_HTML"] = $fixed;
235 }
236
237 $this->messageData = $newRecord;
238 }
239 return null;
240 }
241
249 public function afterInsertTrigger(array $newRecord)
250 {
251 if (
252 $this->tasksForumId > 0
253 && $newRecord['FORUM_ID'] == $this->tasksForumId
254 && $newRecord['PARAM1'] !== 'TK'
255 && $newRecord['PARAM2'] > 0
256 )
257 {
258 $taskId = $newRecord['PARAM2'];
259 if ($taskId > 0)
260 {
261 $fields = ['AUX', 'AUX_DATA'];
262 foreach ($fields as $key)
263 {
264 if (array_key_exists($key, $this->messageData))
265 {
266 $newRecord[$key] = $this->messageData[$key];
267 }
268 }
269
270 \Bitrix\Tasks\Integration\Forum\Task\Comment::onAfterAdd(
271 'TK',
272 $taskId,
273 array(
274 "replica" => true, //will suppress ::fireEvent('Add',
275 "TOPIC_ID" => $newRecord["TOPIC_ID"],
276 "MESSAGE_ID" => $newRecord["ID"],
277 "PARAMS" => array(
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"],
283 //TODO "FILES" => $params["FILES"]
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'),
288 ),
289 "MESSAGE" => $newRecord,
290 "AUX_DATA" => ($newRecord["AUX_DATA"] ?? ''),
291 )
292 );
293 }
294 }
295 }
296
305 public function afterUpdateTrigger(array $oldRecord, array $newRecord)
306 {
307 if (
308 $this->tasksForumId > 0
309 && $newRecord['FORUM_ID'] == 'tasks_forum'
310 && $newRecord['PARAM1'] !== 'TK'
311 )
312 {
313 $taskId = 0;
314 if (preg_match("/^TASK_(.+)\$/", $newRecord["XML_ID"], $match))
315 {
316 $mapper = \Bitrix\Replica\Mapper::getInstance();
317 $taskId = $mapper->resolveLogGuid(false, 'b_tasks.ID', $match[1]);
318 }
319
320 if ($taskId > 0)
321 {
322 \Bitrix\Tasks\Integration\Forum\Task\Comment::onAfterUpdate(
323 'TK',
324 $taskId,
325 array(
326 "ACTION" => "EDIT",
327 "replica" => true, //will suppress ::fireEvent('Add',
328 "TOPIC_ID" => $newRecord["TOPIC_ID"],
329 "MESSAGE_ID" => $newRecord["ID"],
330 "PARAMS" => array(
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"],
336 //TODO "FILES" => $params["FILES"]
337 "AUTHOR_IP" => $newRecord["AUTHOR_IP"],
338 "AUTHOR_REAL_IP" => $newRecord["AUTHOR_REAL_IP"],
339 "GUEST_ID" => $newRecord["GUEST_ID"],
340 ),
341 "MESSAGE" => $newRecord,
342 )
343 );
344 }
345 }
346 }
347
355 public function afterDeleteTrigger(array $oldRecord)
356 {
357 if (
358 $this->tasksForumId > 0
359 && $oldRecord['FORUM_ID'] == $this->tasksForumId
360 )
361 {
362 $taskId = 0;
363 if (preg_match("/^TASK_(.+)\$/", $oldRecord["XML_ID"], $match))
364 {
365 $taskId = intval($match[1]);
366 }
367
368 if ($taskId > 0)
369 {
370 \Bitrix\Tasks\Integration\Forum\Task\Comment::onAfterUpdate(
371 'TK',
372 $taskId,
373 array(
374 "ACTION" => "DEL",
375 "replica" => true, //will suppress ::fireEvent('Add',
376 "TOPIC_ID" => $oldRecord["TOPIC_ID"],
377 "MESSAGE_ID" => $oldRecord["ID"],
378 "MESSAGE" => $oldRecord,
379 )
380 );
381 }
382 }
383 }
384
393 public function beforeUpdateTrigger(array $oldRecord, array &$newRecord)
394 {
395 if ($this->tasksForumId > 0 && $newRecord['FORUM_ID'] === 'tasks_forum')
396 {
397 $newRecord['FORUM_ID'] = $this->tasksForumId;
398 if (preg_match("/^TASK_(.+)\$/", $newRecord["XML_ID"], $match))
399 {
400 $mapper = \Bitrix\Replica\Mapper::getInstance();
401 $taskId = $mapper->resolveLogGuid(false, 'b_tasks.ID', $match[1]);
402 if ($taskId)
403 {
404 $newRecord['XML_ID'] = 'TASK_'.$taskId;
405 $newRecord['PARAM2'] = $taskId;
406 }
407 }
408
409 $fixed = $this->clearUserBbCodes($newRecord['POST_MESSAGE']);
410 if ($fixed != null)
411 {
412 $newRecord["POST_MESSAGE"] = $fixed;
413 }
414
415 $fixed = $this->clearUserBbCodes($newRecord['POST_MESSAGE_HTML']);
416 if ($fixed != null)
417 {
418 $newRecord["POST_MESSAGE_HTML"] = $fixed;
419 }
420
421 $this->fileHandler->replaceGuidsWithFiles($newRecord);
422 }
423 }
424
432 public function beforeLogInsert(array $record)
433 {
434 if ($this->tasksForumId > 0 && $record['FORUM_ID'] == $this->tasksForumId)
435 {
436 return true;
437 }
438 else
439 {
440 return false;
441 }
442 }
443
451 public function beforeLogFormat(array &$record)
452 {
453 if ($this->tasksForumId > 0 && $record['FORUM_ID'] == $this->tasksForumId)
454 {
455 $mapper = \Bitrix\Replica\Mapper::getInstance();
456 $record['FORUM_ID'] = 'tasks_forum';
457
458 if (preg_match("/^TASK_([0-9]+)\$/", $record["XML_ID"], $match))
459 {
460 $guid = $mapper->getLogGuid("b_tasks.ID", $match[1]);
461 if ($guid)
462 $record['XML_ID'] = 'TASK_'.$guid;
463 }
464
465 if ($record["PARAM1"] == "TK" && $record["PARAM2"] > 0)
466 {
467 $guid = $mapper->getLogGuid("b_tasks.ID", $record["PARAM2"]);
468 if ($guid)
469 $record["PARAM2"] = $guid;
470 }
471
472 $this->fileHandler->replaceFilesWithGuids($record["ID"], $record);
473
474 $fields = ['AUX', 'AUX_DATA'];
475 foreach ($fields as $key)
476 {
477 if (array_key_exists($key, $this->messageData))
478 {
479 $record[$key] = $this->messageData[$key];
480 }
481 }
482 }
483 }
484
489 private function clearUserBbCodes($string)
490 {
491 // (\\/|\\\\\\/)
492 // \\/ if for usual messages with bb-code [USER=*]name[/USER]
493 // \\\\\\/ is for service messages with bb-code [USER=*]name[\/USER]
494 return preg_replace("/\\[USER=[0-9]+\\](.*?)\\[(\\/|\\\\\\/)USER\\]/", "\\1", $string);
495 }
496 }
497}