Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
feed.php
1<?php
2
4
5use \Bitrix\Main\Localization\Loc;
6use \Bitrix\Forum\Internals\Error\Error;
7
8Loc::loadMessages(__FILE__);
9
10class Feed extends BaseObject
11{
12 const ERROR_PARAMS_MESSAGE = 'params0004';
13 const ERROR_PERMISSION = 'params0005';
14
15 private function checkTopic()
16 {
17 if (empty($this->topic))
18 {
19 $this->topic = $this->createTopic();
20 }
21 return ($this->topic !== null);
22 }
23
24 public function moveEventCommentsToNewXmlId(string $newEntityXmlId): bool
25 {
26 if (is_null($this->topic))
27 {
28 return true;
29 }
30
31 $forumId = $this->getForum()['ID'];
32
33 $rows = \Bitrix\Forum\MessageTable::query()
34 ->setSelect(['ID'])
35 ->where('FORUM_ID', $forumId)
36 ->where('TOPIC_ID', $this->topic['ID']);
37
38 $comments = $rows->fetchAll();
39 if (empty($comments))
40 {
41 return true;
42 }
43
44 $newFeed = new \Bitrix\Forum\Comments\Feed($forumId, [
45 'type' => 'EV',
46 'id' => $this->getEntity()->getId(),
47 'xml_id' => $newEntityXmlId,
48 ]);
49
50 if (!$newFeed->checkTopic())
51 {
52 return false;
53 }
54
55 $newTopicId = $newFeed->getTopic()['ID'];
56 $commentsIds = array_map('intval', array_column($comments, 'ID'));
57 \Bitrix\Forum\MessageTable::updateMulti($commentsIds, [
58 'TOPIC_ID' => $newTopicId,
59 ]);
60
61 return true;
62 }
63
68 public function canAdd()
69 {
70 return $this->getEntity()->canAdd($this->getUser()->getId());
71 }
72
77 public function canRead()
78 {
79 return $this->getEntity()->canRead($this->getUser()->getId());
80 }
81
86 public function canEdit()
87 {
88 return $this->getEntity()->canEdit($this->getUser()->getId());
89 }
90
95 public function canEditComment($commentId)
96 {
97 return Comment::createFromId($this, $commentId)->canEdit();
98 }
99
104 public function canDelete()
105 {
106 return $this->getEntity()->canEdit($this->getUser()->getId());
107 }
108
113 public function canDeleteComment($commentId)
114 {
115 return Comment::createFromId($this, $commentId)->canEdit();
116 }
117
121 public function canModerate()
122 {
123 return $this->getEntity()->canModerate($this->getUser()->getId());
124 }
128 public function canEditOwn()
129 {
130 return $this->getEntity()->canEditOwn($this->getUser()->getId());
131 }
132
138 public function add(array $params)
139 {
140 if (!$this->canAdd())
141 $this->errorCollection->addOne(new Error(Loc::getMessage("FORUM_CM_RIGHTS1"), self::ERROR_PERMISSION));
142 else if ($this->checkTopic())
143 {
144 $comment = Comment::create($this);
145 $comment->add($params);
146 if ($comment->hasErrors())
147 $this->errorCollection->add($comment->getErrors());
148 else
149 return $comment->getComment();
150 }
151 return false;
152 }
153
160 public function edit($id, array $params)
161 {
162 $comment = Comment::createFromId($this, $id);
163 if (!$this->canEdit() && !$comment->canEdit())
164 $this->errorCollection->addOne(new Error(Loc::getMessage("FORUM_CM_RIGHTS2"), self::ERROR_PERMISSION));
165 else
166 {
167 $comment->edit($params);
168 if ($comment->hasErrors())
169 $this->errorCollection->add($comment->getErrors());
170 else
171 return $comment->getComment();
172 }
173 return false;
174 }
175
181 public function delete($id)
182 {
183 $comment = Comment::createFromId($this, $id);
184 if (!$this->canDelete() && !$comment->canDelete())
185 $this->errorCollection->addOne(new Error(Loc::getMessage("FORUM_CM_RIGHTS3"), self::ERROR_PERMISSION));
186 else
187 {
188 $comment->delete();
189 if ($comment->hasErrors())
190 $this->errorCollection->add($comment->getErrors());
191 else
192 return $comment->getComment();
193 }
194 return false;
195 }
196
203 public function moderate($id, $show)
204 {
205 $comment = Comment::createFromId($this, $id);
206 if (!$this->canModerate())
207 $this->errorCollection->addOne(new Error(Loc::getMessage("FORUM_CM_RIGHTS4"), self::ERROR_PERMISSION));
208 else
209 {
210 $comment->moderate($show);
211 if ($comment->hasErrors())
212 $this->errorCollection->add($comment->getErrors());
213 else
214 return $comment->getComment();
215 }
216 return false;
217 }
224 public function send($id, array $params)
225 {
226 ob_start();
227 try{
228 global $APPLICATION;
229 $APPLICATION->IncludeComponent(
230 "bitrix:forum.comments",
231 "bitrix24",
232 [
233 "FORUM_ID" => $this->getForum()["ID"],
234 "ENTITY_TYPE" => $this->getEntity()->getType(),
235 "ENTITY_ID" => $this->getEntity()->getId(),
236 "ENTITY_XML_ID" => $this->getEntity()->getXmlId(),
237 "MID" => $id,
238 "ACTION" => "SEND",
239 "SHOW_POST_FORM" => "N"] + $params + [
240 "SHOW_RATING" => "Y",
241 "URL_TEMPLATES_PROFILE_VIEW" => "",
242 "CHECK_ACTIONS" => "N",
243 "RECIPIENT_ID" => $this->getUser()->getId()],
244 null,
245 array("HIDE_ICONS" => "Y")
246 );
247 $result = true;
248 }
249 catch (\Throwable $e)
250 {
251 $result = false;
252 }
253 ob_get_clean();
254 return $result;
255 }
263 public function setPermission($permission)
264 {
265 $this->getEntity()->setPermission($this->getUser()->getId(), $permission);
266 return $this;
267 }
268
273 public function setEditOwn($allow)
274 {
275 $this->getEntity()->setEditOwn($allow);
276 return $this;
277 }
278
283 public function getPermission()
284 {
285 return $this->getEntity()->getPermission($this->getUser()->getId());
286 }
287}
static createFromId(Feed $feed, $id)
Definition comment.php:577
static create(Feed $feed)
Definition comment.php:590
edit($id, array $params)
Definition feed.php:160
canDeleteComment($commentId)
Definition feed.php:113
moveEventCommentsToNewXmlId(string $newEntityXmlId)
Definition feed.php:24
canEditComment($commentId)
Definition feed.php:95
send($id, array $params)
Definition feed.php:224
setPermission($permission)
Definition feed.php:263
add(array $params)
Definition feed.php:138
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29