| 1 | <?php |
||
| 2 | |||
| 3 | namespace andmemasin\language\models; |
||
| 4 | |||
| 5 | |||
| 6 | use andmemasin\myabstract\Settings; |
||
| 7 | use yii\base\InvalidConfigException; |
||
| 8 | use yii\base\UserException; |
||
| 9 | use andmemasin\language\interfaces\WithLanguagesInterface; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Class BaseLanguageSettings |
||
| 13 | * @package andmemasin\language\models |
||
| 14 | * @author Tõnis Ormisson <[email protected]> |
||
| 15 | */ |
||
| 16 | class BaseLanguageSettings extends Settings |
||
| 17 | { |
||
| 18 | /** @var BaseLanguageSetting[] */ |
||
| 19 | public $settings; |
||
| 20 | |||
| 21 | /** @var array */ |
||
| 22 | public $settingsData = []; |
||
| 23 | |||
| 24 | /** @var WithLanguagesInterface */ |
||
| 25 | public $parent; |
||
| 26 | |||
| 27 | |||
| 28 | /** @var Language */ |
||
| 29 | public $language; |
||
| 30 | |||
| 31 | /** {@inheritdoc} */ |
||
| 32 | public function init() |
||
| 33 | { |
||
| 34 | parent::init(); |
||
| 35 | |||
| 36 | if (!$this->language) { |
||
| 37 | throw new InvalidConfigException('Language must be defined while initiating languages-settings'); |
||
| 38 | } |
||
| 39 | |||
| 40 | if (!$this->parent) { |
||
| 41 | throw new InvalidConfigException('Parent Model must be defined while initiating languages-settings'); |
||
| 42 | } |
||
| 43 | |||
| 44 | if (!$this->parent instanceof WithLanguagesInterface) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 45 | throw new InvalidConfigException('Parent Model must implement WithLanguagesInterface'); |
||
| 46 | } |
||
| 47 | |||
| 48 | } |
||
| 49 | |||
| 50 | /** {@inheritdoc} */ |
||
| 51 | public function save($runValidation = true, $attributeNames = NULL){ |
||
| 52 | if(!empty($this->settings)){ |
||
| 53 | foreach ($this->settings as $key => $setting) { |
||
| 54 | // delete empty |
||
| 55 | if($setting->value === null || $setting->value === ''){ |
||
| 56 | if(!$setting->isNewRecord){ |
||
| 57 | $setting->delete(); |
||
| 58 | } |
||
| 59 | } else { |
||
| 60 | if (!$setting->save($runValidation, $attributeNames)) { |
||
| 61 | throw new UserException(serialize($setting->errors)); |
||
| 62 | } |
||
| 63 | |||
| 64 | $this->settings[$key] = $setting; |
||
| 65 | $this->addErrors($setting->errors); |
||
| 66 | } |
||
| 67 | |||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | return empty($this->errors); |
||
| 72 | } |
||
| 73 | |||
| 74 | } |