forked from Public/pics
GenericTable: refactor order and pagination initalisation
This commit is contained in:
@@ -28,53 +28,22 @@ class GenericTable
|
||||
|
||||
public function __construct($options)
|
||||
{
|
||||
// Make sure we're actually sorting on something sortable.
|
||||
if (!isset($options['sort_order']) || (!empty($options['sort_order']) && empty($options['columns'][$options['sort_order']]['is_sortable'])))
|
||||
$options['sort_order'] = '';
|
||||
$this->initOrder($options);
|
||||
$this->initPagination($options);
|
||||
|
||||
// Order in which direction?
|
||||
if (!empty($options['sort_direction']) && !in_array($options['sort_direction'], ['up', 'down']))
|
||||
$options['sort_direction'] = 'up';
|
||||
|
||||
// How much data do we have?
|
||||
$this->recordCount = $options['get_count']();
|
||||
|
||||
// How much data do we need to retrieve?
|
||||
$this->items_per_page = !empty($options['items_per_page']) ? $options['items_per_page'] : 30;
|
||||
|
||||
// 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 = max(1, 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'];
|
||||
|
||||
// Gather parameters for the data gather function first.
|
||||
$parameters = [$this->start, $this->items_per_page, $options['sort_order'], $options['sort_direction']];
|
||||
|
||||
// Okay, let's fetch the data!
|
||||
$data = $options['get_data'](...$parameters);
|
||||
|
||||
// Extract data into local variables.
|
||||
$rawRowData = $data['rows'];
|
||||
$this->sort_order = $data['order'];
|
||||
$this->sort_direction = $data['direction'];
|
||||
unset($data);
|
||||
$data = $options['get_data']($this->start, $this->items_per_page,
|
||||
$this->sort_order, $this->sort_direction);
|
||||
|
||||
// Okay, now for the column headers...
|
||||
$this->generateColumnHeaders($options);
|
||||
|
||||
// Should we create a page index?
|
||||
$needsPageIndex = !empty($this->items_per_page) && $this->recordCount > $this->items_per_page;
|
||||
if ($needsPageIndex)
|
||||
if ($this->recordCount > $this->items_per_page)
|
||||
$this->generatePageIndex($options);
|
||||
|
||||
// Process the data to be shown into rows.
|
||||
if (!empty($rawRowData))
|
||||
$this->processAllRows($rawRowData, $options);
|
||||
if (!empty($data))
|
||||
$this->processAllRows($data, $options);
|
||||
else
|
||||
$this->body = $options['no_items_label'] ?? '';
|
||||
|
||||
@@ -89,6 +58,38 @@ class GenericTable
|
||||
$this->form_below = $options['form_below'] ?? $options['form'] ?? null;
|
||||
}
|
||||
|
||||
private function initOrder($options)
|
||||
{
|
||||
assert(isset($options['default_sort_order']));
|
||||
assert(isset($options['default_sort_direction']));
|
||||
|
||||
// Validate sort order (column)
|
||||
$this->sort_order = $options['sort_order'];
|
||||
if (empty($this->sort_order) || empty($options['columns'][$this->sort_order]['is_sortable']))
|
||||
$this->sort_order = $options['default_sort_order'];
|
||||
|
||||
// Validate sort direction
|
||||
$this->sort_direction = $options['sort_direction'];
|
||||
if (empty($this->sort_direction) || !in_array($this->sort_direction, ['up', 'down']))
|
||||
$this->sort_direction = $options['default_sort_direction'];
|
||||
}
|
||||
|
||||
private function initPagination(array $options)
|
||||
{
|
||||
assert(isset($options['base_url']));
|
||||
assert(isset($options['items_per_page']));
|
||||
|
||||
$this->base_url = $options['base_url'];
|
||||
|
||||
$this->recordCount = $options['get_count']();
|
||||
$this->items_per_page = !empty($options['items_per_page']) ? $options['items_per_page'] : 30;
|
||||
|
||||
$this->start = empty($options['start']) || !is_numeric($options['start']) || $options['start'] < 0 || $options['start'] > $this->recordCount ? 0 : $options['start'];
|
||||
|
||||
$numPages = max(1, ceil($this->recordCount / $this->items_per_page));
|
||||
$this->currentPage = min(ceil($this->start / $this->items_per_page) + 1, $numPages);
|
||||
}
|
||||
|
||||
private function generateColumnHeaders($options)
|
||||
{
|
||||
foreach ($options['columns'] as $key => $column)
|
||||
@@ -218,7 +219,7 @@ class GenericTable
|
||||
{
|
||||
// Basic option: simply take a use a particular data property.
|
||||
case 'value':
|
||||
$value = htmlspecialchars($rowData[$options['data']]);
|
||||
$value = htmlspecialchars($rowData[$options['data']] ?? '');
|
||||
break;
|
||||
|
||||
// Processing via a lambda function.
|
||||
|
||||
Reference in New Issue
Block a user