-
Notifications
You must be signed in to change notification settings - Fork 74
/
SettingsAction.php
58 lines (52 loc) · 1.73 KB
/
SettingsAction.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace pheme\settings;
use Yii;
use yii\base\Action;
class SettingsAction extends Action
{
/**
* @var string class name of the model which will be used to validate the attributes.
* The class should have a scenario matching the `scenario` variable.
* The model class must implement [[Model]].
* This property must be set.
*/
public $modelClass;
/**
* @var string The scenario this model should use to make validation
*/
public $scenario;
/**
* @var string the name of the view to generate the form. Defaults to 'settings'.
*/
public $viewName = 'settings';
/**
* @var string name on section. Default is ModelClass formname
*/
public $section = null;
/**
* Render the settings form.
*/
public function run()
{
/* @var $model \yii\db\ActiveRecord */
$model = new $this->modelClass();
$section = ($this->section !== null) ? $this->section : $model->formName();
if ($this->scenario) {
$model->setScenario($this->scenario);
}
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
foreach ($model->toArray() as $key => $value) {
Yii::$app->settings->set($key, $value, $section);
}
Yii::$app->getSession()->addFlash('success',
Module::t('settings', 'Successfully saved settings on {section}',
['section' => $model->formName()]
)
);
}
foreach ($model->attributes() as $key) {
$model->{$key} = Yii::$app->settings->get($key, $section);
}
return $this->controller->render($this->viewName, ['model' => $model]);
}
}