Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
topic.php
1<?php
2namespace Bitrix\Forum;
3
17
70class TopicTable extends Main\Entity\DataManager
71{
77 public static function getTableName()
78 {
79 return 'b_forum_topic';
80 }
81
87 public static function getMap()
88 {
89 return [
90 (new IntegerField("ID", ["primary" => true, "autocomplete" => true])),
91 (new IntegerField("FORUM_ID", ["required" => true])),
92 (new IntegerField("TOPIC_ID")),
93 (new StringField("TITLE", ["required" => true, "size" => 255])),
94 (new StringField("TITLE_SEO", ["size" => 255])),
95 (new StringField("TAGS", ["size" => 255])),
96 (new StringField("DESCRIPTION", ["size" => 255])),
97 (new StringField("ICON", ["size" => 255])),
98 (new EnumField("STATE", ["values" => [Topic::STATE_OPENED, Topic::STATE_CLOSED, Topic::STATE_LINK], "default_value" => Topic::STATE_OPENED])),
99 (new BooleanField("APPROVED", ["values" => [Topic::APPROVED_DISAPPROVED, Topic::APPROVED_APPROVED], "default_value" => Topic::APPROVED_APPROVED])),
100 (new IntegerField("SORT", ["default_value" => 150])),
101 (new IntegerField("VIEWS")),
102 (new IntegerField("USER_START_ID")),
103 (new StringField("USER_START_NAME", ["required" => true, "size" => 255])),
104 (new DatetimeField("START_DATE", ["required" => true, "default_value" => function(){return new DateTime();}])),
105 (new IntegerField("POSTS")),
106 (new IntegerField("POSTS_SERVICE")),
107 (new IntegerField("LAST_POSTER_ID")),
108 (new StringField("LAST_POSTER_NAME", ["required" => true, "size" => 255])),
109 (new DatetimeField("LAST_POST_DATE", ["required" => true, "default_value" => function(){return new DateTime();}])),
110 (new IntegerField("LAST_MESSAGE_ID")),
111 (new IntegerField("POSTS_UNAPPROVED", ["default_value" => 0])),
112 (new IntegerField("ABS_LAST_POSTER_ID")),
113 (new StringField("ABS_LAST_POSTER_NAME", ["size" => 255])),
114 (new DatetimeField("ABS_LAST_POST_DATE", ["required" => true, "default_value" => function(){return new DateTime();}])),
115 (new IntegerField("ABS_LAST_MESSAGE_ID")),
116 (new StringField("XML_ID", ["size" => 255])),
117 (new TextField("HTML")),
118 (new IntegerField("SOCNET_GROUP_ID")),
119 (new IntegerField("OWNER_ID")),
120 (new Reference("FORUM", ForumTable::class, Join::on("this.FORUM_ID", "ref.ID")))
121 ];
122 }
123
124 public static function getFilteredFields()
125 {
126 return [
127 "TITLE",
128 "TAGS",
129 "DESCRIPTION",
130 "USER_START_NAME",
131 "LAST_POSTER_NAME",
132 "ABS_LAST_POSTER_NAME"
133 ];
134 }
135
136 public static function onBeforeAdd(Event $event)
137 {
138 $result = new Main\ORM\EventResult();
140 $data = $event->getParameter("fields");
141 if (Main\Config\Option::get("forum", "FILTER", "Y") == "Y")
142 {
143 $filteredFields = self::getFilteredFields();
144 foreach ($filteredFields as $key)
145 {
146 $res[$key] = $val = array_key_exists($key, $data) ? $data[$key] : "";
147 if (!empty($val))
148 {
149 $res[$key] = \CFilterUnquotableWords::Filter($val);
150 if (empty($res[$key]))
151 {
152 $res[$key] = "*";
153 }
154 }
155 }
156 $data["HTML"] = serialize($res);
157 }
158
159 $data["TITLE_SEO"] = array_key_exists("TITLE_SEO", $data) ? trim($data["TITLE_SEO"], " -") : "";
160 if (empty($data["TITLE_SEO"]))
161 {
162 $data["TITLE_SEO"] = \CUtil::translit($data["TITLE"], LANGUAGE_ID, array("max_len"=>255, "safe_chars"=>".", "replace_space" => '-'));
163 }
164
165 if ($data != $event->getParameter("fields"))
166 {
167 $result->modifyFields($data);
168 }
169
170 return self::modifyData($event, $result);
171 }
172
178 public static function onBeforeUpdate(Main\ORM\Event $event)
179 {
180 $result = new Main\ORM\EventResult();
182 $data = $event->getParameter("fields");
183 $id = $event->getParameter("id");
184 $id = $id["ID"];
185 $topic = TopicTable::getById($id)->fetch();
186
187 if (Main\Config\Option::get("forum", "FILTER", "Y") == "Y")
188 {
189 $filteredFields = self::getFilteredFields();
190 if (!empty(array_intersect($filteredFields, array_keys($data))))
191 {
192 $res = [];
193 foreach ($filteredFields as $key)
194 {
195 $res[$key] = $val = array_key_exists($key, $data) ? $data[$key] : $topic[$key];
196 if (!empty($val))
197 {
198 $res[$key] = \CFilterUnquotableWords::Filter($val);
199 if (empty($res[$key]))
200 {
201 $res[$key] = "*";
202 }
203 }
204 }
205 $data["HTML"] = serialize($res);
206 }
207 }
208 if (array_key_exists("TITLE_SEO", $data) || array_key_exists("TITLE", $data))
209 {
210 $data["TITLE_SEO"] = trim($data["TITLE_SEO"], " -");
211 if ($data["TITLE_SEO"] == '')
212 {
213 $title = array_key_exists("TITLE", $data) ? $data["TITLE"] : $topic["TITLE"];
214 $data["TITLE_SEO"] = \CUtil::translit($title, LANGUAGE_ID, array("max_len"=>255, "safe_chars"=>".", "replace_space" => '-'));
215 }
216 }
217 if ($data != $event->getParameter("fields"))
218 {
219 $result->modifyFields($data);
220 }
221 return self::modifyData($event, $result);
222 }
223
228 public static function onAfterUpdate(Main\ORM\Event $event)
229 {
230 $id = $event->getParameter("id");
231 $id = $id["ID"];
232 $fields = $event->getParameter("fields");
233 if (array_key_exists("FORUM_ID", $fields))
234 {
235 $connection = Main\Application::getInstance()->getConnection();
236 $connection->queryExecute("UPDATE " . FileTable::getTableName() . " SET FORUM_ID={$fields["FORUM_ID"]} WHERE TOPIC_ID={$id}");
237 $connection->queryExecute("UPDATE " . MessageTable::getTableName() . " SET FORUM_ID={$fields["FORUM_ID"]} WHERE TOPIC_ID={$id}");
238 $connection->queryExecute("UPDATE " . SubscribeTable::getTableName() . " SET FORUM_ID={$fields["FORUM_ID"]} WHERE TOPIC_ID={$id}");
239 }
240 }
247 private static function modifyData(Main\ORM\Event $event, Main\ORM\EventResult $result)
248 {
249 $data = array_merge($event->getParameter("fields"), $result->getModified());
250 $fields = [];
251
252 //region check image
253 $key = array_key_exists("VIEWS", $data) ? "VIEWS" : (array_key_exists("=VIEWS", $data) ? "=VIEWS" : null);
254 if ($key !== null && str_replace(" ", "", $data[$key]) === "VIEWS+1")
255 {
256 unset($data[$key]);
257 $fields["VIEWS"] = new Main\DB\SqlExpression('?# + 1', 'VIEWS');
258 }
259 //endregion
260 if (!empty($fields))
261 {
262 $result->modifyFields(array_merge($result->getModified(), $fields));
263 }
264 return $result;
265 }
266}
267
269{
270 use \Bitrix\Forum\Internals\EntityFabric;
271
272 public const STATE_LINK = "L";
273 public const STATE_CLOSED = "N";
274 public const STATE_OPENED = "Y";
275 public const APPROVED_APPROVED = "Y";
276 public const APPROVED_DISAPPROVED = "N";
277
278 public function __construct($id)
279 {
280 if ($id <= 0)
281 {
282 throw new Main\ArgumentNullException("Topic id");
283 }
284 parent::__construct($id);
285 }
286
287 protected function init()
288 {
289 if (!($this->data = TopicTable::getById($this->id)->fetch()))
290 {
291 throw new Main\ObjectNotFoundException(Loc::getMessage("F_ERROR_TID_IS_LOST", ["#id#" => $this->id]));
292 }
293 $this->authorId = intval($this->data["USER_START_ID"]);
294 $this->data["~TITLE_SEO"] = $this->data["TITLE_SEO"];
295 $this->data["TITLE_SEO"] = implode("-", [$this->data["ID"], $this->data["TITLE_SEO"]]);
296 }
297
298 public function moveToForum(int $forumId)
299 {
300 $result = new Main\Result();
301 if ($forumId == $this->forum->getId())
302 {
303 return $result;
304 }
305 $newForum = \Bitrix\Forum\Forum::getById($forumId);
306 TopicTable::update($this->getId(), ["FORUM_ID" => $newForum->getId()]);
307 $this->forum->calculateStatistic();
308 $newForum->calculateStatistic();
309
310 \Bitrix\Forum\Integration\Search\Topic::reindex($this->getId());
311 return $result;
312 }
313
314 public function open()
315 {
316 $result = new Main\Result();
317 if ($this->data["STATE"] === self::STATE_CLOSED)
318 {
319 if (Forum\TopicTable::update($this->getId(), ["STATE" => self::STATE_OPENED])->isSuccess())
320 {
321 $this->data["STATE"] = self::STATE_OPENED;
322 \CForumEventLog::Log("topic", "open", $this->getId(), serialize($this->data));
323 $result->setData(["STATE" => self::STATE_OPENED]);
324 (new Main\Event("forum", "onTopicOpen", [$this->getId(), $this->data]))->send();
325 }
326 else
327 {
328 $result->addError(new Main\Error("Topic is not opened."));
329 }
330 }
331 return $result;
332 }
333
334 public function close()
335 {
336 $result = new Main\Result();
337 if ($this->data["STATE"] === self::STATE_OPENED)
338 {
339 if (Forum\TopicTable::update($this->getId(), ["STATE" => self::STATE_CLOSED])->isSuccess())
340 {
341 $this->data["STATE"] = self::STATE_CLOSED;
342 \CForumEventLog::Log("topic", "close", $this->getId(), serialize($this->data));
343 $result->setData(["STATE" => self::STATE_CLOSED]);
344 (new Main\Event("forum", "onTopicClose", [$this->getId(), $this->data]))->send();
345 }
346 else
347 {
348 $result->addError(new Main\Error("Topic is not closed."));
349 }
350 }
351 return $result;
352 }
353
354 public function disapprove()
355 {
356 $result = new Main\Result();
357 if ($this->data["APPROVED"] === self::APPROVED_APPROVED)
358 {
359 $this->data["APPROVED"] = self::APPROVED_DISAPPROVED;
360 TopicTable::update($this->getId(), ["APPROVED" => self::APPROVED_DISAPPROVED]);
361
362 // region 1. Change rights for search indexes
363 if (Main\Loader::includeModule("search") && $this->forum["INDEXATION"] == "Y")
364 {
365 $res = $this->forum->getPermissions();
366 $groups = [1];
367 foreach ($res as $group => $permission)
368 {
369 if ($permission >= Permission::CAN_MODERATE)
370 {
371 $groups[] = $group;
372 }
373 }
374 \CSearch::ChangePermission("forum", $groups, false, $this->forum["ID"], $this->getId());
375 }
376 //endregion
377 //region 2. Update MessageTable & Forum Statistic
378 $connection = Main\Application::getInstance()->getConnection();
379 $connection->queryExecute("UPDATE " . MessageTable::getTableName() . " SET APPROVED='" . Message::APPROVED_DISAPPROVED . "' WHERE TOPIC_ID={$this->getId()}");
380 $this->forum->calculateStatistic();
381 Forum\Statistic\User::runForTopic($this->getId());
382 //endregion\
383
384 \CForumEventLog::Log("topic", "disapprove", $this->getId(), serialize($this->data));
385 $event = new Main\Event("forum", "onTopicModerate", [$this->getId(), $this->data]);
386 $event->send();
387 $result->setData(["APPROVED" => self::APPROVED_DISAPPROVED]);
388 }
389 return $result;
390 }
391
392 public function approve()
393 {
394 $result = new Main\Result();
395 if ($this->data["APPROVED"] !== self::APPROVED_APPROVED)
396 {
397 $this->data["APPROVED"] = self::APPROVED_APPROVED;
398 TopicTable::update($this->getId(), ["APPROVED" => self::APPROVED_APPROVED]);
399
400 // region 1. Change rights for search indexes
401 if (Main\Loader::includeModule("search") && $this->forum["INDEXATION"] == "Y")
402 {
403 $res = $this->forum->getPermissions();
404 $groups = [];
405 foreach ($res as $group => $permission)
406 {
407 if ($permission > Permission::ACCESS_DENIED)
408 {
409 $groups[] = $group;
410 }
411 }
412 \CSearch::ChangePermission("forum", $groups, false, $this->forum["ID"], $this->getId());
413 }
414 //endregion
415 //region 2. Update MessageTable & Forum Statistic
416 $connection = Main\Application::getInstance()->getConnection();
417 $connection->queryExecute("UPDATE " . MessageTable::getTableName() . " SET APPROVED='" . Message::APPROVED_APPROVED . "' WHERE TOPIC_ID={$this->getId()}");
418 $this->forum->calculateStatistic();
419 \Bitrix\Forum\Statistic\User::runForTopic($this->getId());
420 //endregion\
421
422 \CForumEventLog::Log("topic", "approve", $this->getId(), serialize($this->data));
423 $event = new Main\Event("forum", "onTopicModerate", [$this->getId(), $this->data]);
424 $event->send();
425 $result->setData(["APPROVED" => self::APPROVED_APPROVED]);
426 }
427 return $result;
428 }
429
430 public function remove()
431 {
432 Forum\Statistic\User::runForTopic($this->getId());
433 if (self::delete($this->getId())->isSuccess())
434 {
435 Forum\Integration\Search\Topic::deleteIndex($this);
436 Forum\Forum::getById($this->getForumId())->calculateStatistic();
437 $this->destroy();
438 }
439 }
444 public static function create($parentObject, array $fields)
445 {
446 global $USER_FIELD_MANAGER;
447
448 $forum = Forum\Forum::getInstance($parentObject);
449 $date = new Main\Type\DateTime($fields["START_DATE"] ?: $fields["POST_DATE"]);
450 $author = [
451 "ID" => $fields["USER_START_ID"] ?: $fields["AUTHOR_ID"],
452 "NAME" => $fields["USER_START_NAME"] ?: $fields["AUTHOR_NAME"]
453 ];
454
455 $topicData = [
456 "TITLE" => $fields["TITLE"],
457 "TITLE_SEO" => (array_key_exists("TITLE_SEO", $fields) ? $fields["TITLE_SEO"] : ""),
458 "TAGS" => $fields["TAGS"],
459 "DESCRIPTION" => $fields["DESCRIPTION"],
460 "ICON" => $fields["ICON"],
461 "STATE" => $fields["STATE"] ?: Topic::STATE_OPENED,
462 "APPROVED" => $fields["APPROVED"],
463
464 "POSTS" => 0,
465 "POSTS_SERVICE" => 0,
466 "POSTS_UNAPPROVED" => 0,
467
468 "USER_START_ID" => $author["ID"],
469 "USER_START_NAME" => $author["NAME"],
470 "START_DATE" => $date,
471
472 "LAST_POSTER_ID" => $author["ID"],
473 "LAST_POSTER_NAME" => $author["NAME"],
474 "LAST_POST_DATE" => $date,
475 "LAST_MESSAGE_ID" => 0,
476
477 "ABS_LAST_POSTER_ID" => $author["ID"],
478 "ABS_LAST_POSTER_NAME" => $author["NAME"],
479 "ABS_LAST_POST_DATE" => $date,
480 "ABS_LAST_MESSAGE_ID" => 0,
481
482 "XML_ID" => $fields["TOPIC_XML_ID"],
483
484 "OWNER_ID" => $fields["OWNER_ID"] ?: null,
485 "SOCNET_GROUP_ID" => $fields["SOCNET_GROUP_ID"] ?: null
486 ];
487 $result = Topic::add($forum, $topicData);
488 if ($result->isSuccess())
489 {
490 $messageData = array(
491 "NEW_TOPIC" => "Y",
492 "APPROVED" => $topicData["APPROVED"],
493
494 "USE_SMILES" => $fields["USE_SMILES"],
495 "POST_DATE" => $date,
496 "POST_MESSAGE" => $fields["POST_MESSAGE"],
497
498 "ATTACH_IMG" => $fields["ATTACH_IMG"],
499 "FILES" => $fields["FILES"],
500
501 "PARAM1" => $fields["PARAM1"],
502 "PARAM2" => $fields["PARAM2"],
503
504 "AUTHOR_ID" => $author["ID"],
505 "AUTHOR_NAME" => $author["NAME"],
506 "AUTHOR_EMAIL" => $fields["AUTHOR_EMAIL"],
507
508 "AUTHOR_IP" => $fields["AUTHOR_IP"] ?? null,
509 "AUTHOR_REAL_IP" => $fields["AUTHOR_REAL_IP"] ?? null,
510 "GUEST_ID" => $fields["GUEST_ID"] ?? null,
511 );
512
513 $messageData += array_intersect_key($fields, $USER_FIELD_MANAGER->getUserFields(MessageTable::getUfId()));
514
515 $topic = Topic::getById($result->getId());
516 $result = Message::add($topic, $messageData);
517
518 if ($result->isSuccess())
519 {
520 $result->setData(["MESSAGE_ID" => $result->getId(), "TOPIC_ID" => $topic->getId()]);
521 $message = MessageTable::getDataById($result->getId());
522 //region Update statistic & Seacrh
523 TopicTable::update($topic->getId(), ["LAST_MESSAGE_ID" => $message["ID"], "ABS_LAST_MESSAGE_ID" => $message["ID"]]);
524 User::getById($message["AUTHOR_ID"])->incrementStatistic($message);
525 $forum->incrementStatistic($message);
526 \Bitrix\Forum\Integration\Search\Message::index($forum, $topic, $message);
527 //endregion
528 }
529 else
530 {
531 TopicTable::delete($topic->getId());
532 $topic->destroy();
533 }
534 }
535
536 return $result;
537 }
538
539 public static function add(Forum\Forum $forum, array &$data)
540 {
541 $data['FORUM_ID'] = $forum->getId();
542 $result = new Main\ORM\Data\AddResult();
543 if (($events = GetModuleEvents("forum", "onBeforeTopicAdd", true)) && !empty($events))
544 {
545 global $APPLICATION;
546 foreach ($events as $ev)
547 {
548 $APPLICATION->ResetException();
549
550 if (ExecuteModuleEventEx($ev, array(&$data)) === false)
551 {
552 $errorMessage = Loc::getMessage("FORUM_EVENT_BEFORE_TOPIC_ADD");
553 if (($ex = $APPLICATION->GetException()) && ($ex instanceof \CApplicationException))
554 {
555 $errorMessage = $ex->getString();
556 }
557
558 $result->addError(new Main\Error($errorMessage, "onBeforeTopicAdd"));
559 return $result;
560 }
561 }
562 }
563
564 $dbResult = TopicTable::add($data);
565
566 if (!$dbResult->isSuccess())
567 {
568 $result->addErrors($dbResult->getErrors());
569 }
570 else
571 {
572 $id = $dbResult->getId();
573 $result->setId($id);
574 foreach (GetModuleEvents("forum", "onAfterTopicAdd", true) as $event)
575 {
576 ExecuteModuleEventEx($event, [$id, $data]);
577 }
578 }
579
580 return $result;
581 }
582
586 public function edit(array $fields)
587 {
588 $fieldsBefore = $this->data;
589 //region update Message table and topic table
590 if (!($m = MessageTable::getList([
591 "select" => ["*"],
592 "filter" => ["TOPIC_ID" => $this->getId(), "NEW_TOPIC" => "Y"],
593 "limit" => 1
594 ])->fetch()))
595 {
596 throw new Main\ObjectException(Loc::getMessage("FORUM_ERROR_FIRST_POST_WAS_NOT_FOUND"));
597 }
598 $result = Message::update($m["ID"], $fields);
599 if ($result->isSuccess())
600 {
601 $topicData = array_intersect_key(
602 $fields,
603 array_flip([
604 "TITLE",
605 "TITLE_SEO",
606 "TAGS",
607 "DESCRIPTION",
608 "ICON",
609 "USER_START_NAME"
610 ])
611 );
612 if (array_key_exists("AUTHOR_NAME", $fields))
613 {
614 $topicData["USER_START_NAME"] = $fields["AUTHOR_NAME"];
615 }
616
617 if (!empty($topicData))
618 {
619 $result = Topic::update($this->getId(), $topicData);
620 if ($result->isSuccess())
621 {
622 $this->data = TopicTable::getById($this->getId())->fetch();
623 }
624 }
625 }
626
627 if (!$result->isSuccess())
628 {
629 return $result;
630 }
631 //endregion
632
633 $fieldsAfter = $this->data;
634
635 $result->setPrimary(["ID" => $m["ID"]]);
636 $result->setData(["MESSAGE_ID" => $m["ID"], "TOPIC_ID" => $this->getId()]);
637
638 //region Search indexation
639 $forum = \Bitrix\Forum\Forum::getById($this->getForumId());
640 if ($forum["INDEXATION"] === "Y" && $fieldsBefore !== $fieldsAfter)
641 {
642 $searchKeys = ["TITLE", "TITLE_SEO", "TAGS", "DESCRIPTION"];
643 $diffData = array_intersect_key(
644 array_diff_assoc($fieldsAfter, $fieldsBefore),
645 array_flip($searchKeys)
646 );
647 if (array_key_exists('TITLE', $diffData)
648 || array_key_exists('TITLE_SEO', $diffData))
649 {
650 Forum\Integration\Search\Topic::reindex($this->getId());
651 }
652 else
653 {
654 Forum\Integration\Search\Message::index(
656 $this,
657 Forum\MessageTable::getById($m["ID"])->fetch()
658 );
659 }
660 }
661 //endregion
662 $fieldsAfter += Forum\MessageTable::getDataById($m["ID"]);
663 $log = array_diff_assoc(
664 array_intersect_key($fieldsBefore + $m, $fields),
665 $fieldsAfter
666 );
667 array_walk($log,
668 function (&$item, $key, $fieldsAfter)
669 {
670 $item = [
671 'before' => $item,
672 'after' => $fieldsAfter[$key]
673 ];
674 }, $fieldsAfter);
675 unset($log["HTML"]);
676 unset($log["EDIT_DATE"]);
677 \CForumEventLog::Log("topic", "edit", $this->getId(), serialize($log));
678
679 return $result;
680 }
681
682 public static function update(int $id, array &$data)
683 {
684 unset($data["FORUM_ID"]);
685
686 $topic = Forum\TopicTable::getById($id)->fetch();
687
688 $result = new Main\ORM\Data\UpdateResult();
689 $result->setPrimary(["ID" => $id]);
690
691 if (($events = GetModuleEvents("forum", "onBeforeTopicUpdate", true)) && !empty($events))
692 {
693 global $APPLICATION;
694 foreach ($events as $ev)
695 {
696 $APPLICATION->ResetException();
697
698 if (ExecuteModuleEventEx($ev, array($id, &$data, $topic)) === false)
699 {
700 $errorMessage = Loc::getMessage("FORUM_EVENT_BEFORE_TOPIC_UPDATE_ERROR");
701 if (($ex = $APPLICATION->GetException()) && ($ex instanceof \CApplicationException))
702 {
703 $errorMessage = $ex->getString();
704 }
705 $result->addError(new Main\Error($errorMessage, "onBeforeTopicAdd"));
706 return $result;
707 }
708 }
709 }
710
711 $dbResult = TopicTable::update($id, $data);
712
713 if (!$dbResult->isSuccess())
714 {
715 $result->addErrors($dbResult->getErrors());
716 }
717 else
718 {
719 foreach (GetModuleEvents("forum", "onAfterTopicUpdate", true) as $event)
720 {
721 ExecuteModuleEventEx($event, [$id, $data, []]);
722 }
723 }
724 return $result;
725 }
726
727 public static function delete(int $id)
728 {
729 $result = new Main\Orm\Data\DeleteResult();
730 if (!($topicData = Forum\TopicTable::getById($id)->fetch()))
731 {
732 return $result;
733 }
734
735 /***************** Event onBeforeTopicDelete ***********************/
736 foreach (GetModuleEvents("forum", "onBeforeTopicDelete", true) as $arEvent)
737 {
738 if (ExecuteModuleEventEx($arEvent, [$id, $topicData]) === false)
739 {
740 global $APPLICATION;
741 $errorMessage = 'onBeforeTopicDelete';
742 if (($ex = $APPLICATION->GetException()) && ($ex instanceof \CApplicationException))
743 {
744 $errorMessage = $ex->getString();
745 }
746 $result->addError(new Main\Error($errorMessage, 'onBeforeTopicDelete'));
747 return $result;
748 }
749 }
750 /***************** /Event ******************************************/
751
752 Forum\Internals\MessageCleaner::runForTopic($id);
753 Main\Application::getConnection()->queryExecute("DELETE FROM b_forum_subscribe WHERE TOPIC_ID = ".$id);
754 Main\Application::getConnection()->queryExecute("DELETE FROM b_forum_message WHERE TOPIC_ID = ".$id);
755 Main\Application::getConnection()->queryExecute("DELETE FROM b_forum_user_topic WHERE TOPIC_ID = ".$id);
756 Main\Application::getConnection()->queryExecute("DELETE FROM b_forum_topic WHERE ID = ".$id);
757 Main\Application::getConnection()->queryExecute("DELETE FROM b_forum_topic WHERE TOPIC_ID = ".$id);
758 Main\Application::getConnection()->queryExecute("DELETE FROM b_forum_stat WHERE TOPIC_ID = ".$id);
759
760 /***************** Event onAfterTopicDelete ************************/
761 foreach(GetModuleEvents("forum", "onAfterTopicDelete", true) as $arEvent)
762 {
763 ExecuteModuleEventEx($arEvent, [&$id, &$topicData]);
764 }
765 /***************** /Event ******************************************/
766 return $result;
767 }
768
769 public function incrementStatistic(array $message)
770 {
771 $fields = array(
772 "ABS_LAST_POSTER_ID" => $message["AUTHOR_ID"],
773 "ABS_LAST_POSTER_NAME" => $message["AUTHOR_NAME"],
774 "ABS_LAST_POST_DATE" => $message["POST_DATE"],
775 "ABS_LAST_MESSAGE_ID" => $message["ID"]
776 );
777 $this->data = array_merge($this->data, $fields);
778 if ($message["APPROVED"] == "Y")
779 {
780 $fields += [
781 "APPROVED" => "Y",
782 "LAST_POSTER_ID" => $message["AUTHOR_ID"],
783 "LAST_POSTER_NAME" => $message["AUTHOR_NAME"],
784 "LAST_POST_DATE" => $message["POST_DATE"],
785 "LAST_MESSAGE_ID" => $message["ID"]
786 ];
787 $this->data = array_merge($this->data, $fields);
788 if ($message["NEW_TOPIC"] != "Y")
789 {
790 $fields["POSTS"] = new Main\DB\SqlExpression('?# + 1', "POSTS");
791 $this->data["POSTS"]++;
792 }
793 if (!empty($message["SERVICE_TYPE"]))
794 {
795 $fields["POSTS_SERVICE"] = new Main\DB\SqlExpression('?# + 1', "POSTS_SERVICE");
796 $this->data["POSTS_SERVICE"]++;
797 }
798 }
799 else
800 {
801 $fields["POSTS_UNAPPROVED"] = new Main\DB\SqlExpression('?# + 1', "POSTS_UNAPPROVED");
802 $this->data["POSTS_UNAPPROVED"]++;
803 }
804 return TopicTable::update($this->getId(), $fields);
805 }
806
807 public function decrementStatistic(array $message)
808 {
809 $fields = [];
810 if ($message['APPROVED'] == 'Y')
811 {
812 $fields['POSTS'] = new Main\DB\SqlExpression('CASE WHEN ?# > 0 THEN ?# - 1 ELSE 0 END', 'POSTS', 'POSTS');
813 $this->data['POSTS']--;
814 if (!empty($message["SERVICE_TYPE"]))
815 {
816 $fields["POSTS_SERVICE"] = new Main\DB\SqlExpression('?# - 1', "POSTS_SERVICE");
817 $this->data["POSTS_SERVICE"]--;
818 }
819 }
820 else
821 {
822 $fields['POSTS_UNAPPROVED'] = new Main\DB\SqlExpression('?# - 1', 'POSTS_UNAPPROVED');
823 $this->data['POSTS_UNAPPROVED']--;
824 }
825
826 if ($message['NEW_TOPIC'] === 'Y' && ($firstMessage = MessageTable::getList([
827 'select' => ['*'],
828 'filter' => ['TOPIC_ID' => $this->getId()],
829 'order' => ['ID' => 'ASC'],
830 'limit' => 1
831 ])->fetch()))
832 {
833 $fields['USER_START_ID'] = $firstMessage['AUTHOR_ID'];
834 $fields['USER_START_NAME'] = $firstMessage['AUTHOR_NAME'];
835 $fields['START_DATE'] = $firstMessage['POST_DATE'];
836 if ($this->data['APPROVED'] === 'Y' && $firstMessage['APPROVED'] !== 'Y' && $this->data['POSTS'] < 0)
837 {
838 $fields['APPROVED'] = 'N';
839 }
840 }
841 else if ($message['ID'] >= $this->data['LAST_MESSAGE_ID'])
842 {
843 if ($message['ID'] === $this->data['LAST_MESSAGE_ID']
844 &&
845 ($lastMessage = Forum\MessageTable::getList([
846 'select' => ['ID', 'AUTHOR_ID', 'AUTHOR_NAME', 'POST_DATE'],
847 'filter' => [
848 'TOPIC_ID' => $this->getId(),
850 ],
851 'order' => ['ID' => 'DESC'],
852 'limit' => 1
853 ])->fetch())
854 )
855 {
856 $fields += [
857 'LAST_POSTER_ID' => $lastMessage['AUTHOR_ID'],
858 'LAST_POSTER_NAME' => $lastMessage['AUTHOR_NAME'],
859 'LAST_POST_DATE' => $lastMessage['POST_DATE'],
860 'LAST_MESSAGE_ID' => $lastMessage['ID']
861 ];
862 }
863
864 if ($message['ID'] === $this->data['ABS_LAST_MESSAGE_ID']
865 &&
866 ($lastMessage = Forum\MessageTable::getList([
867 'select' => ['ID', 'AUTHOR_ID', 'AUTHOR_NAME', 'POST_DATE'],
868 'filter' => ['TOPIC_ID' => $this->getId()],
869 'order' => ['ID' => 'DESC'],
870 'limit' => 1
871 ])->fetch())
872 )
873 {
874 $fields += [
875 'ABS_LAST_POSTER_ID' => $lastMessage['AUTHOR_ID'],
876 'ABS_LAST_POSTER_NAME' => $lastMessage['AUTHOR_NAME'],
877 'ABS_LAST_POST_DATE' => $lastMessage['POST_DATE'],
878 'ABS_LAST_MESSAGE_ID' => $lastMessage['ID']
879 ];
880 }
881 }
882 if (!empty($fields))
883 {
884 $this->data = array_merge($this->data, array_filter($fields, function($item) {
885 return !($item instanceof Main\DB\SqlExpression);
886 }));
887 $result = TopicTable::update($this->getId(), $fields);
888 }
889 else
890 {
891 $result = new Main\ORM\Data\UpdateResult;
892 }
893 if (isset($firstMessage) && $firstMessage)
894 {
895 $result->setData($firstMessage);
896 }
897 return $result;
898 }
899
900 public function incrementViews()
901 {
902 TopicTable::update($this->getId(), ['VIEWS' => new Main\DB\SqlExpression('?# + 1', 'VIEWS')]);
903 }
904}
static getTableName()
Definition file.php:62
static add(Forum\Topic $topic, array $fields)
Definition message.php:804
static update($id, array &$fields)
Definition message.php:709
static getDataById($id, $ttl=84600)
Definition message.php:377
edit(array $fields)
Definition topic.php:586
const APPROVED_DISAPPROVED
Definition topic.php:276
incrementStatistic(array $message)
Definition topic.php:769
decrementStatistic(array $message)
Definition topic.php:807
const APPROVED_APPROVED
Definition topic.php:275
moveToForum(int $forumId)
Definition topic.php:298
static update(int $id, array &$data)
Definition topic.php:682
static create($parentObject, array $fields)
Definition topic.php:444
static add(Forum\Forum $forum, array &$data)
Definition topic.php:539
static getFilteredFields()
Definition topic.php:124
static onAfterUpdate(Main\ORM\Event $event)
Definition topic.php:228
static getTableName()
Definition topic.php:77
getParameter($key)
Definition event.php:80
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
setData(array $data)
Definition result.php:113