_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', 'favicon'], '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($section = 'site') { $searchModel = new SettingsSearch(); $searchModel->section = $section; $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render( 'index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'section' => $section, ] ); } public function actionView($section, $key) { return $this->render( 'view', [ 'model' => $this->findModel($section, $key), ] ); } 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', 'section' => $settings->section, 'key' => $settings->key]); } 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($section, $key) { $settings = $this->findModel($section, $key); $form = new SettingsForm($settings); if ($form->load(Yii::$app->request->post()) && $form->validate()) { try { $this->_service->edit($settings->section, $settings->key, $form); return $this->redirect(['view', 'section' => $settings->section, 'key' => $settings->key]); } 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($section, $key) { $this->_service->remove($section, $key); return $this->redirect(['index']); } public function actionFavicon() { $form = new FaviconForm(); if ($form->load(Yii::$app->request->post())) { $form->image = UploadedFile::getInstance($form, 'image'); if ($form->image && $form->validate()) { if (!file_exists(Yii::getAlias('@staticRoot') . '/origin/favicon')) { mkdir(Yii::getAlias('@staticRoot') . '/origin/favicon', 0777, true); } $form->image->saveAs(Yii::getAlias('@staticRoot') . '/origin/favicon/favicon.png'); $this->_service->newFavicon(); Yii::$app->session->setFlash('success', Yii::t('main', 'Favicon generation complete')); return $this->redirect(['settings/list/index']); } } return $this->render( 'favicon', [ 'model' => $form, ] ); } protected function findModel($section, $key) { if (($model = Settings::find()->andWhere(['section' => $section])->andWhere(['key' => $key])->one()) !== null) { return $model; } else { throw new NotFoundHttpException('The requested setting does not exist.'); } } }