OptionController.php 6.8 KB
<?php
    
    namespace artbox\catalog\controllers;
    
    use artbox\catalog\models\Option;
    use artbox\catalog\models\OptionGroup;
    use artbox\catalog\models\OptionSearch;
    use Yii;
    use yii\filters\AccessControl;
    use yii\web\Controller;
    use yii\web\NotFoundHttpException;
    use yii\filters\VerbFilter;
    
    /**
     * Abstract OptionController implements the CRUD actions Option models.
     */
    abstract class OptionController extends Controller
    {
        /**
         * @inheritdoc
         */
        public function behaviors()
        {
            return [
                'access' => [
                    'class' => AccessControl::className(),
                    'rules' => [
                        [
                            'actions' => [
                                'login',
                                'error',
                            ],
                            'allow'   => true,
                        ],
                        [
                            'allow' => true,
                            'roles' => [ '@' ],
                        ],
                    ],
                ],
                'verbs'  => [
                    'class'   => VerbFilter::className(),
                    'actions' => [
                        'delete' => [ 'POST' ],
                    ],
                ],
            ];
        }
        
        /**
         * Lists all Option models.
         *
         * @param int $group_id
         *
         * @return mixed
         */
        public function actionIndex($group_id)
        {
            $group = $this->findGroup($group_id);
            $searchModel = $this->createSearchModel();
            $dataProvider = $searchModel->search(Yii::$app->request->queryParams, $group);
            
            return $this->render(
                'index',
                [
                    'searchModel'  => $searchModel,
                    'dataProvider' => $dataProvider,
                    'group'        => $group,
                ]
            );
        }
        
        /**
         * Displays a single Option model.
         *
         * @param integer $id
         *
         * @return mixed
         */
        public function actionView($id)
        {
            return $this->render(
                'view',
                [
                    'model' => $this->findModel($id),
                ]
            );
        }
        
        /**
         * Creates a new Option model.
         * If creation is successful, the browser will be redirected to the 'view' page.
         *
         * @param int $group_id
         *
         * @return mixed
         */
        public function actionCreate($group_id)
        {
            $group = $this->findGroup($group_id);
            $model = $this->createModel();
            $model->setGroupId($group_id);
            $model->generateLangs();
            if ($model->loadWithLangs(\Yii::$app->request) && $model->saveWithLangs()) {
                return $this->redirect(
                    [
                        'view',
                        'id' => $model->id,
                    ]
                );
            }
            return $this->render(
                'create',
                [
                    'model'      => $model,
                    'modelLangs' => $model->modelLangs,
                    'group'      => $group,
                ]
            );
        }
        
        /**
         * Updates an existing Option model.
         * If update is successful, the browser will be redirected to the 'view' page.
         *
         * @param integer $id
         *
         * @return mixed
         */
        public function actionUpdate($id)
        {
            $model = $this->findModel($id);
            $model->generateLangs();
            
            if ($model->loadWithLangs(\Yii::$app->request) && $model->saveWithLangs()) {
                return $this->redirect(
                    [
                        'view',
                        'id' => $model->id,
                    ]
                );
            }
            return $this->render(
                'update',
                [
                    'model'      => $model,
                    'modelLangs' => $model->modelLangs,
                ]
            );
        }
        
        /**
         * Deletes an existing Option model.
         * If deletion is successful, the browser will be redirected to the 'index' page.
         *
         * @param integer $id
         *
         * @return mixed
         */
        public function actionDelete($id)
        {
            $model = $this->findModel($id);
            $groupId = $model->groupId;
            $model->delete();
            
            return $this->redirect(
                [
                    'index',
                    'group_id' => $groupId,
                ]
            );
        }
        
        /**
         * Finds the Option model based on its primary key value.
         * If the model is not found, a 404 HTTP exception will be thrown.
         *
         * @param integer $id
         *
         * @return Option the loaded model
         * @throws NotFoundHttpException if the model cannot be found
         */
        protected function findModel($id): Option
        {
            if (( $model = $this->findOne($id) ) !== null) {
                return $model;
            } else {
                throw new NotFoundHttpException('The requested page does not exist.');
            }
        }
        
        /**
         * Finds the OptionGroup model based on its primary key value.
         * If the model is not found, a 404 HTTP exception will be thrown.
         *
         * @param integer $id
         *
         * @return OptionGroup the loaded model
         * @throws NotFoundHttpException if the model cannot be found
         */
        protected function findGroup($id): OptionGroup
        {
            if (( $model = $this->findOneGroup($id) ) !== null) {
                return $model;
            } else {
                throw new NotFoundHttpException('The requested page does not exist.');
            }
        }
        
        /**
         * Create exact model
         *
         * @return Option
         */
        protected abstract function createModel(): Option;
        
        /**
         * Create exact search model
         *
         * @return OptionSearch
         */
        protected abstract function createSearchModel(): OptionSearch;
        
        /**
         * Find exact model
         *
         * @param $id
         *
         * @return Option|null
         */
        protected abstract function findOne($id);
        
        /**
         * Find exact group
         *
         * @param $id
         *
         * @return OptionGroup|null
         */
        protected abstract function findOneGroup($id);
    }