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:
Aaron van Geffen 2021-02-17 16:41:58 +01:00
parent 96937b6952
commit cba42a9129
3 changed files with 66 additions and 28 deletions

View File

@ -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;

View File

@ -8,23 +8,35 @@
class PageIndex
{
protected $page_index = [];
protected $base_url;
protected $current_page = 1;
protected $index_class = 'pagination';
protected $items_per_page = 0;
protected $needsPageIndex = false;
protected $num_pages = 1;
protected $recordCount = 0;
protected $start = 0;
protected $sort_order = null;
protected $sort_direction = null;
protected $base_url;
protected $index_class = 'pagination';
protected $page_index = [];
protected $page_slug = '%AMP%page=%PAGE%';
protected $recordCount = 0;
protected $sort_direction = null;
protected $sort_order = null;
protected $start = 0;
public function __construct($options)
{
foreach ($options as $key => $value)
$this->$key = $value;
static $neededKeys = ['base_url', 'items_per_page', 'recordCount'];
foreach ($neededKeys as $key)
{
if (!isset($options[$key]))
throw new Exception('PageIndex: argument ' . $key . ' missing in options');
$this->$key = $options[$key];
}
static $optionalKeys = ['index_class', 'page_slug', 'sort_direction', 'sort_order', 'start'];
foreach ($optionalKeys as $key)
if (isset($options[$key]))
$this->$key = $options[$key];
$this->generatePageIndex();
}

View File

@ -6,12 +6,15 @@
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
*****************************************************************************/
class TabularData extends Pagination
class TabularData extends SubTemplate
{
public function __construct(GenericTable $table)
{
$this->_t = $table;
parent::__construct($table);
$pageIndex = $table->getPageIndex();
if ($pageIndex)
$this->pager = new Pagination($pageIndex);
}
protected function html_content()
@ -25,7 +28,8 @@ class TabularData extends Pagination
<h2>', $title, '</h2>';
// Showing a page index?
parent::html_content();
if (isset($this->pager))
$this->pager->html_content();
// Maybe even a small form?
if (isset($this->_t->form_above))
@ -87,7 +91,8 @@ class TabularData extends Pagination
$this->showForm($this->_t->form_below);
// Showing a page index?
parent::html_content();
if (isset($this->pager))
$this->pager->html_content();
echo '
</div>';