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