From b87eb49b9324a56da723804e088415d52df77d77 Mon Sep 17 00:00:00 2001 From: msquare Date: Fri, 30 Sep 2016 17:38:26 +0200 Subject: reduce complexity of table() --- includes/sys_template.php | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'includes/sys_template.php') diff --git a/includes/sys_template.php b/includes/sys_template.php index 829d45bc..bf2e7027 100644 --- a/includes/sys_template.php +++ b/includes/sys_template.php @@ -341,25 +341,30 @@ function page_with_title($title, $elements) { function table($columns, $rows_raw, $data = true) { // If only one column is given if (! is_array($columns)) { - $columns = [ - 'col' => $columns - ]; - $rows = []; - foreach ($rows_raw as $row) + foreach ($rows_raw as $row) { $rows[] = [ '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) { return info(_("No data found."), true); } - $html = ""; - $html .= ''; + $html = '
'; $html .= ''; foreach ($columns as $key => $column) { $html .= ''; @@ -421,10 +426,11 @@ function template_render($file, $data) { engelsystem_error("Cannot find template file «" . $file . "»."); } -function shorten($str) { - if (strlen($str) < 50) +function shorten($str, $length = 50) { + if (strlen($str) < $length) { return $str; - return '' . substr($str, 0, 47) . '...'; + } + return '' . substr($str, 0, $length - 3) . '...'; } function table_body($array) { -- cgit v1.2.3-54-g00ecf
' . $column . '