-
Notifications
You must be signed in to change notification settings - Fork 0
/
SettingsModel.php
82 lines (69 loc) · 2.32 KB
/
SettingsModel.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
namespace panix\engine;
use Yii;
use yii\helpers\ArrayHelper;
use yii\base\InvalidConfigException;
use panix\engine\base\Model;
use panix\engine\components\Settings;
use yii\helpers\VarDumper;
/**
* Class SettingsModel
* @package panix\engine
*/
class SettingsModel extends Model
{
public static $category = null;
public static function tableName()
{
return Settings::tableName();
}
public static function defaultSettings()
{
return [];
}
public function init()
{
if (!isset($this->module)) {
throw new InvalidConfigException(Yii::t('yii', 'Missing required parameters: {params}', [
'params' => 'module'
]));
}
if (static::$category == null) {
static::$category = $this->module;
}
$this->setAttributes((array)Yii::$app->settings->get(static::$category));
}
public function submitButton()
{
return Html::submitButton(Yii::t('app/default', 'SAVE'), ['class' => 'btn btn-success']);
}
public function save()
{
Yii::$app->settings->set(static::$category, $this->attributes);
}
public function validate($attributeNames = null, $clearErrors = true)
{
if (parent::validate($attributeNames, $clearErrors)) {
Yii::$app->session->addFlash("success", Yii::t('app/default', 'SUCCESS_UPDATE'));
return true;
} else {
//print_r($this->getErrors());die;
Yii::$app->session->addFlash("error", Yii::t('app/default', 'ERROR_UPDATE'));
return false;
}
}
public function saveOLD()
{
// $shortName = (new \ReflectionClass(get_called_class()))->getShortName();
// $this->attributes = ArrayHelper::merge(Yii::$app->request->post($shortName), $this->attributes);
if ($this->validate()) {
Yii::$app->settings->set(static::$category, $this->attributes);
Yii::$app->session->setFlash("success", Yii::t('app/default', 'SUCCESS_UPDATE'));
return true;
} else {
//print_r($this->getErrors());die;
Yii::$app->session->setFlash("error", Yii::t('app/default', 'ERROR_UPDATE'));
return false;
}
}
}