Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
baseobject.php
1<?php
2
4
5use \Bitrix\Forum\Internals\Error\ErrorCollection;
6use \Bitrix\Forum\Internals\Error\Error;
7use \Bitrix\Forum;
9use \Bitrix\Main\ArgumentTypeException;
10use \Bitrix\Main\ArgumentException;
11use \Bitrix\Main\Event;
14
15Loc::loadMessages(__FILE__);
16
17abstract class BaseObject
18{
19 const ERROR_PARAMS_FORUM_ID = 'params0001';
20 const ERROR_PARAMS_TOPIC_ID = 'params0002';
21 const ERROR_PARAMS_ENTITY_ID = 'params0003';
22 private static $topics = array();
23 private static $users = array();
24 /* @var \Bitrix\Forum\Comments\User */
25 protected $user;
26 /* @var \Bitrix\Forum\Comments\Entity */
27 protected $entity;
29 protected $forum;
31 protected $topic;
34
35 public function __construct($forumId, $entity, $userId = null)
36 {
37 global $USER;
38 $this->errorCollection = new ErrorCollection();
39 if (is_null($userId))
40 {
41 $userId = ($USER instanceof \CUser ? $USER->getId() : 0);
42 }
43 else
44 {
45 $userId = intval($userId);
46 }
47 $this->setUser($userId);
48
49 $this->setForum($forumId);
50 $this->setEntity($entity);
51 $this->setTopic();
52 }
53
54 protected function setEntity(array $id)
55 {
56 if (($id = array_change_key_case($id, CASE_LOWER)) && $id["id"] > 0)
57 $this->entity = $id;
58 else
59 throw new ArgumentException(Loc::getMessage("FORUM_CM_WRONG_ENTITY"), self::ERROR_PARAMS_ENTITY_ID);
60 }
61
66 public function getEntity()
67 {
68 if ($this->entity instanceof Entity)
69 return $this->entity;
70
71 if (!is_array($this->entity))
72 throw new ArgumentTypeException("entity");
73
74 $id = $this->entity;
75 $protoEntity = Entity::getEntityByType($id["type"]);
76 if (is_null($protoEntity))
77 {
78 $protoEntity = Entity::getEntityByType("default");
79 if (!array_key_exists('xml_id', $id) || empty($id["xml_id"]))
80 $id['xml_id'] = mb_strtoupper($id["type"]."_".$id['id']);
81 }
82 elseif (!array_key_exists('xml_id', $id) || empty($id["xml_id"]))
83 $id['xml_id'] = $protoEntity["xmlIdPrefix"]."_".$id['id'];
84 if (!Loader::includeModule($protoEntity["moduleId"]))
85 throw new SystemException("Module {$protoEntity["moduleId"]} is not included.");
86
87 $this->entity = new $protoEntity["className"]($id, $this->getForum());
88 if (! $this->entity instanceof Entity)
89 throw new SystemException("Entity Class does not descended from \\Bitrix\\Forum\\Comments\\Entity.");
90
91 return $this->entity;
92 }
93
94 protected function setTopic()
95 {
96 if (!array_key_exists($this->getEntity()->getXmlId(), self::$topics))
97 {
98 $dbRes = \CForumTopic::getList(null, array(
99 "FORUM_ID" => $this->forum["ID"],
100 "XML_ID" => $this->getEntity()->getXmlId()
101 ));
102 self::$topics[$this->getEntity()->getXmlId()] = (($res = $dbRes->fetch()) && $res ? $res : null);
103 }
104 $this->topic = self::$topics[$this->getEntity()->getXmlId()];
105 return $this;
106 }
107
108 protected function createTopic()
109 {
110 $topic = array(
111 'TITLE' => $this->getEntity()->getXmlId(),
112 'TAGS' => '',
113 'MESSAGE' => $this->getEntity()->getXmlId(),
114 'AUTHOR_ID' => 0
115 );
117 $request = \Bitrix\Main\Context::getCurrent()->getRequest();
118 $post = array_merge($request->getQueryList()->toArray(), $request->getPostList()->toArray());
119
120 $event = new Event("forum", "OnCommentTopicAdd", array($this->getEntity()->getType(), $this->getEntity()->getId(), $post, &$topic));
121 $event->send();
122
123 if (!isset($topic["AUTHOR_NAME"]) || strlen($topic["AUTHOR_NAME"]) <= 0)
124 $topic["AUTHOR_NAME"] = ($topic["AUTHOR_ID"] <= 0 ? Loc::getMessage("FORUM_USER_SYSTEM") : self::getUserName($topic["AUTHOR_ID"]));
125
126 $topic = array_merge($topic, array(
127 "FORUM_ID" => $this->forum["ID"],
128 'TITLE' => $topic["TITLE"],
129 'TAGS' => $topic["TAGS"],
130 'MESSAGE' => $topic["MESSAGE"],
131 "USER_START_ID" => $topic["AUTHOR_ID"],
132 "USER_START_NAME" => $topic["AUTHOR_NAME"],
133 "LAST_POSTER_NAME" => $topic["AUTHOR_NAME"],
134 "XML_ID" => $this->getEntity()->getXmlId(),
135 "APPROVED" => "Y"
136 ));
137 if (($tid = \CForumTopic::add($topic)) > 0)
138 {
139 if ($this->forum["ALLOW_HTML"] != "Y")
140 $topic['MESSAGE'] = strip_tags($topic['MESSAGE']);
141
142 $fields = Array(
143 "POST_MESSAGE" => $topic['MESSAGE'],
144 "AUTHOR_ID" => $topic["AUTHOR_ID"],
145 "AUTHOR_NAME" => $topic["AUTHOR_NAME"],
146 "FORUM_ID" => $topic["FORUM_ID"],
147 "TOPIC_ID" => $tid,
148 "APPROVED" => $topic["APPROVED"],
149 "NEW_TOPIC" => "Y",
150 "XML_ID" => $this->getEntity()->getXmlId(),
151 "PARAM1" => $this->getEntity()->getType(),
152 "PARAM2" => $this->getEntity()->getId()
153 );
154 if ((\CForumMessage::add($fields)) > 0)
155 {
156 $event = new Event("forum", "OnAfterCommentTopicAdd", array($this->getEntity()->getType(), $this->getEntity()->getId(), $tid));
157 $event->send();
158
159 self::$topics[$this->getEntity()->getXmlId()] = $topic + array("ID" => $tid);
160 return self::$topics[$this->getEntity()->getXmlId()];
161 }
162 \CForumTopic::delete($tid);
163 }
164 $this->errorCollection->add(array(new Error(Loc::getMessage("FORUM_CM_TOPIC_IS_NOT_CREATED"), self::ERROR_PARAMS_TOPIC_ID)));
165 return null;
166 }
167
172 public function getTopic()
173 {
174 return $this->topic;
175 }
176
180 public function hasErrors()
181 {
182 return $this->errorCollection->hasErrors();
183 }
184
188 public function getErrors()
189 {
190 return $this->errorCollection->toArray();
191 }
192
193 protected static function checkForumId(&$forumId)
194 {
195 $res = (is_integer($forumId) || is_string($forumId) ? intval($forumId) : 0);
196 if ($res > 0)
197 {
198 $forumId = $res;
199 // TODO Complete forum verifying
200 return true;
201 }
202 return false;
203 }
204
205 protected function setForum($id)
206 {
207 if (!$this->checkForumId($id))
208 throw new ArgumentTypeException(Loc::getMessage("FORUM_CM_FORUM_IS_WRONG"), self::ERROR_PARAMS_FORUM_ID);
209
210 $this->forum = Forum\ForumTable::getMainData($id, SITE_ID);
211
212 if (!$this->forum)
213 throw new ArgumentException(Loc::getMessage("FORUM_CM_FORUM_IS_LOST"), self::ERROR_PARAMS_FORUM_ID);
214
215 return $this;
216 }
217
222 public function getForum()
223 {
224 return $this->forum;
225 }
226
233 public function setForumFields(array $params)
234 {
235 foreach ($params as $key => $val)
236 {
237 if (array_key_exists($key, $this->forum))
238 $this->forum[$key] = $val;
239 }
240 return $this;
241 }
242
247 protected function setUser($userId)
248 {
249 $this->user = new \Bitrix\Forum\Comments\User($userId);
250 return $this->user;
251 }
255 public function getUser()
256 {
257 return $this->user;
258 }
259
260 public function getUserUnreadMessageId()
261 {
262 return $this->user->getUnreadMessageId($this->getTopic() ? $this->getTopic()["ID"] : 0);
263 }
264
265 public function setUserAsRead()
266 {
267 $this->user->readTopic($this->getTopic() ? $this->getTopic()["ID"] : 0);
268 }
269
270 public function setUserLocation()
271 {
272 $this->user->setLocation($this->forum["ID"], $this->getTopic() ? $this->getTopic()["ID"] : 0);
273 }
274
278 public function getApplication()
279 {
280 global $APPLICATION;
281 return $APPLICATION;
282 }
283
284 private static function getUserFromForum($userId)
285 {
286 if ($userId > 0 && !array_key_exists($userId, self::$users))
287 {
288 self::$users[$userId] = \CForumUser::getListEx(array(), array("USER_ID" => $userId))->fetch();
289 if(!self::$users[$userId])
290 {
291 self::$users[$userId] = \CUser::getById($userId)->fetch();
292 self::$users[$userId]["SHOW_NAME"] = \COption::getOptionString("forum", "USER_SHOW_NAME", "Y");
293 }
294 }
295 return self::$users[$userId];
296 }
297
298 protected function getUserName($userId)
299 {
300 $user = self::getUserFromForum($userId);
301 $name = "";
302 if (is_array($user))
303 {
304 $name = ($user["SHOW_NAME"] == "Y" ? trim($user["NAME"]." ".$user["LAST_NAME"]) : "");
305 $name = (empty($name) ? $user["LOGIN"] : $name);
306 }
307 return $name;
308 }
309}
310
__construct($forumId, $entity, $userId=null)
static getEntityByType($type="")
Definition entity.php:214
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29