|
1
|
|
|
<?php |
|
2
|
|
|
namespace app\models; |
|
3
|
|
|
|
|
4
|
|
|
use app\modules\shop\models\FilterSets; |
|
5
|
|
|
use app\properties\HasProperties; |
|
6
|
|
|
use app\properties\PropertyHandlers; |
|
7
|
|
|
use app\traits\GetImages; |
|
8
|
|
|
use Yii; |
|
9
|
|
|
use yii\behaviors\AttributeBehavior; |
|
10
|
|
|
use yii\caching\TagDependency; |
|
11
|
|
|
use yii\data\ActiveDataProvider; |
|
12
|
|
|
use yii\db\ActiveRecord; |
|
13
|
|
|
use yii\helpers\Json; |
|
14
|
|
|
use \devgroup\TagDependencyHelper\ActiveRecordHelper; |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* This is the model class for table "{{%property}}". |
|
19
|
|
|
* @property integer $id |
|
20
|
|
|
* @property integer $property_group_id |
|
21
|
|
|
* @property string $name |
|
22
|
|
|
* @property string $key |
|
23
|
|
|
* @property string $value_type |
|
24
|
|
|
* @property integer $property_handler_id |
|
25
|
|
|
* @property integer $has_static_values |
|
26
|
|
|
* @property integer $has_slugs_in_values |
|
27
|
|
|
* @property integer $is_eav |
|
28
|
|
|
* @property integer $is_column_type_stored |
|
29
|
|
|
* @property integer $multiple |
|
30
|
|
|
* @property integer $sort_order |
|
31
|
|
|
* @property string $handler_additional_params |
|
32
|
|
|
* @property integer $display_only_on_depended_property_selected |
|
33
|
|
|
* @property integer $depends_on_property_id |
|
34
|
|
|
* @property string $depended_property_values |
|
35
|
|
|
* @property integer $depends_on_category_group_id |
|
36
|
|
|
* @property boolean $dont_filter |
|
37
|
|
|
* @property integer $required |
|
38
|
|
|
* @property integer $interpret_as |
|
39
|
|
|
* @property integer $captcha |
|
40
|
|
|
* @property string $mask |
|
41
|
|
|
* @property integer $alias |
|
42
|
|
|
* @property PropertyGroup $group |
|
43
|
|
|
*/ |
|
44
|
|
|
class Property extends ActiveRecord |
|
45
|
|
|
{ |
|
46
|
|
|
use GetImages; |
|
47
|
|
|
|
|
48
|
|
|
public static $identity_map = []; |
|
49
|
|
|
public static $group_id_to_property_ids = []; |
|
50
|
|
|
private $handlerAdditionalParams = []; |
|
51
|
|
|
public $required; |
|
52
|
|
|
public $interpret_as; |
|
53
|
|
|
public $captcha; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @inheritdoc |
|
57
|
|
|
*/ |
|
58
|
|
|
public function behaviors() |
|
59
|
|
|
{ |
|
60
|
|
|
return [ |
|
61
|
|
|
[ |
|
62
|
|
|
'class' => AttributeBehavior::className(), |
|
|
|
|
|
|
63
|
|
|
'attributes' => [ |
|
64
|
|
|
ActiveRecord::EVENT_BEFORE_INSERT => 'sort_order', |
|
65
|
|
|
], |
|
66
|
|
|
'value' => 0, |
|
67
|
|
|
], |
|
68
|
|
|
[ |
|
69
|
|
|
'class' => ActiveRecordHelper::className(), |
|
|
|
|
|
|
70
|
|
|
], |
|
71
|
|
|
[ |
|
72
|
|
|
'class' => HasProperties::className(), |
|
|
|
|
|
|
73
|
|
|
], |
|
74
|
|
|
]; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @inheritdoc |
|
79
|
|
|
*/ |
|
80
|
|
|
public static function tableName() |
|
81
|
|
|
{ |
|
82
|
|
|
return '{{%property}}'; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @inheritdoc |
|
87
|
|
|
*/ |
|
88
|
|
|
public function rules() |
|
89
|
|
|
{ |
|
90
|
|
|
return [ |
|
91
|
|
|
[['property_group_id', 'name', 'property_handler_id', 'handler_additional_params', 'key'], 'required'], |
|
92
|
|
|
[ |
|
93
|
|
|
[ |
|
94
|
|
|
'property_group_id', |
|
95
|
|
|
'property_handler_id', |
|
96
|
|
|
'has_static_values', |
|
97
|
|
|
'has_slugs_in_values', |
|
98
|
|
|
'is_eav', |
|
99
|
|
|
'is_column_type_stored', |
|
100
|
|
|
'multiple', |
|
101
|
|
|
'sort_order', |
|
102
|
|
|
'alias', |
|
103
|
|
|
], |
|
104
|
|
|
'integer' |
|
105
|
|
|
], |
|
106
|
|
|
[ |
|
107
|
|
|
[ |
|
108
|
|
|
'display_only_on_depended_property_selected', |
|
109
|
|
|
'depends_on_property_id', |
|
110
|
|
|
'depends_on_category_group_id', |
|
111
|
|
|
'hide_other_values_if_selected' |
|
112
|
|
|
], |
|
113
|
|
|
'integer' |
|
114
|
|
|
], |
|
115
|
|
|
[['interpret_as'], 'string'], |
|
116
|
|
|
[['name', 'handler_additional_params', 'depended_property_values', 'value_type', 'mask'], 'string'], |
|
117
|
|
|
[['key'], 'string', 'max' => 20], |
|
118
|
|
|
[['key'], 'match', 'pattern' => '#^[\w]+$#'], |
|
119
|
|
|
[['depends_on_property_id', 'depends_on_category_group_id'], 'default', 'value' => 0], |
|
120
|
|
|
[['required', 'captcha'], 'integer', 'min' => 0, 'max' => 1], |
|
121
|
|
|
[['dont_filter'], 'safe'], |
|
122
|
|
|
[['key'], 'unique', 'targetAttribute' => ['key', 'property_group_id']], |
|
123
|
|
|
]; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @inheritdoc |
|
128
|
|
|
*/ |
|
129
|
|
|
public function attributeLabels() |
|
130
|
|
|
{ |
|
131
|
|
|
return [ |
|
132
|
|
|
'id' => Yii::t('app', 'ID'), |
|
133
|
|
|
'property_group_id' => Yii::t('app', 'Property Group ID'), |
|
134
|
|
|
'name' => Yii::t('app', 'Name'), |
|
135
|
|
|
'key' => Yii::t('app', 'Key'), |
|
136
|
|
|
'value_type' => Yii::t('app', 'Value Type'), |
|
137
|
|
|
'property_handler_id' => Yii::t('app', 'Property Handler ID'), |
|
138
|
|
|
'has_static_values' => Yii::t('app', 'Has Static Values'), |
|
139
|
|
|
'has_slugs_in_values' => Yii::t('app', 'Has Slugs In Values'), |
|
140
|
|
|
'is_eav' => Yii::t('app', 'Is Eav'), |
|
141
|
|
|
'is_column_type_stored' => Yii::t('app', 'Is Column Type Stored'), |
|
142
|
|
|
'multiple' => Yii::t('app', 'Multiple'), |
|
143
|
|
|
'sort_order' => Yii::t('app', 'Sort Order'), |
|
144
|
|
|
'handler_additional_params' => Yii::t('app', 'Handler Additional Params'), |
|
145
|
|
|
'required' => Yii::t('app', 'Required'), |
|
146
|
|
|
'interpret_as' => Yii::t('app', 'Interpret Field As'), |
|
147
|
|
|
'captcha' => Yii::t('app', 'Captcha'), |
|
148
|
|
|
'dont_filter' => Yii::t('app', 'Don\'t use in filtration'), |
|
149
|
|
|
'hide_other_values_if_selected' => Yii::t('app', 'Hide Other Values If Selected'), |
|
150
|
|
|
'display_only_on_depended_property_selected' => Yii::t('app', 'Display Only On Depended Property Selected'), |
|
151
|
|
|
'depends_on_property_id' => Yii::t('app', 'Depends On Property Id'), |
|
152
|
|
|
'depended_property_values' => Yii::t('app', 'Depended Property Values'), |
|
153
|
|
|
'depends_on_category_group_id' => Yii::t('app', 'Depends On Category Group Id'), |
|
154
|
|
|
'mask' => Yii::t('app', 'Mask'), |
|
155
|
|
|
'alias' => Yii::t('app', 'Alias'), |
|
156
|
|
|
]; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Search tasks |
|
161
|
|
|
* @param $params |
|
162
|
|
|
* @return ActiveDataProvider |
|
163
|
|
|
*/ |
|
164
|
|
|
public function search($params) |
|
165
|
|
|
{ |
|
166
|
|
|
/* @var $query \yii\db\ActiveQuery */ |
|
167
|
|
|
$query = static::find()->where(['property_group_id' => $this->property_group_id]); |
|
168
|
|
|
$dataProvider = new ActiveDataProvider([ |
|
169
|
|
|
'query' => $query, |
|
170
|
|
|
'pagination' => [ |
|
171
|
|
|
'pageSize' => 10, |
|
172
|
|
|
], |
|
173
|
|
|
]); |
|
174
|
|
|
if (!($this->load($params))) { |
|
175
|
|
|
return $dataProvider; |
|
176
|
|
|
} |
|
177
|
|
|
$query->andFilterWhere(['id' => $this->id]); |
|
178
|
|
|
$query->andFilterWhere(['like', 'name', $this->name]); |
|
179
|
|
|
$query->andFilterWhere(['like', 'key', $this->key]); |
|
180
|
|
|
$query->andFilterWhere(['property_handler_id' => $this->property_handler_id]); |
|
181
|
|
|
$query->andFilterWhere(['has_static_values' => $this->has_static_values]); |
|
182
|
|
|
$query->andFilterWhere(['has_slugs_in_values' => $this->has_slugs_in_values]); |
|
183
|
|
|
$query->andFilterWhere(['is_eav' => $this->is_eav]); |
|
184
|
|
|
$query->andFilterWhere(['is_column_type_stored' => $this->is_column_type_stored]); |
|
185
|
|
|
$query->andFilterWhere(['multiple' => $this->multiple]); |
|
186
|
|
|
return $dataProvider; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @return \yii\db\ActiveQuery |
|
191
|
|
|
*/ |
|
192
|
|
|
public function getGroup() |
|
193
|
|
|
{ |
|
194
|
|
|
return $this->hasOne(PropertyGroup::className(), ['id' => 'property_group_id']); |
|
|
|
|
|
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @return PropertyHandler |
|
199
|
|
|
*/ |
|
200
|
|
|
public function getHandler() |
|
201
|
|
|
{ |
|
202
|
|
|
// return $this->hasOne(PropertyHandler::className(), ['id' => 'property_handler_id']); |
|
203
|
|
|
return PropertyHandler::findById($this->property_handler_id); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Возвращает модель по ID с использованием IdentityMap |
|
208
|
|
|
* @param int $id |
|
209
|
|
|
* @return null|Property |
|
210
|
|
|
*/ |
|
211
|
|
View Code Duplication |
public static function findById($id) |
|
212
|
|
|
{ |
|
213
|
|
|
if (!isset(static::$identity_map[$id])) { |
|
214
|
|
|
$cacheKey = "Property:$id"; |
|
215
|
|
|
if (false === $prop = Yii::$app->cache->get($cacheKey)) { |
|
216
|
|
|
if (null === $prop = static::findOne($id)) { |
|
217
|
|
|
return null; |
|
218
|
|
|
} |
|
219
|
|
|
Yii::$app->cache->set( |
|
220
|
|
|
$cacheKey, |
|
221
|
|
|
$prop, |
|
222
|
|
|
0, |
|
223
|
|
|
new TagDependency([ |
|
224
|
|
|
'tags' => [ |
|
225
|
|
|
ActiveRecordHelper::getObjectTag($prop, $id) |
|
|
|
|
|
|
226
|
|
|
], |
|
227
|
|
|
]) |
|
228
|
|
|
); |
|
229
|
|
|
} |
|
230
|
|
|
static::$identity_map[$id] = $prop; |
|
231
|
|
|
} |
|
232
|
|
|
return static::$identity_map[$id]; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* @param $group_id |
|
237
|
|
|
* @return null|Property[] |
|
238
|
|
|
*/ |
|
239
|
|
|
public static function getForGroupId($group_id) |
|
240
|
|
|
{ |
|
241
|
|
|
if (!isset(static::$group_id_to_property_ids[$group_id])) { |
|
242
|
|
|
$cacheKey = "PropsForGroup:$group_id"; |
|
243
|
|
|
if (false === $props = Yii::$app->cache->get($cacheKey)) { |
|
244
|
|
|
if (null !== $props = static::find()->where(['property_group_id' => $group_id])->orderBy( |
|
245
|
|
|
'sort_order' |
|
246
|
|
|
)->all() |
|
247
|
|
|
) { |
|
248
|
|
|
Yii::$app->cache->set( |
|
249
|
|
|
$cacheKey, |
|
250
|
|
|
$props, |
|
251
|
|
|
0, |
|
252
|
|
|
new TagDependency([ |
|
253
|
|
|
'tags' => [ |
|
254
|
|
|
ActiveRecordHelper::getObjectTag( |
|
255
|
|
|
PropertyGroup::className(), |
|
|
|
|
|
|
256
|
|
|
$group_id |
|
257
|
|
|
) |
|
258
|
|
|
], |
|
259
|
|
|
]) |
|
260
|
|
|
); |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
static::$group_id_to_property_ids[$group_id] = []; |
|
264
|
|
|
foreach ($props as $property) { |
|
265
|
|
|
static::$identity_map[$property->id] = $property; |
|
266
|
|
|
static::$group_id_to_property_ids[$group_id][] = $property->id; |
|
267
|
|
|
} |
|
268
|
|
|
return $props; |
|
269
|
|
|
} |
|
270
|
|
|
$properties = []; |
|
271
|
|
|
foreach (static::$group_id_to_property_ids[$group_id] as $property_id) { |
|
272
|
|
|
$properties[] = static::findById($property_id); |
|
273
|
|
|
} |
|
274
|
|
|
return $properties; |
|
|
|
|
|
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* @param $form |
|
279
|
|
|
* @param $model |
|
280
|
|
|
* @param $values |
|
281
|
|
|
* @param string $renderType |
|
282
|
|
|
* @return string |
|
283
|
|
|
*/ |
|
284
|
|
|
public function handler($form, $model, $values, $renderType = 'frontend_render_view') |
|
285
|
|
|
{ |
|
286
|
|
|
$handler = $this->handler; |
|
|
|
|
|
|
287
|
|
|
if (null === $handler) { |
|
288
|
|
|
return ''; |
|
289
|
|
|
} |
|
290
|
|
|
$handler = PropertyHandlers::createHandler($handler); |
|
291
|
|
|
if (null === $handler) { |
|
292
|
|
|
return ''; |
|
293
|
|
|
} |
|
294
|
|
|
return $handler->render($this, $model, $values, $form, $renderType); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* @inheritdoc |
|
299
|
|
|
*/ |
|
300
|
|
|
public function afterFind() |
|
301
|
|
|
{ |
|
302
|
|
|
parent::afterFind(); |
|
303
|
|
|
$this->handlerAdditionalParams = Json::decode($this->handler_additional_params); |
|
|
|
|
|
|
304
|
|
|
$this->required = isset($this->handlerAdditionalParams['rules']) && is_array( |
|
305
|
|
|
$this->handlerAdditionalParams['rules'] |
|
306
|
|
|
) && in_array('required', $this->handlerAdditionalParams['rules']); |
|
307
|
|
|
$this->interpret_as = isset($this->handlerAdditionalParams['interpret_as']) ? $this->handlerAdditionalParams['interpret_as'] : 0; |
|
308
|
|
|
if (isset($this->handlerAdditionalParams['rules']) && is_array($this->handlerAdditionalParams['rules'])) { |
|
309
|
|
|
foreach ($this->handlerAdditionalParams['rules'] as $rule) { |
|
310
|
|
|
if (is_array($rule)) { |
|
311
|
|
|
if (in_array('captcha', $rule, true)) { |
|
312
|
|
|
$this->captcha = true; |
|
313
|
|
|
} |
|
314
|
|
|
} else { |
|
315
|
|
|
switch ($rule) { |
|
316
|
|
|
case 'required': |
|
317
|
|
|
$this->required = true; |
|
318
|
|
|
break; |
|
319
|
|
|
} |
|
320
|
|
|
} |
|
321
|
|
|
} |
|
322
|
|
|
} |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
/** |
|
326
|
|
|
* @inheritdoc |
|
327
|
|
|
* @param bool $insert |
|
328
|
|
|
* @return bool |
|
329
|
|
|
*/ |
|
330
|
|
|
public function beforeSave($insert) |
|
331
|
|
|
{ |
|
332
|
|
|
if (!parent::beforeSave($insert)) { |
|
333
|
|
|
return false; |
|
334
|
|
|
} |
|
335
|
|
|
$handlerAdditionalParams = $this->isNewRecord ? [] : Json::decode($this->handler_additional_params); |
|
336
|
|
|
$handlerRules = []; |
|
337
|
|
|
if (1 === intval($this->required)) { |
|
338
|
|
|
$handlerRules[] = 'required'; |
|
339
|
|
|
} |
|
340
|
|
|
if (PropertyHandler::findByName('File') === intval($this->property_handler_id)) { |
|
341
|
|
|
if (1 === intval($this->multiple)) { |
|
342
|
|
|
$handlerRules[] = ['file', 'maxFiles' => 0]; |
|
343
|
|
|
} else { |
|
344
|
|
|
$handlerRules[] = ['file', 'maxFiles' => 1]; |
|
345
|
|
|
} |
|
346
|
|
|
} |
|
347
|
|
|
if (1 === intval($this->captcha)) { |
|
348
|
|
|
$handlerRules[] = ['captcha', 'captchaAction' => '/default/captcha']; |
|
349
|
|
|
} |
|
350
|
|
|
$handlerAdditionalParams['interpret_as'] = $this->interpret_as; |
|
351
|
|
|
$handlerAdditionalParams['rules'] = $handlerRules; |
|
352
|
|
|
$this->handlerAdditionalParams = $handlerAdditionalParams; |
|
|
|
|
|
|
353
|
|
|
$this->handler_additional_params = Json::encode($handlerAdditionalParams); |
|
354
|
|
|
return true; |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
/** |
|
358
|
|
|
* |
|
359
|
|
|
*/ |
|
360
|
|
|
public function invalidateModelCache() |
|
361
|
|
|
{ |
|
362
|
|
|
TagDependency::invalidate( |
|
363
|
|
|
Yii::$app->cache, |
|
364
|
|
|
[ |
|
365
|
|
|
ActiveRecordHelper::getObjectTag( |
|
366
|
|
|
PropertyGroup::className(), |
|
|
|
|
|
|
367
|
|
|
$this->property_group_id |
|
368
|
|
|
), |
|
369
|
|
|
ActiveRecordHelper::getObjectTag(Property::className(), $this->id) |
|
|
|
|
|
|
370
|
|
|
] |
|
371
|
|
|
); |
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
|
|
/** |
|
375
|
|
|
* @param bool $insert |
|
376
|
|
|
* @param array $changedAttributes |
|
377
|
|
|
*/ |
|
378
|
|
|
public function afterSave($insert, $changedAttributes) |
|
379
|
|
|
{ |
|
380
|
|
|
// @todo clear table schema |
|
381
|
|
|
$this->invalidateModelCache(); |
|
382
|
|
|
parent::afterSave($insert, $changedAttributes); |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
/** |
|
386
|
|
|
* @return bool |
|
387
|
|
|
*/ |
|
388
|
|
|
public function beforeDelete() |
|
389
|
|
|
{ |
|
390
|
|
|
$this->invalidateModelCache(); |
|
391
|
|
|
return parent::beforeDelete(); |
|
392
|
|
|
} |
|
393
|
|
|
|
|
394
|
|
|
public function afterDelete() |
|
395
|
|
|
{ |
|
396
|
|
|
$object = BaseObject::findById($this->group->object_id); |
|
397
|
|
|
$staticValues = PropertyStaticValues::find()->where(['property_id' => $this->id])->all(); |
|
398
|
|
|
foreach ($staticValues as $psv) { |
|
399
|
|
|
$psv->delete(); |
|
400
|
|
|
} |
|
401
|
|
|
if (null !== $object) { |
|
402
|
|
|
if ($this->is_eav) { |
|
403
|
|
|
Yii::$app->db->createCommand()->delete( |
|
404
|
|
|
$object->eav_table_name, |
|
405
|
|
|
['key' => $this->key, 'property_group_id' => $this->group->id] |
|
406
|
|
|
)->execute(); |
|
407
|
|
|
} |
|
408
|
|
|
if ($this->is_column_type_stored) { |
|
409
|
|
|
Yii::$app->db->createCommand()->dropColumn($object->column_properties_table_name, $this->key)->execute(); |
|
410
|
|
|
// if ($object->object_class == Form::className()) { |
|
411
|
|
|
// $submissionObject = Object::getForClass(Submission::className()); |
|
412
|
|
|
// Yii::$app->db->createCommand() |
|
413
|
|
|
// ->dropColumn($submissionObject->column_properties_table_name, $this->key) |
|
414
|
|
|
// ->execute(); |
|
415
|
|
|
// } |
|
416
|
|
|
} |
|
417
|
|
|
} |
|
418
|
|
|
FilterSets::deleteAll(['property_id' => $this->id]); |
|
419
|
|
|
parent::afterDelete(); |
|
420
|
|
|
} |
|
421
|
|
|
|
|
422
|
|
|
/** |
|
423
|
|
|
* @param $name |
|
424
|
|
|
* @return null|mixed |
|
425
|
|
|
*/ |
|
426
|
|
|
public function getAdditionalParam($name) |
|
427
|
|
|
{ |
|
428
|
|
|
if (isset($this->handlerAdditionalParams[$name])) { |
|
429
|
|
|
return $this->handlerAdditionalParams[$name]; |
|
430
|
|
|
} |
|
431
|
|
|
return null; |
|
432
|
|
|
} |
|
433
|
|
|
|
|
434
|
|
|
/** |
|
435
|
|
|
* @return array |
|
|
|
|
|
|
436
|
|
|
*/ |
|
437
|
|
|
public static function getAliases() |
|
438
|
|
|
{ |
|
439
|
|
|
return [ |
|
440
|
|
|
0 => Yii::t('app', 'Not selected'), |
|
441
|
|
|
1 => 'date', |
|
442
|
|
|
2 => 'ip', |
|
443
|
|
|
3 => 'url', |
|
444
|
|
|
4 => 'email', |
|
445
|
|
|
]; |
|
446
|
|
|
} |
|
447
|
|
|
} |
|
448
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.