reduce complexity of table()

This commit is contained in:
msquare 2016-09-30 17:38:26 +02:00
parent 3738d071f8
commit b87eb49b93
1 changed files with 18 additions and 12 deletions

View File

@ -341,25 +341,30 @@ function page_with_title($title, $elements) {
function table($columns, $rows_raw, $data = true) { function table($columns, $rows_raw, $data = true) {
// If only one column is given // If only one column is given
if (! is_array($columns)) { if (! is_array($columns)) {
$columns = [
'col' => $columns
];
$rows = []; $rows = [];
foreach ($rows_raw as $row) foreach ($rows_raw as $row) {
$rows[] = [ $rows[] = [
'col' => $row 'col' => $row
]; ];
} else { }
$rows = $rows_raw; return render_table([
'col' => $columns
], $rows, $data);
} }
return render_table($columns, $rows_raw, $data);
}
/**
* Helper for rendering a html-table.
* use table()
*/
function render_table($columns, $rows, $data = true) {
if (count($rows) == 0) { if (count($rows) == 0) {
return info(_("No data found."), true); return info(_("No data found."), true);
} }
$html = ""; $html = '<table class="table table-striped' . ($data ? ' data' : '') . '">';
$html .= '<table class="table table-striped' . ($data ? ' data' : '') . '">';
$html .= '<thead><tr>'; $html .= '<thead><tr>';
foreach ($columns as $key => $column) { foreach ($columns as $key => $column) {
$html .= '<th class="column_' . $key . '">' . $column . '</th>'; $html .= '<th class="column_' . $key . '">' . $column . '</th>';
@ -421,10 +426,11 @@ function template_render($file, $data) {
engelsystem_error("Cannot find template file &laquo;" . $file . "&raquo;."); engelsystem_error("Cannot find template file &laquo;" . $file . "&raquo;.");
} }
function shorten($str) { function shorten($str, $length = 50) {
if (strlen($str) < 50) if (strlen($str) < $length) {
return $str; return $str;
return '<span title="' . htmlentities($str, ENT_COMPAT, 'UTF-8') . '">' . substr($str, 0, 47) . '...</span>'; }
return '<span title="' . htmlentities($str, ENT_COMPAT, 'UTF-8') . '">' . substr($str, 0, $length - 3) . '...</span>';
} }
function table_body($array) { function table_body($array) {