diff --git a/.travis.yml b/.travis.yml index 8f9bace..2b97385 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,8 @@ language: php php: - - 5.5 - 5.6 - - 7.0 + - 7.1 # faster builds on new travis setup not using sudo sudo: false @@ -16,7 +15,7 @@ cache: install: - travis_retry composer self-update && composer --version - - travis_retry composer global require "fxp/composer-asset-plugin:~1.1.1" + - travis_retry composer global require "fxp/composer-asset-plugin:^1.2.0" - export PATH="$HOME/.composer/vendor/bin:$PATH" - travis_retry composer install --prefer-dist --no-interaction diff --git a/README.md b/README.md index b35b925..0333e19 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,124 @@ $settings->removeAll(); $settings->invalidateCache(); // automatically called on set(), remove(); ``` +Manage custom settings +---------------------- + +You can use your own form model to manage custom settings for your web application via `SettingsAction`. +To use the `SettingsAction` class you need to follow the following steps: + +1) Create your own model, for example: + +```php + Yii::t('app', 'Application Name'), + 'adminEmail' => Yii::t('app', 'Admin Email'), + ]; + } +} +``` + +2) Create view file, named `settings.php` with the following content: + +```php +title = Yii::t('app', 'Manage Application Settings'); +?> + + +field($model, 'appName'); ?> + +field($model, 'adminEmail'); ?> + + 'btn btn-success']) ?> + + + +``` + +3) Add settings action to your controller class as follows: + +```php + [ + 'class' => \yii2mod\settings\actions\SettingsAction::class, + // also you can use events as follows: + 'on beforeSave' => function ($event) { + // your custom code + }, + 'on afterSave' => function ($event) { + // your custom code + }, + 'modelClass' => \app\models\forms\ConfigurationForm::class, + ], + ]; + } +} +``` + +*Now you can access to the settings page by the following URL: http://localhost/path/to/index.php?r=site/manage-settings/* + + + Internationalization ---------------------- diff --git a/actions/SettingsAction.php b/actions/SettingsAction.php new file mode 100644 index 0000000..060123b --- /dev/null +++ b/actions/SettingsAction.php @@ -0,0 +1,94 @@ +modelClass === null) { + throw new InvalidConfigException('The "modelClass" property must be set.'); + } + } + + /** + * Renders the settings form. + * + * @return string + */ + public function run() + { + $model = Yii::createObject($this->modelClass); + $event = Yii::createObject(['class' => FormEvent::class, 'form' => $model]); + + if ($model->load(Yii::$app->request->post()) && $model->validate()) { + $this->trigger(self::EVENT_BEFORE_SAVE, $event); + + foreach ($model->toArray() as $key => $value) { + Yii::$app->settings->set($model->formName(), $key, $value); + } + + $this->trigger(self::EVENT_AFTER_SAVE, $event); + + if ($this->successMessage !== null) { + Yii::$app->session->setFlash('success', $this->successMessage); + } + } + + foreach ($model->attributes() as $attribute) { + $model->{$attribute} = Yii::$app->settings->get($model->formName(), $attribute); + } + + return $this->controller->render($this->view, ArrayHelper::merge($this->viewParams, [ + 'model' => $model, + ])); + } +} diff --git a/events/FormEvent.php b/events/FormEvent.php new file mode 100644 index 0000000..66f8f24 --- /dev/null +++ b/events/FormEvent.php @@ -0,0 +1,35 @@ +_form; + } + + /** + * @param Model $form + */ + public function setForm(Model $form) + { + $this->_form = $form; + } +}