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