Skip to content

Commit dd39721

Browse files
Dynamic canurl params by ListView or GridView
1 parent 161500b commit dd39721

2 files changed

Lines changed: 75 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
CHANGELOG
22
==============
33

4+
2.1.4
5+
-----------------
6+
* Dynamic canurl params by ListView or GridView
7+
48
2.1.3.1
59
-----------------
610
* Fixed bugs

src/CmsSeoComponent.php

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@
1515
use yii\base\ActionEvent;
1616
use yii\base\BootstrapInterface;
1717
use yii\base\Event;
18+
use yii\base\Widget;
19+
use yii\base\WidgetEvent;
20+
use yii\grid\GridView;
1821
use yii\helpers\ArrayHelper;
1922
use yii\helpers\Url;
2023
use yii\web\Application;
2124
use yii\web\Controller;
2225
use yii\web\NotFoundHttpException;
2326
use yii\web\View;
2427
use yii\widgets\ActiveForm;
28+
use yii\widgets\BaseListView;
2529
use yii\widgets\LinkPager;
30+
use yii\widgets\ListView;
2631

2732
/**
2833
* @property CanUrl $canUrl;
@@ -262,6 +267,9 @@ public function bootstrap($application)
262267
Event::on(Controller::class, Controller::EVENT_BEFORE_ACTION, function (ActionEvent $e) {
263268
$this->_initDefaultCanUrl();
264269

270+
/**
271+
* Редирект 404 ошибок
272+
*/
265273
if (\Yii::$app->controller->uniqueId == 'cms/error') {
266274
if (\Yii::$app->getErrorHandler()->exception instanceof NotFoundHttpException && $this->isRedirectNotFoundHttpException) {
267275
\Yii::$app->response->redirect(Url::home());
@@ -293,14 +301,23 @@ public function bootstrap($application)
293301

294302
protected function _initDefaultCanUrl() {
295303

304+
/**
305+
* Канурл может быть отключен вовсе
306+
*/
296307
if ($this->canUrl === false) {
297308
return false;
298309
}
299310

311+
/**
312+
* Хост может быть не указан, тогда будет взят из запроса
313+
*/
300314
if (!$this->canUrl->host) {
301315
$this->canUrl->host = \Yii::$app->request->hostName;
302316
}
303317

318+
/**
319+
* Аналогично со схемой
320+
*/
304321
if (!$this->canUrl->scheme) {
305322
$this->canUrl->scheme = \Yii::$app->request->isSecureConnection ? "https" : "http";
306323
}
@@ -314,23 +331,68 @@ protected function _initDefaultCanUrl() {
314331
} else {
315332
if (\Yii::$app->cms->currentTree) {
316333
$this->canUrl->path = \Yii::$app->cms->currentTree->url;
317-
} else {
318-
//print_r(\Yii::$app->request);
319334
}
320335
}
321336

322337
$this->canUrl->SETcore_params([]);
323338
$this->canUrl->SETimportant_params([]);
324339

325-
if (in_array(\Yii::$app->controller->action->uniqueId, ['cms/tree/view', 'savedFilters/saved-filters/view'])) {
326-
$this->canUrl->ADDminor_params(['per-page' => null]);
327-
$this->canUrl->ADDminor_params(['page' => null]);
328-
$this->canUrl->ADDimportant_pnames(['ProductFilters']);
329-
$this->canUrl->ADDimportant_pnames(['SearchProductsModel']);
330-
$this->canUrl->ADDimportant_pnames(['SearchRelatedPropertiesModel']);
331-
}
340+
Event::on(ListView::class, Widget::EVENT_AFTER_RUN, [$this, '_addCanurlParams']);
341+
Event::on(GridView::class, Widget::EVENT_AFTER_RUN, [$this, '_addCanurlParams']);
342+
332343
}
333344

345+
public function _addCanurlParams(WidgetEvent $e)
346+
{
347+
//Только для этих действий
348+
if (!in_array(\Yii::$app->controller->uniqueId, [
349+
'cms/tree',
350+
'cms/content-element',
351+
'savedFilters/saved-filters',
352+
])) {
353+
return true;
354+
}
355+
356+
$this->canUrl->ADDimportant_pnames(['ProductFilters']);
357+
$this->canUrl->ADDimportant_pnames(['SearchProductsModel']);
358+
$this->canUrl->ADDimportant_pnames(['SearchRelatedPropertiesModel']);
359+
360+
/**
361+
* @var $sender ListView|GridView
362+
*/
363+
$sender = $e->sender;
364+
$r = new \ReflectionClass($sender);
365+
366+
\Yii::info('_addCanurlParams: ' . $r->getName());
367+
368+
if ($pagination = $sender->dataProvider->getPagination()) {
369+
370+
if ($pagination->pageCount > 1) {
371+
$pageParam = $pagination->pageParam;
372+
$this->canUrl->ADDimportant_params([$pagination->pageSizeParam => null]);
373+
/*for ($i = 2; $i <= $pagination->pageCount; $i++) {
374+
$pages[] = $i;
375+
}*/
376+
if ($currentPage = \Yii::$app->request->get($pageParam)) {
377+
if ($currentPage > $pagination->pageCount && $currentPage != 1) {
378+
$this->canUrl->ADDimportant_params([$pagination->pageParam => $pagination->pageCount]);
379+
} elseif($currentPage != 1) {
380+
$this->canUrl->ADDimportant_params([$pagination->pageParam => null]);
381+
}
382+
} else {
383+
$this->canUrl->ADDimportant_params([$pagination->pageParam => null]);
384+
}
385+
}
386+
387+
388+
389+
\Yii::info('_addCanurlParams: totalCount ' . $pagination->totalCount);
390+
\Yii::info('_addCanurlParams: pageParam ' . $pagination->pageParam);
391+
\Yii::info('_addCanurlParams: pageCount ' . $pagination->pageCount);
392+
393+
}
394+
395+
}
334396

335397
public function _isTrigerEventCanUrl()
336398
{

0 commit comments

Comments
 (0)