Skip to content

Commit

Permalink
Merge pull request #16 from GergeFreeman/master
Browse files Browse the repository at this point in the history
Возможность изменения section у настроек
  • Loading branch information
Igor Chepurnoy authored May 30, 2017
2 parents 9725158 + 43a58bd commit 8ba9d6e
Showing 1 changed file with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions actions/SettingsAction.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
<?php

/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace yii2mod\settings\actions;

use Yii;
Expand All @@ -10,9 +20,7 @@
use yii2mod\settings\events\FormEvent;

/**
* Class SettingsAction
*
* @package yii2mod\settings\actions
* Class SettingsAction.
*/
class SettingsAction extends Action
{
Expand All @@ -35,8 +43,8 @@ class SettingsAction extends Action

/**
* @var callable a PHP callable that will be called for save the settings.
* If not set, [[saveSettings()]] will be used instead.
* The signature of the callable should be:
* If not set, [[saveSettings()]] will be used instead.
* The signature of the callable should be:
*
* ```php
* function ($model) {
Expand All @@ -48,8 +56,8 @@ class SettingsAction extends Action

/**
* @var callable a PHP callable that will be called to prepare a model.
* If not set, [[prepareModel()]] will be used instead.
* The signature of the callable should be:
* If not set, [[prepareModel()]] will be used instead.
* The signature of the callable should be:
*
* ```php
* function ($model) {
Expand All @@ -59,6 +67,11 @@ class SettingsAction extends Action
*/
public $prepareModel;

/**
* @var string
*/
public $sectionSettings;

/**
* @var string message to be set on successful save a model
*/
Expand All @@ -75,7 +88,7 @@ class SettingsAction extends Action
public $viewParams = [];

/**
* @inheritdoc
* {@inheritdoc}
*/
public function init()
{
Expand Down Expand Up @@ -119,7 +132,7 @@ public function run()
}

/**
* Prepares the model which will be used to validate the attributes
* Prepares the model which will be used to validate the attributes.
*
* @param Model $model
*/
Expand All @@ -129,7 +142,7 @@ protected function prepareModel(Model $model)
call_user_func($this->prepareModel, $model);
} else {
foreach ($model->attributes() as $attribute) {
$model->{$attribute} = Yii::$app->settings->get($model->formName(), $attribute);
$model->{$attribute} = Yii::$app->settings->get($this->getSection($model), $attribute);
}
}
}
Expand All @@ -143,8 +156,22 @@ protected function saveSettings(Model $model)
call_user_func($this->saveSettings, $model);
} else {
foreach ($model->toArray() as $key => $value) {
Yii::$app->settings->set($model->formName(), $key, $value);
Yii::$app->settings->set($this->getSection($model), $key, $value);
}
}
}

/**
* @param Model $model
*
* @return string
*/
protected function getSection(Model $model)
{
if ($this->sectionSettings) {
return $this->sectionSettings;
}

return $model->formName();
}
}

0 comments on commit 8ba9d6e

Please sign in to comment.