1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\caching\TagDependency; |
7
|
|
|
use yii\db\ActiveRecord; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This is the model class for table "view_object". |
11
|
|
|
* |
12
|
|
|
* @property integer $id |
13
|
|
|
* @property integer $object_id |
14
|
|
|
* @property integer $object_model_id |
15
|
|
|
* @property integer $view_id |
16
|
|
|
* @property integer $child_view_id |
17
|
|
|
*/ |
18
|
|
|
class ViewObject extends ActiveRecord |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @inheritdoc |
22
|
|
|
*/ |
23
|
|
|
public static function tableName() |
24
|
|
|
{ |
25
|
|
|
return '{{%view_object}}'; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @inheritdoc |
30
|
|
|
*/ |
31
|
|
|
public function rules() |
32
|
|
|
{ |
33
|
|
|
return [ |
34
|
|
|
[['view_id', 'child_view_id'], 'default', 'value' => 1], |
35
|
|
|
[['object_id', 'object_model_id', 'view_id', 'child_view_id'], 'required'], |
36
|
|
|
[['object_id', 'object_model_id', 'view_id', 'child_view_id'], 'integer'] |
37
|
|
|
]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritdoc |
42
|
|
|
*/ |
43
|
|
|
public function attributeLabels() |
44
|
|
|
{ |
45
|
|
|
return [ |
46
|
|
|
'id' => Yii::t('app', 'ID'), |
47
|
|
|
'object_id' => Yii::t('app', 'Object ID'), |
48
|
|
|
'object_model_id' => Yii::t('app', 'Object Model ID'), |
49
|
|
|
'view_id' => Yii::t('app', 'View'), |
50
|
|
|
'child_view_id' => Yii::t('app', 'Child view') |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Поиск представления по модели |
56
|
|
|
* |
57
|
|
|
* @param BaseObject $model |
|
|
|
|
58
|
|
|
* @return string|null Возвращает имя файла или null, если ничего не найдено |
59
|
|
|
*/ |
60
|
|
|
public static function getViewByModel($model = null, $childView = false) |
61
|
|
|
{ |
62
|
|
|
if ((null === $model) || !is_object($model)) { |
63
|
|
|
return null; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (null === $object = BaseObject::getForClass($model::className())) { |
|
|
|
|
67
|
|
|
return null; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$cacheKey = "View:Object:ModelId" . $object->id . ":" . $model->id; |
71
|
|
|
$viewObject = Yii::$app->cache->get($cacheKey); |
72
|
|
|
if ($viewObject === false) { |
73
|
|
|
|
74
|
|
|
$viewObject = static::find() |
75
|
|
|
->where( |
76
|
|
|
[ |
77
|
|
|
'object_id' => $object->id, |
78
|
|
|
'object_model_id' => $model->id, |
79
|
|
|
] |
80
|
|
|
)->one(); |
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
if ($viewObject !== null) { |
84
|
|
|
Yii::$app->cache->set( |
85
|
|
|
$cacheKey, |
86
|
|
|
$viewObject, |
87
|
|
|
86400, |
88
|
|
|
new TagDependency([ |
89
|
|
|
'tags' => [ |
90
|
|
|
\devgroup\TagDependencyHelper\ActiveRecordHelper::getObjectTag($viewObject, $viewObject->id), |
91
|
|
|
\devgroup\TagDependencyHelper\ActiveRecordHelper::getObjectTag($object, $object->id), |
92
|
|
|
\devgroup\TagDependencyHelper\ActiveRecordHelper::getObjectTag($model, $model->id) |
93
|
|
|
] |
94
|
|
|
]) |
95
|
|
|
); |
96
|
|
|
return View::getViewById($childView ? $viewObject->child_view_id : $viewObject->view_id); |
97
|
|
|
} else { |
98
|
|
|
return null; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Поиск связи по модели |
105
|
|
|
* |
106
|
|
|
* @param \yii\db\ActiveRecord $model |
|
|
|
|
107
|
|
|
* @param boolean $forceDefault Флаг для принудительного возврата модели |
108
|
|
|
* @return \yii\db\ActiveRecord|null Возвращает модель или null, если ничего не найдено |
|
|
|
|
109
|
|
|
*/ |
110
|
|
|
public static function getByModel($model = null, $forceDefault = false) |
111
|
|
|
{ |
112
|
|
|
if ((null === $model) || !is_object($model)) { |
113
|
|
|
return null; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if (null === $object = BaseObject::getForClass($model::className())) { |
|
|
|
|
117
|
|
|
return null; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if ( |
121
|
|
|
null === $result = static::find()->where( |
122
|
|
|
[ |
123
|
|
|
'object_id' => $object->id, |
124
|
|
|
'object_model_id' => $model->id, |
125
|
|
|
] |
126
|
|
|
)->one() |
127
|
|
|
) { |
128
|
|
|
if ($forceDefault) { |
129
|
|
|
$result = new static; |
130
|
|
|
$result->object_id = $object->id; |
131
|
|
|
$result->object_model_id = $model->id; |
132
|
|
|
$result->view_id = 1; |
133
|
|
|
$result->child_view_id = 1; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $result; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public static function deleteByViewId($view_id = null) |
141
|
|
|
{ |
142
|
|
|
if (null === $view_id) { |
143
|
|
|
return null; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return static::deleteAll(['view_id' => $view_id]); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.