88 return 'b_forum_message';
93 return 'FORUM_MESSAGE';
96 private static $post_message_hash = [];
97 private static $messageById = [];
107 (
new IntegerField(
"ID", [
"primary" =>
true,
"autocomplete" =>
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])),
118 (
new StringField(
"POST_MESSAGE_CHECK", [
"size" => 32])),
124 (
new StringField(
"AUTHOR_NAME", [
"required" =>
true,
"size" => 255])),
125 (
new StringField(
"AUTHOR_EMAIL", [
"size" => 255])),
127 (
new StringField(
"AUTHOR_REAL_IP", [
"size" => 255])),
132 (
new StringField(
"EDITOR_EMAIL", [
"size" => 255])),
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")))
161 private static function modifyMessageFields(array &$data)
163 unset($data[
"UPLOAD_DIR"]);
164 if (array_key_exists(
"USE_SMILES", $data))
166 $data[
"USE_SMILES"] = $data[
"USE_SMILES"] ===
"N" ?
"N" :
"Y";
168 if (array_key_exists(
"NEW_TOPIC", $data))
170 $data[
"NEW_TOPIC"] = $data[
"NEW_TOPIC"] ===
"Y" ?
"Y" :
"N";
172 if (array_key_exists(
"APPROVED", $data))
176 if (array_key_exists(
"SOURCE_ID", $data))
184 if (in_array($sourceId, [self::SOURCE_ID_WEB, self::SOURCE_ID_MOBILE, self::SOURCE_ID_EMAIL],
true))
191 public static function onBeforeAdd(
Event $event)
196 $strUploadDir = array_key_exists(
"UPLOAD_DIR", $data) ? $data[
"UPLOAD_DIR"] :
"forum";
197 self::modifyMessageFields($data);
199 if (array_key_exists(
"ATTACH_IMG", $data) && !empty($data[
"ATTACH_IMG"]))
201 if (!array_key_exists(
"FILES", $data))
205 $data[
"FILES"][] = $data[
"ATTACH_IMG"];
206 unset($data[
"ATTACH_IMG"]);
208 if (array_key_exists(
"FILES", $data))
210 $data[
"FILES"] = is_array($data[
"FILES"]) ? $data[
"FILES"] : [$data[
"FILES"]];
211 if (!empty($data[
"FILES"]))
217 "FORUM_ID" => $data[
"FORUM_ID"],
218 "TOPIC_ID" => ($data[
"NEW_TOPIC"] ===
"Y" ? 0 : $data[
"TOPIC_ID"]),
220 "USER_ID" => $data[
"AUTHOR_ID"]
223 if (!$res->isSuccess())
225 $result->setErrors($res->getErrors());
232 $object->sysSetRuntime(
"FILES", $data[
"FILES"]);
233 $object->sysSetRuntime(
"UPLOAD_DIR", $strUploadDir);
236 unset($data[
"FILES"]);
240 $data[
"POST_MESSAGE_CHECK"] = md5($data[
"POST_MESSAGE"] . (array_key_exists(
"FILES", $data) ? serialize($data[
"FILES"]) :
""));
243 $forum = Forum\Forum::getById($data[
"FORUM_ID"]);
244 $deduplication =
null;
245 if (array_key_exists(
"AUX", $data))
247 if ($data[
"AUX"] ==
"Y")
249 $deduplication =
false;
253 if (array_key_exists(
"DEDUPLICATION", $data))
255 $deduplication = $data[
"DEDUPLICATION"] ==
"Y";
256 unset($data[
"DEDUPLICATION"]);
258 if ($deduplication ===
null)
260 $deduplication = $forum[
"DEDUPLICATION"] ===
"Y";
262 if ($deduplication && $data[
"NEW_TOPIC"] !==
"Y")
264 if (self::getLastMessageHashInTopic($data[
"TOPIC_ID"]) === $data[
"POST_MESSAGE_CHECK"])
266 $result->addError(
new EntityError(Loc::getmessage(
"F_ERR_MESSAGE_ALREADY_EXISTS"),
"onBeforeMessageAdd"));
272 $data[
"POST_MESSAGE"] = Main\Text\Emoji::encode($data[
"POST_MESSAGE"]);
275 if (Main\
Config\Option::get(
"forum",
"FILTER",
"Y") ==
"Y")
277 $data[
"POST_MESSAGE_FILTER"] = \CFilterUnquotableWords::Filter($data[
"POST_MESSAGE"]);
280 foreach ($filteredFields as $key)
282 $res[$key] = array_key_exists($key, $data) ? $data[$key] :
"";
283 if (!empty($res[$key]))
285 $res[$key] = \CFilterUnquotableWords::Filter($res[$key]);
286 if ($res[$key] ==
'')
292 $data[
"HTML"] = serialize($res);
297 if ($data != $fields)
299 foreach ($fields as $key => $val)
301 if (!array_key_exists($key, $data))
303 $result->unsetField($key);
305 else if ($data[$key] == $val)
310 $result->modifyFields($data);
322 if (Main\
Config\Option::get(
"forum",
"MESSAGE_HTML",
"N") ==
"Y")
324 $fields = $event->getParameter(
"fields");
325 $object = $event->getParameter(
"object");
327 if ($files = $object->sysGetRuntime(
"FILES"))
332 "FORUM_ID" => $fields[
"FORUM_ID"],
333 "TOPIC_ID" => $fields[
"TOPIC_ID"],
335 "USER_ID" => $fields[
"AUTHOR_ID"],
337 ($object->sysGetRuntime(
"UPLOAD_DIR") ?:
"forum/upload"));
338 $object->sysSetRuntime(
"FILES", $files);
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)
358 $object = $event->getParameter(
"object");
360 if ($files = $object->sysGetRuntime(
"FILES"))
362 $id = $event->getParameter(
"id");
363 $id = is_array($id) && array_key_exists(
"ID", $id) ? $id[
"ID"] : $id;
364 $fields = $event->getParameter(
"fields");
368 "FORUM_ID" => $fields[
"FORUM_ID"],
369 "TOPIC_ID" => $fields[
"TOPIC_ID"],
371 "USER_ID" => $fields[
"AUTHOR_ID"],
373 ($object->sysGetRuntime(
"UPLOAD_DIR") ?:
"forum/upload"));
379 if (!array_key_exists($id, self::$messageById))
381 self::$messageById[$id] = self::getList([
383 "filter" => [
"ID" => $id],
389 return self::$messageById[$id];
397 public static function onBeforeUpdate(Main\ORM\
Event $event)
401 $data = $event->getParameter(
"fields");
402 $id = $event->getParameter(
"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))))
411 array_intersect(self::getFilteredFields(), array_keys($data)) !== self::getFilteredFields() &&
415 $forFilter = array_merge($message, $forFilter);
418 foreach (self::getFilteredFields() as $key)
420 $res[$key] = array_key_exists($key, $forFilter) ? $forFilter[$key] :
"";
421 if (!empty($res[$key]))
423 $res[$key] = \CFilterUnquotableWords::Filter($res[$key]);
424 if ($res[$key] ==
'' )
430 $data[
"HTML"] = serialize($res);
432 if (array_key_exists(
"POST_MESSAGE", $data))
434 $data[
"POST_MESSAGE"] = Main\Text\Emoji::encode($data[
"POST_MESSAGE"]);
435 if (Main\
Config\Option::get(
"forum",
"FILTER",
"Y") ==
"Y")
437 $data[
"POST_MESSAGE_FILTER"] = \CFilterUnquotableWords::Filter($data[
"POST_MESSAGE"]);
441 unset($data[
"DEDUPLICATION"]);
444 if (array_key_exists(
"ATTACH_IMG", $data) && !empty($data[
"ATTACH_IMG"]))
446 if (!array_key_exists(
"FILES", $data))
450 $data[
"FILES"][] = $data[
"ATTACH_IMG"];
451 unset($data[
"ATTACH_IMG"]);
453 if (array_key_exists(
"FILES", $data))
455 $data[
"FILES"] = is_array($data[
"FILES"]) ? $data[
"FILES"] : [$data[
"FILES"]];
456 if (!empty($data[
"FILES"]))
459 $res = Forum\File::checkFiles(
463 "FORUM_ID" => $fileFields[
"FORUM_ID"],
464 "TOPIC_ID" => $fileFields[
"TOPIC_ID"],
466 "USER_ID" => $fileFields[
"AUTHOR_ID"]
469 if (!$res->isSuccess())
471 $result->setErrors($res->getErrors());
476 $object = $event->getParameter(
"object");
478 $object->sysSetRuntime(
"FILES", $data[
"FILES"]);
479 $object->sysSetRuntime(
"UPLOAD_DIR", $strUploadDir);
480 $object->sysSetRuntime(
"FILE_FIELDS", $fileFields);
483 unset($data[
"FILES"]);
486 $fields = $event->getParameter(
"fields");
487 if ($data != $fields)
489 foreach ($fields as $key => $val)
491 if (!array_key_exists($key, $data))
493 $result->unsetField($key);
495 else if ($data[$key] == $val)
500 $result->modifyFields($data);
510 $id = $event->getParameter(
"id");
514 $fields = $event->getParameter(
"fields") + $message;
515 $object = $event->getParameter(
"object");
517 if ($files = $object->sysGetRuntime(
"FILES"))
522 "FORUM_ID" => $fields[
"FORUM_ID"],
523 "TOPIC_ID" => $fields[
"TOPIC_ID"],
525 "USER_ID" => $fields[
"AUTHOR_ID"],
527 ($object->sysGetRuntime(
"UPLOAD_DIR") ?:
"forum/upload"));
529 if (Main\
Config\Option::get(
"forum",
"MESSAGE_HTML",
"N") ==
"Y")
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)
548 $id = $event->getParameter(
"id");
550 unset(self::$messageById[$id]);
561 parent::checkFields($result, $primary, $data);
570 if (array_key_exists(
"TOPIC_ID", $data))
572 if (!($topic = TopicTable::getById($data[
"TOPIC_ID"])->fetch()))
582 catch (\Exception $e)
591 private static function getLastMessageHashInTopic(
int $topicId): ?string
593 $res = MessageTable::query()
594 ->setSelect([
'ID',
'POST_MESSAGE_CHECK'])
595 ->where(
'TOPIC_ID',
'=', $topicId)
596 ->where(
'APPROVED',
'=',
'Y')
597 ->setOrder([
'ID' =>
'DESC'])
602 return $res ? $res[
'POST_MESSAGE_CHECK'] :
null;
static loadMessages($file)
static getMessage($code, $replace=null, $language=null)
isSuccess($internalCall=false)