Exemplo n.º 1
0
 public function actionRemove($id)
 {
     $userId = Yii::$app->user->identity->username;
     $sil = Comment::findOne($id);
     $sil->delete();
     return $this->redirect(['index']);
 }
 /**
  * If validation is ok, gets POST data and updates data in DB
  * @param $id
  * @return bool
  */
 public function updateComment($id)
 {
     if ($this->validate()) {
         $comment = Comment::findOne($id);
         $comment->comment_writer = $this->comment_writer;
         $comment->comment_w_email = $this->comment_w_email;
         $comment->comment_w_phone = $this->comment_w_phone;
         $comment->comment_w_gender = $this->comment_w_gender;
         $comment->comment_country_id = $this->country;
         $comment->comment_subject = $this->comment_subject;
         $comment->comment_message = $this->comment_message;
         $comment->save();
         return true;
     }
     return false;
 }
Exemplo n.º 3
0
 /**
  * Finds the Comment model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Comment the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Comment::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Exemplo n.º 4
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]) : []]);
 }