GenericTable: move link generation out of from formatting options
This commit is contained in:
parent
77809faada
commit
6ca3ee6d9d
@ -104,7 +104,7 @@ class GenericTable
|
|||||||
'class' => isset($column['class']) ? $column['class'] : '',
|
'class' => isset($column['class']) ? $column['class'] : '',
|
||||||
'cell_class' => isset($column['cell_class']) ? $column['cell_class'] : null,
|
'cell_class' => isset($column['cell_class']) ? $column['cell_class'] : null,
|
||||||
'colspan' => !empty($column['header_colspan']) ? $column['header_colspan'] : 1,
|
'colspan' => !empty($column['header_colspan']) ? $column['header_colspan'] : 1,
|
||||||
'href' => $isSortable ? $this->getLink($this->start, $key, $sortDirection) : null,
|
'href' => $isSortable ? $this->getHeaderLink($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,
|
||||||
@ -121,7 +121,7 @@ class GenericTable
|
|||||||
'base_url' => $this->base_url,
|
'base_url' => $this->base_url,
|
||||||
'index_class' => $options['index_class'] ?? '',
|
'index_class' => $options['index_class'] ?? '',
|
||||||
'items_per_page' => $this->items_per_page,
|
'items_per_page' => $this->items_per_page,
|
||||||
'linkBuilder' => [$this, 'getLink'],
|
'linkBuilder' => [$this, 'getHeaderLink'],
|
||||||
'recordCount' => $this->recordCount,
|
'recordCount' => $this->recordCount,
|
||||||
'sort_direction' => $this->sort_direction,
|
'sort_direction' => $this->sort_direction,
|
||||||
'sort_order' => $this->sort_order,
|
'sort_order' => $this->sort_order,
|
||||||
@ -129,7 +129,7 @@ class GenericTable
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getLink($start = null, $order = null, $dir = null)
|
public function getHeaderLink($start = null, $order = null, $dir = null)
|
||||||
{
|
{
|
||||||
if ($start === null)
|
if ($start === null)
|
||||||
$start = $this->start;
|
$start = $this->start;
|
||||||
@ -197,6 +197,10 @@ class GenericTable
|
|||||||
else
|
else
|
||||||
$value = $row[$column['value']];
|
$value = $row[$column['value']];
|
||||||
|
|
||||||
|
// Turn value into a link?
|
||||||
|
if (!empty($column['link']))
|
||||||
|
$value = $this->processLink($column['link'], $value, $row);
|
||||||
|
|
||||||
// Append the cell to the row.
|
// Append the cell to the row.
|
||||||
$newRow['cells'][] = [
|
$newRow['cells'][] = [
|
||||||
'class' => $column['cell_class'] ?? '',
|
'class' => $column['cell_class'] ?? '',
|
||||||
@ -259,18 +263,28 @@ class GenericTable
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate a link, if requested.
|
// TODO: deprecated
|
||||||
if (!empty($options['link']))
|
if (!empty($options['link']))
|
||||||
{
|
{
|
||||||
// First, generate the replacement variables.
|
$value = $this->processLink($options['link'], $value, $rowData);
|
||||||
$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;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function processLink($template, $value, array $rowData)
|
||||||
|
{
|
||||||
|
$href = $this->rowReplacements($template, $rowData);
|
||||||
|
return '<a href="' . $href . '">' . $value . '</a>';
|
||||||
|
}
|
||||||
|
|
||||||
|
private function rowReplacements($template, array $rowData)
|
||||||
|
{
|
||||||
|
$keys = array_keys($rowData);
|
||||||
|
$values = array_values($rowData);
|
||||||
|
foreach ($keys as $keyKey => $keyValue)
|
||||||
|
$keys[$keyKey] = '{' . strtoupper($keyValue) . '}';
|
||||||
|
|
||||||
|
return str_replace($keys, $values, $template);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user