Pagination.php
1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
namespace thread\app\web;
use Yii;
use yii\web\NotFoundHttpException;
/**
* Class Pagination
*
* @package thread\components
* @author FilamentV <vortex.filament@gmail.com>
* @copyright (c), Thread
*/
class Pagination extends \yii\data\Pagination
{
public $generateExeption = true;
/**
* @param int $value
* @param bool $validatePage
* @throws NotFoundHttpException
*/
public function setPage($value, $validatePage = false)
{
parent::setPage($value, $validatePage);
if ($value < 0 && $this->generateExeption == true) {
throw new NotFoundHttpException;
}
$pageCount = $this->getPageCount();
if ($value > 0 && $value >= $pageCount && $this->generateExeption == true) {
// throw new NotFoundHttpException;
}
if (Yii::$app->getRequest()->get($this->pageParam) == 1 && $this->generateExeption == true) {
throw new NotFoundHttpException;
}
}
/**
* @param int $page
* @param null $pageSize
* @param bool $absolute
* @return string
*/
public function createUrl($page, $pageSize = null, $absolute = false)
{
if (($params = $this->params) === null) {
$request = Yii::$app->getRequest();
$params = $request instanceof Request ? $request->getQueryParams() : [];
}
if ($page > 1 || $page >= 1 && $this->forcePageParam) {
$params[$this->pageParam] = $page + 1;
} else {
unset($params[$this->pageParam]);
}
$params[0] = $this->route === null ? Yii::$app->controller->getRoute() : $this->route;
$urlManager = $this->urlManager === null ? Yii::$app->getUrlManager() : $this->urlManager;
if ($absolute) {
return $urlManager->createAbsoluteUrl($params);
} else {
return $urlManager->createUrl($params);
}
}
}