From b77b9863cad88f6d1734d6eb8dbe9f6d423a8aeb Mon Sep 17 00:00:00 2001 From: Kyle Alexander Buan <79783707+kylxbn@users.noreply.github.com> Date: Tue, 1 Mar 2022 14:00:46 +0800 Subject: [PATCH 1/3] Make page_count and current accessible from Twig There are times when it is necessary to show the page_count and current page in Twig templates (for example, "2/6" to mean "page 2 of 6"). This pull request aims to expose variables `current`, `items_per_page`, and `page_count`. I am not good with making pull requests, so if there is any mistake, please tell me, and I will try my best to fix it. Thanks! --- classes/plugin/PaginationHelper.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/classes/plugin/PaginationHelper.php b/classes/plugin/PaginationHelper.php index 9c10a87..1edf8b2 100644 --- a/classes/plugin/PaginationHelper.php +++ b/classes/plugin/PaginationHelper.php @@ -122,6 +122,30 @@ public function nextUrl() return null; } + + /** + * Return the current page + */ + public function currentPage() + { + return $this->current; + } + + /** + * Return the items per page + */ + public function itemsPerPage() + { + return $this->items_per_page; + } + + /** + * Return the total number of pages + */ + public function pageCount() + { + return $this->page_count; + } public function params() { From 1598b2821ee63ac3da59c42ae557b3693c98336a Mon Sep 17 00:00:00 2001 From: Kyle Alexander Buan <79783707+kylxbn@users.noreply.github.com> Date: Wed, 2 Mar 2022 13:46:38 +0800 Subject: [PATCH 2/3] Add @return annotations I had to use `intval()` because `ceil()` returns a `double` by default, so I wanted to cast it to `int` first. --- classes/plugin/PaginationHelper.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/classes/plugin/PaginationHelper.php b/classes/plugin/PaginationHelper.php index 1edf8b2..f0b98ed 100644 --- a/classes/plugin/PaginationHelper.php +++ b/classes/plugin/PaginationHelper.php @@ -56,7 +56,7 @@ public function __construct(Collection $collection) } $this->items_per_page = $params['limit']; - $this->page_count = ceil($collection->count() / $this->items_per_page); + $this->page_count = intval(ceil($collection->count() / $this->items_per_page)); for ($x=1; $x <= $this->page_count; $x++) { if ($x === 1) { @@ -125,6 +125,8 @@ public function nextUrl() /** * Return the current page + * + * @return int */ public function currentPage() { @@ -133,6 +135,8 @@ public function currentPage() /** * Return the items per page + * + * @return int */ public function itemsPerPage() { @@ -141,6 +145,8 @@ public function itemsPerPage() /** * Return the total number of pages + * + * @return int */ public function pageCount() { From cca92e0632aaab0b0cd82f741e3ab200170530b9 Mon Sep 17 00:00:00 2001 From: Kyle Alexander Buan <79783707+kylxbn@users.noreply.github.com> Date: Wed, 2 Mar 2022 17:36:42 +0800 Subject: [PATCH 3/3] Use int cast instead of intval() Sorry! --- classes/plugin/PaginationHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/plugin/PaginationHelper.php b/classes/plugin/PaginationHelper.php index f0b98ed..6f87278 100644 --- a/classes/plugin/PaginationHelper.php +++ b/classes/plugin/PaginationHelper.php @@ -56,7 +56,7 @@ public function __construct(Collection $collection) } $this->items_per_page = $params['limit']; - $this->page_count = intval(ceil($collection->count() / $this->items_per_page)); + $this->page_count = (int)ceil($collection->count() / $this->items_per_page); for ($x=1; $x <= $this->page_count; $x++) { if ($x === 1) {