|  |  |  | <?php
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace backend\controllers;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use core\entities\Slider;
 | 
					
						
							|  |  |  | use core\forms\SliderForm;
 | 
					
						
							|  |  |  | use core\services\SliderService;
 | 
					
						
							|  |  |  | use Yii;
 | 
					
						
							|  |  |  | use yii\data\ActiveDataProvider;
 | 
					
						
							|  |  |  | use yii\web\Controller;
 | 
					
						
							|  |  |  | use yii\web\NotFoundHttpException;
 | 
					
						
							|  |  |  | use yii\filters\VerbFilter;
 | 
					
						
							|  |  |  | use yii\filters\AccessControl;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SliderController extends Controller
 | 
					
						
							|  |  |  | {
 | 
					
						
							|  |  |  |     private $service;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function __construct($id, $module, SliderService $service, $config = [])
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         parent::__construct($id, $module, $config);
 | 
					
						
							|  |  |  |         $this->service = $service;
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * @inheritdoc
 | 
					
						
							|  |  |  |      */
 | 
					
						
							|  |  |  |     public function behaviors()
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         return [
 | 
					
						
							|  |  |  | 	        'access' => [
 | 
					
						
							|  |  |  | 		        'class' => AccessControl::class,
 | 
					
						
							|  |  |  | 		        'rules' => [
 | 
					
						
							|  |  |  | 			        [
 | 
					
						
							|  |  |  | 				        'actions' => ['create', 'view', 'index', 'update', 'delete'],
 | 
					
						
							|  |  |  | 				        'allow' => true,
 | 
					
						
							|  |  |  | 				        'roles' => ['SliderManagement'],
 | 
					
						
							|  |  |  | 			        ],
 | 
					
						
							|  |  |  | 			        [    // all the action are accessible to admin
 | 
					
						
							|  |  |  | 				        'allow' => true,
 | 
					
						
							|  |  |  | 				        'roles' => ['admin'],
 | 
					
						
							|  |  |  | 			        ],
 | 
					
						
							|  |  |  | 		        ],
 | 
					
						
							|  |  |  | 	        ],
 | 
					
						
							|  |  |  |             'verbs' => [
 | 
					
						
							|  |  |  |                 'class' => VerbFilter::class,
 | 
					
						
							|  |  |  |                 'actions' => [
 | 
					
						
							|  |  |  |                     'delete' => ['POST'],
 | 
					
						
							|  |  |  |                 ],
 | 
					
						
							|  |  |  |             ],
 | 
					
						
							|  |  |  |         ];
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function actionIndex()
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         //$searchModel = new UserSearch();
 | 
					
						
							|  |  |  |         //$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
 | 
					
						
							|  |  |  | 	    $query = Slider::find();
 | 
					
						
							|  |  |  | 	    $dataProvider = new ActiveDataProvider([
 | 
					
						
							|  |  |  | 		    'query' => $query,
 | 
					
						
							|  |  |  | 		    'pagination' => [
 | 
					
						
							|  |  |  | 			    'pageSize' => 20,
 | 
					
						
							|  |  |  | 		    ],
 | 
					
						
							|  |  |  | 		    'sort' => [
 | 
					
						
							|  |  |  | 			    'defaultOrder' => [
 | 
					
						
							|  |  |  | 				    'id' => SORT_ASC,
 | 
					
						
							|  |  |  | 			    ]
 | 
					
						
							|  |  |  | 		    ],
 | 
					
						
							|  |  |  | 	    ]);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this->render('index', [
 | 
					
						
							|  |  |  |             'dataProvider' => $dataProvider,
 | 
					
						
							|  |  |  |         ]);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/**
 | 
					
						
							|  |  |  | 	 * @param $id
 | 
					
						
							|  |  |  | 	 *
 | 
					
						
							|  |  |  | 	 * @return string
 | 
					
						
							|  |  |  | 	 * @throws NotFoundHttpException
 | 
					
						
							|  |  |  | 	 */
 | 
					
						
							|  |  |  |     public function actionView($id)
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         return $this->render('view', [
 | 
					
						
							|  |  |  |             'model' => $this->findModel($id),
 | 
					
						
							|  |  |  |         ]);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * Creates a new User model.
 | 
					
						
							|  |  |  |      * If creation is successful, the browser will be redirected to the 'view' page.
 | 
					
						
							|  |  |  |      * @return mixed
 | 
					
						
							|  |  |  |      */
 | 
					
						
							|  |  |  |     public function actionCreate()
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         $form = new SliderForm();
 | 
					
						
							|  |  |  |         $form->scenario = Slider::SCENARIO_CREATE;
 | 
					
						
							|  |  |  |         if ($form->load(Yii::$app->request->post()) && $form->validate()) {
 | 
					
						
							|  |  |  |             try {
 | 
					
						
							|  |  |  |                 $slider = $this->service->create($form);
 | 
					
						
							|  |  |  |                 return $this->redirect(['view', 'id' => $slider->id]);
 | 
					
						
							|  |  |  |             } catch (\DomainException $e) {
 | 
					
						
							|  |  |  |                 Yii::$app->errorHandler->logException($e);
 | 
					
						
							|  |  |  |                 Yii::$app->session->setFlash('error', $e->getMessage());
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |         return $this->render('create', [
 | 
					
						
							|  |  |  |             'model' => $form,
 | 
					
						
							|  |  |  |         ]);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/**
 | 
					
						
							|  |  |  | 	 * @param $id
 | 
					
						
							|  |  |  | 	 *
 | 
					
						
							|  |  |  | 	 * @return string|\yii\web\Response
 | 
					
						
							|  |  |  | 	 * @throws NotFoundHttpException
 | 
					
						
							|  |  |  | 	 */
 | 
					
						
							|  |  |  |     public function actionUpdate($id)
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         $slider = $this->findModel($id);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $form = new SliderForm($slider);
 | 
					
						
							|  |  |  | 	    $form->scenario = Slider::SCENARIO_UPDATE;
 | 
					
						
							|  |  |  |         if ($form->load(Yii::$app->request->post()) && $form->validate()) {
 | 
					
						
							|  |  |  |             try {
 | 
					
						
							|  |  |  |                 $this->service->edit($slider->id, $form);
 | 
					
						
							|  |  |  |                 return $this->redirect(['view', 'id' => $slider->id]);
 | 
					
						
							|  |  |  |             } catch (\DomainException $e) {
 | 
					
						
							|  |  |  |                 Yii::$app->errorHandler->logException($e);
 | 
					
						
							|  |  |  |                 Yii::$app->session->setFlash('error', $e->getMessage());
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |         return $this->render('update', [
 | 
					
						
							|  |  |  |             'model' => $form,
 | 
					
						
							|  |  |  |             'slider' => $slider,
 | 
					
						
							|  |  |  |         ]);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/**
 | 
					
						
							|  |  |  | 	 * @param $id
 | 
					
						
							|  |  |  | 	 *
 | 
					
						
							|  |  |  | 	 * @return \yii\web\Response
 | 
					
						
							|  |  |  | 	 */
 | 
					
						
							|  |  |  |     public function actionDelete($id)
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         $this->service->remove($id);
 | 
					
						
							|  |  |  |         return $this->redirect(['index']);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected function findModel($id): ?Slider
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         if (($model = Slider::findOne($id)) !== null) {
 | 
					
						
							|  |  |  |             return $model;
 | 
					
						
							|  |  |  |         } else {
 | 
					
						
							|  |  |  |             throw new NotFoundHttpException('The requested page does not exist.');
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | }
 |