-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPageSize.php
52 lines (46 loc) · 1.69 KB
/
PageSize.php
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
<?php
namespace nkovacs\pagesizer;
use Yii;
use yii\web\Request;
/**
* Helper class to create an url with the specified page size.
*/
class PageSize
{
/**
* Creates an url for the specified page size.
* @param \yii\data\Pagination $pagination
* @param integer $pageSize page size
* @param boolean $absolute whether to create an absolute URL. Defaults to `false`.
*
* @return string the created URL
*/
public static function createSizeUrl($pagination, $pageSize, $absolute = false)
{
if (($params = $pagination->params) === null) {
$request = Yii::$app->getRequest();
$params = $request instanceof Request ? $request->getQueryParams() : [];
}
$currentPageSize = $pagination->getPageSize();
$currentPage = $pagination->getPage();
$target = $currentPage*$currentPageSize;
$page = (int)($target/$pageSize);
if ($page > 0 || $page >= 0 && $pagination->forcePageParam) {
$params[$pagination->pageParam] = $page + 1;
} else {
unset($params[$pagination->pageParam]);
}
if ($pageSize != $pagination->defaultPageSize) {
$params[$pagination->pageSizeParam] = $pageSize;
} else {
unset($params[$pagination->pageSizeParam]);
}
$params[0] = $pagination->route === null ? Yii::$app->controller->getRoute() : $pagination->route;
$urlManager = $pagination->urlManager === null ? Yii::$app->getUrlManager() : $pagination->urlManager;
if ($absolute) {
return $urlManager->createAbsoluteUrl($params);
} else {
return $urlManager->createUrl($params);
}
}
}