You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					172 lines
				
				4.3 KiB
			
		
		
			
		
	
	
					172 lines
				
				4.3 KiB
			| 
											8 years ago
										 | <?php
 | ||
|  | 
 | ||
|  | namespace backend\controllers;
 | ||
|  | 
 | ||
|  | use core\forms\PageForm;
 | ||
|  | use core\services\PageManageService;
 | ||
|  | use Yii;
 | ||
|  | use core\entities\Page;
 | ||
|  | use backend\forms\PageSearch;
 | ||
|  | use yii\web\Controller;
 | ||
|  | use yii\web\NotFoundHttpException;
 | ||
|  | use yii\filters\VerbFilter;
 | ||
|  | use yii\filters\AccessControl;
 | ||
|  | 
 | ||
|  | class PageController extends Controller
 | ||
|  | {
 | ||
|  |     private $service;
 | ||
|  | 
 | ||
|  |     public function __construct($id, $module, PageManageService $service, $config = [])
 | ||
|  |     {
 | ||
|  |         parent::__construct($id, $module, $config);
 | ||
|  |         $this->service = $service;
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     public function behaviors(): array
 | ||
|  |     {
 | ||
|  |         return [
 | ||
|  | 	        'access' => [
 | ||
|  | 		        'class' => AccessControl::className(),
 | ||
|  | 		        'rules' => [
 | ||
|  | 			        [
 | ||
|  | 				        'actions' => ['create', 'view', 'index', 'update', 'delete', 'move-up', 'move-down'],
 | ||
|  | 				        'allow' => true,
 | ||
|  | 				        'roles' => ['PagesManagement'],
 | ||
|  | 			        ],
 | ||
|  | 			        [    // all the action are accessible to admin
 | ||
|  | 				        'allow' => true,
 | ||
|  | 				        'roles' => ['admin'],
 | ||
|  | 			        ],
 | ||
|  | 		        ],
 | ||
|  | 	        ],
 | ||
|  |             'verbs' => [
 | ||
|  |                 'class' => VerbFilter::className(),
 | ||
|  |                 'actions' => [
 | ||
|  |                     'delete' => ['POST'],
 | ||
|  |                 ],
 | ||
|  |             ],
 | ||
|  |         ];
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * @return mixed
 | ||
|  |      */
 | ||
|  |     public function actionIndex()
 | ||
|  |     {
 | ||
|  |         $searchModel = new PageSearch();
 | ||
|  |         $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
 | ||
|  | 
 | ||
|  |         return $this->render('index', [
 | ||
|  |             'searchModel' => $searchModel,
 | ||
|  |             'dataProvider' => $dataProvider,
 | ||
|  |         ]);
 | ||
|  |     }
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * @param $id
 | ||
|  | 	 *
 | ||
|  | 	 * @return string
 | ||
|  | 	 * @throws NotFoundHttpException
 | ||
|  | 	 */
 | ||
|  |     public function actionView($id)
 | ||
|  |     {
 | ||
|  |         return $this->render('view', [
 | ||
|  |             'page' => $this->findModel($id),
 | ||
|  |         ]);
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * @return mixed
 | ||
|  |      */
 | ||
|  |     public function actionCreate()
 | ||
|  |     {
 | ||
|  |         $form = new PageForm();
 | ||
|  |         if ($form->load(Yii::$app->request->post()) && $form->validate()) {
 | ||
|  |             try {
 | ||
|  |                 $page = $this->service->create($form);
 | ||
|  |                 return $this->redirect(['view', 'id' => $page->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)
 | ||
|  |     {
 | ||
|  |         $page = $this->findModel($id);
 | ||
|  | 
 | ||
|  |         $form = new PageForm($page);
 | ||
|  |         if ($form->load(Yii::$app->request->post()) && $form->validate()) {
 | ||
|  |             try {
 | ||
|  |                 $this->service->edit($page->id, $form);
 | ||
|  |                 return $this->redirect(['view', 'id' => $page->id]);
 | ||
|  |             } catch (\DomainException $e) {
 | ||
|  |                 Yii::$app->errorHandler->logException($e);
 | ||
|  |                 Yii::$app->session->setFlash('error', $e->getMessage());
 | ||
|  |             }
 | ||
|  |         }
 | ||
|  |         return $this->render('update', [
 | ||
|  |             'model' => $form,
 | ||
|  |             'page' => $page,
 | ||
|  |         ]);
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * @param integer $id
 | ||
|  |      * @return mixed
 | ||
|  |      */
 | ||
|  |     public function actionDelete($id)
 | ||
|  |     {
 | ||
|  |         try {
 | ||
|  |             $this->service->remove($id);
 | ||
|  |         } catch (\DomainException $e) {
 | ||
|  |             Yii::$app->errorHandler->logException($e);
 | ||
|  |             Yii::$app->session->setFlash('error', $e->getMessage());
 | ||
|  |         }
 | ||
|  |         return $this->redirect(['index']);
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * @param integer $id
 | ||
|  |      * @return mixed
 | ||
|  |      */
 | ||
|  |     public function actionMoveUp($id)
 | ||
|  |     {
 | ||
|  |         $this->service->moveUp($id);
 | ||
|  |         return $this->redirect(['index']);
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * @param integer $id
 | ||
|  |      * @return mixed
 | ||
|  |      */
 | ||
|  |     public function actionMoveDown($id)
 | ||
|  |     {
 | ||
|  |         $this->service->moveDown($id);
 | ||
|  |         return $this->redirect(['index']);
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * @param integer $id
 | ||
|  |      * @return Page the loaded model
 | ||
|  |      * @throws NotFoundHttpException if the model cannot be found
 | ||
|  |      */
 | ||
|  |     protected function findModel($id): Page
 | ||
|  |     {
 | ||
|  |         if (($model = Page::findOne($id)) !== null) {
 | ||
|  |             return $model;
 | ||
|  |         }
 | ||
|  |         throw new NotFoundHttpException('The requested page does not exist.');
 | ||
|  |     }
 | ||
|  | }
 |