1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://phe.me |
4
|
|
|
* @copyright Copyright (c) 2014 Pheme |
5
|
|
|
* @license MIT http://opensource.org/licenses/MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace pheme\settings\models; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\helpers\Json; |
12
|
|
|
use yii\base\DynamicModel; |
13
|
|
|
use pheme\settings\Module; |
14
|
|
|
use yii\base\InvalidParamException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* This is the model class for table "settings". |
18
|
|
|
* |
19
|
|
|
* @author Aris Karageorgos <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class Setting extends BaseSetting |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @param bool $forDropDown if false - return array or validators, true - key=>value for dropDown |
25
|
|
|
* @return array |
26
|
|
|
*/ |
27
|
18 |
|
public function getTypes($forDropDown = true) |
28
|
|
|
{ |
29
|
|
|
$values = [ |
30
|
18 |
|
'string' => ['value', 'string'], |
31
|
18 |
|
'integer' => ['value', 'integer'], |
32
|
18 |
|
'boolean' => ['value', 'boolean', 'trueValue' => "1", 'falseValue' => "0", 'strict' => true], |
33
|
18 |
|
'float' => ['value', 'number'], |
34
|
18 |
|
'email' => ['value', 'email'], |
35
|
18 |
|
'ip' => ['value', 'ip'], |
36
|
18 |
|
'url' => ['value', 'url'], |
37
|
|
|
'object' => [ |
38
|
18 |
|
'value', |
39
|
|
|
function ($attribute, $params) { |
|
|
|
|
40
|
|
|
$object = null; |
|
|
|
|
41
|
|
|
try { |
42
|
|
|
Json::decode($this->$attribute); |
43
|
|
|
} catch (InvalidParamException $e) { |
44
|
|
|
$this->addError($attribute, Module::t('settings', '"{attribute}" must be a valid JSON object', [ |
45
|
|
|
'attribute' => $attribute, |
46
|
|
|
])); |
47
|
|
|
} |
48
|
|
|
} |
49
|
18 |
|
], |
50
|
18 |
|
]; |
51
|
|
|
|
52
|
18 |
|
if (!$forDropDown) { |
53
|
18 |
|
return $values; |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
$return = []; |
57
|
1 |
|
foreach ($values as $key => $value) { |
58
|
1 |
|
$return[$key] = Module::t('settings', $key); |
59
|
1 |
|
} |
60
|
|
|
|
61
|
1 |
|
return $return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @inheritdoc |
66
|
|
|
*/ |
67
|
18 |
|
public function rules() |
68
|
|
|
{ |
69
|
|
|
return [ |
70
|
18 |
|
[['value'], 'string'], |
71
|
18 |
|
[['section', 'key'], 'string', 'max' => 255], |
72
|
|
|
[ |
73
|
18 |
|
['key'], |
74
|
18 |
|
'unique', |
75
|
18 |
|
'targetAttribute' => ['section', 'key'], |
76
|
|
|
'message' => |
77
|
18 |
|
Module::t('settings', '{attribute} "{value}" already exists for this section.') |
78
|
18 |
|
], |
79
|
18 |
|
['type', 'in', 'range' => array_keys($this->getTypes(false))], |
80
|
18 |
|
[['type', 'created', 'modified'], 'safe'], |
81
|
18 |
|
[['active'], 'boolean'], |
82
|
18 |
|
]; |
83
|
|
|
} |
84
|
|
|
|
85
|
17 |
|
public function beforeSave($insert) |
86
|
|
|
{ |
87
|
17 |
|
$validators = $this->getTypes(false); |
88
|
17 |
|
if (!array_key_exists($this->type, $validators)) { |
89
|
|
|
$this->addError('type', Module::t('settings', 'Please select correct type')); |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
17 |
|
$model = DynamicModel::validateData([ |
94
|
17 |
|
'value' => $this->value |
95
|
17 |
|
], [ |
96
|
17 |
|
$validators[$this->type], |
97
|
17 |
|
]); |
98
|
|
|
|
99
|
17 |
|
if ($model->hasErrors()) { |
100
|
1 |
|
$this->addError('value', $model->getFirstError('value')); |
101
|
1 |
|
return false; |
102
|
|
|
} |
103
|
|
|
|
104
|
17 |
|
if ($this->hasErrors()) { |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
17 |
|
return parent::beforeSave($insert); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @inheritdoc |
113
|
|
|
*/ |
114
|
2 |
|
public function attributeLabels() |
115
|
|
|
{ |
116
|
|
|
return [ |
117
|
2 |
|
'id' => Module::t('settings', 'ID'), |
118
|
2 |
|
'type' => Module::t('settings', 'Type'), |
119
|
2 |
|
'section' => Module::t('settings', 'Section'), |
120
|
2 |
|
'key' => Module::t('settings', 'Key'), |
121
|
2 |
|
'value' => Module::t('settings', 'Value'), |
122
|
2 |
|
'active' => Module::t('settings', 'Active'), |
123
|
2 |
|
'created' => Module::t('settings', 'Created'), |
124
|
2 |
|
'modified' => Module::t('settings', 'Modified'), |
125
|
2 |
|
]; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.