|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\modules\page\controllers; |
|
4
|
|
|
|
|
5
|
|
|
use app\components\Controller; |
|
6
|
|
|
use app\modules\core\helpers\ContentBlockHelper; |
|
7
|
|
|
use app\modules\core\models\ContentBlock; |
|
8
|
|
|
use app\modules\page\models\Page; |
|
9
|
|
|
use app\models\Search; |
|
10
|
|
|
use app\modules\seo\behaviors\MetaBehavior; |
|
11
|
|
|
use app\traits\LoadModel; |
|
12
|
|
|
use devgroup\TagDependencyHelper\ActiveRecordHelper; |
|
13
|
|
|
use Yii; |
|
14
|
|
|
use yii\caching\TagDependency; |
|
15
|
|
|
use yii\data\Pagination; |
|
16
|
|
|
use yii\db\ActiveQuery; |
|
17
|
|
|
use yii\web\ForbiddenHttpException; |
|
18
|
|
|
use yii\web\NotFoundHttpException; |
|
19
|
|
|
use yii\web\Response; |
|
20
|
|
|
|
|
21
|
|
|
class PageController extends Controller |
|
22
|
|
|
{ |
|
23
|
|
|
use LoadModel; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param $id |
|
27
|
|
|
* @return string |
|
28
|
|
|
* @throws NotFoundHttpException |
|
29
|
|
|
* @throws \yii\web\ServerErrorHttpException |
|
30
|
|
|
*/ |
|
31
|
|
|
public function actionShow($id) |
|
32
|
|
|
{ |
|
33
|
|
|
if (null === $model = Page::findById($id)) { |
|
34
|
|
|
throw new NotFoundHttpException; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$this->registerMetaDescription($model); |
|
38
|
|
|
|
|
39
|
|
|
$this->view->title = $model->title; |
|
40
|
|
|
if (!empty($model->h1)) { |
|
41
|
|
|
$this->view->blocks['h1'] = $model->h1; |
|
42
|
|
|
} |
|
43
|
|
|
$this->view->blocks['content'] = $model->content; |
|
44
|
|
|
$this->view->blocks['announce'] = $model->announce; |
|
45
|
|
|
|
|
46
|
|
|
return $this->render( |
|
47
|
|
|
$this->computeViewFile($model, 'show'), |
|
48
|
|
|
[ |
|
49
|
|
|
'model' => $model, |
|
50
|
|
|
'breadcrumbs' => $this->buildBreadcrumbsArray($model) |
|
51
|
|
|
] |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param $id |
|
57
|
|
|
* @return string |
|
58
|
|
|
* @throws NotFoundHttpException |
|
59
|
|
|
* @throws \yii\web\ServerErrorHttpException |
|
60
|
|
|
*/ |
|
61
|
|
|
public function actionList($id) |
|
62
|
|
|
{ |
|
63
|
|
|
if (null === $model = Page::findById($id)) { |
|
64
|
|
|
throw new NotFoundHttpException; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$this->registerMetaDescription($model); |
|
68
|
|
|
|
|
69
|
|
|
$cacheKey = 'PagesList:'.$model->id; |
|
70
|
|
|
|
|
71
|
|
|
// query that needed for pages retrieve and pagination |
|
72
|
|
|
$children = Page::find() |
|
73
|
|
|
->where(['parent_id' => $model->id, 'published' => 1]) |
|
74
|
|
|
->orderBy('date_added DESC, sort_order') |
|
75
|
|
|
->with('images'); |
|
76
|
|
|
|
|
77
|
|
|
// count all pages |
|
78
|
|
|
$count = Yii::$app->cache->get($cacheKey.';count'); |
|
79
|
|
|
if ($count === false) { |
|
80
|
|
|
$countQuery = clone $children; |
|
81
|
|
|
$count = $countQuery->count(); |
|
82
|
|
|
Yii::$app->cache->set($cacheKey.';count', $count, 86400, new TagDependency([ |
|
83
|
|
|
'tags' => [ |
|
84
|
|
|
ActiveRecordHelper::getCommonTag(Page::className()) |
|
85
|
|
|
] |
|
86
|
|
|
])); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$pages = new Pagination( |
|
90
|
|
|
[ |
|
91
|
|
|
'defaultPageSize' => $this->module->pagesPerList, |
|
92
|
|
|
'forcePageParam' => false, |
|
93
|
|
|
'pageSizeLimit' => [ |
|
94
|
|
|
$this->module->minPagesPerList, |
|
95
|
|
|
$this->module->maxPagesPerList |
|
96
|
|
|
], |
|
97
|
|
|
'totalCount' => $count, |
|
98
|
|
|
] |
|
99
|
|
|
); |
|
100
|
|
|
|
|
101
|
|
|
// append current page number to cache key |
|
102
|
|
|
$cacheKey .= ';page:' . $pages->page; |
|
103
|
|
|
|
|
104
|
|
|
$childrenModels = Yii::$app->cache->get($cacheKey); |
|
105
|
|
|
if ($childrenModels === false) { |
|
106
|
|
|
|
|
107
|
|
|
/** @var ActiveQuery $children */ |
|
108
|
|
|
|
|
109
|
|
|
$children = $children->offset($pages->offset) |
|
110
|
|
|
->limit($pages->limit) |
|
111
|
|
|
->all(); |
|
112
|
|
|
Yii::$app->cache->set($cacheKey, $children, 86400, new TagDependency([ |
|
113
|
|
|
'tags' => [ |
|
114
|
|
|
ActiveRecordHelper::getCommonTag(Page::className()) |
|
115
|
|
|
] |
|
116
|
|
|
])); |
|
117
|
|
|
} else { |
|
118
|
|
|
$children = $childrenModels; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$this->view->title = $model->title; |
|
122
|
|
|
if (!empty($model->h1)) { |
|
123
|
|
|
$this->view->blocks['h1'] = $model->h1; |
|
124
|
|
|
} |
|
125
|
|
|
$this->view->blocks['content'] = $model->content; |
|
126
|
|
|
$this->view->blocks['announce'] = $model->announce; |
|
127
|
|
|
|
|
128
|
|
|
return $this->render( |
|
129
|
|
|
$this->computeViewFile($model, 'list'), |
|
130
|
|
|
[ |
|
131
|
|
|
'model' => $model, |
|
132
|
|
|
'children' => $children, |
|
133
|
|
|
'pages' => $pages, |
|
134
|
|
|
'breadcrumbs' => $this->buildBreadcrumbsArray($model), |
|
135
|
|
|
] |
|
136
|
|
|
); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return array |
|
141
|
|
|
* @throws ForbiddenHttpException |
|
142
|
|
|
*/ |
|
143
|
|
|
public function actionSearch() |
|
144
|
|
|
{ |
|
145
|
|
|
/** |
|
146
|
|
|
* @param $module \app\modules\page\PageModule |
|
147
|
|
|
*/ |
|
148
|
|
|
if (!Yii::$app->request->isAjax) { |
|
149
|
|
|
throw new ForbiddenHttpException(); |
|
150
|
|
|
} |
|
151
|
|
|
$model = new Search(); |
|
152
|
|
|
$model->load(Yii::$app->request->get()); |
|
153
|
|
|
$cacheKey = 'PageSearchIds: ' . $model->q; |
|
154
|
|
|
$ids = Yii::$app->cache->get($cacheKey); |
|
155
|
|
|
if ($ids === false) { |
|
156
|
|
|
$ids = $model->searchPagesByDescription(); |
|
157
|
|
|
Yii::$app->cache->set( |
|
158
|
|
|
$cacheKey, |
|
159
|
|
|
$ids, |
|
160
|
|
|
86400, |
|
161
|
|
|
new TagDependency( |
|
162
|
|
|
[ |
|
163
|
|
|
'tags' => ActiveRecordHelper::getCommonTag(Page::className()), |
|
164
|
|
|
] |
|
165
|
|
|
) |
|
166
|
|
|
); |
|
167
|
|
|
} |
|
168
|
|
|
$pages = new Pagination( |
|
169
|
|
|
[ |
|
170
|
|
|
'defaultPageSize' => $this->module->searchResultsLimit, |
|
171
|
|
|
'forcePageParam' => false, |
|
172
|
|
|
'pageSizeLimit' => [ |
|
173
|
|
|
$this->module->minPagesPerList, |
|
174
|
|
|
$this->module->maxPagesPerList |
|
175
|
|
|
], |
|
176
|
|
|
'totalCount' => count($ids), |
|
177
|
|
|
] |
|
178
|
|
|
); |
|
179
|
|
|
$cacheKey .= ' : ' . $pages->offset; |
|
180
|
|
|
$pagelist = Yii::$app->cache->get($cacheKey); |
|
181
|
|
View Code Duplication |
if ($pagelist === false) { |
|
182
|
|
|
$pagelist = Page::find()->where( |
|
183
|
|
|
[ |
|
184
|
|
|
'in', |
|
185
|
|
|
'`id`', |
|
186
|
|
|
array_slice( |
|
187
|
|
|
$ids, |
|
188
|
|
|
$pages->offset, |
|
189
|
|
|
$pages->limit |
|
190
|
|
|
) |
|
191
|
|
|
] |
|
192
|
|
|
)->addOrderBy('sort_order')->with('images')->all(); |
|
193
|
|
|
Yii::$app->cache->set( |
|
194
|
|
|
$cacheKey, |
|
195
|
|
|
$pagelist, |
|
196
|
|
|
86400, |
|
197
|
|
|
new TagDependency( |
|
198
|
|
|
[ |
|
199
|
|
|
'tags' => ActiveRecordHelper::getCommonTag(Page::className()), |
|
200
|
|
|
] |
|
201
|
|
|
) |
|
202
|
|
|
); |
|
203
|
|
|
} |
|
204
|
|
|
Yii::$app->response->format = Response::FORMAT_JSON; |
|
205
|
|
|
return [ |
|
206
|
|
|
'view' => $this->renderPartial( |
|
207
|
|
|
'search', |
|
208
|
|
|
[ |
|
209
|
|
|
'model' => $model, |
|
210
|
|
|
'pagelist' => $pagelist, |
|
211
|
|
|
'pages' => $pages, |
|
212
|
|
|
] |
|
213
|
|
|
), |
|
214
|
|
|
'totalCount' => count($ids), |
|
215
|
|
|
]; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/* |
|
219
|
|
|
* This function build array for widget "Breadcrumbs" |
|
220
|
|
|
* $model - model of current page |
|
221
|
|
|
* Return an array for widget or empty array |
|
222
|
|
|
*/ |
|
223
|
|
|
private function buildBreadcrumbsArray($model) |
|
224
|
|
|
{ |
|
225
|
|
|
if ($model === null || $model->id === 1) { |
|
226
|
|
|
return []; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
// init |
|
230
|
|
|
$breadcrumbs = []; |
|
231
|
|
|
$crumbs[$model->slug] = $model->breadcrumbs_label; |
|
|
|
|
|
|
232
|
|
|
|
|
233
|
|
|
// get basic data |
|
234
|
|
|
$parent = Page::findById($model->parent_id); |
|
235
|
|
|
// if parent exists and not a main page |
|
236
|
|
View Code Duplication |
while ($parent !== null && $parent->id != 1) { |
|
237
|
|
|
$crumbs[$parent->slug] = $parent->breadcrumbs_label; |
|
238
|
|
|
$parent = $parent->parent; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
// build array for widget |
|
242
|
|
|
$url = ''; |
|
243
|
|
|
$crumbs = array_reverse($crumbs, true); |
|
244
|
|
View Code Duplication |
foreach ($crumbs as $slug => $label) { |
|
245
|
|
|
$url .= '/' . $slug; |
|
246
|
|
|
$breadcrumbs[] = [ |
|
247
|
|
|
'label' => (string) $label, |
|
248
|
|
|
'url' => $url |
|
249
|
|
|
]; |
|
250
|
|
|
} |
|
251
|
|
|
unset($breadcrumbs[count($breadcrumbs) - 1]['url']); // last item is not a link |
|
252
|
|
|
|
|
253
|
|
|
return $breadcrumbs; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* @param Page $model |
|
258
|
|
|
*/ |
|
259
|
|
|
private function registerMetaDescription($model) |
|
260
|
|
|
{ |
|
261
|
|
View Code Duplication |
if (!empty($model->meta_description)) { |
|
262
|
|
|
$this->view->registerMetaTag( |
|
263
|
|
|
[ |
|
264
|
|
|
'name' => 'description', |
|
265
|
|
|
'content' => ContentBlockHelper::compileContentString( |
|
266
|
|
|
$model->meta_description, |
|
267
|
|
|
Page::className() . ":{$model->id}:meta_description", |
|
268
|
|
|
new TagDependency( |
|
269
|
|
|
[ |
|
270
|
|
|
'tags' => [ |
|
271
|
|
|
ActiveRecordHelper::getCommonTag(ContentBlock::className()), |
|
272
|
|
|
ActiveRecordHelper::getCommonTag(Page::className()) |
|
273
|
|
|
] |
|
274
|
|
|
] |
|
275
|
|
|
) |
|
276
|
|
|
) |
|
277
|
|
|
], |
|
278
|
|
|
'meta_description' |
|
279
|
|
|
); |
|
280
|
|
|
} |
|
281
|
|
|
} |
|
282
|
|
|
} |
|
283
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.