From 681af0798500910930efb906ae949167fcda1e67 Mon Sep 17 00:00:00 2001 From: Aaron van Geffen Date: Wed, 17 Feb 2021 20:06:33 +0100 Subject: [PATCH] Pass builder function for custom pagination link generation. This makes pagination for GenericTables work properly again. --- models/GenericTable.php | 1 + models/PageIndex.php | 26 +++++++++++++++----------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/models/GenericTable.php b/models/GenericTable.php index 89f6c4c..d8ac071 100644 --- a/models/GenericTable.php +++ b/models/GenericTable.php @@ -118,6 +118,7 @@ class GenericTable 'base_url' => $this->base_url, 'index_class' => $options['index_class'] ?? '', 'items_per_page' => $this->items_per_page, + 'linkBuilder' => [$this, 'getLink'], 'recordCount' => $this->recordCount, 'sort_direction' => $this->sort_direction, 'sort_order' => $this->sort_order, diff --git a/models/PageIndex.php b/models/PageIndex.php index a0ebced..fe8a1a0 100644 --- a/models/PageIndex.php +++ b/models/PageIndex.php @@ -12,6 +12,7 @@ class PageIndex protected $current_page = 1; protected $index_class = 'pagination'; protected $items_per_page = 0; + private $linkBuilder; protected $needsPageIndex = false; protected $num_pages = 1; protected $page_index = []; @@ -32,7 +33,7 @@ class PageIndex $this->$key = $options[$key]; } - static $optionalKeys = ['index_class', 'page_slug', 'sort_direction', 'sort_order', 'start']; + static $optionalKeys = ['index_class', 'linkBuilder', 'page_slug', 'sort_direction', 'sort_order', 'start']; foreach ($optionalKeys as $key) if (isset($options[$key])) $this->$key = $options[$key]; @@ -40,6 +41,14 @@ class PageIndex $this->generatePageIndex(); } + private function buildLink($start = null, $order = null, $dir = null) + { + if (isset($this->linkBuilder)) + return call_user_func($this->linkBuilder, $start, $order, $dir); + else + return $this->getLink($start, $order, $dir); + } + protected function generatePageIndex() { /* @@ -80,7 +89,7 @@ class PageIndex $this->page_index[$p] = [ 'index' => $p, 'is_selected' => $this->current_page == $p, - 'href'=> $this->getLink(($p - 1) * $this->items_per_page, $this->sort_order, $this->sort_direction), + 'href'=> $this->buildLink(($p - 1) * $this->items_per_page, $this->sort_order, $this->sort_direction), ]; // The center of the page index. @@ -93,7 +102,7 @@ class PageIndex $this->page_index[$center] = [ 'index' => $center, 'is_selected' => $this->current_page == $center, - 'href'=> $this->getLink(($center - 1) * $this->items_per_page, $this->sort_order, $this->sort_direction), + 'href'=> $this->buildLink(($center - 1) * $this->items_per_page, $this->sort_order, $this->sort_direction), ]; } @@ -106,7 +115,7 @@ class PageIndex $this->page_index[$p] = [ 'index' => $p, 'is_selected' => $this->current_page == $p, - 'href'=> $this->getLink(($p - 1) * $this->items_per_page, $this->sort_order, $this->sort_direction), + 'href'=> $this->buildLink(($p - 1) * $this->items_per_page, $this->sort_order, $this->sort_direction), ]; // The center of the page index. @@ -119,7 +128,7 @@ class PageIndex $this->page_index[$center] = [ 'index' => $center, 'is_selected' => $this->current_page == $center, - 'href'=> $this->getLink(($center - 1) * $this->items_per_page, $this->sort_order, $this->sort_direction), + 'href'=> $this->buildLink(($center - 1) * $this->items_per_page, $this->sort_order, $this->sort_direction), ]; } @@ -132,7 +141,7 @@ class PageIndex $this->page_index[$p] = [ 'index' => $p, 'is_selected' => $this->current_page == $p, - 'href'=> $this->getLink(($p - 1) * $this->items_per_page, $this->sort_order, $this->sort_direction), + 'href'=> $this->buildLink(($p - 1) * $this->items_per_page, $this->sort_order, $this->sort_direction), ]; // Previous page? @@ -169,11 +178,6 @@ class PageIndex return $url; } - public function getArray() - { - return $this->page_index; - } - public function getPageIndex() { return $this->page_index;