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