<?php
    
    namespace frontend\controllers;
    
    use common\models\blog\Article;
    use common\models\blog\Category;
    use common\models\blog\Tag;
    use yii\data\ActiveDataProvider;
    use yii\db\ActiveQuery;
    use yii\web\Controller;
    use yii\web\NotFoundHttpException;
    
    /**
     * Class BlogController
     *
     * @package frontend\controllers
     */
    class BlogController extends Controller
    {
        public function actionIndex($q = '')
        {
            $tags = Tag::find()
                       ->with(
                           [
                               'language',
                           ]
                       )
                       ->orderBy([ 'sort' => SORT_ASC ])
                       ->all();

            $categories = Category::find()
                                  ->with(
                    [
                        'language',
                    ]
                )
                                  ->where(['status' => true])
                                  ->orderBy([ 'sort' => SORT_ASC ])
                                  ->all();

            $dataProvider = new ActiveDataProvider(
                [
                    'query'      => Article::find()
                                         ->orderBy(
                                               [
                                                   'created_at' => SORT_DESC,
                                               ]
                                           )
                                         ->with(
                                               [
                                                   'categories' => function (ActiveQuery $query){
                                                        $query->with('language')->where(['status' => true]);
                                                   },
                                               ]

                                           )
                        ->joinWith('language')
                        ->where([ 'blog_article.status' => true ])
                        ->andFilterWhere(
                                               [
                                                   'ilike',
                                                   'blog_article_lang.title',
                                                   $q,
                                               ]
                        )
                        ->distinct(),
                    'pagination' => [
                        'pageSize' => 3,
                    ],
                ]
            );
        
            return $this->render(
                'index',
                [
                    'tags'         => $tags,
                    'categories' => $categories,
                    'dataProvider' => $dataProvider,
                ]
            );
        }
    
        public function actionView($id)
        {
            $model = $this->findModel($id);
    
            $tags = Tag::find()
                       ->with([ 'language' ])
                       ->orderBy([ 'sort' => SORT_ASC ])
                       ->all();
    
            return $this->render(
                'view',
                [
                    'tags'  => $tags,
                    'model' => $model,
                ]
            );
        }
    
        public function actionCategory($id)
        {
            $tags = Tag::find()
                       ->with(
                           [
                               'language',
                           ]
                       )
                       ->orderBy([ 'sort' => SORT_ASC ])
                       ->all();
    
            /**
             * @var Category $model
             */
            $model = Category::find()
                             ->where(
                                 [
                                     'id' => $id,
                                 ]
                             )
                             ->with(
                                 [
                                     'articles',
                                 ]
                             )
                             ->where(['status' => true])
                             ->orderBy([ 'sort' => SORT_ASC ])
                             ->one();
            if (empty($model)){
                throw new NotFoundHttpException();
            }
            $dataProvider = new ActiveDataProvider(
                [
                    'query'      => $model->getArticles()
                                          ->with(
                                              [
                                                  'language',
                                                  'categories.language',
                                              ]
                                          )
                        ->where(['blog_article.status' => true])
                                          ->orderBy(
                                              [
                                                  'created_at' => SORT_DESC,
                                              ]
                                          ),
                    'pagination' => [
                        'pageSize' => 3,
                    ],
                ]
            );
    
            return $this->render(
                'category',
                [
                    'tags'         => $tags,
                    'dataProvider' => $dataProvider,
                    'model'        => $model,
                ]
            );
        }
    
        public function actionTag($id)
        {
            $tags = Tag::find()
                       ->with(
                           [
                               'language',
                           ]
                       )
                       ->orderBy([ 'sort' => SORT_ASC ])
                       ->all();
    
            /**
             * @var Category $model
             */
            $model = Tag::find()
                        ->where(
                            [
                                'id' => $id,
                            ]
                        )
                        ->with(
                            [
                                'articles',
                            ]
                        )
                        ->one();
    
            $dataProvider = new ActiveDataProvider(
                [
                    'query'      => $model->getArticles()
                                          ->with(
                                              [
                                                  'language',
                                              ]
                                          )->with(
                            [
                                'categories' => function (ActiveQuery $query){
                                    $query->with('language')->where(['status' => true]);
                                },
                            ]
    
                        )
                        ->where(['blog_article.status' => true])
                                          ->orderBy(
                                              [
                                                  'created_at' => SORT_DESC,
                                              ]
                                          ),
                    'pagination' => [
                        'pageSize' => 3,
                    ],
                ]
            );
    
            return $this->render(
                'tag',
                [
                    'tags'         => $tags,
                    'dataProvider' => $dataProvider,
                    'model'        => $model,
                ]
            );
        }
    
        /**
         * @param $id
         *
         * @return Article
         * @throws \yii\web\NotFoundHttpException
         */
        protected function findModel($id)
        {
            /**
             * @var Article | null $model
             */
            $model = Article::find()
                          ->where([ 'id' => $id ])
                          ->with(
                                [
                                    'language',
                                    'tags.language',
                                ]
                            )->with(
                    [
                        'categories' => function (ActiveQuery $query){
                            $query->with('language')->where(['status' => true]);
                        },
                    ]
    
                )
                          ->andWhere([ 'status' => true ])
                          ->one();
    
            if (empty($model)) {
                throw new NotFoundHttpException(\Yii::t('app', 'Article not found'));
            } else {
                return $model;
            }
        }
    }