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.
		
		
		
		
		
			
		
			
				
					
					
						
							150 lines
						
					
					
						
							3.0 KiB
						
					
					
				
			
		
		
	
	
							150 lines
						
					
					
						
							3.0 KiB
						
					
					
				| <?php | |
| /** | |
|  * Created by Error202 | |
|  * Date: 04.06.2018 | |
|  */ | |
|  | |
| namespace backend\controllers\settings; | |
|  | |
|  | |
| use backend\forms\SettingsSearch; | |
| use core\entities\Settings; | |
| use core\forms\SettingsForm; | |
| use core\services\SettingsService; | |
| use yii\filters\AccessControl; | |
| use yii\filters\VerbFilter; | |
| use yii\web\Controller; | |
| use backend\components\ToggleAction; | |
| use Yii; | |
| use yii\web\NotFoundHttpException; | |
|  | |
| class ListController extends Controller | |
| { | |
| 	private $service; | |
|  | |
| 	public function __construct( string $id, $module, SettingsService $service, array $config = [] ) { | |
| 		parent::__construct( $id, $module, $config ); | |
| 		$this->service = $service; | |
| 	} | |
|  | |
| 	public function behaviors() | |
| 	{ | |
| 		return [ | |
| 			'verbs' => [ | |
| 				'class' => VerbFilter::class, | |
| 				'actions' => [ | |
| 					'delete' => ['POST'], | |
| 				], | |
| 			], | |
| 			'access' => [ | |
| 				'class' => AccessControl::class, | |
| 				'rules' => [ | |
| 					[ | |
| 						'actions' => ['create','view','index', 'update', 'delete', 'toggle'], | |
| 						'allow' => true, | |
| 						'roles' => ['SettingsManagement'], | |
| 					], | |
| 					[    // all the action are accessible to admin | |
| 						'allow' => true, | |
| 						'roles' => ['admin'], | |
| 					], | |
| 				], | |
| 			], | |
| 		]; | |
| 	} | |
|  | |
| 	public function actions() | |
| 	{ | |
| 		return [ | |
| 			'toggle' => [ | |
| 				'class' => ToggleAction::class, | |
| 				'modelClass' => Settings::class, | |
| 				//'setFlash' => true, | |
| 			] | |
| 		]; | |
| 	} | |
|  | |
| 	public function actionIndex() | |
| 	{ | |
| 		$searchModel = new SettingsSearch(); | |
| 		$dataProvider = $searchModel->search(Yii::$app->request->queryParams); | |
| 		return $this->render( | |
| 			'index', | |
| 			[ | |
| 				'searchModel' => $searchModel, | |
| 				'dataProvider' => $dataProvider, | |
| 			] | |
| 		); | |
| 	} | |
|  | |
| 	public function actionView($id) | |
| 	{ | |
| 		return $this->render( | |
| 			'view', | |
| 			[ | |
| 				'model' => $this->findModel($id), | |
| 			] | |
| 		); | |
| 	} | |
|  | |
| 	public function actionCreate() | |
| 	{ | |
| 		$form = new SettingsForm(); | |
| 		if ($form->load(Yii::$app->request->post()) && $form->validate()) { | |
| 			try { | |
| 				$settings = $this->service->create($form); | |
| 				return $this->redirect(['view', 'id' => $settings->id]); | |
| 			} catch (\DomainException $e) { | |
| 				Yii::$app->errorHandler->logException($e); | |
| 				Yii::$app->session->setFlash('error', $e->getMessage()); | |
| 			} | |
| 		} | |
| 		else { | |
| 			$form->active = 1; | |
| 		} | |
| 		return $this->render( | |
| 			'create', | |
| 			[ | |
| 				'model' => $form, | |
| 			] | |
| 		); | |
| 	} | |
|  | |
| 	public function actionUpdate($id) | |
| 	{ | |
| 		$settings = $this->findModel($id); | |
|  | |
| 		$form = new SettingsForm($settings); | |
| 		if ($form->load(Yii::$app->request->post()) && $form->validate()) { | |
| 			try { | |
| 				$this->service->edit($settings->id, $form); | |
| 				return $this->redirect(['view', 'id' => $settings->id]); | |
| 			} catch (\DomainException $e) { | |
| 				Yii::$app->errorHandler->logException($e); | |
| 				Yii::$app->session->setFlash('error', $e->getMessage()); | |
| 			} | |
| 		} | |
| 		return $this->render( | |
| 			'update', | |
| 			[ | |
| 				'model' => $form, | |
| 				'settings' => $settings, | |
| 			] | |
| 		); | |
| 	} | |
|  | |
| 	public function actionDelete($id) | |
| 	{ | |
| 		$this->service->remove($id); | |
| 		return $this->redirect(['index']); | |
| 	} | |
|  | |
| 	protected function findModel($id) | |
| 	{ | |
| 		if (($model = Settings::findOne($id)) !== null) { | |
| 			return $model; | |
| 		} else { | |
| 			throw new NotFoundHttpException('The requested page does not exist.'); | |
| 		} | |
| 	} | |
| } |