SlideController.php 5.28 KB
<?php
    namespace backend\controllers;
    
    use artbox\core\controllers\SlideController as ArtboxSlideController;
    use common\models\Slider;
    use common\models\SlideSearch;
    use common\models\Slide;
    use yii\web\NotFoundHttpException;
    use Yii;
    /**
     * Created by PhpStorm.
     * User: timur
     * Date: 24.01.18
     * Time: 15:09
     */
    
    class SlideController extends ArtboxSlideController
    {
        /**
         * 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.');
            }
        }
    }