/** * AJAX * Оставляет комментарий в статье * * @return string * { * text: str, * user: { * id: int, * name: str, * avatar: str, * } * } */ public function actionSend() { $type_id = self::getParam('type_id'); $row_id = self::getParam('row_id'); $content = self::getParam('Form')['content']; Model::insert(['type_id' => $type_id, 'row_id' => $row_id, 'content' => $content, 'user_id' => Yii::$app->user->getId()]); (new \app\modules\Comment\Cache(['typeId' => $type_id, 'rowId' => $row_id]))->clear(); return self::jsonSuccess(['text' => $content, 'user' => (new UsersInCache())->getUserData(Yii::$app->user->identity)]); }
<?php /** @var $this \yii\base\View */ /** @var $type_id */ /** @var $row_id */ use app\modules\Comment\Cache as CommentCache; use yii\helpers\Html; use yii\bootstrap\ActiveForm; use yii\captcha\Captcha; $cache = new CommentCache(['typeId' => $type_id, 'rowId' => $row_id, 'template' => '@app/modules/Comment/views/commentsCache.php']); $functionGet = function (CommentCache $class) { $query = \app\modules\Comment\Model::query(['type_id' => $class->typeId, 'row_id' => $class->rowId, 'parent_id' => null]); $count = $query->count(); $html = $this->renderFile($class->template, ['rows' => $query->orderBy(['date_insert' => SORT_ASC])->limit(10)->offset($count - 10)->all(), 'countPrevision' => $count - 10 > 0 ? $count - 10 : 0, 'type_id' => $class->typeId, 'row_id' => $class->rowId]); $data = ['html' => $html, 'ids' => \app\modules\Comment\Model::query(['type_id' => $class->typeId, 'row_id' => $class->rowId, 'parent_id' => null])->select('user_id')->groupBy('user_id')->column()]; return $data; }; echo $cache->getExtended($functionGet); ?> <?php \app\modules\Comment\Asset\Asset::register(Yii::$app->view); if (\Yii::$app->user->isGuest) { echo Html::button('Войти чтобы комментировать', ['class' => 'btn btn-default', 'name' => 'contact-button', 'style' => 'width: 100%;', 'id' => 'authCommentButton']); } else { $model = new \app\modules\Comment\Form(); $form = ActiveForm::begin(['id' => 'comment-form', 'enableClientScript' => false]); ?> <?php echo Html::hiddenInput('type_id', $type_id); ?>