From 88e3d78c32501d1857a0efdc3babcc7367b629a9 Mon Sep 17 00:00:00 2001 From: Morozov Roman Date: Sun, 29 Dec 2019 00:29:08 +0400 Subject: [PATCH 1/6] Fix that Pager::getCurrentPage() try to return string type --- system/Model.php | 7 +++---- system/Pager/Pager.php | 5 ++++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/system/Model.php b/system/Model.php index 93d55feaa985..a3ece2c79d02 100644 --- a/system/Model.php +++ b/system/Model.php @@ -1122,14 +1122,13 @@ public function chunk(int $size, Closure $userFunc) */ public function paginate(int $perPage = 20, string $group = 'default', int $page = 0) { - // Get the necessary parts. - $page = $page >= 1 ? $page : (ctype_digit($_GET['page'] ?? '') && $_GET['page'] > 1 ? $_GET['page'] : 1); - + $pager = \Config\Services::pager(); + $page = $page >= 1 ? $page : $pager->getCurrentPage($group); + $total = $this->countAllResults(false); // Store it in the Pager library so it can be // paginated in the views. - $pager = \Config\Services::pager(); $this->pager = $pager->store($group, $page, $perPage, $total); $offset = ($page - 1) * $perPage; diff --git a/system/Pager/Pager.php b/system/Pager/Pager.php index b80b0e885d8d..b62fc1687e91 100644 --- a/system/Pager/Pager.php +++ b/system/Pager/Pager.php @@ -523,7 +523,10 @@ protected function ensureGroup(string $group) } else { - $this->groups[$group]['currentPage'] = $_GET['page_' . $group] ?? $_GET['page'] ?? 1; + $page = $_GET['page_' . $group] ?? $_GET['page'] ?? 1; + $page = intval($page); + + $this->groups[$group]['currentPage'] = $page < 1 ? 1 : $page; } if ($_GET) From df5642480d788d0489f8358733770c526de84c2a Mon Sep 17 00:00:00 2001 From: MashinaMashina Date: Tue, 14 Jan 2020 15:34:57 +0400 Subject: [PATCH 2/6] 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), ]; From 1cb04cbb10eae639b6065affb22e54cc96ce6dce Mon Sep 17 00:00:00 2001 From: MashinaMashina Date: Tue, 14 Jan 2020 15:42:35 +0400 Subject: [PATCH 3/6] Priority fix --- system/Pager/Pager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Pager/Pager.php b/system/Pager/Pager.php index c2d2d03f8cc9..c36a29f55a9c 100644 --- a/system/Pager/Pager.php +++ b/system/Pager/Pager.php @@ -564,7 +564,7 @@ protected function calculateCurrentPage(string $group) { $pageSelector = $this->groups[$group]['pageSelector']; - $page = (int) $_GET[$pageSelector] ?? 1; + $page = (int) ($_GET[$pageSelector] ?? 1); $this->groups[$group]['currentPage'] = $page < 1 ? 1 : $page; } From 7436e69f9fd319772e50b51f5cc33d6fdf60c3d6 Mon Sep 17 00:00:00 2001 From: MashinaMashina Date: Tue, 14 Jan 2020 15:45:00 +0400 Subject: [PATCH 4/6] pageSelector is always set --- system/Pager/PagerRenderer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Pager/PagerRenderer.php b/system/Pager/PagerRenderer.php index f86dcda26214..d298d64a05e7 100644 --- a/system/Pager/PagerRenderer.php +++ b/system/Pager/PagerRenderer.php @@ -116,7 +116,7 @@ public function __construct(array $details) $this->uri = $details['uri']; $this->pageCount = $details['pageCount']; $this->segment = $details['segment'] ?? 0; - $this->pageSelector = $details['pageSelector'] ?? 'page'; + $this->pageSelector = $details['pageSelector']; } //-------------------------------------------------------------------- From 86ac8c8b9de51a03e10031ee6e67465babe034e0 Mon Sep 17 00:00:00 2001 From: MashinaMashina Date: Tue, 14 Jan 2020 17:00:53 +0400 Subject: [PATCH 5/6] Remove setPageSelector(), fix backward compatibility --- system/Pager/Pager.php | 21 --------------------- system/Pager/PagerRenderer.php | 2 +- 2 files changed, 1 insertion(+), 22 deletions(-) diff --git a/system/Pager/Pager.php b/system/Pager/Pager.php index c36a29f55a9c..3ec23ce07b83 100644 --- a/system/Pager/Pager.php +++ b/system/Pager/Pager.php @@ -234,27 +234,6 @@ 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; - } - //-------------------------------------------------------------------- /** diff --git a/system/Pager/PagerRenderer.php b/system/Pager/PagerRenderer.php index d298d64a05e7..f86dcda26214 100644 --- a/system/Pager/PagerRenderer.php +++ b/system/Pager/PagerRenderer.php @@ -116,7 +116,7 @@ public function __construct(array $details) $this->uri = $details['uri']; $this->pageCount = $details['pageCount']; $this->segment = $details['segment'] ?? 0; - $this->pageSelector = $details['pageSelector']; + $this->pageSelector = $details['pageSelector'] ?? 'page'; } //-------------------------------------------------------------------- From 309afd1854dbe56f0c38976e0ee0fc1486541490 Mon Sep 17 00:00:00 2001 From: MashinaMashina Date: Tue, 14 Jan 2020 17:43:18 +0400 Subject: [PATCH 6/6] Pager new tests --- tests/system/Pager/PagerTest.php | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/tests/system/Pager/PagerTest.php b/tests/system/Pager/PagerTest.php index a507d48ae2ae..76ce1e70e0a7 100644 --- a/tests/system/Pager/PagerTest.php +++ b/tests/system/Pager/PagerTest.php @@ -203,12 +203,12 @@ public function testGetTotalPagesCalcsCorrectValue() public function testGetNextURIUsesCurrentURI() { - $_GET['page'] = 2; + $_GET['page_foo'] = 2; $this->pager->store('foo', 2, 12, 70); $expected = current_url(true); - $expected = (string)$expected->setQuery('page=3'); + $expected = (string)$expected->setQuery('page_foo=3'); $this->assertEquals((string)$expected, $this->pager->getNextPageURI('foo')); } @@ -225,19 +225,19 @@ public function testGetNextURICorrectOnFirstPage() $this->pager->store('foo', 1, 12, 70); $expected = current_url(true); - $expected = (string)$expected->setQuery('page=2'); + $expected = (string)$expected->setQuery('page_foo=2'); $this->assertEquals($expected, $this->pager->getNextPageURI('foo')); } public function testGetPreviousURIUsesCurrentURI() { - $_GET['page'] = 2; + $_GET['page_foo'] = 2; $this->pager->store('foo', 2, 12, 70); $expected = current_url(true); - $expected = (string)$expected->setQuery('page=1'); + $expected = (string)$expected->setQuery('page_foo=1'); $this->assertEquals((string)$expected, $this->pager->getPreviousPageURI('foo')); } @@ -252,14 +252,14 @@ public function testGetNextURIReturnsNullOnFirstPage() public function testGetNextURIWithQueryStringUsesCurrentURI() { $_GET = [ - 'page' => 3, - 'status' => 1, + 'page_foo' => 3, + 'status' => 1, ]; $expected = current_url(true); $expected = (string)$expected->setQueryArray($_GET); - $this->pager->store('foo', $_GET['page'] - 1, 12, 70); + $this->pager->store('foo', $_GET['page_foo'] - 1, 12, 70); $this->assertEquals((string)$expected, $this->pager->getNextPageURI('foo')); } @@ -267,13 +267,13 @@ public function testGetNextURIWithQueryStringUsesCurrentURI() public function testGetPreviousURIWithQueryStringUsesCurrentURI() { $_GET = [ - 'page' => 1, - 'status' => 1, + 'page_foo' => 1, + 'status' => 1, ]; $expected = current_url(true); $expected = (string)$expected->setQueryArray($_GET); - $this->pager->store('foo', $_GET['page'] + 1, 12, 70); + $this->pager->store('foo', $_GET['page_foo'] + 1, 12, 70); $this->assertEquals((string)$expected, $this->pager->getPreviousPageURI('foo')); } @@ -383,12 +383,12 @@ public function testBasedURI() $this->config = new Pager(); $this->pager = new \CodeIgniter\Pager\Pager($this->config, Services::renderer()); - $_GET['page'] = 2; + $_GET['page_foo'] = 2; $this->pager->store('foo', 2, 12, 70); $expected = current_url(true); - $expected = (string)$expected->setQuery('page=1'); + $expected = (string)$expected->setQuery('page_foo=1'); $this->assertEquals((string)$expected, $this->pager->getPreviousPageURI('foo')); }