|
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
|
|
|
*/ |
|
22
|
|
|
class Counter extends ActiveRecord |
|
23
|
|
|
{ |
|
24
|
|
|
const POSITION_AT_END_OF_BODY = 0; |
|
25
|
|
|
const POSITION_AT_BEGIN_OF_BODY = 1; |
|
26
|
|
|
const POSITION_AT_HEAD = 2; |
|
27
|
|
|
const MESSAGE_END_OF_BODY_TAG = "End of body tag"; |
|
28
|
|
|
const MESSAGE_BEGINNING_OF_BODY_TAG = "Beginning of body tag"; |
|
29
|
|
|
const MESSAGE_INSIDE_OF_HEAD_TAG = "Inside of head tag"; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @inheritdoc |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function tableName() |
|
35
|
|
|
{ |
|
36
|
|
|
return '{{%seo_counter}}'; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param $counter |
|
41
|
|
|
* @return string |
|
42
|
|
|
*/ |
|
43
|
|
|
private static function renderCounter($counter) |
|
44
|
|
|
{ |
|
45
|
|
|
return |
|
46
|
|
|
"\n<!-- {$counter->name} counter -->\n" |
|
47
|
|
|
. "{$counter->code}" |
|
48
|
|
|
. "\n<!-- /{$counter->name} counter -->\n"; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @inheritdoc |
|
53
|
|
|
*/ |
|
54
|
|
View Code Duplication |
public function rules() |
|
55
|
|
|
{ |
|
56
|
|
|
return [ |
|
57
|
|
|
[['description', 'code'], 'string'], |
|
58
|
|
|
[['code'], 'required'], |
|
59
|
|
|
[['name'], 'string', 'max' => 255], |
|
60
|
|
|
[['position'], 'integer'] |
|
61
|
|
|
]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function behaviors() |
|
65
|
|
|
{ |
|
66
|
|
|
return [ |
|
67
|
|
|
ActiveRecordHelper::className(), |
|
68
|
|
|
]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @inheritdoc |
|
73
|
|
|
*/ |
|
74
|
|
View Code Duplication |
public function attributeLabels() |
|
75
|
|
|
{ |
|
76
|
|
|
return [ |
|
77
|
|
|
'id' => \Yii::t('app', 'ID'), |
|
78
|
|
|
'name' => \Yii::t('app', 'Name'), |
|
79
|
|
|
'description' => \Yii::t('app', 'Description'), |
|
80
|
|
|
'code' => \Yii::t('app', 'Code'), |
|
81
|
|
|
'position' => \Yii::t('app', 'Position') |
|
82
|
|
|
]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public static function renderCountersAtHead(Event $event) |
|
86
|
|
|
{ |
|
87
|
|
|
$counters = self::find()->where(["position" => self::POSITION_AT_HEAD])->all(); |
|
88
|
|
|
foreach ($counters as $counter) { |
|
89
|
|
|
$event->sender->jsFiles[View::POS_HEAD][] = static::renderCounter($counter); |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public static function renderCountersAtBeginningOfBody(Event $event) |
|
94
|
|
|
{ |
|
95
|
|
|
static::renderCounters($event, static::POSITION_AT_BEGIN_OF_BODY); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public static function renderCountersAtEndOfBody(Event $event) |
|
99
|
|
|
{ |
|
100
|
|
|
static::renderCounters($event, static::POSITION_AT_END_OF_BODY); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return array |
|
|
|
|
|
|
105
|
|
|
*/ |
|
106
|
|
|
public static function getPositionVariants() |
|
107
|
|
|
{ |
|
108
|
|
|
return [ |
|
109
|
|
|
self::POSITION_AT_END_OF_BODY => \Yii::t("app", self::MESSAGE_END_OF_BODY_TAG), |
|
110
|
|
|
self::POSITION_AT_BEGIN_OF_BODY => \Yii::t("app", self::MESSAGE_BEGINNING_OF_BODY_TAG), |
|
111
|
|
|
self::POSITION_AT_HEAD => \Yii::t("app", self::MESSAGE_INSIDE_OF_HEAD_TAG) |
|
112
|
|
|
]; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param Counter $model |
|
117
|
|
|
* @return null|string |
|
118
|
|
|
*/ |
|
119
|
|
|
public static function updateInfoForEditable(Counter $model) |
|
120
|
|
|
{ |
|
121
|
|
|
if ($model === null) { |
|
122
|
|
|
return null; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
switch($model->position) { |
|
|
|
|
|
|
126
|
|
|
case Counter::POSITION_AT_BEGIN_OF_BODY: |
|
|
|
|
|
|
127
|
|
|
$value = self::MESSAGE_BEGINNING_OF_BODY_TAG; |
|
128
|
|
|
break; |
|
129
|
|
|
case Counter::POSITION_AT_END_OF_BODY: |
|
|
|
|
|
|
130
|
|
|
$value = self::MESSAGE_END_OF_BODY_TAG; |
|
131
|
|
|
break; |
|
132
|
|
|
case Counter::POSITION_AT_HEAD: |
|
|
|
|
|
|
133
|
|
|
$value = self::MESSAGE_INSIDE_OF_HEAD_TAG; |
|
134
|
|
|
break; |
|
135
|
|
|
default: |
|
136
|
|
|
$value = 'Unexpected value'; |
|
137
|
|
|
break; |
|
138
|
|
|
} |
|
139
|
|
|
return \yii\helpers\Html::tag( |
|
140
|
|
|
'span', |
|
141
|
|
|
\Yii::t('app', $value) |
|
142
|
|
|
); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public static function renderCounters(Event $event, $mode = self::POSITION_AT_END_OF_BODY) |
|
|
|
|
|
|
146
|
|
|
{ |
|
147
|
|
|
if ( |
|
148
|
|
|
\Yii::$app->request->isAjax === false && |
|
149
|
|
|
\Yii::$app->controller->module instanceof BackendModule === false && |
|
150
|
|
|
\Yii::$app->controller instanceof BackendController === false |
|
151
|
|
|
) { |
|
152
|
|
|
$cacheKey = \Yii::$app->getModule('seo')->cacheConfig['counterCache']['name'] . $mode; |
|
153
|
|
|
$counter_str = ''; |
|
154
|
|
|
/* @var $counters Counter[] */ |
|
155
|
|
|
if (false === $counters = \Yii::$app->getCache()->get($cacheKey)) { |
|
156
|
|
|
$counters = self::find()->where(["position" => $mode])->all(); |
|
157
|
|
|
\Yii::$app->getCache()->set( |
|
158
|
|
|
$cacheKey, |
|
159
|
|
|
$counters, |
|
160
|
|
|
\Yii::$app->getModule('seo')->cacheConfig['counterCache']['expire'], |
|
161
|
|
|
new TagDependency( |
|
162
|
|
|
[ |
|
163
|
|
|
'tags' => [ |
|
164
|
|
|
ActiveRecordHelper::getCommonTag(self::className()), |
|
165
|
|
|
], |
|
166
|
|
|
] |
|
167
|
|
|
) |
|
168
|
|
|
); |
|
169
|
|
|
} |
|
170
|
|
|
foreach ($counters as $counter) { |
|
171
|
|
|
$counter_str .= self::renderCounter($counter); |
|
172
|
|
|
} |
|
173
|
|
|
echo $counter_str; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public function scenarios() |
|
178
|
|
|
{ |
|
179
|
|
|
return [ |
|
180
|
|
|
'default' => ['id', 'name', 'description', 'code', 'position'], |
|
181
|
|
|
]; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Search counters |
|
187
|
|
|
* @param $params |
|
188
|
|
|
* @return ActiveDataProvider |
|
189
|
|
|
*/ |
|
190
|
|
|
public |
|
191
|
|
|
function search( |
|
192
|
|
|
$params |
|
193
|
|
|
) { |
|
194
|
|
|
$this->load($params); |
|
195
|
|
|
$query = self::find(); |
|
196
|
|
|
foreach ($this->attributes as $name => $value) { |
|
197
|
|
|
if (!empty($value)) { |
|
198
|
|
|
if ($name == 'id') { |
|
199
|
|
|
$query->andWhere("`$name` = :$name", [":$name" => $value]); |
|
200
|
|
|
} else { |
|
201
|
|
|
$query->andWhere("`$name` LIKE :$name", [":$name" => "%$value%"]); |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
$dataProvider = new ActiveDataProvider( |
|
206
|
|
|
[ |
|
207
|
|
|
'query' => $query, |
|
208
|
|
|
'pagination' => [ |
|
209
|
|
|
'pageSize' => 10, |
|
210
|
|
|
], |
|
211
|
|
|
] |
|
212
|
|
|
); |
|
213
|
|
|
return $dataProvider; |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
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
SomeClassto useselfinstead: