1
|
|
|
<?php |
2
|
|
|
namespace zacksleo\yii2\cms\models; |
3
|
|
|
|
4
|
|
|
use Yii; |
5
|
|
|
use yii\db\ActiveRecord; |
6
|
|
|
use yii\helpers\ArrayHelper; |
7
|
|
|
use yii\helpers\Json; |
8
|
|
|
use yii\base\DynamicModel; |
9
|
|
|
use zacksleo\yii2\cms\Module; |
10
|
|
|
use yii\base\InvalidParamException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Setting |
14
|
|
|
* @package zacksleo\yii2\cms\models |
15
|
|
|
* @property string $type |
16
|
|
|
* @property string $key |
17
|
|
|
* @property string $value |
18
|
|
|
* @property integer $item_id |
19
|
|
|
*/ |
20
|
|
|
class ItemField extends ActiveRecord |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @inheritdoc |
24
|
|
|
*/ |
25
|
3 |
|
public static function tableName() |
26
|
|
|
{ |
27
|
3 |
|
return '{{%item_field}}'; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param bool $forDropDown if false - return array or validators, true - key=>value for dropDown |
32
|
|
|
* @return array |
33
|
|
|
*/ |
34
|
1 |
|
public function getTypes($forDropDown = true) |
35
|
|
|
{ |
36
|
|
|
$values = [ |
37
|
1 |
|
'string' => ['value', 'string'], |
38
|
1 |
|
'integer' => ['value', 'integer'], |
39
|
1 |
|
'boolean' => ['value', 'boolean', 'trueValue' => "1", 'falseValue' => "0", 'strict' => true], |
40
|
1 |
|
'float' => ['value', 'number'], |
41
|
1 |
|
'email' => ['value', 'email'], |
42
|
1 |
|
'ip' => ['value', 'ip'], |
43
|
1 |
|
'url' => ['value', 'url'], |
44
|
|
|
'object' => [ |
45
|
1 |
|
'value', |
46
|
|
|
function ($attribute, $params) { |
|
|
|
|
47
|
|
|
$object = null; |
|
|
|
|
48
|
|
|
try { |
49
|
|
|
Json::decode($this->$attribute); |
50
|
|
|
} catch (InvalidParamException $e) { |
51
|
|
|
$this->addError($attribute, Module::t('settings', '"{attribute}" must be a valid JSON object', [ |
52
|
|
|
'attribute' => $attribute, |
53
|
|
|
])); |
54
|
|
|
} |
55
|
|
|
} |
56
|
1 |
|
], |
57
|
1 |
|
]; |
58
|
|
|
|
59
|
1 |
|
if (!$forDropDown) { |
60
|
1 |
|
return $values; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$return = []; |
64
|
|
|
foreach ($values as $key => $value) { |
65
|
|
|
$return[$key] = Module::t('settings', $key); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $return; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @inheritdoc |
73
|
|
|
*/ |
74
|
1 |
|
public function rules() |
75
|
|
|
{ |
76
|
|
|
return [ |
77
|
1 |
|
['item_id', 'integer'], |
78
|
1 |
|
[['value'], 'string'], |
79
|
1 |
|
['key', 'string', 'max' => 255], |
80
|
|
|
[ |
81
|
1 |
|
['key'], |
82
|
1 |
|
'unique', |
83
|
1 |
|
'targetAttribute' => ['item_id', 'key'], |
84
|
|
|
'message' => |
85
|
1 |
|
Module::t('cms', '{attribute} "{value}" already exists for this section.') |
86
|
1 |
|
], |
87
|
1 |
|
['type', 'in', 'range' => array_keys($this->getTypes(false))], |
88
|
1 |
|
['type', 'safe'], |
89
|
1 |
|
]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @inheritdoc |
94
|
|
|
*/ |
95
|
|
|
public function attributeLabels() |
96
|
|
|
{ |
97
|
|
|
return [ |
98
|
|
|
'id' => Module::t('cms', 'ID'), |
99
|
|
|
'type' => Module::t('cms', 'Type'), |
100
|
|
|
'item_id' => Module::t('cms', 'Item ID'), |
101
|
|
|
'key' => Module::t('cms', 'Key'), |
102
|
|
|
'value' => Module::t('cms', 'Value'), |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
public function beforeSave($insert) |
107
|
|
|
{ |
108
|
1 |
|
$validators = $this->getTypes(false); |
109
|
1 |
|
if (!array_key_exists($this->type, $validators)) { |
110
|
|
|
$this->addError('type', Module::t('cms', 'Please select correct type')); |
111
|
|
|
return false; |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
$model = DynamicModel::validateData([ |
115
|
1 |
|
'value' => $this->value |
116
|
1 |
|
], [ |
117
|
1 |
|
$validators[$this->type], |
118
|
1 |
|
]); |
119
|
|
|
|
120
|
1 |
|
if ($model->hasErrors()) { |
121
|
|
|
$this->addError('value', $model->getFirstError('value')); |
122
|
|
|
return false; |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
if ($this->hasErrors()) { |
126
|
|
|
return false; |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
return parent::beforeSave($insert); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @inheritdoc |
134
|
|
|
*/ |
135
|
|
|
public function getSettings() |
136
|
|
|
{ |
137
|
|
|
$settings = static::find()->asArray()->all(); |
138
|
|
|
return array_merge_recursive( |
139
|
|
|
ArrayHelper::map($settings, 'key', 'value', 'item_id'), |
140
|
|
|
ArrayHelper::map($settings, 'key', 'type', 'item_id') |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @inheritdoc |
146
|
|
|
*/ |
147
|
1 |
|
public function setSetting($item_id, $key, $value, $type = null) |
148
|
|
|
{ |
149
|
1 |
|
$model = static::findOne(['item_id' => $item_id, 'key' => $key]); |
150
|
|
|
|
151
|
1 |
|
if ($model === null) { |
152
|
1 |
|
$model = new static(); |
153
|
1 |
|
} |
154
|
1 |
|
$model->item_id = $item_id; |
155
|
1 |
|
$model->key = $key; |
156
|
1 |
|
$model->value = strval($value); |
157
|
|
|
|
158
|
1 |
|
if ($type !== null) { |
159
|
1 |
|
$model->type = $type; |
160
|
1 |
|
} else { |
161
|
|
|
$t = gettype($value); |
162
|
|
|
if ($t == 'string') { |
163
|
|
|
$error = false; |
164
|
|
|
try { |
165
|
|
|
Json::decode($value); |
166
|
|
|
} catch (InvalidParamException $e) { |
167
|
|
|
$error = true; |
168
|
|
|
} |
169
|
|
|
if (!$error) { |
170
|
|
|
$t = 'object'; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
$model->type = $t; |
174
|
|
|
} |
175
|
|
|
|
176
|
1 |
|
return $model->save(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @inheritdoc |
181
|
|
|
*/ |
182
|
1 |
|
public function deleteSetting($item_id, $key) |
183
|
|
|
{ |
184
|
1 |
|
$model = static::findOne(['item_id' => $item_id, 'key' => $key]); |
185
|
|
|
|
186
|
1 |
|
if ($model) { |
187
|
|
|
return $model->delete(); |
188
|
|
|
} |
189
|
1 |
|
return true; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param $key |
194
|
|
|
* @param $item_id |
195
|
|
|
* @return array|null|ActiveRecord |
196
|
|
|
*/ |
197
|
2 |
|
public function findSetting($key, $item_id) |
198
|
|
|
{ |
199
|
2 |
|
return $this->find()->where(['item_id' => $item_id, 'key' => $key])->limit(1)->one(); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.