Refactor GenericTable to use PageIndex rather than inherit from it
This has on my todo list for years... I'm glad to finally get around to it.
This commit is contained in:
@@ -6,11 +6,12 @@
|
||||
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
|
||||
*****************************************************************************/
|
||||
|
||||
class GenericTable extends PageIndex
|
||||
class GenericTable
|
||||
{
|
||||
protected $header = [];
|
||||
protected $body = [];
|
||||
protected $page_index = [];
|
||||
protected $pageIndex = null;
|
||||
protected $currentPage = 1;
|
||||
|
||||
protected $title;
|
||||
protected $title_class;
|
||||
@@ -36,17 +37,19 @@ class GenericTable extends PageIndex
|
||||
// Make sure we know whether we can actually sort on something.
|
||||
$this->tableIsSortable = !empty($options['base_url']);
|
||||
|
||||
// How much stuff do we have?
|
||||
// How much data do we have?
|
||||
$this->recordCount = $options['get_count'](...(!empty($options['get_count_params']) ? $options['get_count_params'] : []));
|
||||
|
||||
// Should we create a page index?
|
||||
// How much data do we need to retrieve?
|
||||
$this->items_per_page = !empty($options['items_per_page']) ? $options['items_per_page'] : 30;
|
||||
$this->needsPageIndex = !empty($this->items_per_page) && $this->recordCount > $this->items_per_page;
|
||||
$this->index_class = $options['index_class'] ?? '';
|
||||
|
||||
// Figure out where to start.
|
||||
$this->start = empty($options['start']) || !is_numeric($options['start']) || $options['start'] < 0 || $options['start'] > $this->recordCount ? 0 : $options['start'];
|
||||
|
||||
// Figure out where we are on the whole, too.
|
||||
$numPages = ceil($this->recordCount / $this->items_per_page);
|
||||
$this->currentPage = min(ceil($this->start / $this->items_per_page) + 1, $numPages);
|
||||
|
||||
// Let's bear a few things in mind...
|
||||
$this->base_url = $options['base_url'];
|
||||
|
||||
@@ -67,9 +70,10 @@ class GenericTable extends PageIndex
|
||||
// Okay, now for the column headers...
|
||||
$this->generateColumnHeaders($options);
|
||||
|
||||
// Generate a pagination if requested
|
||||
if ($this->needsPageIndex)
|
||||
$this->generatePageIndex();
|
||||
// Should we create a page index?
|
||||
$needsPageIndex = !empty($this->items_per_page) && $this->recordCount > $this->items_per_page;
|
||||
if ($needsPageIndex)
|
||||
$this->generatePageIndex($options);
|
||||
|
||||
// Not a single row in sight?
|
||||
if (empty($rows))
|
||||
@@ -108,6 +112,19 @@ class GenericTable extends PageIndex
|
||||
}
|
||||
}
|
||||
|
||||
private function generatePageIndex($options)
|
||||
{
|
||||
$this->pageIndex = new PageIndex([
|
||||
'base_url' => $this->base_url,
|
||||
'index_class' => $options['index_class'] ?? '',
|
||||
'items_per_page' => $this->items_per_page,
|
||||
'recordCount' => $this->recordCount,
|
||||
'sort_direction' => $this->sort_direction,
|
||||
'sort_order' => $this->sort_order,
|
||||
'start' => $this->start,
|
||||
]);
|
||||
}
|
||||
|
||||
private function parseAllRows($rows, $options)
|
||||
{
|
||||
foreach ($rows as $i => $row)
|
||||
@@ -220,12 +237,6 @@ class GenericTable extends PageIndex
|
||||
return $this->start;
|
||||
}
|
||||
|
||||
public function getArray()
|
||||
{
|
||||
// Makes no sense to call it for a table, but inherits from PageIndex due to poor design, sorry.
|
||||
throw new Exception('Function call is ambiguous.');
|
||||
}
|
||||
|
||||
public function getHeader()
|
||||
{
|
||||
return $this->header;
|
||||
@@ -236,6 +247,16 @@ class GenericTable extends PageIndex
|
||||
return $this->body;
|
||||
}
|
||||
|
||||
public function getCurrentPage()
|
||||
{
|
||||
return $this->currentPage;
|
||||
}
|
||||
|
||||
public function getPageIndex()
|
||||
{
|
||||
return $this->pageIndex;
|
||||
}
|
||||
|
||||
public function getTitle()
|
||||
{
|
||||
return $this->title;
|
||||
|
||||
Reference in New Issue
Block a user