/**
  * Displays homepage.
  *
  * @return mixed
  */
 public function actionIndex($slug = 'index')
 {
     // display home page
     if (empty($slug) || $slug == 'index') {
         $query = Post::find()->where(['status' => Post::STATUS_PUBLISHED]);
         $countQuery = clone $query;
         $pagination = new Pagination(['totalCount' => $countQuery->count(), 'defaultPageSize' => 5]);
         $posts = $query->offset($pagination->offset)->limit($pagination->limit)->all();
         return $this->render('index', ['posts' => $posts, 'pagination' => $pagination]);
     }
     //try to display action from controller
     try {
         return $this->runAction($slug);
     } catch (\yii\base\InvalidRouteException $ex) {
     }
     //try to display static page from datebase
     $page = Page::getDb()->cache(function ($db) use($slug) {
         return Page::findOne(['slug' => $slug, 'status' => Page::STATUS_PUBLISHED]);
     }, 3600);
     if ($page) {
         $pageAction = new PageAction($slug, $this, ['slug' => $slug, 'page' => $page]);
         return $pageAction->run();
     }
     //try to display post from datebase
     $post = Post::getDb()->cache(function ($db) use($slug) {
         return Post::findOne(['slug' => $slug, 'status' => Post::STATUS_PUBLISHED]);
     }, 3600);
     if ($post) {
         $postAction = new PostAction($slug, $this, ['slug' => $slug, 'post' => $post]);
         return $postAction->run();
     }
     //if nothing suitable was found then throw 404 error
     throw new \yii\web\NotFoundHttpException('Page not found.');
 }
Beispiel #2
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Post::find()->joinWith('translations');
     $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => Yii::$app->request->cookies->getValue('_grid_page_size', 20)], 'sort' => ['defaultOrder' => ['id' => SORT_DESC]]]);
     $this->load($params);
     if (!$this->validate()) {
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'created_by' => $this->created_by, 'updated_by' => $this->updated_by, 'status' => $this->status, 'comment_status' => $this->comment_status, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, 'revision' => $this->revision]);
     $query->andFilterWhere([$this->published_at_operand ? $this->published_at_operand : '=', 'published_at', $this->published_at ? strtotime($this->published_at) : null]);
     $query->andFilterWhere(['like', 'slug', $this->slug])->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'content', $this->content]);
     return $dataProvider;
 }
Beispiel #3
0
 public function run()
 {
     if (!$this->options) {
         $this->options = $this->getDefaultOptions();
     }
     if (User::hasPermission('viewPosts')) {
         $searchModel = new PostSearch();
         $formName = $searchModel->formName();
         $recentPosts = Post::find()->orderBy(['id' => SORT_DESC])->limit($this->recentLimit)->all();
         foreach ($this->options as &$option) {
             $count = Post::find()->filterWhere($option['filterWhere'])->count();
             $option['count'] = $count;
             $option['url'] = [$this->indexAction, $formName => $option['filterWhere']];
         }
         return $this->render('posts', ['height' => $this->height, 'width' => $this->width, 'position' => $this->position, 'posts' => $this->options, 'recentPosts' => $recentPosts]);
     }
 }
Beispiel #4
0
 /**
  * Displays homepage.
  *
  * @return mixed
  */
 public function actionIndex($slug = 'index')
 {
     if (empty($slug) || $slug == 'index') {
         throw new NotFoundHttpException('Page not found.');
     } else {
         $tag = Tag::find()->where(['slug' => $slug]);
         $tagCount = clone $tag;
         if (!$tagCount->count()) {
             throw new NotFoundHttpException('Page not found.');
         }
     }
     $query = Post::find()->joinWith('tags')->where(['status' => Post::STATUS_PUBLISHED, Tag::tableName() . '.slug' => $slug])->orderBy('published_at DESC');
     $countQuery = clone $query;
     $pagination = new Pagination(['totalCount' => $countQuery->count(), 'defaultPageSize' => Yii::$app->settings->get('reading.page_size', 10)]);
     $posts = $query->offset($pagination->offset)->limit($pagination->limit)->all();
     return $this->render('index', ['posts' => $posts, 'tag' => $tag->one(), 'pagination' => $pagination]);
 }