1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\modules\seo\models; |
4
|
|
|
|
5
|
|
|
use app\backend\BackendModule; |
6
|
|
|
use app\backend\components\BackendController; |
7
|
|
|
use devgroup\TagDependencyHelper\ActiveRecordHelper; |
8
|
|
|
use yii\base\Event; |
9
|
|
|
use yii\caching\TagDependency; |
10
|
|
|
use yii\data\ActiveDataProvider; |
11
|
|
|
use yii\db\ActiveRecord; |
12
|
|
|
use yii\web\View; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* This is the model class for table "seo_counter". |
16
|
|
|
* |
17
|
|
|
* @property integer $id |
18
|
|
|
* @property string $name |
19
|
|
|
* @property string $description |
20
|
|
|
* @property string $code |
21
|
|
|
* @property int $position see POSITION_* constants for available options |
22
|
|
|
*/ |
23
|
|
|
class Counter extends ActiveRecord |
24
|
|
|
{ |
25
|
|
|
const POSITION_AT_END_OF_BODY = 0; |
26
|
|
|
const POSITION_AT_BEGIN_OF_BODY = 1; |
27
|
|
|
const POSITION_AT_HEAD = 2; |
28
|
|
|
|
29
|
|
|
const MESSAGE_END_OF_BODY_TAG = "End of body tag"; |
30
|
|
|
const MESSAGE_BEGINNING_OF_BODY_TAG = "Beginning of body tag"; |
31
|
|
|
const MESSAGE_INSIDE_OF_HEAD_TAG = "Inside of head tag"; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @inheritdoc |
35
|
|
|
*/ |
36
|
|
|
public static function tableName() |
37
|
|
|
{ |
38
|
|
|
return '{{%seo_counter}}'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param $counter |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
private static function renderCounter($counter) |
46
|
|
|
{ |
47
|
|
|
return |
48
|
|
|
"\n<!-- {$counter->name} counter -->\n" |
49
|
|
|
. "{$counter->code}" |
50
|
|
|
. "\n<!-- /{$counter->name} counter -->\n"; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritdoc |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public function rules() |
57
|
|
|
{ |
58
|
|
|
return [ |
59
|
|
|
[['description', 'code'], 'string'], |
60
|
|
|
[['code'], 'required'], |
61
|
|
|
[['name'], 'string', 'max' => 255], |
62
|
|
|
[['position'], 'integer'] |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function behaviors() |
67
|
|
|
{ |
68
|
|
|
return [ |
69
|
|
|
ActiveRecordHelper::className(), |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @inheritdoc |
75
|
|
|
*/ |
76
|
|
|
public function attributeLabels() |
77
|
|
|
{ |
78
|
|
|
return [ |
79
|
|
|
'id' => \Yii::t('app', 'ID'), |
80
|
|
|
'name' => \Yii::t('app', 'Name'), |
81
|
|
|
'description' => \Yii::t('app', 'Description'), |
82
|
|
|
'code' => \Yii::t('app', 'Code'), |
83
|
|
|
'position' => \Yii::t('app', 'Position') |
84
|
|
|
]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public static function renderCountersAtHead(Event $event) |
88
|
|
|
{ |
89
|
|
|
$counters = self::find()->where(["position" => self::POSITION_AT_HEAD])->all(); |
90
|
|
|
foreach ($counters as $counter) { |
91
|
|
|
$event->sender->jsFiles[View::POS_HEAD][] = static::renderCounter($counter); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public static function renderCountersAtBeginningOfBody(Event $event) |
96
|
|
|
{ |
97
|
|
|
static::renderCounters($event, static::POSITION_AT_BEGIN_OF_BODY); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public static function renderCountersAtEndOfBody(Event $event) |
101
|
|
|
{ |
102
|
|
|
static::renderCounters($event, static::POSITION_AT_END_OF_BODY); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return string[] |
107
|
|
|
*/ |
108
|
|
|
public static function getPositionVariants() |
109
|
|
|
{ |
110
|
|
|
return [ |
111
|
|
|
self::POSITION_AT_END_OF_BODY => \Yii::t("app", self::MESSAGE_END_OF_BODY_TAG), |
112
|
|
|
self::POSITION_AT_BEGIN_OF_BODY => \Yii::t("app", self::MESSAGE_BEGINNING_OF_BODY_TAG), |
113
|
|
|
self::POSITION_AT_HEAD => \Yii::t("app", self::MESSAGE_INSIDE_OF_HEAD_TAG) |
114
|
|
|
]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param Counter $model |
119
|
|
|
* @return null|string |
120
|
|
|
*/ |
121
|
|
|
public static function updateInfoForEditable(Counter $model) |
122
|
|
|
{ |
123
|
|
|
if ($model === null) { |
124
|
|
|
return null; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
switch($model->position) { |
128
|
|
|
case self::POSITION_AT_BEGIN_OF_BODY: |
129
|
|
|
$value = self::MESSAGE_BEGINNING_OF_BODY_TAG; |
130
|
|
|
break; |
131
|
|
|
case self::POSITION_AT_END_OF_BODY: |
132
|
|
|
$value = self::MESSAGE_END_OF_BODY_TAG; |
133
|
|
|
break; |
134
|
|
|
case self::POSITION_AT_HEAD: |
135
|
|
|
$value = self::MESSAGE_INSIDE_OF_HEAD_TAG; |
136
|
|
|
break; |
137
|
|
|
default: |
138
|
|
|
$value = 'Unexpected value'; |
139
|
|
|
break; |
140
|
|
|
} |
141
|
|
|
return \yii\helpers\Html::tag( |
142
|
|
|
'span', |
143
|
|
|
\Yii::t('app', $value) |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public static function renderCounters(Event $event, $mode = self::POSITION_AT_END_OF_BODY) |
|
|
|
|
148
|
|
|
{ |
149
|
|
|
if ( |
150
|
|
|
\Yii::$app->request->isAjax === false && |
151
|
|
|
\Yii::$app->controller->module instanceof BackendModule === false && |
152
|
|
|
\Yii::$app->controller instanceof BackendController === false |
153
|
|
|
) { |
154
|
|
|
$cacheKey = \Yii::$app->getModule('seo')->cacheConfig['counterCache']['name'] . $mode; |
155
|
|
|
$counter_str = ''; |
156
|
|
|
/* @var $counters Counter[] */ |
157
|
|
|
if (false === $counters = \Yii::$app->getCache()->get($cacheKey)) { |
158
|
|
|
$counters = self::find()->where(["position" => $mode])->all(); |
159
|
|
|
\Yii::$app->getCache()->set( |
160
|
|
|
$cacheKey, |
161
|
|
|
$counters, |
162
|
|
|
\Yii::$app->getModule('seo')->cacheConfig['counterCache']['expire'], |
163
|
|
|
new TagDependency( |
164
|
|
|
[ |
165
|
|
|
'tags' => [ |
166
|
|
|
ActiveRecordHelper::getCommonTag(self::className()), |
167
|
|
|
], |
168
|
|
|
] |
169
|
|
|
) |
170
|
|
|
); |
171
|
|
|
} |
172
|
|
|
foreach ($counters as $counter) { |
173
|
|
|
$counter_str .= self::renderCounter($counter); |
174
|
|
|
} |
175
|
|
|
echo $counter_str; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function scenarios() |
180
|
|
|
{ |
181
|
|
|
return [ |
182
|
|
|
'default' => ['id', 'name', 'description', 'code', 'position'], |
183
|
|
|
]; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Search counters |
189
|
|
|
* @param $params |
190
|
|
|
* @return ActiveDataProvider |
191
|
|
|
*/ |
192
|
|
|
public |
193
|
|
|
function search( |
194
|
|
|
$params |
195
|
|
|
) { |
196
|
|
|
$this->load($params); |
197
|
|
|
$query = self::find(); |
198
|
|
|
foreach ($this->attributes as $name => $value) { |
199
|
|
|
if (!empty($value)) { |
200
|
|
|
if ($name == 'id') { |
201
|
|
|
$query->andWhere("`$name` = :$name", [":$name" => $value]); |
202
|
|
|
} else { |
203
|
|
|
$query->andWhere("`$name` LIKE :$name", [":$name" => "%$value%"]); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
$dataProvider = new ActiveDataProvider( |
208
|
|
|
[ |
209
|
|
|
'query' => $query, |
210
|
|
|
'pagination' => [ |
211
|
|
|
'pageSize' => 10, |
212
|
|
|
], |
213
|
|
|
] |
214
|
|
|
); |
215
|
|
|
return $dataProvider; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: