|  |  |  | <?php
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Created by Error202
 | 
					
						
							|  |  |  |  * Date: 17.08.2018
 | 
					
						
							|  |  |  |  */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace backend\controllers;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use core\entities\ModuleRecord;
 | 
					
						
							|  |  |  | use core\services\ModuleService;
 | 
					
						
							|  |  |  | use yii\web\Controller;
 | 
					
						
							|  |  |  | use yii\filters\VerbFilter;
 | 
					
						
							|  |  |  | use yii\filters\AccessControl;
 | 
					
						
							|  |  |  | use yii\web\NotFoundHttpException;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Modules Management
 | 
					
						
							|  |  |  |  * Class ModuleController
 | 
					
						
							|  |  |  |  * @package backend\controllers
 | 
					
						
							|  |  |  |  */
 | 
					
						
							|  |  |  | class ModuleController extends Controller
 | 
					
						
							|  |  |  | {
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * @var ModuleService Modules management service
 | 
					
						
							|  |  |  |      */
 | 
					
						
							|  |  |  |     private $_service;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function __construct(string $id, $module, ModuleService $service, array $config = [])
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         parent::__construct($id, $module, $config);
 | 
					
						
							|  |  |  |         $this->_service = $service;
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function behaviors(): array
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         return [
 | 
					
						
							|  |  |  |             'access' => [
 | 
					
						
							|  |  |  |                 'class' => AccessControl::class,
 | 
					
						
							|  |  |  |                 'rules' => [
 | 
					
						
							|  |  |  |                     [
 | 
					
						
							|  |  |  |                         'actions' => ['list', 'disable', 'enable', 'delete'],
 | 
					
						
							|  |  |  |                         'allow'   => true,
 | 
					
						
							|  |  |  |                         'roles'   => ['ModuleManagement'],
 | 
					
						
							|  |  |  |                     ],
 | 
					
						
							|  |  |  |                     [    // all the action are accessible to admin
 | 
					
						
							|  |  |  |                         'allow' => true,
 | 
					
						
							|  |  |  |                         'roles' => ['admin'],
 | 
					
						
							|  |  |  |                     ],
 | 
					
						
							|  |  |  |                 ],
 | 
					
						
							|  |  |  |             ],
 | 
					
						
							|  |  |  |             'verbs'  => [
 | 
					
						
							|  |  |  |                 'class'   => VerbFilter::class,
 | 
					
						
							|  |  |  |                 'actions' => [
 | 
					
						
							|  |  |  |                     'delete'  => ['POST'],
 | 
					
						
							|  |  |  |                     'disable' => ['POST'],
 | 
					
						
							|  |  |  |                     'enable'  => ['POST'],
 | 
					
						
							|  |  |  |                 ],
 | 
					
						
							|  |  |  |             ],
 | 
					
						
							|  |  |  |         ];
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * List of modules
 | 
					
						
							|  |  |  |      * @return string
 | 
					
						
							|  |  |  |      */
 | 
					
						
							|  |  |  |     public function actionList()
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         $modules = \Yii::$app->moduleManager->getModules();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this->render('list', [
 | 
					
						
							|  |  |  |             'modules' => $modules,
 | 
					
						
							|  |  |  |         ]);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function actionDelete($id)
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         $module = $this->findModel($id);
 | 
					
						
							|  |  |  |         $this->_service->delete($module);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this->redirect(['module/list']);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function actionDisable($id)
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         $module = $this->findModel($id);
 | 
					
						
							|  |  |  |         $this->_service->disable($module);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this->redirect(['module/list']);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function actionEnable($id)
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         $module = $this->findModel($id);
 | 
					
						
							|  |  |  |         $this->_service->enable($module);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this->redirect(['module/list']);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected function findModel($id): ModuleRecord
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         if (($model = ModuleRecord::findOne($id)) !== null) {
 | 
					
						
							|  |  |  |             return $model;
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |         throw new NotFoundHttpException('The requested module does not exist.');
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | }
 |