Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
comment.php
1<?php
2
4
11use \Bitrix\Main\Localization\Loc;
12use \Bitrix\Forum\Internals\Error\Error;
13use \Bitrix\Main\Event;
14use \Bitrix\Main\EventResult;
15use \Bitrix\Main\ArgumentException;
17
18Loc::loadMessages(__FILE__);
19
20class Comment extends BaseObject
21{
22
23 const ERROR_PARAMS_MESSAGE = 'params0006';
24 const ERROR_PERMISSION = 'params0007';
25 const ERROR_MESSAGE_IS_NULL = 'params0008';
26 const ERROR_PARAMS_TYPE = 'params0009';
27
28
29 /* @var integer */
30 private $id = 0;
31 /* @var array */
32 private $message = null;
33
34 private function prepareFields(array &$params, ErrorCollection $errorCollectionParam)
35 {
36 $result = array(
37 "FORUM_ID" => $this->topic["FORUM_ID"],
38 "TOPIC_ID" => $this->topic["ID"],
39 "POST_MESSAGE" => trim($params["POST_MESSAGE"]),
40 "AUTHOR_ID" => $params["AUTHOR_ID"],
41 "AUTHOR_NAME" => trim($params["AUTHOR_NAME"]),
42 "AUTHOR_EMAIL" => trim($params["AUTHOR_EMAIL"]),
43 "USE_SMILES" => ($params["USE_SMILES"] == "Y" ? "Y" : "N"),
44 "APPROVED" => $this->topic["APPROVED"],
45 "XML_ID" => $this->getEntity()->getXmlId(),
46 "AUX" => ($params["AUX"] ?? 'N'),
47 "AUX_DATA" => ($params["AUX_DATA"] ?? ''),
48 ) + array_intersect_key($params, array_flip([
49 "POST_DATE", "SOURCE_ID",
50 "AUTHOR_IP", "AUTHOR_REAL_IP",
51 "GUEST_ID"
52 ]));
53
55 if (isset($params["SERVICE_TYPE"]))
56 {
57 if (!in_array($params["SERVICE_TYPE"], \Bitrix\Forum\Comments\Service\Manager::getTypesList()))
58 {
59 $errorCollection->addOne(new Error(Loc::getMessage("FORUM_CM_ERR_TYPE_INCORRECT"), self::ERROR_PARAMS_TYPE));
60 }
61 else
62 {
63 $result["SERVICE_TYPE"] = $params["SERVICE_TYPE"];
64 if (!isset($params["SERVICE_DATA"]))
65 {
66 if (($result["SERVICE_TYPE"] === \Bitrix\Forum\Comments\Service\Manager::TYPE_TASK_INFO ||
67 $result["SERVICE_TYPE"] === \Bitrix\Forum\Comments\Service\Manager::TYPE_TASK_CREATED)
68 && JSon::decode($result["POST_MESSAGE"]) == $params["AUX_DATA"])
69 {
70 $params["SERVICE_DATA"] = $result["POST_MESSAGE"];
71 $result["POST_MESSAGE"] = "";
72 }
73 else
74 {
75 $params["SERVICE_DATA"] = Json::encode($params["AUX_DATA"] ?? []);
76 }
77 }
78 $result["SERVICE_DATA"] = $params["SERVICE_DATA"];
79 if ($result["POST_MESSAGE"] == "" &&
80 ($handler = \Bitrix\Forum\Comments\Service\Manager::find(
81 ["SERVICE_TYPE" => $result["SERVICE_TYPE"]]
82 )))
83 {
84 $result["POST_MESSAGE"] = $handler->getText($result["SERVICE_DATA"]);
85 }
86 }
87 }
88 if ($result["POST_MESSAGE"] == '')
89 {
90 $errorCollection->addOne(new Error(Loc::getMessage("FORUM_CM_ERR_EMPTY_TEXT"), self::ERROR_PARAMS_MESSAGE));
91 }
92
93 if ($result["AUTHOR_NAME"] == '' && $result["AUTHOR_ID"] > 0)
94 $result["AUTHOR_NAME"] = self::getUserName($result["AUTHOR_ID"]);
95 if ($result["AUTHOR_NAME"] == '')
96 $errorCollection->addOne(new Error(Loc::getMessage("FORUM_CM_ERR_EMPTY_AUTHORS_NAME"), self::ERROR_PARAMS_MESSAGE));
97
98 if (is_array($params["FILES"]) && in_array($this->forum["ALLOW_UPLOAD"], array("Y", "F", "A")))
99 {
100 $result["FILES"] = array();
101 foreach ($params["FILES"] as $key => $val)
102 {
103 if (intval($val["FILE_ID"]) > 0 && $val["del"] !== "Y")
104 {
105 unset($val["del"]);
106 }
107 $result["FILES"][$key] = $val;
108 }
109 $res = array(
110 "FORUM_ID" => $this->forum["ID"],
111 "TOPIC_ID" => $this->topic["ID"],
112 "MESSAGE_ID" => 0,
113 "USER_ID" => $result["AUTHOR_ID"],
114 "FORUM" => $this->forum
115 );
116 if (!\CForumFiles::checkFields($result["FILES"], $res, "NOT_CHECK_DB"))
117 {
118 $text = "File upload error.";
119 if (($ex = $this->getApplication()->getException()) && $ex)
120 $text = $ex->getString();
121 $errorCollection->addOne(new Error($text, self::ERROR_PARAMS_MESSAGE));
122 }
123 }
124 if ($result["APPROVED"] != "N")
125 {
126 $result["APPROVED"] = ($this->forum["MODERATION"] != "Y" || $this->getEntity()->canModerate($this->getUser()->getId())) ? "Y" : "N";
127 }
128 if ($errorCollection->hasErrors())
129 {
130 $errorCollectionParam->add($errorCollection->toArray());
131 return false;
132 }
133
134 global $USER_FIELD_MANAGER;
135 if (!empty($result["SERVICE_TYPE"]))
136 {
137 $fields = $USER_FIELD_MANAGER->getUserFields("FORUM_MESSAGE");
138 if (($ufData = array_intersect_key($params, $fields)) && !empty($ufData))
139 {
140 $USER_FIELD_MANAGER->editFormAddFields("FORUM_MESSAGE", $result, ["FORM" => $ufData]);
141 }
142 }
143 else
144 {
145 $USER_FIELD_MANAGER->editFormAddFields("FORUM_MESSAGE", $result);
146 }
147 $params = $result;
148 return true;
149 }
150
151 private function updateStatisticModule($messageId)
152 {
153 if (Loader::includeModule("statistic"))
154 {
155 $forumEvent1 = $this->forum["EVENT1"];
156 $forumEvent2 = $this->forum["EVENT2"];
157 $forumEvent3 = $this->forum["EVENT3"];
158 if (empty($forumEvent3))
159 {
160 $site = (array) \CForumNew::getSites($this->forum["ID"]);
161 $forumEvent3 = \CForumNew::preparePath2Message((array_key_exists(SITE_ID, $site) ? $site[SITE_ID] : reset($site)),
162 array(
163 "FORUM_ID" => $this->forum["ID"],
164 "TOPIC_ID" => $this->topic["ID"],
165 "MESSAGE_ID" => $messageId
166 )
167 );
168 }
169 \CStatistics::set_Event($forumEvent1, $forumEvent2, $forumEvent3);
170 }
171 }
172
178 public function add(array $params)
179 {
180 $aux = (isset($params['AUX']) && $params['AUX'] === "Y");
181 $auxData = ($params['AUX_DATA'] ?? '');
182
183 $params = array(
184 "SOURCE_ID" => $params["SOURCE_ID"] ?? 0,
185
186 "POST_DATE" => array_key_exists("POST_DATE", $params) ? $params["POST_DATE"] : new \Bitrix\Main\Type\DateTime(),
187 "POST_MESSAGE" => trim($params["POST_MESSAGE"]),
188 "FILES" => $params["FILES"] ?? null,
189
190 "USE_SMILES" => $params["USE_SMILES"],
191
192 "AUTHOR_ID" => $this->getUser()->getId(),
193 "AUTHOR_NAME" => trim($params["AUTHOR_NAME"] ?? ''),
194 "AUTHOR_EMAIL" => trim($params["AUTHOR_EMAIL"] ?? ''),
195
196 "AUTHOR_IP" => $params["AUTHOR_IP"] ?? "<no address>",
197 "AUTHOR_REAL_IP" => $params["AUTHOR_REAL_IP"] ?? "<no address>",
198 "GUEST_ID" => $params["GUEST_ID"] ?? null,
199
200 "AUX" => $params["AUX"] ?? null,
201 "AUX_DATA" => $auxData,
202 "SERVICE_TYPE" => ($params["SERVICE_TYPE"] ?? null),
203 "SERVICE_DATA" => ($params["SERVICE_DATA"] ?? null),
204
205 "UF_TASK_COMMENT_TYPE" => ($params["UF_TASK_COMMENT_TYPE"] ?? null),
206 "UF_FORUM_MES_URL_PRV" => ($params["UF_FORUM_MES_URL_PRV"] ?? null),
207 );
208
209 if ($this->prepareFields($params, $this->errorCollection))
210 {
211 /***************** Events OnBeforeCommentAdd ******************/
212 $event = new Event("forum", "OnBeforeCommentAdd", [
213 $this->getEntity()->getType(),
214 $this->getEntity()->getId(),
215 $params
216 ]);
217 $event->send($this);
218 if($event->getResults())
219 {
220 foreach($event->getResults() as $eventResult)
221 {
222 if($eventResult->getType() != EventResult::SUCCESS)
223 {
224 $run = false;
225 break;
226 }
227 }
228 }
229 /***************** /Events *****************************************/
230
231 $topic = \Bitrix\Forum\Topic::getById($params["TOPIC_ID"]);
232 $result = \Bitrix\Forum\Message::create($topic, $params);
233
234 if ($result->isSuccess())
235 {
236 $mid = $result->getId();
237
238 if (!$aux)
239 {
240 $this->updateStatisticModule($mid);
241 \CForumMessage::sendMailMessage($mid, array(), false, "NEW_FORUM_MESSAGE");
242 }
243
244 $this->setComment($mid);
245
246 if (
247 !$aux // create task from livefeed
248 || $auxData <> '' // tasks commentposter, add to livefeed
249 )
250 {
251 $event = new Event("forum", "OnAfterCommentAdd", array(
252 $this->getEntity()->getType(),
253 $this->getEntity()->getId(),
254 array(
255 "TOPIC_ID" => $this->topic["ID"],
256 "MESSAGE_ID" => $mid,
257 "PARAMS" => $params,
258 "MESSAGE" => $this->getComment(),
259 "AUX_DATA" => $auxData
260 ))
261 );
262 $event->send();
263 }
264
265 return $this->getComment();
266 }
267 $this->errorCollection->addFromResult($result);
268 }
269 return false;
270 }
276 public function edit(array $params)
277 {
278 $paramsRaw = $params;
279 if ($this->message === null)
280 {
281 $this->errorCollection->addOne(new Error(Loc::getMessage("FORUM_CM_ERR_COMMENT_IS_LOST1"), self::ERROR_MESSAGE_IS_NULL));
282 }
283 else
284 {
285 $run = true;
286 $fields = array(
287 $this->getEntity()->getType(),
288 $this->getEntity()->getId(),
289 array(
290 "TOPIC_ID" => $this->topic["ID"],
291 "MESSAGE_ID" => $this->message["ID"],
292 "PARAMS" => &$paramsRaw,
293 "ACTION" => "EDIT",
294 "MESSAGE" => $this->getComment()
295 )
296 );
297 /***************** Events OnBeforeCommentUpdate ******************/
298 $event = new Event("forum", "OnBeforeCommentUpdate", $fields);
299 $event->send($this);
300 if($event->getResults())
301 {
302 foreach($event->getResults() as $eventResult)
303 {
304 if($eventResult->getType() != EventResult::SUCCESS)
305 {
306 $run = false;
307 break;
308 }
309 }
310 }
311 /***************** /Events *****************************************/
312 if (!$run)
313 {
314 $text = Loc::getMessage("ADDMESS_ERROR_EDIT_MESSAGE");
315 if (($str = $this->getApplication()->getException()) && $str)
316 $text = $str->getString();
317 $this->errorCollection->addOne(new Error($text, self::ERROR_PARAMS_MESSAGE));
318 }
319 else if (($params = array(
320 "POST_MESSAGE" => trim($params["POST_MESSAGE"]),
321 "AUTHOR_ID" => $this->message["AUTHOR_ID"],
322 "AUTHOR_NAME" => (array_key_exists("AUTHOR_NAME", $params) ? trim($params["AUTHOR_NAME"]) : $this->message["AUTHOR_NAME"]),
323 "AUTHOR_EMAIL" => (array_key_exists("AUTHOR_EMAIL", $params) ? trim($params["AUTHOR_EMAIL"]) : $this->message["AUTHOR_EMAIL"]),
324 "USE_SMILES" => $params["USE_SMILES"],
325 "FILES" => $params["FILES"],
326 "AUX" => $params["AUX"],
327 "AUX_DATA" => $params["AUX_DATA"],
328 )) && $this->prepareFields($params, $this->errorCollection))
329 {
330 if (array_key_exists("POST_DATE", $paramsRaw))
331 {
332 $params["POST_DATE"] = $paramsRaw["POST_DATE"];
333 }
334 if (array_key_exists("EDIT_REASON", $paramsRaw))
335 {
336 $params += array(
337 "EDITOR_ID" => $this->getUser()->getId(),
338 "EDITOR_NAME" => trim($paramsRaw["EDITOR_NAME"]),
339 "EDITOR_EMAIL" => trim($paramsRaw["EDITOR_EMAIL"]),
340 "EDIT_REASON" => trim($paramsRaw["EDIT_REASON"]),
341 "EDIT_DATE" => ""
342 );
343 if ($params["EDITOR_NAME"] == '')
344 $params["EDITOR_NAME"] = ($params["EDITOR_ID"] > 0 ? self::getUserName($params["EDITOR_ID"]) : Loc::getMessage("GUEST"));
345 }
346 $result = \Bitrix\Forum\Message::getById($this->message["ID"])->edit($params);
347 if ($result->isSuccess())
348 {
349 $mid = $this->message["ID"];
350 unset($GLOBALS["FORUM_CACHE"]["MESSAGE"][$mid]);
351 unset($GLOBALS["FORUM_CACHE"]["MESSAGE_FILTER"][$mid]);
352
353 if ($params["AUTHOR_ID"] != $this->getUser()->getId() || Option::get("forum", "LOGS", "Q") < "U")
354 {
355 $resLog = array();
356 foreach ($paramsRaw as $key => $val)
357 {
358 if ($val == $this->message[$key])
359 continue;
360 else if ($key == "FILES")
361 $resLog["FILES"] = GetMessage("F_ATTACH_IS_MODIFIED");
362 else
363 $resLog[$key] = array(
364 "before" => $this->message[$key],
365 "after" => $val
366 );
367 }
368 if (!empty($resLog))
369 {
370 $resLog["FORUM_ID"] = $this->forum["ID"];
371 $resLog["TOPIC_ID"] = $this->topic["ID"];
372 $resLog["TITLE"] = $this->topic["TITLE"];
373 \CForumEventLog::log("message", "edit", $this->message["ID"], serialize($resLog));
374 }
375 }
376 $this->updateStatisticModule($mid);
377 \CForumMessage::sendMailMessage($mid, array(), false, "EDIT_FORUM_MESSAGE");
378
379 $this->setComment($mid);
380 $fields["PARAMS"] = $params;
381 /***************** Events OnAfterCommentUpdate *******************/
382 $event = new Event("forum", "OnAfterCommentUpdate", $fields);
383 $event->send();
384 /***************** /Events *****************************************/
385 return $this->getComment();
386 }
387 $this->errorCollection->addFromResult($result);
388 }
389 }
390 return false;
391 }
392
393 public function delete()
394 {
395 if ($this->message === null)
396 {
397 $this->errorCollection->addOne(new Error(Loc::getMessage("FORUM_CM_ERR_COMMENT_IS_LOST2"), self::ERROR_MESSAGE_IS_NULL));
398 }
399 else
400 {
401 $run = true;
402 $fields = array(
403 $this->getEntity()->getType(),
404 $this->getEntity()->getId(),
405 array(
406 "TOPIC_ID" => $this->topic["ID"],
407 "MESSAGE_ID" => $this->message["ID"],
408 "MESSAGE" => $this->getComment(),
409 "ACTION" => "DEL"
410 ));
411 /***************** Events OnBeforeCommentDelete ******************/
412 $event = new Event("forum", "OnBeforeCommentDelete", $fields);
413 $event->send($this);
414 if($event->getResults())
415 {
416 foreach($event->getResults() as $eventResult)
417 {
418 if($eventResult->getType() != EventResult::SUCCESS)
419 {
420 $run = false;
421 break;
422 }
423 }
424 }
425 /***************** /Events *****************************************/
426 if ($run && \CForumMessage::delete($this->message["ID"]))
427 {
428 \CForumEventLog::log("message", "delete", $this->message["ID"], serialize($this->message + array("TITLE" => $this->topic["TITLE"])));
429 /***************** Events OnCommentDelete ************************/
430 $event = new Event("forum", "OnCommentDelete", $fields);
431 $event->send();
432 /***************** Events OnAfterCommentUpdate *********************/
433 $event = new Event("forum", "OnAfterCommentUpdate", $fields); // It is not a mistake
434 $event->send();
435 /***************** /Events *****************************************/
436 }
437 else
438 {
439 $text = Loc::getMessage("FORUM_CM_ERR_DELETE");
440 if (($ex = $this->getApplication()->getException()) && $ex)
441 $text = $ex->getString();
442 $this->errorCollection->addOne(new Error($text, self::ERROR_PARAMS_MESSAGE));
443 }
444 }
445 return true;
446 }
447
448 public function moderate($show)
449 {
450 if ($this->message === null)
451 {
452 $this->errorCollection->addOne(new Error(Loc::getMessage("FORUM_CM_ERR_COMMENT_IS_LOST3"), self::ERROR_MESSAGE_IS_NULL));
453 }
454 else
455 {
456 $run = true;
457 $fields = array(
458 $this->getEntity()->getType(),
459 $this->getEntity()->getId(),
460 array(
461 "TOPIC_ID" => $this->topic["ID"],
462 "MESSAGE_ID" => $this->message["ID"],
463 "MESSAGE" => $this->getComment(),
464 "ACTION" => $show ? "SHOW" : "HIDE",
465 "PARAMS" => array("APPROVED" => ($show ? "Y" : "N"))
466 ));
467 /***************** Events OnBeforeCommentModerate ****************/
468 $event = new Event("forum", "OnBeforeCommentModerate", $fields);
469 $event->send($this);
470 if($event->getResults())
471 {
472 foreach($event->getResults() as $eventResult)
473 {
474 if($eventResult->getType() != EventResult::SUCCESS)
475 {
476 $run = false;
477 break;
478 }
479 }
480 }
481 /***************** /Events *****************************************/
482 if ($run && $this->message["APPROVED"] == $fields[2]["PARAMS"]["APPROVED"] || ($mid = \CForumMessage::update($this->message["ID"], $fields[2]["PARAMS"])) > 0)
483 {
484 $this->setComment($this->message["ID"]);
485 /***************** Event onMessageModerate ***********************/
486 $event = new Event("forum", "onMessageModerate", array($this->message["ID"], ($show ? "SHOW" : "HIDE"), $this->message, $this->topic));
487 $event->send();
488 /***************** Events OnCommentModerate ************************/
489 $event = new Event("forum", "OnCommentModerate", $fields);
490 $event->send();
491 /***************** Events OnAfterCommentUpdate *********************/
492 $event = new Event("forum", "OnAfterCommentUpdate", $fields); // It is not a mistake
493 $event->send();
494 /***************** /Events *****************************************/
495 $res = serialize(array(
496 "ID" => $this->message["ID"],
497 "AUTHOR_NAME" => $this->message["AUTHOR_NAME"],
498 "POST_MESSAGE" => $this->message["POST_MESSAGE"],
499 "TITLE" => $this->topic["TITLE"],
500 "TOPIC_ID" => $this->topic["ID"],
501 "FORUM_ID" => $this->topic["FORUM_ID"]));
502 \CForumMessage::sendMailMessage($this->message["ID"], array(), false, ($show ? "NEW_FORUM_MESSAGE" : "EDIT_FORUM_MESSAGE"));
503 \CForumEventLog::log("message", ($show ? "approve" : "unapprove"), $this->message["ID"], $res);
504 return $this->getComment();
505 }
506 else
507 {
508 $text = Loc::getMessage("FORUM_CM_ERR_MODERATE");
509 if (($ex = $this->getApplication()->getException()) && $ex)
510 $text = $ex->getString();
511 $this->errorCollection->addOne(new Error($text, self::ERROR_PARAMS_MESSAGE));
512 }
513 }
514 return false;
515 }
516
517 public function canEdit()
518 {
519 $result = false;
520 if ($this->message === null)
521 {
522 $this->errorCollection->addOne(new Error(Loc::getMessage("FORUM_CM_ERR_COMMENT_IS_LOST4"), self::ERROR_MESSAGE_IS_NULL));
523 }
524 else
525 {
526 $result = ($this->getEntity()->canEdit($this->getUser()->getId()) || (
527 ((int) $this->message["AUTHOR_ID"] > 0) &&
528 ((int) $this->message["AUTHOR_ID"] == (int) $this->getUser()->getId()) &&
529 $this->getEntity()->canEditOwn($this->getUser()->getId())
530 ));
531 }
532 return $result;
533 }
534
538 public function canEditOwn()
539 {
540 return $this->getEntity()->canEditOwn($this->getUser()->getId());
541 }
542
546 public function canDelete()
547 {
548 return $this->canEdit();
549 }
550
551 public function setComment($id)
552 {
553 $id = intval($id);
554 $message = ($id > 0 ? \CForumMessage::getById($id) : null);
555 if (!empty($message))
556 {
557 if ($message["TOPIC_ID"] != $this->topic["ID"])
558 {
559 throw new ArgumentException(Loc::getMessage("ACCESS_DENIED"), self::ERROR_PERMISSION);
560 }
561 $this->id = $id;
562 $this->message = $message;
563 }
564 }
565
566 public function getComment()
567 {
568 return $this->message;
569 }
570
577 public static function createFromId(Feed $feed, $id)
578 {
579 $forum = $feed->getForum();
580 $comment = new Comment($forum["ID"], $feed->getEntity()->getFullId(), $feed->getUser()->getId());
581 $comment->getEntity()->setPermission($feed->getUser()->getId(), $feed->getEntity()->getPermission($feed->getUser()->getId()));
582 $comment->setComment($id);
583 return $comment;
584 }
590 public static function create(Feed $feed)
591 {
592 $forum = $feed->getForum();
593 $comment = new Comment($forum["ID"], $feed->getEntity()->getFullId(), $feed->getUser()->getId());
594 $comment->getEntity()->setPermission($feed->getUser()->getId(), $feed->getEntity()->getPermission($feed->getUser()->getId()));
595 return $comment;
596 }
597}
static createFromId(Feed $feed, $id)
Definition comment.php:577
static create(Feed $feed)
Definition comment.php:590
send($sender=null)
Definition event.php:139
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
$GLOBALS['____1444769544']
Definition license.php:1