From df5642480d788d0489f8358733770c526de84c2a Mon Sep 17 00:00:00 2001 From: MashinaMashina Date: Tue, 14 Jan 2020 15:34:57 +0400 Subject: [PATCH] Opportunity to change parameter whith we get current page --- system/Model.php | 2 +- system/Pager/Pager.php | 65 ++++++++++++++++++++++++++-------- system/Pager/PagerRenderer.php | 35 ++++++++++-------- 3 files changed, 72 insertions(+), 30 deletions(-) diff --git a/system/Model.php b/system/Model.php index a3ece2c79d02..0dee63c05f26 100644 --- a/system/Model.php +++ b/system/Model.php @@ -1122,7 +1122,7 @@ public function chunk(int $size, Closure $userFunc) */ public function paginate(int $perPage = 20, string $group = 'default', int $page = 0) { - $pager = \Config\Services::pager(); + $pager = \Config\Services::pager(null, null, false); $page = $page >= 1 ? $page : $pager->getCurrentPage($group); $total = $this->countAllResults(false); diff --git a/system/Pager/Pager.php b/system/Pager/Pager.php index b62fc1687e91..c2d2d03f8cc9 100644 --- a/system/Pager/Pager.php +++ b/system/Pager/Pager.php @@ -234,6 +234,27 @@ public function setPath(string $path, string $group = 'default') return $this; } + //-------------------------------------------------------------------- + + /** + * Sets the $_GET parameter from which we take the page number + * + * @param string $pageSelector + * @param string $group + * + * @return mixed + */ + public function setPageSelector(string $pageSelector, string $group = 'default') + { + $this->ensureGroup($group); + + $this->groups[$group]['pageSelector'] = $pageSelector; + + $this->calculateCurrentPage($group); + + return $this; + } + //-------------------------------------------------------------------- /** @@ -348,7 +369,7 @@ public function getPageURI(int $page = null, string $group = 'default', bool $re } else { - $uri->addQuery('page', $page); + $uri->addQuery($this->groups[$group]['pageSelector'], $page); } if ($this->only) @@ -357,7 +378,7 @@ public function getPageURI(int $page = null, string $group = 'default', bool $re if (! $segment) { - $query['page'] = $page; + $query[$this->groups[$group]['pageSelector']] = $page; } $uri->setQueryArray($query); @@ -503,13 +524,31 @@ protected function ensureGroup(string $group) } $this->groups[$group] = [ - 'uri' => clone Services::request()->uri, - 'hasMore' => false, - 'total' => null, - 'perPage' => $this->config->perPage, - 'pageCount' => 1, + 'uri' => clone Services::request()->uri, + 'hasMore' => false, + 'total' => null, + 'perPage' => $this->config->perPage, + 'pageCount' => 1, + 'pageSelector' => $group === 'default' ? 'page' : 'page_' . $group, ]; + $this->calculateCurrentPage($group); + + if ($_GET) + { + $this->groups[$group]['uri'] = $this->groups[$group]['uri']->setQueryArray($_GET); + } + } + + //-------------------------------------------------------------------- + + /** + * Calculating the current page + * + * @param string $group + */ + protected function calculateCurrentPage(string $group) + { if (array_key_exists($group, $this->segment)) { try @@ -523,15 +562,11 @@ protected function ensureGroup(string $group) } else { - $page = $_GET['page_' . $group] ?? $_GET['page'] ?? 1; - $page = intval($page); - - $this->groups[$group]['currentPage'] = $page < 1 ? 1 : $page; - } + $pageSelector = $this->groups[$group]['pageSelector']; - if ($_GET) - { - $this->groups[$group]['uri'] = $this->groups[$group]['uri']->setQueryArray($_GET); + $page = (int) $_GET[$pageSelector] ?? 1; + + $this->groups[$group]['currentPage'] = $page < 1 ? 1 : $page; } } diff --git a/system/Pager/PagerRenderer.php b/system/Pager/PagerRenderer.php index c21ba40ac22d..f86dcda26214 100644 --- a/system/Pager/PagerRenderer.php +++ b/system/Pager/PagerRenderer.php @@ -93,7 +93,13 @@ class PagerRenderer * @var integer */ protected $segment; - + /** + * Name of $_GET parameter + * + * @var integer + */ + protected $pageSelector; + //-------------------------------------------------------------------- /** @@ -103,13 +109,14 @@ class PagerRenderer */ public function __construct(array $details) { - $this->first = 1; - $this->last = $details['pageCount']; - $this->current = $details['currentPage']; - $this->total = $details['total']; - $this->uri = $details['uri']; - $this->pageCount = $details['pageCount']; - $this->segment = $details['segment'] ?? 0; + $this->first = 1; + $this->last = $details['pageCount']; + $this->current = $details['currentPage']; + $this->total = $details['total']; + $this->uri = $details['uri']; + $this->pageCount = $details['pageCount']; + $this->segment = $details['segment'] ?? 0; + $this->pageSelector = $details['pageSelector'] ?? 'page'; } //-------------------------------------------------------------------- @@ -164,7 +171,7 @@ public function getPrevious() if ($this->segment === 0) { - $uri->addQuery('page', $this->first - 1); + $uri->addQuery($this->pageSelector, $this->first - 1); } else { @@ -208,7 +215,7 @@ public function getNext() if ($this->segment === 0) { - $uri->addQuery('page', $this->last + 1); + $uri->addQuery($this->pageSelector, $this->last + 1); } else { @@ -231,7 +238,7 @@ public function getFirst(): string if ($this->segment === 0) { - $uri->addQuery('page', 1); + $uri->addQuery($this->pageSelector, 1); } else { @@ -254,7 +261,7 @@ public function getLast(): string if ($this->segment === 0) { - $uri->addQuery('page', $this->pageCount); + $uri->addQuery($this->pageSelector, $this->pageCount); } else { @@ -277,7 +284,7 @@ public function getCurrent(): string if ($this->segment === 0) { - $uri->addQuery('page', $this->current); + $uri->addQuery($this->pageSelector, $this->current); } else { @@ -306,7 +313,7 @@ public function links(): array for ($i = $this->first; $i <= $this->last; $i ++) { $links[] = [ - 'uri' => (string) ($this->segment === 0 ? $uri->addQuery('page', $i) : $uri->setSegment($this->segment, $i)), + 'uri' => (string) ($this->segment === 0 ? $uri->addQuery($this->pageSelector, $i) : $uri->setSegment($this->segment, $i)), 'title' => (int) $i, 'active' => ($i === $this->current), ];