Merge pull request 'Refactor generic tables and page index classes' (#26) from refactor-tables into master

Reviewed-on: Public/pics#26
This commit is contained in:
Roflin 2021-05-17 20:19:18 +02:00
commit 4863561129
4 changed files with 195 additions and 163 deletions

View File

@ -6,19 +6,16 @@
* Kabuki CMS (C) 2013-2015, Aaron van Geffen * Kabuki CMS (C) 2013-2015, Aaron van Geffen
*****************************************************************************/ *****************************************************************************/
class GenericTable extends PageIndex class GenericTable
{ {
protected $header = []; private $header = [];
protected $body = []; private $body = [];
protected $page_index = []; private $pageIndex = null;
private $currentPage = 1;
protected $title; private $title;
protected $title_class; private $title_class;
protected $tableIsSortable = false; private $tableIsSortable = false;
protected $recordCount;
protected $needsPageIndex = false;
protected $current_page;
protected $num_pages;
public $form_above; public $form_above;
public $form_below; public $form_below;
@ -36,34 +33,32 @@ 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 = call_user_func_array($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 = isset($options['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'];
// This should be set at all times, too.
if (empty($options['no_items_label']))
$options['no_items_label'] = '';
// Gather parameters for the data gather function first. // Gather parameters for the data gather function first.
$parameters = [$this->start, $this->items_per_page, $options['sort_order'], $options['sort_direction']]; $parameters = [$this->start, $this->items_per_page, $options['sort_order'], $options['sort_direction']];
if (!empty($options['get_data_params']) && is_array($options['get_data_params'])) if (!empty($options['get_data_params']) && is_array($options['get_data_params']))
$parameters = array_merge($parameters, $options['get_data_params']); $parameters = array_merge($parameters, $options['get_data_params']);
// Okay, let's fetch the data! // Okay, let's fetch the data!
$data = call_user_func_array($options['get_data'], $parameters); $data = $options['get_data'](...$parameters);
// Clean up a bit. // Extract data into local variables.
$rows = $data['rows']; $rawRowData = $data['rows'];
$this->sort_order = $data['order']; $this->sort_order = $data['order'];
$this->sort_direction = $data['direction']; $this->sort_direction = $data['direction'];
unset($data); unset($data);
@ -71,24 +66,24 @@ 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? // Process the data to be shown into rows.
if (empty($rows)) if (!empty($rawRowData))
$this->body = $options['no_items_label']; $this->processAllRows($rawRowData, $options);
// Otherwise, parse it all!
else else
$this->parseAllRows($rows, $options); $this->body = $options['no_items_label'] ?? '';
// Got a title? // Got a title?
$this->title = isset($options['title']) ? htmlentities($options['title']) : ''; $this->title = $options['title'] ?? '';
$this->title_class = isset($options['title_class']) ? $options['title_class'] : ''; $this->title_class = $options['title_class'] ?? '';
// Maybe even a form or two? // Maybe even a form or two?
$this->form_above = isset($options['form_above']) ? $options['form_above'] : (isset($options['form']) ? $options['form'] : null); $this->form_above = $options['form_above'] ?? $options['form'] ?? null;
$this->form_below = isset($options['form_below']) ? $options['form_below'] : (isset($options['form']) ? $options['form'] : null); $this->form_below = $options['form_below'] ?? $options['form'] ?? null;
} }
private function generateColumnHeaders($options) private function generateColumnHeaders($options)
@ -98,10 +93,13 @@ class GenericTable extends PageIndex
if (empty($column['header'])) if (empty($column['header']))
continue; continue;
$isSortable = $this->tableIsSortable && !empty($column['is_sortable']);
$sortDirection = $key == $this->sort_order && $this->sort_direction === 'up' ? 'down' : 'up';
$header = [ $header = [
'class' => isset($column['class']) ? $column['class'] : '', 'class' => isset($column['class']) ? $column['class'] : '',
'colspan' => !empty($column['header_colspan']) ? $column['header_colspan'] : 1, 'colspan' => !empty($column['header_colspan']) ? $column['header_colspan'] : 1,
'href' => !$this->tableIsSortable || empty($column['is_sortable']) ? '' : $this->getLink($this->start, $key, $key == $this->sort_order && $this->sort_direction == 'up' ? 'down' : 'up'), 'href' => $isSortable ? $this->getLink($this->start, $key, $sortDirection) : null,
'label' => $column['header'], 'label' => $column['header'],
'scope' => 'col', 'scope' => 'col',
'sort_mode' => $key == $this->sort_order ? $this->sort_direction : null, 'sort_mode' => $key == $this->sort_order ? $this->sort_direction : null,
@ -112,99 +110,18 @@ class GenericTable extends PageIndex
} }
} }
private function parseAllRows($rows, $options) private function generatePageIndex($options)
{ {
foreach ($rows as $i => $row) $this->pageIndex = new PageIndex([
{ 'base_url' => $this->base_url,
$className = $i & 1 ? 'even' : 'odd'; 'index_class' => $options['index_class'] ?? '',
if (isset($options['row_classifier'])) 'items_per_page' => $this->items_per_page,
$className .= $options['row_classifier']($row); 'linkBuilder' => [$this, 'getLink'],
'recordCount' => $this->recordCount,
$newRow = [ 'sort_direction' => $this->sort_direction,
'class' => $className, 'sort_order' => $this->sort_order,
'cells' => [], 'start' => $this->start,
]; ]);
foreach ($options['columns'] as $column)
{
if (isset($column['enabled']) && $column['enabled'] == false)
continue;
// The hard way?
if (isset($column['parse']))
{
if (!isset($column['parse']['type']))
$column['parse']['type'] = 'value';
// Parse the basic value first.
switch ($column['parse']['type'])
{
// value: easy as pie.
default:
case 'value':
$value = $row[$column['parse']['data']];
break;
// sprintf: filling the gaps!
case 'sprintf':
$parameters = [$column['parse']['data']['pattern']];
foreach ($column['parse']['data']['arguments'] as $identifier)
$parameters[] = $row[$identifier];
$value = call_user_func_array('sprintf', $parameters);
break;
// timestamps: let's make them readable!
case 'timestamp':
if (empty($column['parse']['data']['pattern']) || $column['parse']['data']['pattern'] === 'long')
$pattern = '%F %H:%M';
elseif ($column['parse']['data']['pattern'] === 'short')
$pattern = '%F';
else
$pattern = $column['parse']['data']['pattern'];
if (!is_numeric($row[$column['parse']['data']['timestamp']]))
$timestamp = strtotime($row[$column['parse']['data']['timestamp']]);
else
$timestamp = (int) $row[$column['parse']['data']['timestamp']];
if (isset($column['parse']['data']['if_null']) && $timestamp == 0)
$value = $column['parse']['data']['if_null'];
else
$value = strftime($pattern, $timestamp);
break;
// function: the flexible way!
case 'function':
$value = $column['parse']['data']($row);
break;
}
// Generate a link, if requested.
if (!empty($column['parse']['link']))
{
// First, generate the replacement variables.
$keys = array_keys($row);
$values = array_values($row);
foreach ($keys as $keyKey => $keyValue)
$keys[$keyKey] = '{' . strtoupper($keyValue) . '}';
$value = '<a href="' . str_replace($keys, $values, $column['parse']['link']) . '">' . $value . '</a>';
}
}
// The easy way!
else
$value = $row[$column['value']];
// Append the cell to the row.
$newRow['cells'][] = [
'width' => !empty($column['cell_width']) && is_int($column['cell_width']) ? $column['cell_width'] : null,
'value' => $value,
];
}
// Append the new row in the body.
$this->body[] = $newRow;
}
} }
public function getLink($start = null, $order = null, $dir = null) public function getLink($start = null, $order = null, $dir = null)
@ -224,12 +141,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;
@ -240,6 +151,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;
@ -249,4 +170,94 @@ class GenericTable extends PageIndex
{ {
return $this->title_class; return $this->title_class;
} }
private function processAllRows($rows, $options)
{
foreach ($rows as $i => $row)
{
$newRow = [
'cells' => [],
];
foreach ($options['columns'] as $column)
{
// Process data for this particular cell.
if (isset($column['parse']))
$value = self::processCell($column['parse'], $row);
else
$value = $row[$column['value']];
// Append the cell to the row.
$newRow['cells'][] = [
'value' => $value,
];
}
// Append the new row in the body.
$this->body[] = $newRow;
}
}
private function processCell($options, $rowData)
{
if (!isset($options['type']))
$options['type'] = 'value';
// Parse the basic value first.
switch ($options['type'])
{
// Basic option: simply take a use a particular data property.
case 'value':
$value = htmlspecialchars($rowData[$options['data']]);
break;
// Processing via a lambda function.
case 'function':
$value = $options['data']($rowData);
break;
// Using sprintf to fill out a particular pattern.
case 'sprintf':
$parameters = [$options['data']['pattern']];
foreach ($options['data']['arguments'] as $identifier)
$parameters[] = $rowData[$identifier];
$value = sprintf(...$parameters);
break;
// Timestamps get custom treatment.
case 'timestamp':
if (empty($options['data']['pattern']) || $options['data']['pattern'] === 'long')
$pattern = '%F %H:%M';
elseif ($options['data']['pattern'] === 'short')
$pattern = '%F';
else
$pattern = $options['data']['pattern'];
if (!is_numeric($rowData[$options['data']['timestamp']]))
$timestamp = strtotime($rowData[$options['data']['timestamp']]);
else
$timestamp = (int) $rowData[$options['data']['timestamp']];
if (isset($options['data']['if_null']) && $timestamp == 0)
$value = $options['data']['if_null'];
else
$value = strftime($pattern, $timestamp);
break;
}
// Generate a link, if requested.
if (!empty($options['link']))
{
// First, generate the replacement variables.
$keys = array_keys($rowData);
$values = array_values($rowData);
foreach ($keys as $keyKey => $keyValue)
$keys[$keyKey] = '{' . strtoupper($keyValue) . '}';
$value = '<a href="' . str_replace($keys, $values, $options['link']) . '">' . $value . '</a>';
}
return $value;
}
} }

View File

@ -8,26 +8,47 @@
class PageIndex class PageIndex
{ {
protected $page_index = []; private $base_url;
protected $current_page = 1; private $current_page = 1;
protected $items_per_page = 0; private $index_class = 'pagination';
protected $needsPageIndex = false; private $items_per_page = 0;
protected $num_pages = 1; private $linkBuilder;
protected $recordCount = 0; private $needsPageIndex = false;
protected $start = 0; private $num_pages = 1;
protected $sort_order = null; private $page_index = [];
protected $sort_direction = null; private $page_slug = '%AMP%page=%PAGE%';
protected $base_url; private $recordCount = 0;
protected $index_class = 'pagination'; private $sort_direction = null;
protected $page_slug = '%AMP%page=%PAGE%'; private $sort_order = null;
private $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', 'linkBuilder', 'page_slug', 'sort_direction', 'sort_order', 'start'];
foreach ($optionalKeys as $key)
if (isset($options[$key]))
$this->$key = $options[$key];
$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()
{ {
/* /*
@ -68,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.
@ -81,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),
]; ];
} }
@ -94,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.
@ -107,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),
]; ];
} }
@ -120,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?
@ -157,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;

View File

@ -25,7 +25,7 @@ class FormView extends SubTemplate
if (!empty($this->title)) if (!empty($this->title))
echo ' echo '
<div class="admin_box"> <div class="admin_box">
<h2>', $this->title, '</h2>'; <h2>', htmlspecialchars($this->title), '</h2>';
foreach ($this->_subtemplates as $template) foreach ($this->_subtemplates as $template)
$template->html_main(); $template->html_main();

View File

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