public function actionSingle()
 {
     $model = new CommentForm();
     $blog = Blog::getInstance();
     $uri = \Yii::$app->request->get('uri');
     $article = Articles::findOne(['id' => \Yii::$app->request->get('id')]);
     if (!$article) {
         \Yii::$app->session->setFlash('articleNotFound', 'warning');
         // Look at UrlManager configurations
         // Real address is['/blog/category?uri=' . $uri]
         return $this->redirect(['/category/' . $uri], 302);
     }
     $articleCatUri = $article->category->uri;
     $articleId = $article->id;
     if ($articleCatUri != $uri) {
         // Look at UrlManager configurations
         // Real address is ['/blog/single?uri=' . $articleCatUri . '&id=' . $articleId]
         return $this->redirect(['/category/' . $articleCatUri . '/' . $articleId], 302);
     }
     if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
         $model->article_id = $articleId;
         $answer = $model->addComment() ? ['name' => 'commentSuccessSending', 'type' => 'success'] : ['name' => 'commentWarningSending', 'type' => 'error'];
         \Yii::$app->session->setFlash($answer['name'], $answer['type']);
         return $this->refresh();
     }
     $data = $blog->getComments($articleId);
     return $this->render('single', ['article' => $article, 'model' => $model, 'data' => $data]);
 }
 public function actionAdd()
 {
     $model = new Comment();
     $commentForm = new CommentForm(Url::to(['comment/add', 'id' => Yii::$app->request->get('id')]));
     $model->post_id = Yii::$app->request->get('id');
     if (Yii::$app->user instanceof User) {
         $model->author_id = Yii::$app->user->getIdentity()->getId();
     }
     if ($commentForm->save($model, Yii::$app->request->post('CommentForm'))) {
         return $this->redirect(['post/view', 'id' => Yii::$app->request->get('id')]);
     } else {
         return $this->render('create', ['model' => $commentForm]);
     }
 }
Exemple #3
0
 public function actionNews($id, $ignoreLink = false)
 {
     $news = News::find()->andWhere(['id' => filter_var($id, FILTER_SANITIZE_NUMBER_INT)])->with('author')->one();
     if (!$news) {
         throw new NotFoundHttpException();
     }
     if ($news->fullLink != \Yii::$app->request->url && !$ignoreLink) {
         return $this->redirect($news->fullLink, 301);
     }
     if (\Yii::$app->request->isAjax) {
         \Yii::$app->response->format = 'json';
         switch (\Yii::$app->request->post('action')) {
             case 'voteComment':
                 $commentID = \Yii::$app->request->post('commentID');
                 $comment = Comment::findOne($commentID);
                 if (!$comment) {
                     throw new NotFoundHttpException("Комментарий с идентификатором {$commentID} не найден!");
                 }
                 $isGood = \Yii::$app->request->post('isGood') == 'true';
                 if (!empty($comment->myVote) && $comment->myVote->operation == ($isGood ? CommentVote::OPERATION_GOOD : CommentVote::OPERATION_BAD)) {
                     return ['result' => $comment->rating];
                 }
                 $comment->vote($isGood);
                 $comment = Comment::findOne($commentID);
                 return ['result' => $comment->rating];
                 break;
         }
     }
     if ($date = \Yii::$app->request->headers->get('If-Modified-Since', 0)) {
         if ($date = strtotime($date) && ($date >= $news->updated || $date >= $news->publishDate)) {
             header('HTTP/1.1 304 Not Modified');
             die;
         }
     }
     \Yii::$app->response->headers->add('Last-Modified', gmdate('D, d M Y H:i:s', $news->updated ? $news->updated : $news->publishDate) . ' GMT');
     if (!preg_match('/(googlebot|google.com\\/bot|yandex(\\w+|)bot|yandex\\.com\\/bots)/im', \Yii::$app->request->userAgent)) {
         (new Query())->createCommand(\Yii::$app->db)->setSql("UPDATE `news` SET `hits` = `hits` + 1 WHERE `id` = '{$news->id}'")->execute();
         NewsViews::addView($news->id);
     }
     $commentForm = new CommentForm();
     if (\Yii::$app->request->post('CommentForm')) {
         $commentForm->load(\Yii::$app->request->post());
         $commentForm->news = $news;
         if ($commentForm->save()) {
             \Yii::$app->session->setFlash('saved', 'Комментарий успешно добавлен!' . ($news->moderatedComments == 1 ? ' Он будет опубликован после проверки модератором.' : ''));
             $commentForm = new CommentForm();
         }
     }
     if (!empty($news->category)) {
         if (!empty($news->category->parentCategory)) {
             $this->getView()->params['breadcrumbs'][] = ['label' => $news->category->parentCategory->title, 'url' => yii\helpers\Url::toRoute(['/' . $news->category->parentCategory->fullLink], true)];
         }
         $this->getView()->params['breadcrumbs'][] = ['label' => $news->category->title, 'url' => yii\helpers\Url::toRoute(['/' . $news->category->fullLink], true)];
     }
     $today = strtotime(date('Y-m-d'));
     $interestNews = [];
     if ($news->category->showPopular) {
         $interestNews = NewsViews::find()->select('newsID')->where(['date' => $today])->andWhere(['not in', 'newsID', $news->id])->orderBy('views DESC')->limit(5)->asArray()->all();
         if (empty($interestNews)) {
             $interestNews = NewsViews::find()->select('newsID')->where(['date' => $today - 86400])->andWhere(['not in', 'newsID', $news->id])->orderBy('views DESC')->limit(5)->asArray()->all();
         }
         $interestNews = News::find()->andWhere(['in', 'id', ArrayHelper::getColumn($interestNews, 'newsID')])->andWhere(['not in', 'categoryID', '49'])->with('category')->limit(3)->all();
     }
     return $this->render('article', ['article' => $news, 'interest' => $interestNews, 'commentForm' => $commentForm, 'categoryNews' => $news->category->showSimilar ? $news->category->getPossibleNews(3, [$news->id]) : []]);
 }
 /**
  * /snippets/language-slug/snippet-slug/comment
  *
  * @return string
  */
 public function actionComment()
 {
     if (!$this->parseQueryParams()) {
         return $this->goHome();
     }
     $model = new CommentForm();
     if ($this->request->method == 'POST') {
         // populate model attributes with user inputs
         $model->load($this->request->post());
         if ($model->validate() && $model->save($this->member, $this->snippet)) {
             $this->notifySuccess('Your comment has been added.');
         } else {
             $this->notifyError('Could not add your comment.');
         }
     }
     return $this->redirect('/snippets/' . $this->language->slug . '/' . $this->snippet->slug);
 }