PageController.php 3.47 KB
<?php
    namespace frontend\controllers;
    
    use artbox\core\components\SeoComponent;
    use artbox\core\models\Page;
    use artbox\core\models\PageCategory;
    use yii\db\ActiveQuery;
    use yii\web\Controller;
    use yii\web\NotFoundHttpException;
    use Yii;
    
    /**
     * Class PageController
     *
     * @package frontend\controllers
     */
    class PageController extends Controller
    {
        public function actionView($id)
        {
            $model = $this->findModel($id);
            
            /**
             * @var SeoComponent $seo
             */
            $seo = Yii::$app->get('seo');
            $seo->setModel($model->lang);
            
            if (strpos($model->lang->body, '[[gallery]]')) {
                $splited = explode('[[gallery]]', $model->lang->body);
                $body = array_shift($splited);
                $lefts = implode('', $splited);
            } else {
                $body = $model->lang->body;
                $lefts = null;
            }
            
            $categories = PageCategory::find()
                                      ->with('lang')
                                      ->with(
                                          [
                                              'pages' => function (ActiveQuery $query) use ($id) {
                                                  $query->with('lang.alias');
                                                  $query->where(
                                                      [
                                                          'not',
                                                          [
                                                              'id' => $id,
                                                          ],
                                                      ]
                                                  );
                                              },
                                          ]
                                      )
                                      ->all();
            
            if ($model->id == 6) {
                return $this->render(
                    '@frontend/views/site/about',
                    [
                        'page_about' => $model,
                    ]
                );
            }
            
            return $this->render(
                'view',
                [
                    'model'      => $model,
                    'categories' => $categories,
                    'body'       => $body,
                    'lefts'      => $lefts,
                    'images'     => $model->getImages(),
                ]
            );
        }
        
        protected function findModel($id)
        {
            /**
             * @var Page         $model
             * @var SeoComponent $seo
             */
            $seo = \Yii::$app->get('seo');
            $model = Page::find()
                         ->where(
                             [
                                 'id' => $id,
                             ]
                         )
                         ->with('lang')
                         ->one();
            
            if (!empty($model)) {
                if ($model->lang->alias_id !== $seo->aliasId) {
                    throw new NotFoundHttpException('Wrong language');
                }
                return $model;
            } else {
                throw new NotFoundHttpException('Model not found');
            }
        }
    }