<?php
    namespace frontend\controllers;
    
    use artbox\core\components\SeoComponent;
    use artbox\core\models\Image;
    use common\models\page\Page;
    use yii\helpers\Json;
    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
             */
            $mages = null;
            $seo = Yii::$app->get('seo');
            $seo->setModel($model->language);
            $images = null;
            if (!empty($model->gallery)){
                $images = Image::find()->where(['id' => Json::decode($model->gallery)])->all();
            }
            
            return $this->render(
                'view',
                [
                    'model'  => $model,
                    'images' => $images
                ]
            );
        }
        
        protected function findModel($id)
        {
            /**
             * @var Page         $model
             * @var SeoComponent $seo
             */
            $seo = \Yii::$app->get('seo');
            $model = Page::find()
                         ->where(
                             [
                                 'id' => $id,
                             ]
                         )
                         ->with('language')
                         ->one();
            
            if (!empty($model)) {
                return $model;
            } else {
                throw new NotFoundHttpException('Model not found');
            }
        }
    }