Пример #1
0
 /**
  * Render home page of the site.
  *
  * @throws \yii\web\NotFoundHttpException
  * @return string
  */
 public function actionIndex()
 {
     /* @var $post Post */
     $query = Post::find()->from(['t' => Post::tableName()])->andWhere(['post_status' => 'publish'])->orderBy(['t.id' => SORT_DESC]);
     if (Option::get('show_on_front') == 'page' && ($front_page = Option::get('front_page'))) {
         $render = '/post/view';
         $comment = new PostComment();
         $query = $query->andWhere(['id' => $front_page]);
         $post = $query->one();
         if (is_file($this->view->theme->basePath . '/site/' . $post->post_template . '.php')) {
             $render = '/site/' . $post->post_template;
             $this->layout = 'blank';
         } elseif (is_file($this->view->theme->basePath . '/post/view-' . $post->postType->post_type_slug . '.php')) {
             $render = '/post/view-' . $post->postType->post_type_slug;
         }
         if ($post) {
             return $this->render($render, ['post' => $post, 'comment' => $comment]);
         } else {
             return new NotFoundHttpException();
         }
     } else {
         if (Option::get('front_post_type') !== 'all') {
             $query->innerJoinWith(['postType'])->andWhere(['post_type_name' => Option::get('front_post_type')]);
         }
         $countQuery = clone $query;
         $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => Option::get('posts_per_page')]);
         $query->offset($pages->offset)->limit($pages->limit);
         $posts = $query->all();
         if ($posts) {
             return $this->render('index', ['posts' => $posts, 'pages' => isset($pages) ? $pages : null]);
         } else {
             throw new NotFoundHttpException(Yii::t('content', 'Page not found.'));
         }
     }
 }