Example #1
0
 public function actionIndex()
 {
     $todayStart = strtotime(date('Y-m-d'));
     $newsViews = NewsViews::find()->select("SUM(`views`)")->where(['date' => $todayStart])->scalar();
     if (!$newsViews) {
         $newsViews = 0;
     }
     $todayViewedNews = NewsViews::find()->select('newsID')->where(['date' => $todayStart])->orderBy('views DESC')->asArray()->all();
     $popularFiveToday = $popularToday = $todayPopularNews = [];
     if ($todayViewedNews) {
         $popularFiveToday = ArrayHelper::getColumn(array_slice($todayViewedNews, 0, 5), 'newsID');
     }
     $sViews = NewsViews::find()->select(['title' => 'date', 'views' => 'COUNT(`views`)'])->groupBy('date')->limit(30)->asArray()->all();
     array_walk($sViews, function (&$item, $key) {
         $item['title'] = \Yii::$app->formatter->asDate($item['title']);
     });
     return $this->render('index', ['monthlyViews' => $sViews, 'todayViewedNewsCount' => count($todayViewedNews), 'lastNews' => new ActiveDataProvider(['query' => News::find(), 'pagination' => ['pageSize' => 5], 'sort' => ['defaultOrder' => ['publishDate' => SORT_DESC]]]), 'popularToday' => new ActiveDataProvider(['query' => News::find()->select(['news.*', 'todayViews' => 'newsViews.views'])->where(['in', 'id', $popularFiveToday])->with('todayViews')->joinWith('todayViews')->orderBy(['todayViews' => SORT_DESC]), 'pagination' => ['pageSize' => 5], 'sort' => false]), 'lastAuth' => new ActiveDataProvider(['query' => Authorization::find()->with('user'), 'pagination' => ['pageSize' => 5], 'sort' => ['defaultOrder' => ['date' => SORT_DESC]]]), 'newsCount' => News::find()->where("publishDate >= '{$todayStart}'")->count(), 'newsViews' => $newsViews, 'commentsCount' => Comment::find()->where("date >= '{$todayStart}'")->count()]);
 }
Example #2
0
 public function getTodayViews()
 {
     return $this->hasOne(NewsViews::className(), ['newsID' => 'id'])->where(['date' => strtotime(date('Y-m-d'))]);
 }
Example #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]) : []]);
 }