SlideController.php 6.23 KB
<?php
    
    namespace backend\controllers;
    
    use artbox\core\models\Slide;
    use artbox\core\models\Slider;
    use artbox\core\models\SlideSearch;
    use Yii;
    use yii\filters\AccessControl;
    use yii\web\Controller;
    use yii\web\NotFoundHttpException;
    use yii\filters\VerbFilter;
    
    /**
     * SlideController implements the CRUD actions for Slide model.
     */
    class SlideController 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 Slide models.
         *
         * @param int $slider_id
         *
         * @return mixed
         */
        public function actionIndex($slider_id)
        {
            $slider = $this->findSlider($slider_id);
            $searchModel = new SlideSearch();
            $dataProvider = $searchModel->search(Yii::$app->request->queryParams, $slider);
            
            return $this->render(
                'index',
                [
                    'searchModel'  => $searchModel,
                    'dataProvider' => $dataProvider,
                    'slider'       => $slider,
                ]
            );
        }
        
        /**
         * Displays a single Slide model.
         *
         * @param integer $id
         *
         * @return mixed
         */
        public function actionView($id)
        {
            return $this->render(
                'view',
                [
                    'model' => $this->findModel($id),
                ]
            );
        }
        
        /**
         * Creates a new Slide model.
         * If creation is successful, the browser will be redirected to the 'view' page.
         *
         * @param int $slider_id
         *
         * @return mixed
         */
        public function actionCreate(int $slider_id)
        {
            $slider = $this->findSlider($slider_id);
            
            $model = new Slide();
            $model->generateLangs();
            
            if ($model->loadWithLangs(\Yii::$app->request)) {
                if ($model->saveWithLangs()) {
                    return $this->redirect(
                        [
                            'view',
                            'id' => $model->id,
                        ]
                    );
                }
            }
            return $this->render(
                'create',
                [
                    'model'      => $model,
                    'modelLangs' => $model->modelLangs,
                    'slider'     => $slider,
                ]
            );
        }
        
        /**
         * Updates an existing Slide 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();
            
            $slider = $this->findSlider($model->slider_id);
            
            if ($model->loadWithLangs(\Yii::$app->request)) {
                if ($model->saveWithLangs()) {
                    return $this->redirect(
                        [
                            'view',
                            'id' => $model->id,
                        ]
                    );
                }
            }
            return $this->render(
                'update',
                [
                    'model'      => $model,
                    'modelLangs' => $model->modelLangs,
                    'slider'     => $slider,
                ]
            );
        }
        
        /**
         * Deletes an existing Slide 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);
    
            $slider_id = $model->slider_id;
            $model->delete();
            return $this->redirect(
                [
                    'index',
                    'slider_id' => $slider_id,
                ]
            );
        }
        
        /**
         * Finds the Slide model based on its primary key value.
         * If the model is not found, a 404 HTTP exception will be thrown.
         *
         * @param integer $id
         *
         * @return Slide the loaded model
         * @throws NotFoundHttpException if the model cannot be found
         */
        protected function findModel($id)
        {
            if (( $model = Slide::findOne($id) ) !== null) {
                return $model;
            } else {
                throw new NotFoundHttpException('The requested page does not exist.');
            }
        }
        
        /**
         * Finds the Slider model based on its primary key value.
         * If the model is not found, a 404 HTTP exception will be thrown.
         *
         * @param integer $id
         *
         * @return Slider the loaded model
         * @throws NotFoundHttpException if the model cannot be found
         */
        protected function findSlider($id)
        {
            if (( $model = Slider::findOne($id) ) !== null) {
                return $model;
            } else {
                throw new NotFoundHttpException('The requested page does not exist.');
            }
        }
    }