Refactor generic tables and page index classes #26
@ -6,11 +6,12 @@
|
|||||||
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
|
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
|
||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
class GenericTable extends PageIndex
|
class GenericTable
|
||||||
{
|
{
|
||||||
protected $header = [];
|
protected $header = [];
|
||||||
protected $body = [];
|
protected $body = [];
|
||||||
protected $page_index = [];
|
protected $pageIndex = null;
|
||||||
|
protected $currentPage = 1;
|
||||||
|
|
||||||
protected $title;
|
protected $title;
|
||||||
protected $title_class;
|
protected $title_class;
|
||||||
@ -36,17 +37,19 @@ class GenericTable extends PageIndex
|
|||||||
// Make sure we know whether we can actually sort on something.
|
// Make sure we know whether we can actually sort on something.
|
||||||
$this->tableIsSortable = !empty($options['base_url']);
|
$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'] : []));
|
$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->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.
|
// Figure out where to start.
|
||||||
$this->start = empty($options['start']) || !is_numeric($options['start']) || $options['start'] < 0 || $options['start'] > $this->recordCount ? 0 : $options['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...
|
// Let's bear a few things in mind...
|
||||||
$this->base_url = $options['base_url'];
|
$this->base_url = $options['base_url'];
|
||||||
|
|
||||||
@ -67,9 +70,10 @@ class GenericTable extends PageIndex
|
|||||||
// Okay, now for the column headers...
|
// Okay, now for the column headers...
|
||||||
$this->generateColumnHeaders($options);
|
$this->generateColumnHeaders($options);
|
||||||
|
|
||||||
// Generate a pagination if requested
|
// Should we create a page index?
|
||||||
if ($this->needsPageIndex)
|
$needsPageIndex = !empty($this->items_per_page) && $this->recordCount > $this->items_per_page;
|
||||||
$this->generatePageIndex();
|
if ($needsPageIndex)
|
||||||
|
$this->generatePageIndex($options);
|
||||||
|
|
||||||
// Not a single row in sight?
|
// Not a single row in sight?
|
||||||
if (empty($rows))
|
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)
|
private function parseAllRows($rows, $options)
|
||||||
{
|
{
|
||||||
foreach ($rows as $i => $row)
|
foreach ($rows as $i => $row)
|
||||||
@ -220,12 +237,6 @@ class GenericTable extends PageIndex
|
|||||||
return $this->start;
|
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()
|
public function getHeader()
|
||||||
{
|
{
|
||||||
return $this->header;
|
return $this->header;
|
||||||
@ -236,6 +247,16 @@ class GenericTable extends PageIndex
|
|||||||
return $this->body;
|
return $this->body;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getCurrentPage()
|
||||||
|
{
|
||||||
|
return $this->currentPage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getPageIndex()
|
||||||
|
{
|
||||||
|
return $this->pageIndex;
|
||||||
|
}
|
||||||
|
|
||||||
public function getTitle()
|
public function getTitle()
|
||||||
{
|
{
|
||||||
return $this->title;
|
return $this->title;
|
||||||
|
@ -8,23 +8,35 @@
|
|||||||
|
|
||||||
class PageIndex
|
class PageIndex
|
||||||
{
|
{
|
||||||
protected $page_index = [];
|
protected $base_url;
|
||||||
protected $current_page = 1;
|
protected $current_page = 1;
|
||||||
|
protected $index_class = 'pagination';
|
||||||
protected $items_per_page = 0;
|
protected $items_per_page = 0;
|
||||||
protected $needsPageIndex = false;
|
protected $needsPageIndex = false;
|
||||||
protected $num_pages = 1;
|
protected $num_pages = 1;
|
||||||
protected $recordCount = 0;
|
protected $page_index = [];
|
||||||
protected $start = 0;
|
|
||||||
protected $sort_order = null;
|
|
||||||
protected $sort_direction = null;
|
|
||||||
protected $base_url;
|
|
||||||
protected $index_class = 'pagination';
|
|
||||||
protected $page_slug = '%AMP%page=%PAGE%';
|
protected $page_slug = '%AMP%page=%PAGE%';
|
||||||
|
protected $recordCount = 0;
|
||||||
|
protected $sort_direction = null;
|
||||||
|
protected $sort_order = null;
|
||||||
|
protected $start = 0;
|
||||||
|
|
||||||
public function __construct($options)
|
public function __construct($options)
|
||||||
{
|
{
|
||||||
foreach ($options as $key => $value)
|
static $neededKeys = ['base_url', 'items_per_page', 'recordCount'];
|
||||||
$this->$key = $value;
|
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();
|
$this->generatePageIndex();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,12 +6,15 @@
|
|||||||
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
|
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
|
||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
class TabularData extends Pagination
|
class TabularData extends SubTemplate
|
||||||
{
|
{
|
||||||
public function __construct(GenericTable $table)
|
public function __construct(GenericTable $table)
|
||||||
{
|
{
|
||||||
$this->_t = $table;
|
$this->_t = $table;
|
||||||
parent::__construct($table);
|
|
||||||
|
$pageIndex = $table->getPageIndex();
|
||||||
|
if ($pageIndex)
|
||||||
|
$this->pager = new Pagination($pageIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function html_content()
|
protected function html_content()
|
||||||
@ -25,7 +28,8 @@ class TabularData extends Pagination
|
|||||||
<h2>', $title, '</h2>';
|
<h2>', $title, '</h2>';
|
||||||
|
|
||||||
// Showing a page index?
|
// Showing a page index?
|
||||||
parent::html_content();
|
if (isset($this->pager))
|
||||||
|
$this->pager->html_content();
|
||||||
|
|
||||||
// Maybe even a small form?
|
// Maybe even a small form?
|
||||||
if (isset($this->_t->form_above))
|
if (isset($this->_t->form_above))
|
||||||
@ -87,7 +91,8 @@ class TabularData extends Pagination
|
|||||||
$this->showForm($this->_t->form_below);
|
$this->showForm($this->_t->form_below);
|
||||||
|
|
||||||
// Showing a page index?
|
// Showing a page index?
|
||||||
parent::html_content();
|
if (isset($this->pager))
|
||||||
|
$this->pager->html_content();
|
||||||
|
|
||||||
echo '
|
echo '
|
||||||
</div>';
|
</div>';
|
||||||
|
Loading…
Reference in New Issue
Block a user