From 6ca3ee6d9d97b59426e464c646a91a42fb60267f Mon Sep 17 00:00:00 2001 From: Aaron van Geffen Date: Wed, 8 Jan 2025 18:27:09 +0100 Subject: [PATCH] GenericTable: move link generation out of from formatting options --- models/GenericTable.php | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/models/GenericTable.php b/models/GenericTable.php index 08aa226..f564d29 100644 --- a/models/GenericTable.php +++ b/models/GenericTable.php @@ -104,7 +104,7 @@ class GenericTable 'class' => isset($column['class']) ? $column['class'] : '', 'cell_class' => isset($column['cell_class']) ? $column['cell_class'] : null, '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'], 'scope' => 'col', 'sort_mode' => $key == $this->sort_order ? $this->sort_direction : null, @@ -121,7 +121,7 @@ class GenericTable 'base_url' => $this->base_url, 'index_class' => $options['index_class'] ?? '', 'items_per_page' => $this->items_per_page, - 'linkBuilder' => [$this, 'getLink'], + 'linkBuilder' => [$this, 'getHeaderLink'], 'recordCount' => $this->recordCount, 'sort_direction' => $this->sort_direction, '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) $start = $this->start; @@ -197,6 +197,10 @@ class GenericTable else $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. $newRow['cells'][] = [ 'class' => $column['cell_class'] ?? '', @@ -259,18 +263,28 @@ class GenericTable break; } - // Generate a link, if requested. + // TODO: deprecated 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 = '' . $value . ''; + $value = $this->processLink($options['link'], $value, $rowData); } return $value; } + + private function processLink($template, $value, array $rowData) + { + $href = $this->rowReplacements($template, $rowData); + return '' . $value . ''; + } + + 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); + } }