1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\properties; |
4
|
|
|
|
5
|
|
|
use app\models\BaseObject; |
6
|
|
|
use app\models\ObjectPropertyGroup; |
7
|
|
|
use app\models\Property; |
8
|
|
|
use app\models\PropertyGroup; |
9
|
|
|
use app\models\PropertyStaticValues; |
10
|
|
|
use devgroup\TagDependencyHelper\ActiveRecordHelper; |
11
|
|
|
use Yii; |
12
|
|
|
use yii\base\Widget; |
13
|
|
|
use yii\caching\TagDependency; |
14
|
|
|
use yii\db\ActiveRecord; |
15
|
|
|
use yii\db\Query; |
16
|
|
|
use yii\helpers\ArrayHelper; |
17
|
|
|
use yii\widgets\ActiveForm; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class PropertiesWidget |
21
|
|
|
* @property ActiveRecord $model |
22
|
|
|
* @property ActiveForm $form |
23
|
|
|
* @property BaseObject $object |
24
|
|
|
* @property array $objectPropertyGroups |
25
|
|
|
* @property array $propertyGroupsToAdd |
26
|
|
|
* @property string $viewFile |
27
|
|
|
* @package app\properties |
28
|
|
|
*/ |
29
|
|
|
class PropertiesWidget extends Widget |
30
|
|
|
{ |
31
|
|
|
private $object; |
32
|
|
|
private $objectPropertyGroups = []; |
33
|
|
|
private $propertyGroupsToAdd = []; |
34
|
|
|
public $form; |
35
|
|
|
public $model; |
36
|
|
|
public $viewFile = 'properties-widget'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @inheritdoc |
40
|
|
|
*/ |
41
|
|
|
public function run() |
42
|
|
|
{ |
43
|
|
|
$this->object = BaseObject::getForClass(get_class($this->model)); |
44
|
|
|
$cacheKey = 'PropertiesWidget: ' . get_class($this->model) . ':' . $this->model->id; |
45
|
|
|
$data = Yii::$app->cache->get($cacheKey); |
46
|
|
|
if ($data === false) { |
47
|
|
|
$this->objectPropertyGroups = ObjectPropertyGroup::getForModel($this->model); |
48
|
|
|
$addedPropertyGroupsIds = []; |
49
|
|
|
foreach ($this->objectPropertyGroups as $opg) { |
50
|
|
|
$addedPropertyGroupsIds[] = $opg->property_group_id; |
51
|
|
|
} |
52
|
|
|
$restPg = (new Query()) |
53
|
|
|
->select('id, name') |
54
|
|
|
->from(PropertyGroup::tableName()) |
55
|
|
|
->where( |
56
|
|
|
[ |
57
|
|
|
'object_id' => $this->object->id, |
58
|
|
|
] |
59
|
|
|
)->andWhere( |
60
|
|
|
[ |
61
|
|
|
'not in', 'id', $addedPropertyGroupsIds, |
62
|
|
|
] |
63
|
|
|
)->orderBy('sort_order') |
64
|
|
|
->all(); |
65
|
|
|
$this->propertyGroupsToAdd = ArrayHelper::map($restPg, 'id', 'name'); |
66
|
|
|
Yii::$app->cache->set( |
67
|
|
|
$cacheKey, |
68
|
|
|
[ |
69
|
|
|
'objectPropertyGroups' => $this->objectPropertyGroups, |
70
|
|
|
'propertyGroupsToAdd' => $this->propertyGroupsToAdd, |
71
|
|
|
], |
72
|
|
|
86400, |
73
|
|
|
new TagDependency( |
74
|
|
|
[ |
75
|
|
|
'tags' => [ |
76
|
|
|
ActiveRecordHelper::getCommonTag(get_class($this->model)), |
77
|
|
|
ActiveRecordHelper::getCommonTag(PropertyGroup::className()), |
|
|
|
|
78
|
|
|
ActiveRecordHelper::getCommonTag(Property::className()), |
|
|
|
|
79
|
|
|
], |
80
|
|
|
] |
81
|
|
|
) |
82
|
|
|
); |
83
|
|
|
} else { |
84
|
|
|
$this->objectPropertyGroups = $data['objectPropertyGroups']; |
85
|
|
|
$this->propertyGroupsToAdd = $data['propertyGroupsToAdd']; |
86
|
|
|
} |
87
|
|
|
return $this->render( |
88
|
|
|
$this->viewFile, |
89
|
|
|
[ |
90
|
|
|
'model' => $this->model, |
91
|
|
|
'object' => $this->object, |
92
|
|
|
'object_property_groups' => $this->objectPropertyGroups, |
93
|
|
|
'property_groups_to_add' => $this->propertyGroupsToAdd, |
94
|
|
|
'form' => $this->form, |
95
|
|
|
'widget_id' => $this->getId(), |
96
|
|
|
] |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
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.