Refactor generic tables and page index classes #26

Merged
Roflin merged 9 commits from refactor-tables into master 2021-05-17 20:19:18 +02:00
2 changed files with 16 additions and 11 deletions
Showing only changes of commit 681af07985 - Show all commits

View File

@ -118,6 +118,7 @@ class GenericTable
'base_url' => $this->base_url, 'base_url' => $this->base_url,
'index_class' => $options['index_class'] ?? '', 'index_class' => $options['index_class'] ?? '',
'items_per_page' => $this->items_per_page, 'items_per_page' => $this->items_per_page,
'linkBuilder' => [$this, 'getLink'],
'recordCount' => $this->recordCount, 'recordCount' => $this->recordCount,
'sort_direction' => $this->sort_direction, 'sort_direction' => $this->sort_direction,
'sort_order' => $this->sort_order, 'sort_order' => $this->sort_order,

View File

@ -12,6 +12,7 @@ class PageIndex
protected $current_page = 1; protected $current_page = 1;
protected $index_class = 'pagination'; protected $index_class = 'pagination';
protected $items_per_page = 0; protected $items_per_page = 0;
private $linkBuilder;
protected $needsPageIndex = false; protected $needsPageIndex = false;
protected $num_pages = 1; protected $num_pages = 1;
protected $page_index = []; protected $page_index = [];
@ -32,7 +33,7 @@ class PageIndex
$this->$key = $options[$key]; $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) foreach ($optionalKeys as $key)
if (isset($options[$key])) if (isset($options[$key]))
$this->$key = $options[$key]; $this->$key = $options[$key];
@ -40,6 +41,14 @@ class PageIndex
$this->generatePageIndex(); $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() protected function generatePageIndex()
{ {
/* /*
@ -80,7 +89,7 @@ class PageIndex
$this->page_index[$p] = [ $this->page_index[$p] = [
'index' => $p, 'index' => $p,
'is_selected' => $this->current_page == $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. // The center of the page index.
@ -93,7 +102,7 @@ class PageIndex
$this->page_index[$center] = [ $this->page_index[$center] = [
'index' => $center, 'index' => $center,
'is_selected' => $this->current_page == $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] = [ $this->page_index[$p] = [
'index' => $p, 'index' => $p,
'is_selected' => $this->current_page == $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. // The center of the page index.
@ -119,7 +128,7 @@ class PageIndex
$this->page_index[$center] = [ $this->page_index[$center] = [
'index' => $center, 'index' => $center,
'is_selected' => $this->current_page == $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] = [ $this->page_index[$p] = [
'index' => $p, 'index' => $p,
'is_selected' => $this->current_page == $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? // Previous page?
@ -169,11 +178,6 @@ class PageIndex
return $url; return $url;
} }
public function getArray()
{
return $this->page_index;
}
public function getPageIndex() public function getPageIndex()
{ {
return $this->page_index; return $this->page_index;