1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
topic.php
См. документацию.
1<?php
2namespace Bitrix\Forum;
3
4use Bitrix\Main;
5use Bitrix\Forum;
6use Bitrix\Main\Localization\Loc;
7use Bitrix\Main\ORM\Event;
8use Bitrix\Main\ORM\Fields\BooleanField;
9use Bitrix\Main\ORM\Fields\DatetimeField;
10use Bitrix\Main\ORM\Fields\EnumField;
11use Bitrix\Main\ORM\Fields\IntegerField;
12use Bitrix\Main\ORM\Fields\Relations\Reference;
13use Bitrix\Main\ORM\Fields\StringField;
14use Bitrix\Main\ORM\Fields\TextField;
15use Bitrix\Main\ORM\Query\Join;
16use Bitrix\Main\Type\DateTime;
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 {
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 {
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 = null;
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 $topic = TopicTable::getById($id)->fetch();
193 $res = [];
194 foreach ($filteredFields as $key)
195 {
196 $res[$key] = $val = array_key_exists($key, $data) ? $data[$key] : $topic[$key];
197 if (!empty($val))
198 {
199 $res[$key] = \CFilterUnquotableWords::Filter($val);
200 if (empty($res[$key]))
201 {
202 $res[$key] = "*";
203 }
204 }
205 }
206 $data["HTML"] = serialize($res);
207 }
208 }
209 if (array_key_exists("TITLE_SEO", $data) || array_key_exists("TITLE", $data))
210 {
211 $data["TITLE_SEO"] = trim($data["TITLE_SEO"], " -");
212 if ($data["TITLE_SEO"] == '')
213 {
214 if (array_key_exists("TITLE", $data))
215 {
216 $title = $data["TITLE"];
217 }
218 else
219 {
220 if (is_null($topic))
221 {
222 $res = \Bitrix\Forum\TopicTable::query()->setSelect(['TITLE'])
223 ->where('ID', $id)
224 ->fetch();
225 $title = $res["TITLE"];
226 }
227 else
228 {
229 $title = $topic["TITLE"];
230 }
231 }
232 $data["TITLE_SEO"] = \CUtil::translit($title, LANGUAGE_ID, array("max_len"=>255, "safe_chars"=>".", "replace_space" => '-'));
233 }
234 }
235 if ($data != $event->getParameter("fields"))
236 {
237 $result->modifyFields($data);
238 }
239 return self::modifyData($event, $result);
240 }
241
246 public static function onAfterUpdate(Main\ORM\Event $event)
247 {
248 $id = $event->getParameter("id");
249 $id = $id["ID"];
250 $fields = $event->getParameter("fields");
251 if (array_key_exists("FORUM_ID", $fields))
252 {
253 $connection = Main\Application::getInstance()->getConnection();
254 $connection->queryExecute("UPDATE " . FileTable::getTableName() . " SET FORUM_ID={$fields["FORUM_ID"]} WHERE TOPIC_ID={$id}");
255 $connection->queryExecute("UPDATE " . MessageTable::getTableName() . " SET FORUM_ID={$fields["FORUM_ID"]} WHERE TOPIC_ID={$id}");
256 $connection->queryExecute("UPDATE " . SubscribeTable::getTableName() . " SET FORUM_ID={$fields["FORUM_ID"]} WHERE TOPIC_ID={$id}");
257 }
258 }
259
265 private static function modifyData(Main\ORM\Event $event, Main\ORM\EventResult $result)
266 {
267 $data = array_merge($event->getParameter("fields"), $result->getModified());
268 $fields = [];
269
270 //region check image
271 $key = array_key_exists("VIEWS", $data) ? "VIEWS" : (array_key_exists("=VIEWS", $data) ? "=VIEWS" : null);
272 if ($key !== null && str_replace(" ", "", $data[$key]) === "VIEWS+1")
273 {
274 unset($data[$key]);
275 $fields["VIEWS"] = new Main\DB\SqlExpression('?# + 1', 'VIEWS');
276 }
277 //endregion
278 if (!empty($fields))
279 {
280 $result->modifyFields(array_merge($result->getModified(), $fields));
281 }
282 return $result;
283 }
284}
285
287{
288 use \Bitrix\Forum\Internals\EntityFabric;
289
290 public const STATE_LINK = "L";
291 public const STATE_CLOSED = "N";
292 public const STATE_OPENED = "Y";
293 public const APPROVED_APPROVED = "Y";
294 public const APPROVED_DISAPPROVED = "N";
295
296 public function __construct($id)
297 {
298 if ($id <= 0)
299 {
300 throw new Main\ArgumentNullException("Topic id");
301 }
302 parent::__construct($id);
303 }
304
305 protected function init()
306 {
307 if (!($this->data = TopicTable::getById($this->id)->fetch()))
308 {
309 throw new Main\ObjectNotFoundException(Loc::getMessage("F_ERROR_TID_IS_LOST", ["#id#" => $this->id]));
310 }
311 $this->authorId = intval($this->data["USER_START_ID"]);
312 $this->data["~TITLE_SEO"] = $this->data["TITLE_SEO"];
313 $this->data["TITLE_SEO"] = implode("-", [$this->data["ID"], $this->data["TITLE_SEO"]]);
314 }
315
316 public function moveToForum(int $forumId)
317 {
318 $result = new Main\Result();
319 if ($forumId == $this->forum->getId())
320 {
321 return $result;
322 }
323 $newForum = \Bitrix\Forum\Forum::getById($forumId);
324 TopicTable::update($this->getId(), ["FORUM_ID" => $newForum->getId()]);
325 $this->forum->calculateStatistic();
326 $newForum->calculateStatistic();
327
329 return $result;
330 }
331
332 public function open()
333 {
334 $result = new Main\Result();
335 if ($this->data["STATE"] === self::STATE_CLOSED)
336 {
337 if (Forum\TopicTable::update($this->getId(), ["STATE" => self::STATE_OPENED])->isSuccess())
338 {
339 $this->data["STATE"] = self::STATE_OPENED;
340 \CForumEventLog::Log("topic", "open", $this->getId(), serialize($this->data));
341 $result->setData(["STATE" => self::STATE_OPENED]);
342 (new Main\Event("forum", "onTopicOpen", [$this->getId(), $this->data]))->send();
343 }
344 else
345 {
346 $result->addError(new Main\Error("Topic is not opened."));
347 }
348 }
349 return $result;
350 }
351
352 public function close()
353 {
354 $result = new Main\Result();
355 if ($this->data["STATE"] === self::STATE_OPENED)
356 {
357 if (Forum\TopicTable::update($this->getId(), ["STATE" => self::STATE_CLOSED])->isSuccess())
358 {
359 $this->data["STATE"] = self::STATE_CLOSED;
360 \CForumEventLog::Log("topic", "close", $this->getId(), serialize($this->data));
361 $result->setData(["STATE" => self::STATE_CLOSED]);
362 (new Main\Event("forum", "onTopicClose", [$this->getId(), $this->data]))->send();
363 }
364 else
365 {
366 $result->addError(new Main\Error("Topic is not closed."));
367 }
368 }
369 return $result;
370 }
371
372 public function disapprove()
373 {
374 $result = new Main\Result();
375 if ($this->data["APPROVED"] === self::APPROVED_APPROVED)
376 {
377 $this->data["APPROVED"] = self::APPROVED_DISAPPROVED;
378 TopicTable::update($this->getId(), ["APPROVED" => self::APPROVED_DISAPPROVED]);
379
380 // region 1. Change rights for search indexes
381 if (Main\Loader::includeModule("search") && $this->forum["INDEXATION"] == "Y")
382 {
383 $res = $this->forum->getPermissions();
384 $groups = [1];
385 foreach ($res as $group => $permission)
386 {
387 if ($permission >= Permission::CAN_MODERATE)
388 {
389 $groups[] = $group;
390 }
391 }
392 \CSearch::ChangePermission("forum", $groups, false, $this->forum["ID"], $this->getId());
393 }
394 //endregion
395 //region 2. Update MessageTable & Forum Statistic
396 $connection = Main\Application::getInstance()->getConnection();
397 $connection->queryExecute("UPDATE " . MessageTable::getTableName() . " SET APPROVED='" . Message::APPROVED_DISAPPROVED . "' WHERE TOPIC_ID={$this->getId()}");
398 $this->forum->calculateStatistic();
400 //endregion\
401
402 \CForumEventLog::Log("topic", "disapprove", $this->getId(), serialize($this->data));
403 $event = new Main\Event("forum", "onTopicModerate", [$this->getId(), $this->data]);
404 $event->send();
405 $result->setData(["APPROVED" => self::APPROVED_DISAPPROVED]);
406 }
407 return $result;
408 }
409
410 public function approve()
411 {
412 $result = new Main\Result();
413 if ($this->data["APPROVED"] !== self::APPROVED_APPROVED)
414 {
415 $this->data["APPROVED"] = self::APPROVED_APPROVED;
416 TopicTable::update($this->getId(), ["APPROVED" => self::APPROVED_APPROVED]);
417
418 // region 1. Change rights for search indexes
419 if (Main\Loader::includeModule("search") && $this->forum["INDEXATION"] == "Y")
420 {
421 $res = $this->forum->getPermissions();
422 $groups = [];
423 foreach ($res as $group => $permission)
424 {
425 if ($permission > Permission::ACCESS_DENIED)
426 {
427 $groups[] = $group;
428 }
429 }
430 \CSearch::ChangePermission("forum", $groups, false, $this->forum["ID"], $this->getId());
431 }
432 //endregion
433 //region 2. Update MessageTable & Forum Statistic
434 $connection = Main\Application::getInstance()->getConnection();
435 $connection->queryExecute("UPDATE " . MessageTable::getTableName() . " SET APPROVED='" . Message::APPROVED_APPROVED . "' WHERE TOPIC_ID={$this->getId()}");
436 $this->forum->calculateStatistic();
438 //endregion\
439
440 \CForumEventLog::Log("topic", "approve", $this->getId(), serialize($this->data));
441 $event = new Main\Event("forum", "onTopicModerate", [$this->getId(), $this->data]);
442 $event->send();
443 $result->setData(["APPROVED" => self::APPROVED_APPROVED]);
444 }
445 return $result;
446 }
447
448 public function remove()
449 {
451 if (self::deleteTopic($this->getId(), $this->data)->isSuccess())
452 {
454 Forum\Forum::getById($this->getForumId())->calculateStatistic();
455 $this->destroy();
456 }
457 }
458
462 public static function create($parentObject, array $fields)
463 {
464 global $USER_FIELD_MANAGER;
465
466 $forum = Forum\Forum::getInstance($parentObject);
467 $date = new Main\Type\DateTime($fields["START_DATE"] ?? $fields["POST_DATE"] ?? null);
468 $author = [
469 "ID" => $fields["USER_START_ID"] ?? $fields["AUTHOR_ID"],
470 "NAME" => $fields["USER_START_NAME"] ?? $fields["AUTHOR_NAME"]
471 ];
472
473 $topicData = [
474 "TITLE" => $fields["TITLE"],
475 "TITLE_SEO" => $fields["TITLE_SEO"] ?? '',
476 "TAGS" => $fields["TAGS"] ?? '',
477 "DESCRIPTION" => $fields["DESCRIPTION"] ?? '',
478 "ICON" => $fields["ICON"] ?? '',
479 "STATE" => $fields["STATE"] ?? Topic::STATE_OPENED,
480 "APPROVED" => $fields["APPROVED"] ?? 'Y',
481
482 "POSTS" => 0,
483 "POSTS_SERVICE" => 0,
484 "POSTS_UNAPPROVED" => 0,
485
486 "USER_START_ID" => $author["ID"],
487 "USER_START_NAME" => $author["NAME"],
488 "START_DATE" => $date,
489
490 "LAST_POSTER_ID" => $author["ID"],
491 "LAST_POSTER_NAME" => $author["NAME"],
492 "LAST_POST_DATE" => $date,
493 "LAST_MESSAGE_ID" => 0,
494
495 "ABS_LAST_POSTER_ID" => $author["ID"],
496 "ABS_LAST_POSTER_NAME" => $author["NAME"],
497 "ABS_LAST_POST_DATE" => $date,
498 "ABS_LAST_MESSAGE_ID" => 0,
499
500 "XML_ID" => $fields["TOPIC_XML_ID"] ?? '',
501
502 "OWNER_ID" => $fields["OWNER_ID"] ?? null,
503 "SOCNET_GROUP_ID" => $fields["SOCNET_GROUP_ID"] ?? null
504 ];
505 $result = Topic::add($forum, $topicData);
506 if ($result->isSuccess())
507 {
508 $messageData = array(
509 "NEW_TOPIC" => "Y",
510 "APPROVED" => $topicData["APPROVED"],
511
512 "USE_SMILES" => $fields["USE_SMILES"] ?? 'Y',
513 "POST_DATE" => $date,
514 "POST_MESSAGE" => $fields["POST_MESSAGE"],
515
516 "ATTACH_IMG" => $fields["ATTACH_IMG"] ?? null,
517 "FILES" => $fields["FILES"] ?? null,
518
519 "PARAM1" => $fields["PARAM1"] ?? null,
520 "PARAM2" => $fields["PARAM2"] ?? null,
521
522 "AUTHOR_ID" => $author["ID"],
523 "AUTHOR_NAME" => $author["NAME"],
524 "AUTHOR_EMAIL" => $fields["AUTHOR_EMAIL"] ?? '',
525
526 "AUTHOR_IP" => $fields["AUTHOR_IP"] ?? null,
527 "AUTHOR_REAL_IP" => $fields["AUTHOR_REAL_IP"] ?? null,
528 "GUEST_ID" => $fields["GUEST_ID"] ?? null,
529 );
530
531 $messageData += array_intersect_key($fields, $USER_FIELD_MANAGER->getUserFields(MessageTable::getUfId()));
532
533 $topic = Topic::getById($result->getId());
534 $result = Message::add($topic, $messageData);
535
536 if ($result->isSuccess())
537 {
538 $result->setData(["MESSAGE_ID" => $result->getId(), "TOPIC_ID" => $topic->getId()]);
540 //region Update statistic & Seacrh
541 TopicTable::update($topic->getId(), ["LAST_MESSAGE_ID" => $message["ID"], "ABS_LAST_MESSAGE_ID" => $message["ID"]]);
542 User::getById($message["AUTHOR_ID"])->incrementStatistic($message);
543 $forum->incrementStatistic($message);
545 //endregion
546 }
547 else
548 {
549 TopicTable::delete($topic->getId());
550 $topic->destroy();
551 }
552 }
553
554 return $result;
555 }
556
557 public static function add(Forum\Forum $forum, array &$data)
558 {
559 $data['FORUM_ID'] = $forum->getId();
561 if (($events = GetModuleEvents("forum", "onBeforeTopicAdd", true)) && !empty($events))
562 {
563 global $APPLICATION;
564 foreach ($events as $ev)
565 {
566 $APPLICATION->ResetException();
567
568 if (ExecuteModuleEventEx($ev, array(&$data)) === false)
569 {
570 $errorMessage = Loc::getMessage("FORUM_EVENT_BEFORE_TOPIC_ADD");
571 if (($ex = $APPLICATION->GetException()) && ($ex instanceof \CApplicationException))
572 {
573 $errorMessage = $ex->getString();
574 }
575
576 $result->addError(new Main\Error($errorMessage, "onBeforeTopicAdd"));
577 return $result;
578 }
579 }
580 }
581
582 $dbResult = TopicTable::add($data);
583
584 if (!$dbResult->isSuccess())
585 {
586 $result->addErrors($dbResult->getErrors());
587 }
588 else
589 {
590 $id = $dbResult->getId();
591 $result->setId($id);
592 foreach (GetModuleEvents("forum", "onAfterTopicAdd", true) as $event)
593 {
595 }
596 }
597
598 return $result;
599 }
600
604 public function edit(array $fields)
605 {
606 $fieldsBefore = $this->data;
607 //region update Message table and topic table
608 if (!($m = MessageTable::getList([
609 "select" => ["*"],
610 "filter" => ["TOPIC_ID" => $this->getId(), "NEW_TOPIC" => "Y"],
611 "limit" => 1
612 ])->fetch()))
613 {
614 throw new Main\ObjectException(Loc::getMessage("FORUM_ERROR_FIRST_POST_WAS_NOT_FOUND"));
615 }
616 $result = Message::update($m["ID"], $fields);
617 if ($result->isSuccess())
618 {
619 $topicData = array_intersect_key(
620 $fields,
621 array_flip([
622 "TITLE",
623 "TITLE_SEO",
624 "TAGS",
625 "DESCRIPTION",
626 "ICON",
627 "USER_START_NAME"
628 ])
629 );
630 if (array_key_exists("AUTHOR_NAME", $fields))
631 {
632 $topicData["USER_START_NAME"] = $fields["AUTHOR_NAME"];
633 }
634
635 if (!empty($topicData))
636 {
637 $result = Topic::update($this->getId(), $topicData);
638 if ($result->isSuccess())
639 {
640 $this->data = TopicTable::getById($this->getId())->fetch();
641 }
642 }
643 }
644
645 if (!$result->isSuccess())
646 {
647 return $result;
648 }
649 //endregion
650
651 $fieldsAfter = $this->data;
652
653 $result->setPrimary(["ID" => $m["ID"]]);
654 $result->setData(["MESSAGE_ID" => $m["ID"], "TOPIC_ID" => $this->getId()]);
655
656 //region Search indexation
657 $forum = \Bitrix\Forum\Forum::getById($this->getForumId());
658 if ($forum["INDEXATION"] === "Y" && $fieldsBefore !== $fieldsAfter)
659 {
660 $searchKeys = ["TITLE", "TITLE_SEO", "TAGS", "DESCRIPTION"];
661 $diffData = array_intersect_key(
662 array_diff_assoc($fieldsAfter, $fieldsBefore),
663 array_flip($searchKeys)
664 );
665 if (array_key_exists('TITLE', $diffData)
666 || array_key_exists('TITLE_SEO', $diffData))
667 {
669 }
670 else
671 {
674 $this,
675 Forum\MessageTable::getById($m["ID"])->fetch()
676 );
677 }
678 }
679 //endregion
680 $fieldsAfter += Forum\MessageTable::getDataById($m["ID"]);
681 $log = array_diff_assoc(
682 array_intersect_key($fieldsBefore + $m, $fields),
683 $fieldsAfter
684 );
685 array_walk($log,
686 function (&$item, $key, $fieldsAfter)
687 {
688 $item = [
689 'before' => $item,
690 'after' => $fieldsAfter[$key]
691 ];
692 }, $fieldsAfter);
693 unset($log["HTML"]);
694 unset($log["EDIT_DATE"]);
695 \CForumEventLog::Log("topic", "edit", $this->getId(), serialize($log));
696
697 return $result;
698 }
699
700 public static function update(int $id, array &$data)
701 {
702 unset($data["FORUM_ID"]);
703
705 $result->setPrimary(["ID" => $id]);
706
707 if (($events = GetModuleEvents("forum", "onBeforeTopicUpdate", true)) && !empty($events))
708 {
709 $topic = Forum\TopicTable::getById($id)->fetch();
710 global $APPLICATION;
711 foreach ($events as $ev)
712 {
713 $APPLICATION->ResetException();
714
715 if (ExecuteModuleEventEx($ev, array($id, &$data, $topic)) === false)
716 {
717 $errorMessage = Loc::getMessage("FORUM_EVENT_BEFORE_TOPIC_UPDATE_ERROR");
718 if (($ex = $APPLICATION->GetException()) && ($ex instanceof \CApplicationException))
719 {
720 $errorMessage = $ex->getString();
721 }
722 $result->addError(new Main\Error($errorMessage, "onBeforeTopicAdd"));
723 return $result;
724 }
725 }
726 }
727
728 $dbResult = TopicTable::update($id, $data);
729
730 if (!$dbResult->isSuccess())
731 {
732 $result->addErrors($dbResult->getErrors());
733 }
734 else
735 {
736 foreach (GetModuleEvents("forum", "onAfterTopicUpdate", true) as $event)
737 {
738 ExecuteModuleEventEx($event, [$id, $data, []]);
739 }
740 }
741 return $result;
742 }
743
744 public static function delete(int $id): Main\Orm\Data\DeleteResult
745 {
746 $result = new Main\Orm\Data\DeleteResult();
747 if (!($topicData = Forum\TopicTable::getById($id)->fetch()))
748 {
749 return $result;
750 }
751
752 return self::deleteTopic($id, $topicData);
753 }
754
755 private static function deleteTopic(int $id, array $topicData): Main\Orm\Data\DeleteResult
756 {
757 $result = new Main\Orm\Data\DeleteResult();
758
759 /***************** Event onBeforeTopicDelete ***********************/
760 foreach (GetModuleEvents("forum", "onBeforeTopicDelete", true) as $arEvent)
761 {
762 if (ExecuteModuleEventEx($arEvent, [$id, $topicData]) === false)
763 {
764 global $APPLICATION;
765 $errorMessage = 'onBeforeTopicDelete';
766 if (($ex = $APPLICATION->GetException()) && ($ex instanceof \CApplicationException))
767 {
768 $errorMessage = $ex->getString();
769 }
770 $result->addError(new Main\Error($errorMessage, 'onBeforeTopicDelete'));
771 return $result;
772 }
773 }
774 /***************** /Event ******************************************/
775
776 Forum\Internals\MessageCleaner::runForTopic($id);
777 Main\Application::getConnection()->queryExecute("DELETE FROM b_forum_subscribe WHERE TOPIC_ID = ".$id);
778 Main\Application::getConnection()->queryExecute("DELETE FROM b_forum_message WHERE TOPIC_ID = ".$id);
779 Main\Application::getConnection()->queryExecute("DELETE FROM b_forum_user_topic WHERE TOPIC_ID = ".$id);
780 Main\Application::getConnection()->queryExecute("DELETE FROM b_forum_topic WHERE ID = ".$id);
781 Main\Application::getConnection()->queryExecute("DELETE FROM b_forum_topic WHERE TOPIC_ID = ".$id);
782 Main\Application::getConnection()->queryExecute("DELETE FROM b_forum_stat WHERE TOPIC_ID = ".$id);
783
784 /***************** Event onAfterTopicDelete ************************/
785 foreach(GetModuleEvents("forum", "onAfterTopicDelete", true) as $arEvent)
786 {
787 ExecuteModuleEventEx($arEvent, [&$id, &$topicData]);
788 }
789 /***************** /Event ******************************************/
790 return $result;
791 }
792
794 {
795 $fields = array(
796 "ABS_LAST_POSTER_ID" => $message["AUTHOR_ID"],
797 "ABS_LAST_POSTER_NAME" => $message["AUTHOR_NAME"],
798 "ABS_LAST_POST_DATE" => $message["POST_DATE"],
799 "ABS_LAST_MESSAGE_ID" => $message["ID"]
800 );
801 $this->data = array_merge($this->data, $fields);
802 if ($message["APPROVED"] == "Y")
803 {
804 $fields += [
805 "APPROVED" => "Y",
806 "LAST_POSTER_ID" => $message["AUTHOR_ID"],
807 "LAST_POSTER_NAME" => $message["AUTHOR_NAME"],
808 "LAST_POST_DATE" => $message["POST_DATE"],
809 "LAST_MESSAGE_ID" => $message["ID"]
810 ];
811 $this->data = array_merge($this->data, $fields);
812 if ($message["NEW_TOPIC"] != "Y")
813 {
814 $fields["POSTS"] = new Main\DB\SqlExpression('?# + 1', "POSTS");
815 $this->data["POSTS"]++;
816 }
817 if (!empty($message["SERVICE_TYPE"]))
818 {
819 $fields["POSTS_SERVICE"] = new Main\DB\SqlExpression('?# + 1', "POSTS_SERVICE");
820 $this->data["POSTS_SERVICE"]++;
821 }
822 }
823 else
824 {
825 $fields["POSTS_UNAPPROVED"] = new Main\DB\SqlExpression('?# + 1', "POSTS_UNAPPROVED");
826 $this->data["POSTS_UNAPPROVED"]++;
827 }
828 return TopicTable::update($this->getId(), $fields);
829 }
830
832 {
833 $fields = [];
834 if ($message['APPROVED'] == 'Y')
835 {
836 $fields['POSTS'] = new Main\DB\SqlExpression('CASE WHEN ?# > 0 THEN ?# - 1 ELSE 0 END', 'POSTS', 'POSTS');
837 $this->data['POSTS']--;
838 if (!empty($message["SERVICE_TYPE"]))
839 {
840 $fields["POSTS_SERVICE"] = new Main\DB\SqlExpression('?# - 1', "POSTS_SERVICE");
841 $this->data["POSTS_SERVICE"]--;
842 }
843 }
844 else
845 {
846 $fields['POSTS_UNAPPROVED'] = new Main\DB\SqlExpression('?# - 1', 'POSTS_UNAPPROVED');
847 $this->data['POSTS_UNAPPROVED']--;
848 }
849
850 if ($message['NEW_TOPIC'] === 'Y' && ($firstMessage = MessageTable::getList([
851 'select' => ['*'],
852 'filter' => ['TOPIC_ID' => $this->getId()],
853 'order' => ['ID' => 'ASC'],
854 'limit' => 1
855 ])->fetch()))
856 {
857 $fields['USER_START_ID'] = $firstMessage['AUTHOR_ID'];
858 $fields['USER_START_NAME'] = $firstMessage['AUTHOR_NAME'];
859 $fields['START_DATE'] = $firstMessage['POST_DATE'];
860 if ($this->data['APPROVED'] === 'Y' && $firstMessage['APPROVED'] !== 'Y' && $this->data['POSTS'] < 0)
861 {
862 $fields['APPROVED'] = 'N';
863 }
864 }
865 else if ($message['ID'] >= $this->data['LAST_MESSAGE_ID'])
866 {
867 if ($message['ID'] === $this->data['LAST_MESSAGE_ID']
868 &&
869 ($lastMessage = Forum\MessageTable::getList([
870 'select' => ['ID', 'AUTHOR_ID', 'AUTHOR_NAME', 'POST_DATE'],
871 'filter' => [
872 'TOPIC_ID' => $this->getId(),
874 ],
875 'order' => ['ID' => 'DESC'],
876 'limit' => 1
877 ])->fetch())
878 )
879 {
880 $fields += [
881 'LAST_POSTER_ID' => $lastMessage['AUTHOR_ID'],
882 'LAST_POSTER_NAME' => $lastMessage['AUTHOR_NAME'],
883 'LAST_POST_DATE' => $lastMessage['POST_DATE'],
884 'LAST_MESSAGE_ID' => $lastMessage['ID']
885 ];
886 }
887
888 if ($message['ID'] === $this->data['ABS_LAST_MESSAGE_ID']
889 &&
890 ($lastMessage = Forum\MessageTable::getList([
891 'select' => ['ID', 'AUTHOR_ID', 'AUTHOR_NAME', 'POST_DATE'],
892 'filter' => ['TOPIC_ID' => $this->getId()],
893 'order' => ['ID' => 'DESC'],
894 'limit' => 1
895 ])->fetch())
896 )
897 {
898 $fields += [
899 'ABS_LAST_POSTER_ID' => $lastMessage['AUTHOR_ID'],
900 'ABS_LAST_POSTER_NAME' => $lastMessage['AUTHOR_NAME'],
901 'ABS_LAST_POST_DATE' => $lastMessage['POST_DATE'],
902 'ABS_LAST_MESSAGE_ID' => $lastMessage['ID']
903 ];
904 }
905 }
906 if (!empty($fields))
907 {
908 $this->data = array_merge($this->data, array_filter($fields, function($item) {
909 return !($item instanceof Main\DB\SqlExpression);
910 }));
911 $result = TopicTable::update($this->getId(), $fields);
912 }
913 else
914 {
916 }
917 if (isset($firstMessage) && $firstMessage)
918 {
919 $result->setData($firstMessage);
920 }
921 return $result;
922 }
923
924 public function incrementViews()
925 {
926 TopicTable::update($this->getId(), ['VIEWS' => new Main\DB\SqlExpression('?# + 1', 'VIEWS')]);
927 }
928}
$connection
Определения actionsdefinitions.php:38
global $APPLICATION
Определения include.php:80
static getTableName()
Определения file.php:62
static index(Forum\Forum $forum, Forum\Topic $topic, array $message)
Определения message.php:13
static reindex(int $topicId, bool $reindexOnlyFirstMessage=false)
Определения topic.php:141
static deleteIndex(Forum\Topic $topic)
Определения topic.php:166
const APPROVED_DISAPPROVED
Определения message.php:615
static add(Forum\Topic $topic, array $fields)
Определения message.php:813
const APPROVED_APPROVED
Определения message.php:614
static update($id, array &$fields)
Определения message.php:718
static getUfId()
Определения message.php:91
static getDataById($id, $ttl=84600)
Определения message.php:377
static getTableName()
Определения message.php:86
const ACCESS_DENIED
Определения permission.php:67
const CAN_MODERATE
Определения permission.php:71
static runForTopic(int $topicId)
Определения user.php:64
static getTableName()
Определения subscribe.php:48
Определения topic.php:287
__construct($id)
Определения topic.php:296
edit(array $fields)
Определения topic.php:604
const APPROVED_DISAPPROVED
Определения topic.php:294
incrementViews()
Определения topic.php:924
incrementStatistic(array $message)
Определения topic.php:793
open()
Определения topic.php:332
init()
Определения topic.php:305
const STATE_LINK
Определения topic.php:290
const STATE_CLOSED
Определения topic.php:291
decrementStatistic(array $message)
Определения topic.php:831
const APPROVED_APPROVED
Определения topic.php:293
approve()
Определения topic.php:410
moveToForum(int $forumId)
Определения topic.php:316
disapprove()
Определения topic.php:372
static update(int $id, array &$data)
Определения topic.php:700
close()
Определения topic.php:352
const STATE_OPENED
Определения topic.php:292
static create($parentObject, array $fields)
Определения topic.php:462
static add(Forum\Forum $forum, array &$data)
Определения topic.php:557
static getFilteredFields()
Определения topic.php:124
static getMap()
Определения topic.php:87
static onAfterUpdate(Main\ORM\Event $event)
Определения topic.php:246
static getTableName()
Определения topic.php:77
static getInstance()
Определения application.php:98
Определения error.php:15
Определения event.php:5
static includeModule($moduleName)
Определения loader.php:67
static getById($id)
Определения datamanager.php:364
static Filter($message)
Определения filter_dictionary.php:590
static ChangePermission($MODULE_ID, $arGroups, $ITEM_ID=false, $PARAM1=false, $PARAM2=false, $SITE_ID=false, $PARAMS=false)
Определения search.php:3097
static Log($object, $action, $id, $description="", $title="")
Определения event_log.php:14
$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
$res
Определения filter_act.php:7
global $USER_FIELD_MANAGER
Определения attempt.php:6
$result
Определения get_property_values.php:14
while($arParentIBlockProperty=$dbParentIBlockProperty->Fetch()) $errorMessage
$groups
Определения options.php:30
ExecuteModuleEventEx($arEvent, $arParams=[])
Определения tools.php:5214
GetModuleEvents($MODULE_ID, $MESSAGE_ID, $bReturnArray=false)
Определения tools.php:5177
Определения arrayresult.php:2
Определения aliases.php:105
Определения ufield.php:9
$message
Определения payment.php:8
$event
Определения prolog_after.php:141
if(empty($signedUserToken)) $key
Определения quickway.php:257
$title
Определения pdf.php:123
$val
Определения options.php:1793
$dbResult
Определения updtr957.php:3
$fields
Определения yandex_run.php:501