-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from yii2mod/add_settings_action
close #5
- Loading branch information
Showing
4 changed files
with
249 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
namespace yii2mod\settings\actions; | ||
|
||
use Yii; | ||
use yii\base\Action; | ||
use yii\base\InvalidConfigException; | ||
use yii\helpers\ArrayHelper; | ||
use yii2mod\settings\events\FormEvent; | ||
|
||
/** | ||
* Class SettingsAction | ||
* | ||
* @package yii2mod\settings\actions | ||
*/ | ||
class SettingsAction extends Action | ||
{ | ||
/** | ||
* Event is triggered before the settings will be saved. | ||
* Triggered with \yii2mod\settings\events\FormEvent. | ||
*/ | ||
const EVENT_BEFORE_SAVE = 'beforeSave'; | ||
|
||
/** | ||
* Event is triggered after the settings have been saved successfully. | ||
* Triggered with \yii2mod\settings\events\FormEvent. | ||
*/ | ||
const EVENT_AFTER_SAVE = 'afterSave'; | ||
|
||
/** | ||
* @var string class name of the model which will be used to validate the attributes | ||
*/ | ||
public $modelClass; | ||
|
||
/** | ||
* @var string message to be set on successful save a model | ||
*/ | ||
public $successMessage = 'Settings have been saved successfully.'; | ||
|
||
/** | ||
* @var string the name of the settings view | ||
*/ | ||
public $view = 'settings'; | ||
|
||
/** | ||
* @var array additional view params | ||
*/ | ||
public $viewParams = []; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function init() | ||
{ | ||
parent::init(); | ||
|
||
if ($this->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, | ||
])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace yii2mod\settings\events; | ||
|
||
use yii\base\Event; | ||
use yii\base\Model; | ||
|
||
/** | ||
* Class FormEvent | ||
* | ||
* @package yii2mod\settings\events | ||
*/ | ||
class FormEvent extends Event | ||
{ | ||
/** | ||
* @var Model | ||
*/ | ||
private $_form; | ||
|
||
/** | ||
* @return Model | ||
*/ | ||
public function getForm() | ||
{ | ||
return $this->_form; | ||
} | ||
|
||
/** | ||
* @param Model $form | ||
*/ | ||
public function setForm(Model $form) | ||
{ | ||
$this->_form = $form; | ||
} | ||
} |