BlogController.php 5 KB
<?php
    
    namespace frontend\controllers;
    
    use artbox\weblog\models\Article;
    use artbox\weblog\models\Category;
    use artbox\weblog\models\Tag;
    use yii\data\ActiveDataProvider;
    use yii\web\Controller;
    use yii\web\NotFoundHttpException;
    
    /**
     * Class BlogController
     *
     * @package frontend\controllers
     */
    class BlogController extends Controller
    {
        public function actionIndex()
        {
            $query = Article::find()
                            ->with('lang.alias')
                            ->with('image')
                            ->with('tags.lang.alias')
                            ->with('categories.lang.alias')
                            ->where([ 'status' => true ]);
            
            $dataProvider = new ActiveDataProvider(
                [
                    'query'      => $query,
                    'pagination' => [
                        'pageSize' => 3,
                    ],
                ]
            );
            
            $this->view->params[ 'breadcrumbs' ][] = \Yii::t('app', 'Блог');
            
            return $this->render(
                'index',
                [
                    'dataProvider' => $dataProvider,
                ]
            );
        }
        
        public function actionCategory(int $id)
        {
            /**
             * @var Category $category
             */
            $query = Article::find()
                            ->with('lang.alias')
                            ->with('image')
                            ->with('tags.lang.alias')
                            ->innerJoinWith('category')
                            ->with('categories.lang.alias')
                            ->where([ 'blog_category_id' => $id ]);
            
            $category = Category::find()
                                ->where([ 'id' => $id ])
                                ->with('lang')
                                ->one();
            
            if (empty($category)) {
                throw new NotFoundHttpException();
            } else {
                $this->view->params[ 'breadcrumbs' ][] = [
                    'url'   => [ 'blog/index' ],
                    'label' => \Yii::t('app', 'Блог'),
                ];
                
                $this->view->params[ 'breadcrumbs' ][] = $category->lang->title;
                
                $this->view->params[ 'category' ] = $category->id;
            }
            
            $dataProvider = new ActiveDataProvider(
                [
                    'query'      => $query,
                    'pagination' => [
                        'pageSize' => 3,
                    ],
                ]
            );
            
            return $this->render(
                'index',
                [
                    'dataProvider' => $dataProvider,
                ]
            );
        }
        
        public function actionTag(int $id)
        {
            /**
             * @var Tag $tag
             */
            $query = Article::find()
                            ->with('lang.alias')
                            ->with('image')
                            ->with('categories.lang.alias')
                            ->innerJoinWith('tags.lang.alias')
                            ->where([ 'blog_tag_id' => $id ])
                            ->where([ 'blog_article.status' => true ]);
            
            $tag = Tag::find()
                      ->with('lang')
                      ->one();
            
            if (empty($tag)) {
                throw new NotFoundHttpException();
            } else {
                $this->view->params[ 'breadcrumbs' ][] = [
                    'url'   => [ 'blog/index' ],
                    'label' => \Yii::t('app', 'Блог'),
                ];
                
                $this->view->params[ 'breadcrumbs' ][] = $tag->lang->label;
                
                $this->view->params[ 'tag' ] = $tag->id;
            }
            
            $dataProvider = new ActiveDataProvider(
                [
                    'query'      => $query,
                    'pagination' => [
                        'pageSize' => 3,
                    ],
                ]
            );
            
            return $this->render(
                'index',
                [
                    'dataProvider' => $dataProvider,
                ]
            );
        }
        
        public function actionArticle(int $id)
        {
            $article = Article::find()
                              ->with('lang.alias')
                              ->with('category.lang.alias')
                              ->where([ 'id' => $id ])
                              ->one();
            
            if (empty($article)) {
                throw new NotFoundHttpException();
            }
            
            return $this->render(
                'article',
                [
                    'model' => $article,
                ]
            );
        }
    }