1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\models; |
4
|
|
|
|
5
|
|
|
use app\properties\HasProperties; |
6
|
|
|
use devgroup\TagDependencyHelper\ActiveRecordHelper; |
7
|
|
|
use Yii; |
8
|
|
|
use yii\behaviors\AttributeBehavior; |
9
|
|
|
use yii\caching\TagDependency; |
10
|
|
|
use yii\data\ActiveDataProvider; |
11
|
|
|
use yii\db\ActiveRecord; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* This is the model class for table "property_group". |
15
|
|
|
* |
16
|
|
|
* @property integer $id |
17
|
|
|
* @property integer $object_id |
18
|
|
|
* @property string $name |
19
|
|
|
* @property integer $sort_order |
20
|
|
|
* @property integer $is_internal |
21
|
|
|
* @property integer $hidden_group_title |
22
|
|
|
*/ |
23
|
|
|
class PropertyGroup extends ActiveRecord |
24
|
|
|
{ |
25
|
|
|
private static $identity_map = []; |
26
|
|
|
private static $groups_by_object_id = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @inheritdoc |
30
|
|
|
*/ |
31
|
|
View Code Duplication |
public function behaviors() |
32
|
|
|
{ |
33
|
|
|
return [ |
34
|
|
|
[ |
35
|
|
|
'class' => AttributeBehavior::className(), |
|
|
|
|
36
|
|
|
'attributes' => [ |
37
|
|
|
ActiveRecord::EVENT_BEFORE_INSERT => 'sort_order', |
38
|
|
|
], |
39
|
|
|
'value' => 0, |
40
|
|
|
], |
41
|
|
|
[ |
42
|
|
|
'class' => ActiveRecordHelper::className(), |
|
|
|
|
43
|
|
|
], |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
/** |
47
|
|
|
* @inheritdoc |
48
|
|
|
*/ |
49
|
|
|
public static function tableName() |
50
|
|
|
{ |
51
|
|
|
return '{{%property_group}}'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritdoc |
56
|
|
|
*/ |
57
|
|
|
public function rules() |
58
|
|
|
{ |
59
|
|
|
return [ |
60
|
|
|
[['object_id', 'name'], 'required'], |
61
|
|
|
[['object_id', 'sort_order', 'is_internal', 'hidden_group_title'], 'integer'], |
62
|
|
|
[['name'], 'string'] |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritdoc |
68
|
|
|
*/ |
69
|
|
View Code Duplication |
public function attributeLabels() |
70
|
|
|
{ |
71
|
|
|
return [ |
72
|
|
|
'id' => Yii::t('app', 'ID'), |
73
|
|
|
'object_id' => Yii::t('app', 'Object ID'), |
74
|
|
|
'name' => Yii::t('app', 'Name'), |
75
|
|
|
'sort_order' => Yii::t('app', 'Sort Order'), |
76
|
|
|
'is_internal' => Yii::t('app', 'Is Internal'), |
77
|
|
|
'hidden_group_title' => Yii::t('app', 'Hidden Group Title'), |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Relation to \app\models\Object |
83
|
|
|
* @return \yii\db\ActiveQuery |
84
|
|
|
*/ |
85
|
|
|
public function getObject() |
86
|
|
|
{ |
87
|
|
|
return $this->hasOne(BaseObject::className(), ['id' => 'object_id']); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Search tasks |
92
|
|
|
* @param $params |
93
|
|
|
* @return ActiveDataProvider |
94
|
|
|
*/ |
95
|
|
|
public function search($params) |
96
|
|
|
{ |
97
|
|
|
/* @var $query \yii\db\ActiveQuery */ |
98
|
|
|
$query = self::find(); |
99
|
|
|
$dataProvider = new ActiveDataProvider( |
100
|
|
|
[ |
101
|
|
|
'query' => $query, |
102
|
|
|
'pagination' => [ |
103
|
|
|
'pageSize' => 10, |
104
|
|
|
], |
105
|
|
|
] |
106
|
|
|
); |
107
|
|
|
if (!($this->load($params))) { |
108
|
|
|
return $dataProvider; |
109
|
|
|
} |
110
|
|
|
$query->andFilterWhere(['id' => $this->id]); |
111
|
|
|
$query->andFilterWhere(['like', 'name', $this->name]); |
112
|
|
|
$query->andFilterWhere(['object_id' => $this->object_id]); |
113
|
|
|
$query->andFilterWhere(['is_internal' => $this->is_internal]); |
114
|
|
|
$query->andFilterWhere(['hidden_group_title' => $this->hidden_group_title]); |
115
|
|
|
return $dataProvider; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Возвращает модель по ID с использованием IdentityMap |
120
|
|
|
* |
121
|
|
|
* @param int $id |
122
|
|
|
* @return null|PropertyGroup |
123
|
|
|
*/ |
124
|
|
View Code Duplication |
public static function findById($id) |
125
|
|
|
{ |
126
|
|
|
if (!isset(static::$identity_map[$id])) { |
127
|
|
|
$cacheKey = "PropertyGroup:$id"; |
128
|
|
|
if (false === $group = Yii::$app->cache->get($cacheKey)) { |
129
|
|
|
if (null !== $group = static::findOne($id)) { |
130
|
|
|
Yii::$app->cache->set( |
131
|
|
|
$cacheKey, |
132
|
|
|
$group, |
133
|
|
|
0, |
134
|
|
|
new TagDependency( |
135
|
|
|
[ |
136
|
|
|
'tags' => [ |
137
|
|
|
ActiveRecordHelper::getObjectTag(static::className(), $id), |
|
|
|
|
138
|
|
|
], |
139
|
|
|
] |
140
|
|
|
) |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
static::$identity_map[$id] = $group; |
145
|
|
|
} |
146
|
|
|
return static::$identity_map[$id]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Relation to properties |
151
|
|
|
* @return \yii\db\ActiveQuery |
152
|
|
|
*/ |
153
|
|
|
public function getProperties() |
154
|
|
|
{ |
155
|
|
|
return $this->hasMany(Property::className(), ['property_group_id' => 'id'])->orderBy('sort_order'); |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param $object_id |
160
|
|
|
* @param bool $withProperties |
161
|
|
|
* @return PropertyGroup[] |
162
|
|
|
*/ |
163
|
|
|
public static function getForObjectId($object_id, $withProperties = false) |
164
|
|
|
{ |
165
|
|
|
if (null === $object_id) { |
166
|
|
|
return []; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if (!isset(static::$groups_by_object_id[$object_id])) { |
170
|
|
|
$cacheKey = 'PropertyGroup:objectId:'.$object_id; |
171
|
|
|
static::$groups_by_object_id[$object_id] = Yii::$app->cache->get($cacheKey); |
172
|
|
|
if (!is_array(static::$groups_by_object_id[$object_id])) { |
173
|
|
|
$query = static::find() |
174
|
|
|
->where(['object_id'=>$object_id]) |
175
|
|
|
->orderBy('sort_order'); |
176
|
|
|
if ($withProperties === true) { |
177
|
|
|
$query = $query->with('properties'); |
178
|
|
|
} |
179
|
|
|
static::$groups_by_object_id[$object_id] = $query->all(); |
180
|
|
|
if (null !== $object = BaseObject::findById($object_id)) { |
181
|
|
|
$tags = [ |
182
|
|
|
ActiveRecordHelper::getObjectTag($object, $object_id) |
183
|
|
|
]; |
184
|
|
|
foreach (static::$groups_by_object_id[$object_id] as $propertyGroup){ |
185
|
|
|
$tags[] = ActiveRecordHelper::getObjectTag($propertyGroup, $propertyGroup->id); |
186
|
|
|
if ($withProperties === true) { |
187
|
|
|
foreach ($propertyGroup->properties as $prop) { |
188
|
|
|
if (isset(Property::$group_id_to_property_ids[$propertyGroup->id]) === false) { |
189
|
|
|
Property::$group_id_to_property_ids[$propertyGroup->id]=[]; |
190
|
|
|
} |
191
|
|
|
Property::$group_id_to_property_ids[$propertyGroup->id][] = $prop->id; |
192
|
|
|
Property::$identity_map[$prop->id] = $prop; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
Yii::$app->cache->set( |
198
|
|
|
$cacheKey, |
199
|
|
|
static::$groups_by_object_id[$object_id], |
200
|
|
|
0, |
201
|
|
|
new TagDependency( |
202
|
|
|
[ |
203
|
|
|
'tags' => $tags, |
204
|
|
|
] |
205
|
|
|
) |
206
|
|
|
); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
return static::$groups_by_object_id[$object_id]; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param int $object_id |
215
|
|
|
* @param int $object_model_id |
216
|
|
|
* @return null|\yii\db\ActiveRecord[] |
217
|
|
|
*/ |
218
|
|
|
public static function getForModel($object_id, $object_model_id) |
219
|
|
|
{ |
220
|
|
|
$cacheKey = "PropertyGroupBy:$object_id:$object_model_id"; |
221
|
|
|
if (false === $groups = Yii::$app->cache->get($cacheKey)) { |
222
|
|
|
$group_ids = ObjectPropertyGroup::find() |
223
|
|
|
->select('property_group_id') |
224
|
|
|
->where([ |
225
|
|
|
'object_id' => $object_id, |
226
|
|
|
'object_model_id' => $object_model_id, |
227
|
|
|
])->column(); |
228
|
|
|
if (null === $group_ids) { |
229
|
|
|
return null; |
230
|
|
|
} |
231
|
|
|
if (null === $groups = static::find()->where(['in', 'id', $group_ids])->all()) { |
232
|
|
|
return null; |
233
|
|
|
} |
234
|
|
|
if (null !== $object = BaseObject::findById($object_id)) { |
235
|
|
|
Yii::$app->cache->set( |
236
|
|
|
$cacheKey, |
237
|
|
|
$groups, |
238
|
|
|
0, |
239
|
|
|
new TagDependency( |
240
|
|
|
[ |
241
|
|
|
'tags' => [ |
242
|
|
|
ActiveRecordHelper::getObjectTag($object, $object_id), |
243
|
|
|
ActiveRecordHelper::getObjectTag($object->object_class, $object_model_id), |
244
|
|
|
], |
245
|
|
|
] |
246
|
|
|
) |
247
|
|
|
); |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
return $groups; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
public function beforeDelete() |
254
|
|
|
{ |
255
|
|
|
if (!parent::beforeDelete()) { |
256
|
|
|
return false; |
257
|
|
|
} |
258
|
|
|
$properties = Property::findAll(['property_group_id' => $this->id]); |
259
|
|
|
foreach ($properties as $prop) { |
260
|
|
|
$prop->delete(); |
261
|
|
|
} |
262
|
|
|
return true; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
public function afterDelete() |
266
|
|
|
{ |
267
|
|
|
ObjectPropertyGroup::deleteAll(['property_group_id' => $this->id]); |
268
|
|
|
parent::afterDelete(); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @param ActiveRecord|HasProperties $model |
|
|
|
|
273
|
|
|
* @param string $idAttribute |
274
|
|
|
* @return bool |
275
|
|
|
*/ |
276
|
|
|
public function appendToObjectModel(ActiveRecord $model, $idAttribute = 'id') |
277
|
|
|
{ |
278
|
|
|
$object = BaseObject::getForClass($model::className()); |
|
|
|
|
279
|
|
|
if (null === $object || !$model->hasAttribute($idAttribute)) { |
280
|
|
|
return false; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
$link = new ObjectPropertyGroup(); |
284
|
|
|
$link->object_id = $object->id; |
285
|
|
|
$link->object_model_id = $model->$idAttribute; |
286
|
|
|
$link->property_group_id = $this->id; |
287
|
|
|
|
288
|
|
|
$result = $link->save(); |
289
|
|
|
$model->updatePropertyGroupsInformation(); |
290
|
|
|
|
291
|
|
|
return $result; |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
?> |
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.