69 return 'b_forum_user';
81 'data_type' =>
'integer',
83 'autocomplete' =>
true,
86 'data_type' =>
'integer',
90 'data_type' =>
'Bitrix\Main\UserTable',
92 '=this.USER_ID' =>
'ref.ID'
95 'DESCRIPTION' => array(
96 'data_type' =>
'string',
99 'data_type' =>
'integer'
102 'data_type' =>
'integer'
105 'data_type' =>
'integer'
107 'NUM_POSTS' => array(
108 'data_type' =>
'integer'
110 'INTERESTS' => array(
111 'data_type' =>
'text'
113 'LAST_POST' => array(
114 'data_type' =>
'integer'
116 'SIGNATURE' => array(
117 'data_type' =>
'string'
119 'IP_ADDRESS' => array(
120 'data_type' =>
'string',
123 'REAL_IP_ADDRESS' => array(
124 'data_type' =>
'string',
128 'data_type' =>
'datetime',
130 'default_value' =>
function(){
return new DateTime();}
132 'LAST_VISIT' => array(
133 'data_type' =>
'datetime',
135 'default_value' =>
function(){
return new DateTime();}
137 'ALLOW_POST' => array(
138 'data_type' =>
"boolean",
139 'values' => array(
"N",
"Y"),
140 'default_value' =>
"Y"
142 'SHOW_NAME' => array(
143 'data_type' =>
"boolean",
144 'values' => array(
"N",
"Y"),
145 'default_value' =>
"Y"
147 'HIDE_FROM_ONLINE' => array(
148 'data_type' =>
"boolean",
149 'values' => array(
"N",
"Y"),
150 'default_value' =>
"N"
152 'SUBSC_GROUP_MESSAGE' => array(
153 'data_type' =>
"boolean",
154 'values' => array(
"N",
"Y"),
155 'default_value' =>
"N"
157 'SUBSC_GET_MY_MESSAGE' => array(
158 'data_type' =>
"boolean",
159 'values' => array(
"N",
"Y"),
160 'default_value' =>
"Y"
164 public static function onBeforeAdd(
Event $event)
169 if ($res = UserTable::getList([
171 "filter" => [
"USER_ID" => $data[
"USER_ID"]]
174 $result->addError(
new EntityError(
"Error: User is already exists.",
"event"));
178 return self::modifyData($event, $result);
185 return self::modifyData($event, $result);
188 private static function modifyData(Main\ORM\
Event $event, Main\ORM\
EventResult $result)
190 $data = array_merge($event->getParameter(
"fields"), $result->getModified());
194 if (array_key_exists(
"AVATAR", $data))
196 \CFile::ResizeImage($data[
"AVATAR"], array(
197 "width" => Main\
Config\Option::get(
"forum",
"avatar_max_width", 100),
198 "height" => Main\
Config\Option::get(
"forum",
"avatar_max_height", 100)));
199 $maxSize = Main\Config\Option::get(
"forum",
"file_max_size", 5242880);
200 if ($str = \CFile::CheckImageFile($data[
"AVATAR"], $maxSize))
202 $result->addError(
new FieldError(static::getEntity()->getField(
"AVATAR"), $str));
206 $fields[
"AVATAR"] = $data[
"AVATAR"];
207 $fields[
"AVATAR"][
"MODULE_ID"] =
"forum";
208 if ($id = $event->getParameter(
"id"))
210 $id = is_integer($id) ? $id : $id[
"ID"];
211 if ($id > 0 && ($res = UserTable::getById($id)->fetch()) && ($res[
"AVATAR"] > 0))
213 $fields[
"AVATAR"][
"old_file"] = $res[
"AVATAR"];
216 if (!\CFile::SaveForDB($fields,
"AVATAR",
"forum/avatar"))
218 $result->unsetField(
"AVATAR");
225 $result->modifyFields(array_merge($result->getModified(), $fields));
233 $id = $event->getParameter(
"id");
235 if (($events = GetModuleEvents(
"forum",
"onBeforeUserDelete",
true)) && !empty($events))
237 foreach ($events as $ev)
239 if (ExecuteModuleEventEx($ev, array($id)) ===
false)
241 $result->addError(
new EntityError(
"Error: ".serialize($ev),
"event"));
246 if (($user = UserTable::getById($id)->fetch()) && $user[
"AVATAR"] > 0)
248 \CFile::Delete($user[
"AVATAR"]);
255 $id = $event->getParameter(
"id");
257 foreach(GetModuleEvents(
"forum",
"onAfterUserDelete",
true) as $arEvent)
259 ExecuteModuleEventEx($arEvent, [$id]);
264class User implements \ArrayAccess {
266 use Internals\EntityBaseMethods;
281 private $editOwn =
false;
286 "VISIBLE_NAME"=>
"Guest"
290 $user = UserTable::getList(array(
293 "ACTIVE" =>
"USER.ACTIVE",
294 "NAME" =>
"USER.NAME",
295 "SECOND_NAME" =>
"USER.SECOND_NAME",
296 "LAST_NAME" =>
"USER.LAST_NAME",
297 "LOGIN" =>
"USER.LOGIN"
299 "filter" => array(
"USER_ID" => (
int)
$id),
304 $this->forumUserId = $user[
"ID"];
305 $this->
id = $user[
"USER_ID"];
306 $this->locked = ($user[
"ACTIVE"] !==
"Y" || $user[
"ALLOW_POST"] !==
"Y");
308 elseif ($user = Main\UserTable::getList(array(
309 'select' => array(
'*'),
310 'filter' => array(
'ID' => (
int)
$id),
314 $this->
id = $user[
"ID"];
315 $this->locked = ($user[
"ACTIVE"] !==
"Y");
317 $this->data[
"ALLOW_POST"] =
"Y";
318 $this->data[
"SHOW_NAME"] = (\COption::GetOptionString(
"forum",
"USER_SHOW_NAME",
"Y") ==
"Y" ?
"Y" :
"N");
325 $this->data[
"NAME"] = $user[
"NAME"];
326 $this->data[
"SECOND_NAME"] = $user[
"SECOND_NAME"];
327 $this->data[
"LAST_NAME"] = $user[
"LAST_NAME"];
328 $this->data[
"LOGIN"] = $user[
"LOGIN"];
329 $this->data[
"ALLOW_POST"] = ($this->data[
"ALLOW_POST"] ===
"N" ?
"N" :
"Y");
330 if ($this->data[
"SHOW_NAME"] !==
"Y" && $this->data[
"SHOW_NAME"] !==
"N")
331 $this->data[
"SHOW_NAME"] = (\COption::GetOptionString(
"forum",
"USER_SHOW_NAME",
"Y") ==
"Y" ?
"Y" :
"N");
332 $this->data[
"VISIBLE_NAME"] = ($this->data[
"SHOW_NAME"] ===
"Y" ? \CUser::FormatName(\CSite::getNameFormat(
false), $user,
true,
false) : $this->data[
"LOGIN"]);
333 $this->editOwn = (\COption::GetOptionString(
"forum",
"USER_EDIT_OWN_POST",
"Y") ==
"Y");
342 return $this->data[
"VISIBLE_NAME"];
347 if ($this->getId() <= 0)
352 $connection = Main\Application::getConnection();
353 $helper = $connection->getSqlHelper();
355 $update = $helper->prepareUpdate($tableName, [
'LAST_VISIT' =>
new Main\DB\
SqlExpression($helper->getCurrentDateTimeFunction())]);
356 $where = $helper->prepareAssignment($tableName,
'USER_ID', $this->getId());
357 $sql =
'UPDATE '.$helper->quote($tableName).
' SET '.$update[0].
' WHERE '.$where;
358 $connection->queryExecute($sql, $update[1]);
359 if ($connection->getAffectedRowsCount() <= 0)
361 $merge = $helper->prepareMerge(
365 'SHOW_NAME' => ($this->data[
'SHOW_NAME'] ===
'N' ?
'N' :
'Y'),
366 'ALLOW_POST' => ($this->data[
'ALLOW_POST'] ===
'N' ?
'N' :
'Y'),
367 'USER_ID' => $this->getId(),
372 'LAST_VISIT' =>
new Main\DB\
SqlExpression($helper->getCurrentDateTimeFunction())
377 $connection->query($merge[0]);
381 unset(
$GLOBALS[
'FORUM_CACHE'][
'USER']);
382 unset(
$GLOBALS[
'FORUM_CACHE'][
'USER_ID']);
388 if (!($USER instanceof \CUser && $this->getId() === $USER->GetID()))
393 $connection = Main\Application::getConnection();
394 $helper = $connection->getSqlHelper();
397 'USER_ID' => $this->getId(),
398 'PHPSESSID' => $this->getSessId()
401 'SHOW_NAME' => $this->
getName(),
402 'IP_ADDRESS' => Main\Service\GeoIp\Manager::getRealIp(),
404 'SITE_ID' => SITE_ID,
405 'FORUM_ID' => $forumId,
406 'TOPIC_ID' => $topicId,
409 if ($this->getId() > 0)
411 $fields[
'PHPSESSID'] = $primaryFields[
'PHPSESSID'];
412 unset($primaryFields[
'PHPSESSID']);
415 $merge = $helper->prepareMerge(
417 array_keys($primaryFields),
418 $primaryFields + $fields,
423 $connection->query($merge[0]);
441 return ($this->getId() <= 0);
446 return ($this->getId() > 0);
449 public function edit(array $fields)
458 foreach (GetModuleEvents(
"forum",
"onBeforeUserEdit",
true) as $arEvent)
460 if (ExecuteModuleEventEx($arEvent, [$this->forumUserId, $this->getId(), &$fields]) ===
false)
462 $result->addError(
new Error(
"Event error"));
467 $result = $this->save($fields);
469 if ($result->isSuccess())
471 foreach(GetModuleEvents(
"forum",
"onAfterUserEdit",
true) as $arEvent)
473 ExecuteModuleEventEx($arEvent, array($this->forumUserId, $this->getId(), $fields));
494 if ($res = MessageTable::getList([
495 "select" => [
"CNT",
"LAST_MESSAGE_ID"],
496 "filter" => [
"AUTHOR_ID" => $this->getId(),
"APPROVED" =>
"Y"],
504 "LAST_POST" => $res[
"LAST_MESSAGE_ID"],
505 "NUM_POSTS" => $res[
"CNT"],
506 "POINTS" => \CForumUser::GetUserPoints($this->getId(), [
"NUM_POSTS" => $res[
"CNT"]])
509 return $this->save($fields);
514 if (!$this->
isAuthorized() || $message[
"APPROVED"] !=
"Y")
519 $this->data[
"NUM_POSTS"]++;
520 $this->data[
"POINTS"] = \CForumUser::GetUserPoints($this->getId(), array(
"INCREMENT" => $this->data[
"NUM_POSTS"]));
521 $this->data[
"LAST_POST"] = $message[
"ID"];
523 "NUM_POSTS" =>
new Main\DB\
SqlExpression(
'?# + 1',
"NUM_POSTS"),
524 "POINTS" => $this->data[
"POINTS"],
525 "LAST_POST" => $message[
"ID"]
531 if (!$this->
isAuthorized() || $message[
'APPROVED'] !=
'Y')
536 $this->data[
'NUM_POSTS']--;
537 $this->data[
'POINTS'] = \CForumUser::GetUserPoints($this->getId(), array(
'INCREMENT' => $this->data[
'NUM_POSTS']));
540 'POINTS' => $this->data[
'POINTS'],
542 if ($message ===
null ||
543 $message[
'ID'] === $this->data[
'LAST_POST']
546 $message = MessageTable::getList([
548 'filter' => [
'AUTHOR_ID' => $this->getId(),
'APPROVED' =>
'Y'],
550 'order' => [
'ID' =>
'DESC']
552 $this->data[
'LAST_POST'] = $message[
'ID'];
553 $fields[
'LAST_POST'] = $message[
'ID'];
555 $this->save($fields);
570 $topic = Topic::getById($topicId);
577 $query = MessageTable::query()
579 ->where(
'TOPIC_ID', $topic->getId())
580 ->registerRuntimeField(
'FORCED_INT_ID',
new Main\
Entity\
ExpressionField(
'FORCED_ID',
'%s + ""', [
'ID']))
581 ->setOrder([
'FORCED_INT_ID' =>
'ASC'])
586 ->registerRuntimeField(
592 '=this.TOPIC_ID' =>
'ref.TOPIC_ID',
593 '=ref.USER_ID' => [
'?i', $this->getId()],
595 [
'join_type' =>
'LEFT']
598 ->registerRuntimeField(
602 UserForumTable::getEntity(),
604 '=this.FORUM_ID' =>
'ref.FORUM_ID',
605 '=ref.USER_ID' => [
'?i', $this->getId()]
607 [
'join_type' =>
'LEFT']
610 ->registerRuntimeField(
614 UserForumTable::getEntity(),
616 '=this.FORUM_ID' => [
'?i', 0],
617 '=ref.USER_ID' => [
'?i', $this->getId()]
619 [
'join_type' =>
'LEFT']
623 Main\ORM\
Query\Query::filter()
626 Main\ORM\
Query\Query::filter()
627 ->whereNotNull(
'USER_TOPIC.LAST_VISIT')
628 ->whereColumn(
'POST_DATE',
'>',
'USER_TOPIC.LAST_VISIT')
631 Main\ORM\
Query\Query::filter()
632 ->whereNull(
'USER_TOPIC.LAST_VISIT')
633 ->whereColumn(
'POST_DATE',
'>',
'USER_FORUM.LAST_VISIT')
636 Main\ORM\
Query\Query::filter()
637 ->whereNull(
'USER_TOPIC.LAST_VISIT')
638 ->whereNull(
'USER_FORUM.LAST_VISIT')
639 ->whereColumn(
'POST_DATE',
'>',
'USER_FORUM_0.LAST_VISIT')
642 Main\ORM\
Query\Query::filter()
643 ->whereNull(
'USER_TOPIC.LAST_VISIT')
644 ->whereNull(
'USER_FORUM.LAST_VISIT')
645 ->whereNull(
'USER_FORUM_0.LAST_VISIT')
652 $timestamps = $this->getFromSession(
'GUEST_TID');
654 $this->getFromSession(
'LAST_VISIT_FORUM_0'),
655 $this->getFromSession(
'LAST_VISIT_FORUM_'.$topic->getForumId()),
656 is_array($timestamps) && array_key_exists($topic->getId(), $timestamps) ? $timestamps[$topic->getId()] : 0
667 if ($res = $query->fetch())
681 || !($topic = Topic::getById($topicId))
682 || !($topic instanceof
Topic)
688 $topic->incrementViews();
692 $connection = Main\Application::getConnection();
693 $helper = $connection->getSqlHelper();
696 'USER_ID' => $this->getId(),
697 'TOPIC_ID' => $topic->getId()
701 'FORUM_ID' => $topic->getForumId(),
706 if ($result->getAffectedRowsCount() <= 0)
708 $merge = $helper->prepareMerge(
709 'b_forum_user_topic',
710 array_keys($primaryFields),
711 $primaryFields + $fields,
716 $connection->query($merge[0]);
723 $this->saveInSession(
'GUEST_TID',
null);
725 if (Main\
Config\Option::set(
'forum',
'USE_COOKIE',
'N') ==
'Y')
727 $GLOBALS[
'APPLICATION']->set_cookie(
'FORUM_GUEST_TID',
'',
false,
'/',
false,
false,
'Y',
false);
736 $connection = Main\Application::getConnection();
737 $helper = $connection->getSqlHelper();
740 'USER_ID' => $this->getId(),
741 'FORUM_ID' => $forumId
748 $result = Forum\UserForumTable::update($primaryFields, $fields);
749 if ($result->getAffectedRowsCount() <= 0)
751 $merge = $helper->prepareMerge(
753 array_keys($primaryFields),
754 $primaryFields + $fields,
759 $connection->query($merge[0]);
765 Forum\UserTopicTable::deleteBatch([
'USER_ID' => $this->getId(),
'FORUM_ID' => $forumId]);
769 Forum\UserTopicTable::deleteBatch([
'USER_ID' => $this->getId()]);
770 Forum\UserForumTable::deleteBatch([
'USER_ID' => $this->getId(),
'>FORUM_ID' => 0]);
776 $topics = $this->saveInSession(
'GUEST_TID', [$topic->getId() => $timestamp->getTimestamp()]);
778 if (Main\
Config\Option::set(
'forum',
'USE_COOKIE',
'N') ==
'Y')
780 foreach ($topics as $topicId => $timestamp)
782 $topics[$topicId] = implode(
'-', [$topicId, $timestamp]);
784 $GLOBALS[
'APPLICATION']->set_cookie(
'FORUM_GUEST_TID', implode(
'/', $topics),
false,
'/',
false,
false,
'Y',
false);
789 private function save(array $fields)
798 if ($this->forumUserId > 0)
804 $fields = [
'USER_ID' => $this->getId()] + $fields +
$this->data;
805 unset($fields[
'ID']);
807 if ($result->isSuccess())
809 $res = $result->getPrimary();
814 $this->forumUserId = $res;
826 if ($this->getId() <= 0 || $this->
isLocked())
828 if (Main\
Config\Option::get(
"main",
"new_user_registration",
"") ==
"Y")
830 $def_group = Main\Config\Option::get(
"main",
"new_user_registration_def_group",
"");
831 if($def_group !=
"" && ($res = explode(
",", $def_group)))
833 $this->groups = array_merge($this->groups, $res);
837 elseif ($USER instanceof \CUser && $this->getId() === $USER->GetID())
839 $this->groups = $USER->GetUserGroupArray();
843 $dbRes = Main\UserGroupTable::getList(array(
844 "select" => [
"GROUP_ID"],
845 "filter" => [
"USER_ID" => $this->getId()],
846 "order" => [
"GROUP_ID" =>
"ASC"]
848 while ($res = $dbRes->fetch())
850 $this->groups[] = $res[
"GROUP_ID"];
859 $forum = Forum\Forum::getInstance($forum);
860 $this->permissions[$forum->getId()] = $permission;
866 $forum = Forum\Forum::getInstance($forum);
867 if (!array_key_exists($forum->getId(), $this->permissions))
869 $this->permissions[$forum->getId()] = $forum->getPermissionForUser($this);
871 return $this->permissions[$forum->getId()];
906 ($this->editOwn || ($topic[
"POSTS"] <= 0 && $topic[
"POSTS_UNAPPROVED"] <= 0))
930 $topic = Topic::getById($message[
"TOPIC_ID"]);
931 if ($topic[
"ABS_LAST_MESSAGE_ID"] == $message->
getId())
967 return (in_array(1,
$groups) || $APPLICATION->GetGroupRight(
"forum",
$groups) >=
"W");
970 private function saveInSession(
string $name, $value)
974 $forumSession = Main\Application::getInstance()->getKernelSession()->get(
'FORUM');
978 $forumSession = $_SESSION[
'FORUM'];
980 $forumSession = is_array($forumSession) ? $forumSession : [];
981 if (is_array($value) && array_key_exists($name, $forumSession) && is_array($forumSession[$name]))
983 $forumSession[$name] = $value + $forumSession[$name];
987 $forumSession[$name] = $value;
991 Main\Application::getInstance()->getKernelSession()->set(
'FORUM', $forumSession);
995 $_SESSION[
'FORUM'] = $forumSession;
997 return $forumSession[$name];
1000 private function getFromSession(
string $name)
1004 $forumSession = Main\Application::getInstance()->getKernelSession()->get(
'FORUM');
1008 $forumSession = $_SESSION[
'FORUM'];
1010 if (is_array($forumSession) && array_key_exists($name, $forumSession))
1012 return $forumSession[$name];
1017 private function getSessId()
1021 return Main\Application::getInstance()->getKernelSession()->getId();
1023 return bitrix_sessid();
1028 $result = new \Bitrix\Main\ORM\Data\AddResult();
1029 if (($events = GetModuleEvents(
"forum",
"onBeforeUserAdd",
true)) && !empty($events))
1031 global $APPLICATION;
1032 foreach ($events as $ev)
1034 $APPLICATION->ResetException();
1036 if (ExecuteModuleEventEx($ev, array(&
$data)) ===
false)
1039 if (($ex = $APPLICATION->GetException()) && ($ex instanceof \CApplicationException))
1041 $errorMessage = $ex->getString();
1044 $result->addError(
new \
Bitrix\Main\
Error($errorMessage,
"onBeforeUserAdd"));
1050 $dbResult = UserTable::add(
$data);
1052 if (!$dbResult->isSuccess())
1054 $result->addErrors($dbResult->getErrors());
1058 $id = $dbResult->getId();
1059 $result->setId(
$id);
1060 foreach (GetModuleEvents(
"forum",
"onAfterUserAdd",
true) as $event)
1062 ExecuteModuleEventEx($event, [
$id,
$data]);
1071 unset(
$data[
"USER_ID"]);
1074 $result->setPrimary([
"ID" =>
$id]);
1076 if (($events = GetModuleEvents(
"forum",
"onBeforeUserUpdate",
true)) && !empty($events))
1078 global $APPLICATION;
1079 foreach ($events as $ev)
1081 $APPLICATION->ResetException();
1083 if (ExecuteModuleEventEx($ev, array(
$id, &
$data)) ===
false)
1085 $errorMessage =
Loc::getMessage(
"FORUM_EVENT_BEFORE_USER_UPDATE_ERROR");
1086 if (($ex = $APPLICATION->GetException()) && ($ex instanceof \CApplicationException))
1088 $errorMessage = $ex->getString();
1090 $result->addError(
new Main\
Error($errorMessage,
"onBeforeUserUpdate"));
1096 $dbResult = UserTable::update(
$id,
$data);
1098 if (!$dbResult->isSuccess())
1100 $result->addErrors($dbResult->getErrors());
1104 foreach (GetModuleEvents(
"forum",
"onAfterUserUpdate",
true) as $event)
1106 ExecuteModuleEventEx($event, [
$id,
$data]);
canReadForum(Forum\Forum $forum)
canDeleteForum(Forum\Forum $forum)
decrementStatistic($message=null)
readTopicsOnForum(int $forumId=0)
incrementStatistic(array $message)
getUnreadMessageId($topicId=0)
setLocation(int $forumId=0, int $topicId=0)
canReadTopic(Topic $topic)
canAddTopic(Forum\Forum $forum)
canEditMessage(Message $message)
canDeleteMessage(Message $message)
canEditForum(Forum\Forum $forum)
static isUserAdmin(array $groups)
setPermissionOnForum($forum, $permission)
canAddMessage(Topic $topic)
static update(int $id, array &$data)
canEditTopic(Topic $topic)
canModerate(Forum\Forum $forum)
getPermissionOnForum($forum)
static onAfterDelete(Main\ORM\Event $event)
static onBeforeUpdate(Main\ORM\Event $event)
static onBeforeDelete(Main\ORM\Event $event)
static loadMessages($file)
static getMessage($code, $replace=null, $language=null)
static update($primary, array $data)
static createFromTimestamp($timestamp)
$GLOBALS['____1444769544']