1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
message.php
См. документацию.
1<?php
2namespace Bitrix\Forum;
3
4use Bitrix\Forum;
5use Bitrix\Main;
6use Bitrix\Main\ArgumentException;
7use Bitrix\Main\Error;
8use Bitrix\Main\Localization\Loc;
9use Bitrix\Main\ModuleManager;
10use Bitrix\Main\ORM\Data\Result;
11use Bitrix\Main\ORM\EntityError;
12use Bitrix\Main\ORM\Event;
13use Bitrix\Main\ORM\Fields\BooleanField;
14use Bitrix\Main\ORM\Fields\DatetimeField;
15use Bitrix\Main\ORM\Fields\IntegerField;
16use Bitrix\Main\ORM\Fields\Relations\Reference;
17use Bitrix\Main\ORM\Fields\StringField;
18use Bitrix\Main\ORM\Fields\TextField;
19use Bitrix\Main\ORM\Query\Join;
20use Bitrix\Main\Type\DateTime;
21
22Loc::loadMessages(__FILE__);
23
75class MessageTable extends Main\Entity\DataManager
76{
77 const SOURCE_ID_EMAIL = "EMAIL";
78 const SOURCE_ID_WEB = "WEB";
79 const SOURCE_ID_MOBILE = "MOBILE";
80
86 public static function getTableName()
87 {
88 return 'b_forum_message';
89 }
90
91 public static function getUfId()
92 {
93 return 'FORUM_MESSAGE';
94 }
95
96 private static $post_message_hash = [];
97 private static $messageById = [];
98
104 public static function getMap()
105 {
106 return array(
107 (new IntegerField("ID", ["primary" => true, "autocomplete" => true])),
108 (new IntegerField("FORUM_ID", ["required" => true])),
109 (new IntegerField("TOPIC_ID", ["required" => true])),
110 (new BooleanField("USE_SMILES", ["values" => ["N", "Y"], "default_value" => "Y"])),
111 (new BooleanField("NEW_TOPIC", ["values" => ["N", "Y"], "default_value" => "N"])),
112 (new BooleanField("APPROVED", ["values" => ["N", "Y"], "default_value" => "Y"])),
113 (new BooleanField("SOURCE_ID", ["values" => [self::SOURCE_ID_EMAIL, self::SOURCE_ID_WEB, self::SOURCE_ID_MOBILE], "default_value" => self::SOURCE_ID_WEB])),
114 (new DatetimeField("POST_DATE", ["required" => true, "default_value" => function(){ return new DateTime();}])),
115 (new TextField("POST_MESSAGE", ["required" => true])),
116 (new TextField("POST_MESSAGE_HTML")),
117 (new TextField("POST_MESSAGE_FILTER")),
118 (new StringField("POST_MESSAGE_CHECK", ["size" => 32])),
119 (new IntegerField("ATTACH_IMG")),
120 (new StringField("PARAM1", ["size" => 2])),
121 (new IntegerField("PARAM2")),
122
123 (new IntegerField("AUTHOR_ID")),
124 (new StringField("AUTHOR_NAME", ["required" => true, "size" => 255])),
125 (new StringField("AUTHOR_EMAIL", ["size" => 255])),
126 (new StringField("AUTHOR_IP", ["size" => 255])),
127 (new StringField("AUTHOR_REAL_IP", ["size" => 255])),
128 (new IntegerField("GUEST_ID")),
129
130 (new IntegerField("EDITOR_ID")),
131 (new StringField("EDITOR_NAME", ["size" => 255])),
132 (new StringField("EDITOR_EMAIL", ["size" => 255])),
133 (new TextField("EDIT_REASON")),
134 (new DatetimeField("EDIT_DATE")),
135
136 (new StringField("XML_ID", ["size" => 255])),
137
138 (new TextField("HTML")),
139 (new TextField("MAIL_HEADER")),
140 (new IntegerField("SERVICE_TYPE")),
141 (new TextField("SERVICE_DATA")),
142
143 (new Reference("TOPIC", TopicTable::class, Join::on("this.TOPIC_ID", "ref.ID"))),
144 (new Reference("FORUM_USER", UserTable::class, Join::on("this.AUTHOR_ID", "ref.USER_ID"))),
145 (new Reference("FORUM_USER_TOPIC", UserTopicTable::class, Join::on("this.TOPIC_ID", "ref.TOPIC_ID"))),
146 (new Reference("USER", Main\UserTable::class, Join::on("this.AUTHOR_ID", "ref.ID")))
147 );
148 }
149
150 public static function getFilteredFields()
151 {
152 return [
153 "AUTHOR_NAME",
154 "AUTHOR_EMAIL",
155 "EDITOR_NAME",
156 "EDITOR_EMAIL",
157 "EDIT_REASON"
158 ];
159 }
160
161 private static function modifyMessageFields(array &$data)
162 {
163 unset($data["UPLOAD_DIR"]);
164 if (array_key_exists("USE_SMILES", $data))
165 {
166 $data["USE_SMILES"] = $data["USE_SMILES"] === "N" ? "N" : "Y";
167 }
168 if (array_key_exists("NEW_TOPIC", $data))
169 {
170 $data["NEW_TOPIC"] = $data["NEW_TOPIC"] === "Y" ? "Y" : "N";
171 }
172 if (array_key_exists("APPROVED", $data))
173 {
175 }
176 if (array_key_exists("SOURCE_ID", $data))
177 {
178 $data["SOURCE_ID"] = self::filterSourceIdParam($data['SOURCE_ID']);
179 }
180 }
181
182 public static function filterSourceIdParam(string $sourceId): string
183 {
184 if (in_array($sourceId, [self::SOURCE_ID_WEB, self::SOURCE_ID_MOBILE, self::SOURCE_ID_EMAIL], true))
185 {
186 return $sourceId;
187 }
188 return self::SOURCE_ID_WEB;
189 }
190
191 public static function onBeforeAdd(Event $event)
192 {
195 $data = $event->getParameter("fields");
196 $strUploadDir = array_key_exists("UPLOAD_DIR", $data) ? $data["UPLOAD_DIR"] : "forum";
197 self::modifyMessageFields($data);
198 //region Files
199 if (array_key_exists("ATTACH_IMG", $data) && !empty($data["ATTACH_IMG"]))
200 {
201 if (!array_key_exists("FILES", $data))
202 {
203 $data["FILES"] = [];
204 }
205 $data["FILES"][] = $data["ATTACH_IMG"];
206 unset($data["ATTACH_IMG"]);
207 }
208 if (array_key_exists("FILES", $data))
209 {
210 $data["FILES"] = is_array($data["FILES"]) ? $data["FILES"] : [$data["FILES"]];
211 if (!empty($data["FILES"]))
212 {
214 Forum\Forum::getById($data["FORUM_ID"]),
215 $data["FILES"],
216 [
217 "FORUM_ID" => $data["FORUM_ID"],
218 "TOPIC_ID" => ($data["NEW_TOPIC"] === "Y" ? 0 : $data["TOPIC_ID"]),
219 "MESSAGE_ID" => 0,
220 "USER_ID" => $data["AUTHOR_ID"]
221 ]
222 );
223 if (!$res->isSuccess())
224 {
225 $result->setErrors($res->getErrors());
226 }
227 else
228 {
229 /*@var Main\ORM\Objectify\EntityObject $object*/
230 $object = $event->getParameter("object");
231 /*@var Main\Dictionary $object->customData*/
232 $object->sysSetRuntime("FILES", $data["FILES"]);
233 $object->sysSetRuntime("UPLOAD_DIR", $strUploadDir);
234 }
235 }
236 unset($data["FILES"]);
237 }
238 //endregion
239
240 $data["POST_MESSAGE_CHECK"] = md5($data["POST_MESSAGE"] . (array_key_exists("FILES", $data) ? serialize($data["FILES"]) : ""));
241
242 //region Deduplication
243 $forum = Forum\Forum::getById($data["FORUM_ID"]);
244 $deduplication = null;
245 if (array_key_exists("AUX", $data))
246 {
247 if ($data["AUX"] == "Y")
248 {
249 $deduplication = false;
250 }
251 unset($data["AUX"]);
252 }
253 if (array_key_exists("DEDUPLICATION", $data))
254 {
255 $deduplication = $data["DEDUPLICATION"] == "Y";
256 unset($data["DEDUPLICATION"]);
257 }
258 if ($deduplication === null)
259 {
260 $deduplication = $forum["DEDUPLICATION"] === "Y";
261 }
262 if ($deduplication && $data["NEW_TOPIC"] !== "Y")
263 {
264 if (self::getLastMessageHashInTopic($data["TOPIC_ID"]) === $data["POST_MESSAGE_CHECK"])
265 {
266 $result->addError(new EntityError(Loc::getmessage("F_ERR_MESSAGE_ALREADY_EXISTS"), "onBeforeMessageAdd"));
267 return $result;
268 }
269 }
270 //endregion
271
272 $data["POST_MESSAGE"] = Main\Text\Emoji::encode($data["POST_MESSAGE"]);
273
274 //region Filter
275 if (Main\Config\Option::get("forum", "FILTER", "Y") == "Y")
276 {
277 $data["POST_MESSAGE_FILTER"] = \CFilterUnquotableWords::Filter($data["POST_MESSAGE"]);
278 $filteredFields = self::getFilteredFields();
279 $res = [];
280 foreach ($filteredFields as $key)
281 {
282 $res[$key] = array_key_exists($key, $data) ? $data[$key] : "";
283 if (!empty($res[$key]))
284 {
285 $res[$key] = \CFilterUnquotableWords::Filter($res[$key]);
286 if ($res[$key] == '')
287 {
288 $res[$key] = "*";
289 }
290 }
291 }
292 $data["HTML"] = serialize($res);
293 }
294 //endregion
295
296 $fields = $event->getParameter("fields");
297 if ($data != $fields)
298 {
299 foreach ($fields as $key => $val)
300 {
301 if (!array_key_exists($key, $data))
302 {
303 $result->unsetField($key);
304 }
305 else if ($data[$key] == $val)
306 {
307 unset($data[$key]);
308 }
309 }
310 $result->modifyFields($data);
311 }
312 return $result;
313 }
314
319 public static function onAdd(Main\ORM\Event $event)
320 {
322 if (Main\Config\Option::get("forum", "MESSAGE_HTML", "N") == "Y")
323 {
324 $fields = $event->getParameter("fields");
325 $object = $event->getParameter("object");
326
327 if ($files = $object->sysGetRuntime("FILES"))
328 {
330 $files,
331 [
332 "FORUM_ID" => $fields["FORUM_ID"],
333 "TOPIC_ID" => $fields["TOPIC_ID"],
334 "MESSAGE_ID" => 0,
335 "USER_ID" => $fields["AUTHOR_ID"],
336 ],
337 ($object->sysGetRuntime("UPLOAD_DIR") ?: "forum/upload"));
338 $object->sysSetRuntime("FILES", $files);
339 }
340
341 $parser = new \forumTextParser(LANGUAGE_ID);
343 $allow["SMILES"] = ($fields["USE_SMILES"] != "Y" ? "N" : $allow["SMILES"]);
344 $result->modifyFields([
345 "POST_MESSAGE_HTML" => $parser->convert($fields["POST_MESSAGE_FILTER"] ?: $fields["POST_MESSAGE"], $allow, "html", $files)
346 ]);
347 }
348 return $result;
349 }
350
351
356 public static function onAfterAdd(Main\ORM\Event $event)
357 {
358 $object = $event->getParameter("object");
359
360 if ($files = $object->sysGetRuntime("FILES"))
361 {
362 $id = $event->getParameter("id");
363 $id = is_array($id) && array_key_exists("ID", $id) ? $id["ID"] : $id;
364 $fields = $event->getParameter("fields");
366 $files,
367 [
368 "FORUM_ID" => $fields["FORUM_ID"],
369 "TOPIC_ID" => $fields["TOPIC_ID"],
370 "MESSAGE_ID" => $id,
371 "USER_ID" => $fields["AUTHOR_ID"],
372 ],
373 ($object->sysGetRuntime("UPLOAD_DIR") ?: "forum/upload"));
374 }
375 }
376
377 public static function getDataById($id, $ttl = 84600)
378 {
379 if (!array_key_exists($id, self::$messageById))
380 {
381 self::$messageById[$id] = self::getList([
382 "select" => ["*"],
383 "filter" => ["ID" => $id],
384 "cache" => [
385 "ttl" => $ttl
386 ]
387 ])->fetch();
388 }
389 return self::$messageById[$id];
390 }
391
397 public static function onBeforeUpdate(Main\ORM\Event $event)
398 {
401 $data = $event->getParameter("fields");
402 $id = $event->getParameter("id");
403 $id = $id["ID"];
404 $strUploadDir = array_key_exists("UPLOAD_DIR", $data) ? $data["UPLOAD_DIR"] : "forum";
405 self::modifyMessageFields($data);
406 if (Main\Config\Option::get("forum", "FILTER", "Y") == "Y" &&
407 !empty(array_intersect(self::getFilteredFields(), array_keys($data))))
408 {
409 $forFilter = $data;
410 if (
411 array_intersect(self::getFilteredFields(), array_keys($data)) !== self::getFilteredFields() &&
413 )
414 {
415 $forFilter = array_merge($message, $forFilter);
416 }
417 $res = [];
418 foreach (self::getFilteredFields() as $key)
419 {
420 $res[$key] = array_key_exists($key, $forFilter) ? $forFilter[$key] : "";
421 if (!empty($res[$key]))
422 {
424 if ($res[$key] == '' )
425 {
426 $res[$key] = "*";
427 }
428 }
429 }
430 $data["HTML"] = serialize($res);
431 }
432 if (array_key_exists("POST_MESSAGE", $data))
433 {
434 $data["POST_MESSAGE"] = Main\Text\Emoji::encode($data["POST_MESSAGE"]);
435 if (Main\Config\Option::get("forum", "FILTER", "Y") == "Y")
436 {
437 $data["POST_MESSAGE_FILTER"] = \CFilterUnquotableWords::Filter($data["POST_MESSAGE"]);
438 }
439 }
440 unset($data["AUX"]);
441 unset($data["DEDUPLICATION"]);
442
443 //region Files
444 if (array_key_exists("ATTACH_IMG", $data) && !empty($data["ATTACH_IMG"]))
445 {
446 if (!array_key_exists("FILES", $data))
447 {
448 $data["FILES"] = [];
449 }
450 $data["FILES"][] = $data["ATTACH_IMG"];
451 unset($data["ATTACH_IMG"]);
452 }
453 if (array_key_exists("FILES", $data))
454 {
455 $data["FILES"] = is_array($data["FILES"]) ? $data["FILES"] : [$data["FILES"]];
456 if (!empty($data["FILES"]))
457 {
458 $fileFields = $data + MessageTable::getDataById($id);
459 $res = Forum\File::checkFiles(
460 Forum\Forum::getById($fileFields["FORUM_ID"]),
461 $data["FILES"],
462 [
463 "FORUM_ID" => $fileFields["FORUM_ID"],
464 "TOPIC_ID" => $fileFields["TOPIC_ID"],
465 "MESSAGE_ID" => $id,
466 "USER_ID" => $fileFields["AUTHOR_ID"]
467 ]
468 );
469 if (!$res->isSuccess())
470 {
471 $result->setErrors($res->getErrors());
472 }
473 else
474 {
475 /*@var Main\ORM\Objectify\EntityObject $object*/
476 $object = $event->getParameter("object");
477 /*@var Main\Dictionary $object->customData*/
478 $object->sysSetRuntime("FILES", $data["FILES"]);
479 $object->sysSetRuntime("UPLOAD_DIR", $strUploadDir);
480 $object->sysSetRuntime("FILE_FIELDS", $fileFields);
481 }
482 }
483 unset($data["FILES"]);
484 }
485 //endregion
486 $fields = $event->getParameter("fields");
487 if ($data != $fields)
488 {
489 foreach ($fields as $key => $val)
490 {
491 if (!array_key_exists($key, $data))
492 {
493 $result->unsetField($key);
494 }
495 else if ($data[$key] == $val)
496 {
497 unset($data[$key]);
498 }
499 }
500 $result->modifyFields($data);
501 }
502 return $result;
503 }
508 public static function onUpdate(Main\ORM\Event $event)
509 {
510 $id = $event->getParameter("id");
511 $id = $id["ID"];
513
514 $fields = $event->getParameter("fields") + $message;
515 $object = $event->getParameter("object");
516
517 if ($files = $object->sysGetRuntime("FILES"))
518 {
520 $files,
521 [
522 "FORUM_ID" => $fields["FORUM_ID"],
523 "TOPIC_ID" => $fields["TOPIC_ID"],
524 "MESSAGE_ID" => $id,
525 "USER_ID" => $fields["AUTHOR_ID"],
526 ],
527 ($object->sysGetRuntime("UPLOAD_DIR") ?: "forum/upload"));
528 }
529 if (Main\Config\Option::get("forum", "MESSAGE_HTML", "N") == "Y")
530 {
532 $parser = new \forumTextParser(LANGUAGE_ID);
534 $allow["SMILES"] = ($fields["USE_SMILES"] != "Y" ? "N" : $allow["SMILES"]);
535 $result->modifyFields([
536 "POST_MESSAGE_HTML" => $parser->convert($fields["POST_MESSAGE_FILTER"] ?: $fields["POST_MESSAGE"], $allow, "html", $files)
537 ]);
538 return $result;
539 }
540 }
541
546 public static function onAfterUpdate(Main\ORM\Event $event)
547 {
548 $id = $event->getParameter("id");
549 $id = $id["ID"];
550 unset(self::$messageById[$id]);
551 }
552
559 public static function checkFields(Result $result, $primary, array $data)
560 {
561 parent::checkFields($result, $primary, $data);
562 if ($result->isSuccess())
563 {
564 try
565 {
566 if (array_key_exists("FORUM_ID", $data) && ForumTable::getMainData($data["FORUM_ID"]) === null)
567 {
568 throw new Main\ObjectNotFoundException(Loc::getMessage("F_ERR_INVALID_FORUM_ID"));
569 }
570 if (array_key_exists("TOPIC_ID", $data))
571 {
572 $topic = \Bitrix\Forum\TopicTable::query()->setSelect(['STATE'])
573 ->where('ID', $data["TOPIC_ID"])
574 ->fetch();
575
576 if (!$topic)
577 {
578 throw new Main\ObjectNotFoundException(Loc::getMessage("F_ERR_TOPIC_IS_NOT_EXISTS"));
579 }
580 if ($topic["STATE"] == Topic::STATE_LINK)
581 {
582 throw new Main\ObjectPropertyException(Loc::getMessage("F_ERR_TOPIC_IS_LINK"));
583 }
584 }
585 }
586 catch (\Exception $e)
587 {
588 $result->addError(new Error(
589 $e->getMessage()
590 ));
591 }
592 }
593 }
594
595 private static function getLastMessageHashInTopic(int $topicId): ?string
596 {
597 $res = MessageTable::query()
598 ->setSelect(['ID', 'POST_MESSAGE_CHECK'])
599 ->where('TOPIC_ID', '=', $topicId)
600 ->where('APPROVED', '=', 'Y')
601 ->setOrder(['ID' => 'DESC'])
602 ->setLimit(1)
603 ->exec()
604 ->fetch()
605 ;
606 return $res ? $res['POST_MESSAGE_CHECK'] : null;
607 }
608}
609
611{
613
614 public const APPROVED_APPROVED = "Y";
615 public const APPROVED_DISAPPROVED = "N";
616
617 protected function init()
618 {
619 if (!($this->data = MessageTable::getById($this->id)->fetch()))
620 {
621 throw new Main\ObjectNotFoundException("Message with id {$this->id} is not found.");
622 }
623 $this->authorId = intval($this->data["AUTHOR_ID"]);
624 }
625
626 public function edit(array $fields)
627 {
628 $result = self::update($this->getId(), $fields);
629
630 if ($result->isSuccess() )
631 {
632 $this->data = MessageTable::getById($result->getId())->fetch();
633
636 Forum\Topic::getById($this->data["TOPIC_ID"]),
637 $this->data
638 );
639 }
640
641 return $result;
642 }
643
644 public function remove(): Main\ORM\Data\DeleteResult
645 {
646 $result = self::delete($this->getId());
647
648 if ($result->isSuccess())
649 {
650 if ($topic = Forum\Topic::getById($this->data['TOPIC_ID']))
651 {
652 $decrementStatisticResult = $topic->decrementStatistic($this->data);
653 if ($this->data['NEW_TOPIC'] === 'Y' && $decrementStatisticResult->getData())
654 {
655 if (!($newFirstMessage = $decrementStatisticResult->getData()) || empty($newFirstMessage))
656 {
657 $newFirstMessage = MessageTable::getList([
658 'select' => ['*'],
659 'filter' => ['TOPIC_ID' => $this->getId()],
660 'order' => ['ID' => 'ASC'],
661 'limit' => 1
662 ])->fetch();
663 }
665 Forum\Forum::getById($topic->getForumId()),
666 $topic,
667 $newFirstMessage
668 );
669 }
670 }
671
672 if ($forum = Forum\Forum::getById($this->getForumId()))
673 {
674 $forum->decrementStatistic($this->data);
675 }
676
678
679 if ($this->data['AUTHOR_ID'] > 0 && ($author = User::getById($this->data['AUTHOR_ID'])))
680 {
681 $author->decrementStatistic($this->data);
682 }
683 }
684
685 return $result;
686 }
687
688 public function getXmlId(): string
689 {
690 return (string)($this->data['XML_ID'] ?? '');
691 }
692
697 public static function create($parentObject, array $fields)
698 {
699 $topic = Forum\Topic::getInstance($parentObject);
700 $result = self::add($topic, $fields);
701 if (!$result->isSuccess() )
702 {
703 return $result;
704 }
705
707 $forum = Forum\Forum::getById($topic->getForumId());
708 //region Update statistic & Seacrh
709 User::getById($message["AUTHOR_ID"])->incrementStatistic($message);
710 $topic->incrementStatistic($message);
711 $forum->incrementStatistic($message);
713 //endregion
714
715 return $result;
716 }
717
718 public static function update($id, array &$fields)
719 {
721 $result->setPrimary(["ID" => $id]);
722 $data = [];
723 $temporaryFields = ['AUX', 'AUX_DATA'];
724
725 foreach (array_merge([
726 "USE_SMILES",
727 "POST_MESSAGE",
728 "ATTACH_IMG",
729 "FILES",
730 "AUTHOR_NAME",
731 "AUTHOR_EMAIL",
732 "EDITOR_ID",
733 "EDITOR_NAME",
734 "EDITOR_EMAIL",
735 "EDIT_REASON",
736 "EDIT_DATE",
737 'SERVICE_TYPE',
738 'SERVICE_DATA',
739 'SOURCE_ID',
740 'PARAM1',
741 'PARAM2',
742 'XML_ID',
743 ], $temporaryFields) as $field)
744 {
745 if (array_key_exists($field, $fields))
746 {
747 $data[$field] = $fields[$field];
748 }
749 }
750 if (!empty(array_diff_key($fields, $data)))
751 {
752 global $USER_FIELD_MANAGER;
753 $data += array_intersect_key($fields, $USER_FIELD_MANAGER->getUserFields(MessageTable::getUfId()));
754 }
755
756 if (($events = GetModuleEvents("forum", "onBeforeMessageUpdate", true)) && !empty($events))
757 {
758 $strUploadDir = "forum";
759 global $APPLICATION;
760 foreach ($events as $ev)
761 {
762 $APPLICATION->ResetException();
763 if (ExecuteModuleEventEx($ev, array($id, &$data, &$strUploadDir)) === false)
764 {
765 $errorMessage = Loc::getMessage("FORUM_EVENT_BEFOREUPDATE_ERROR");
766 if (($ex = $APPLICATION->GetException()) && ($ex instanceof \CApplicationException))
767 {
768 $errorMessage = $ex->getString();
769 }
770
771 $result->addError(new Main\Error($errorMessage, "onBeforeMessageUpdate"));
772 return $result;
773 }
774 }
775 $data["UPLOAD_DIR"] = $strUploadDir;
776 }
777
778 foreach ($temporaryFields as $field)
779 {
780 unset($data[$field]);
781 }
782
783 if (isset($fields['EDITOR_ID']))
784 {
785 $authContext = new Main\Authentication\Context();
786 $authContext->setUserId($fields['EDITOR_ID']);
787 $data = [
788 'fields' => $data,
789 'auth_context' => $authContext
790 ];
791 }
792
793 $dbResult = MessageTable::update($id, $data);
794
795 if (!$dbResult->isSuccess())
796 {
797 $result->addErrors($dbResult->getErrors());
798 }
799 else
800 {
802 foreach (GetModuleEvents("forum", "onAfterMessageUpdate", true) as $event)
803 {
805 }
806 }
807 return $result;
808 }
809
813 public static function add(Forum\Topic $topic, array $fields): Main\ORM\Data\AddResult
814 {
815 $data = [
816 "FORUM_ID" => $topic->getForumId(),
817 "TOPIC_ID" => $topic->getId(),
818
819 "USE_SMILES" => $fields["USE_SMILES"],
820 "NEW_TOPIC" => (isset($fields["NEW_TOPIC"]) && $fields["NEW_TOPIC"] === "Y" ? "Y" : "N"),
822
823 "POST_DATE" => $fields["POST_DATE"] ?: new Main\Type\DateTime(),
824 "POST_MESSAGE" => $fields["POST_MESSAGE"],
825 "ATTACH_IMG" => $fields["ATTACH_IMG"] ?? null,
826 "FILES" => $fields["FILES"] ?? [],
827
828 "AUTHOR_ID" => $fields["AUTHOR_ID"],
829 "AUTHOR_NAME" => $fields["AUTHOR_NAME"],
830 "AUTHOR_EMAIL" => $fields["AUTHOR_EMAIL"],
831 "AUTHOR_IP" => $fields["AUTHOR_IP"] ?? null,
832 "AUTHOR_REAL_IP" => $fields["AUTHOR_REAL_IP"] ?? null,
833 "GUEST_ID" => $fields["GUEST_ID"] ?? null,
834 ];
835
836 if (!empty(array_diff_key($fields, $data)))
837 {
838 global $USER_FIELD_MANAGER;
839 $data += array_intersect_key($fields, $USER_FIELD_MANAGER->getUserFields(MessageTable::getUfId()));
840 }
841
842 $temporaryFields = ['AUX', 'AUX_DATA'];
843 $additionalFields = array_merge(['SERVICE_TYPE', 'SERVICE_DATA', 'SOURCE_ID', 'PARAM1', 'PARAM2', 'XML_ID'], $temporaryFields);
844 foreach ($additionalFields as $key)
845 {
846 if (array_key_exists($key, $fields))
847 {
849 }
850 }
851
853
854 if (($events = GetModuleEvents("forum", "onBeforeMessageAdd", true)) && !empty($events))
855 {
856 $strUploadDir = "forum";
857 global $APPLICATION;
858
859 foreach ($events as $ev)
860 {
861 $APPLICATION->ResetException();
862 if (ExecuteModuleEventEx($ev, array(&$data, &$strUploadDir)) === false)
863 {
864 $errorMessage = Loc::getMessage("FORUM_EVENT_BEFOREADD_ERROR");
865 if (($ex = $APPLICATION->GetException()) && ($ex instanceof \CApplicationException))
866 {
867 $errorMessage = $ex->getString();
868 }
869
870 $result->addError(new Main\Error($errorMessage, "onBeforeMessageAdd"));
871 return $result;
872 }
873 }
874 $data["UPLOAD_DIR"] = $strUploadDir;
875 }
876
877 foreach ($temporaryFields as $field)
878 {
879 unset($data[$field]);
880 }
881
882 $authContext = new Main\Authentication\Context();
883 $authContext->setUserId($fields['AUTHOR_ID']);
884
885 $dbResult = MessageTable::add([
886 "fields" => $data,
887 "auth_context" => $authContext
888 ]);
889
890 if (!$dbResult->isSuccess())
891 {
892 $result->addErrors($dbResult->getErrors());
893 }
894 else
895 {
896 $id = $dbResult->getId();
897 $result->setId($dbResult->getId());
898
900 $forum = Forum\Forum::getById($topic->getForumId());
901 foreach (GetModuleEvents("forum", "onAfterMessageAdd", true) as $event)
902 {
904 }
905 }
906 return $result;
907 }
908
909 public static function delete(int $id): Main\ORM\Data\DeleteResult
910 {
912
914 {
915 $result->addError(new Main\Error( Loc::getMessage("FORUM_MESSAGE_HAS_NOT_BEEN_FOUND")));
916 return $result;
917 }
918
920 if (($events = GetModuleEvents("forum", "onBeforeMessageDelete", true))
921 && !empty($events))
922 {
923 foreach ($events as $ev)
924 {
925 $APPLICATION->ResetException();
926 if (ExecuteModuleEventEx($ev, [$id, $message]) === false)
927 {
928 $errorMessage = Loc::getMessage("FORUM_EVENT_BEFOREDELETE_ERROR");
929 if (($ex = $APPLICATION->GetException()) && ($ex instanceof \CApplicationException))
930 {
931 $errorMessage = $ex->getString();
932 }
933
934 $result->addError(new Main\Error($errorMessage, "onBeforeMessageDelete"));
935 break;
936 }
937 }
938 }
939
940 if ($result->isSuccess())
941 {
942 if ($message['PARAM1'] == 'VT' && $message['PARAM2'] > 0
943 && IsModuleInstalled('vote') && Main\Loader::includeModule('vote'))
944 {
945 \CVote::Delete($message['PARAM2']);
946 }
947 $USER_FIELD_MANAGER->Delete("FORUM_MESSAGE", $id);
948 FileTable::deleteBatch(['MESSAGE_ID' => $id]);
949 MessageTable::delete($id);
950
951 if (!($nextMessage = MessageTable::getList([
952 'select' => ['ID'],
953 'filter' => [
954 'TOPIC_ID' => $message['TOPIC_ID'],
955 ],
956 'order' => ['ID' => 'ASC'],
957 'limit' => 1
958 ])->fetch()))
959 {
960 Topic::delete($message['TOPIC_ID']);
961 }
962 else if ($message['NEW_TOPIC'] === 'Y')
963 {
964 MessageTable::update($nextMessage['ID'], ['NEW_TOPIC' => 'Y']);
965 }
966 /***************** Event onBeforeMessageAdd ************************/
967 foreach (GetModuleEvents("forum", "onAfterMessageDelete", true) as $event)
968 {
970 }
971 /***************** /Event ******************************************/
972 }
973 return $result;
974 }
975}
global $APPLICATION
Определения include.php:80
static checkFiles(Forum $forum, &$files, $params=["TOPIC_ID"=> 0, "MESSAGE_ID"=> 0, "USER_ID"=> 0])
Определения file.php:111
static saveFiles(&$files, $params, $uploadDir="forum/upload")
Определения file.php:197
static deleteBatch(array $filter)
Определения file.php:89
static getMainData(int $forumId, ?string $siteId=null)
Определения forum.php:198
static index(Forum\Forum $forum, Forum\Topic $topic, array $message)
Определения message.php:13
static deleteIndex(array $message)
Определения message.php:109
edit(array $fields)
Определения message.php:626
const APPROVED_DISAPPROVED
Определения message.php:615
static delete(int $id)
Определения message.php:909
init()
Определения message.php:617
static add(Forum\Topic $topic, array $fields)
Определения message.php:813
const APPROVED_APPROVED
Определения message.php:614
getXmlId()
Определения message.php:688
static update($id, array &$fields)
Определения message.php:718
static create($parentObject, array $fields)
Определения message.php:697
static getFilteredFields()
Определения message.php:150
static getMap()
Определения message.php:104
static onAfterUpdate(Main\ORM\Event $event)
Определения message.php:546
const SOURCE_ID_MOBILE
Определения message.php:79
static getUfId()
Определения message.php:91
static onAdd(Main\ORM\Event $event)
Определения message.php:319
static filterSourceIdParam(string $sourceId)
Определения message.php:182
static getDataById($id, $ttl=84600)
Определения message.php:377
static checkFields(Result $result, $primary, array $data)
Определения message.php:559
static onUpdate(Main\ORM\Event $event)
Определения message.php:508
static onAfterAdd(Main\ORM\Event $event)
Определения message.php:356
const SOURCE_ID_WEB
Определения message.php:78
static getTableName()
Определения message.php:86
const SOURCE_ID_EMAIL
Определения message.php:77
Определения topic.php:287
const APPROVED_DISAPPROVED
Определения topic.php:294
static delete(int $id)
Определения topic.php:744
const STATE_LINK
Определения topic.php:290
Определения 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 Delete($ID)
Определения vote.php:245
static GetFeatures($arForum)
Определения functions.php:50
$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
ExecuteModuleEventEx($arEvent, $arParams=[])
Определения tools.php:5214
IsModuleInstalled($module_id)
Определения tools.php:5301
GetModuleEvents($MODULE_ID, $MESSAGE_ID, $bReturnArray=false)
Определения tools.php:5177
trait EntityFabric
Определения entityfabric.php:5
Определения culture.php:9
Определения aliases.php:105
Определения ufield.php:9
$files
Определения mysql_to_pgsql.php:30
$message
Определения payment.php:8
$event
Определения prolog_after.php:141
if(empty($signedUserToken)) $key
Определения quickway.php:257
$val
Определения options.php:1793
$dbResult
Определения updtr957.php:3
$fields
Определения yandex_run.php:501