BlogController.php 4.61 KB
<?php
    namespace frontend\controllers;

    use yii\data\ActiveDataProvider;
    use yii\helpers\Html;
    use yii\helpers\Url;
    use yii\web\Controller;
    use common\models\Article;
    use yii\web\NotFoundHttpException;
    use yii\db\ActiveQuery;

    /**
     * User: timur
     * Date: 26.01.18
     * Time: 8:46
     */
    
    class BlogController extends Controller
    {
    
        protected function prepareProviderAndRender(ActiveQuery $query): string {
            $dataProvider = new ActiveDataProvider(
                [
                    'query' => $query,
                    'pagination' => [
                        'pageSize' => 5,
                    ],
                ]
            );
        
            return $this->render(
                'index',
                [
                    'dataProvider' => $dataProvider,
                ]
            );
        }
        
        public function actionIndex()
        {
    
            $query = Article::find()
                            ->with('lang.image')
                            ->where(
                                [
                                    'status' => true,
                                ]
                            )
                            ->orderBy("sort");
    
            return $this->prepareProviderAndRender($query);
        }
        
        public function actionArticle($id)
        {
            
            $model = $this->findModel($id);
            
            return $this->render(
                'view',
                [
                    'article' => $model,
                ]
            );
            
        }
    
        protected function findModel($id)
        {
            $model = Article::find()
                ->with('lang.image')

                ->where(
                                [
                                    'id' => $id
                                ]
                            )
                            ->with("lang.image")
                            ->one();
            
            if ( $model !== NULL) {
                return $model;
            }
            else {
                throw new NotFoundHttpException('The requested page does not exist.');
            }
        }
        
        public function actionSearch()
        {
            
            if( \Yii::$app->request->isPost ){

                $req = \Yii::$app->request;
                if (!empty($req->post("title"))){
                    $title = Html::encode($req->post("title"));
                    $query = Article::find()
                                    ->joinWith("lang.image")
                                    ->where(
                                        [
                                            'status' => true,
                                        ]
                                    )
                                    ->andWhere(
                                        [
                                            "like", "lower(title)", $title
                                        ]
                                    )
                                    ->orderBy("sort");
    
                    return $this->prepareProviderAndRender($query);
                }
            }
            
            return $this->redirect(Url::toRoute(['blog/index']));
            
        }
        
        public function actionCategory($id) {
            
            $query = Article::find()
                            ->joinWith(["categories.lang", 'lang.image'])
                            ->where(
                                [
                                    'blog_article.status' => true,
                                ]
                            )
                            ->andWhere(
                                [
                                    "blog_category.id" => $id,
                                    "blog_category.status" => true,
                                ]
                            )
                            ->orderBy("sort");
    
            return $this->prepareProviderAndRender($query);
        }
    
        public function actionTag($id){
            $query = Article::find()
                            ->joinWith(["tags.lang", 'lang.image'])
                            ->where(
                                [
                                    'blog_article.status' => true,
                                    'blog_tag.id' => $id,
                                ]
                            )
                            ->orderBy('sort');
            
            return $this->prepareProviderAndRender($query);
        }
        
    }