Реализация компонента для отображения и добавления комментариев через API Битрикс D7.
Код |
---|
<?php
if (!defined('B_PROLOG_INCLUDED') || B_PROLOG_INCLUDED !== true) die();
use Bitrix\Main\Application;
use Bitrix\Main\Loader;
use Bitrix\Iblock\Elements\ElementCommentsTable;
class CustomCommentsComponent extends CBitrixComponent
{
public function executeComponent()
{
try {
Loader::includeModule('iblock');
$this->processForm();
$this->getComments();
$this->includeComponentTemplate();
} catch (Exception $e) {
ShowError($e->getMessage());
}
}
private function processForm()
{
$request = Application::getInstance()->getContext()->getRequest();
if ($request->isPost() && $request->getPost('submit_comment')) {
$this->addComment($request->getPost('comment_text'));
}
}
private function addComment($text)
{
$result = ElementCommentsTable::createObject();
$result->setName('Комментарий ' . time());
$result->setPreviewText($text);
$result->setIblockId($this->arParams['IBLOCK_ID']);
$result->save();
}
private function getComments()
{
$comments = ElementCommentsTable::getList([
'filter' => [
'IBLOCK_ID' => $this->arParams['IBLOCK_ID'],
'ACTIVE' => 'Y'
],
'order' => ['ID' => 'DESC']
])->fetchAll();
$this->arResult['COMMENTS'] = $comments;
}
} |