diff options
author | msquare <msquare@notrademark.de> | 2016-09-30 17:38:26 +0200 |
---|---|---|
committer | msquare <msquare@notrademark.de> | 2016-09-30 17:38:26 +0200 |
commit | b87eb49b9324a56da723804e088415d52df77d77 (patch) | |
tree | 25775de5e898811895e903d44be1b4af3349e4e9 /includes | |
parent | 3738d071f86aa5016ce326a009a278085b41fb48 (diff) |
reduce complexity of table()
Diffstat (limited to 'includes')
-rw-r--r-- | includes/sys_template.php | 30 |
1 files changed, 18 insertions, 12 deletions
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 .= '<table class="table table-striped' . ($data ? ' data' : '') . '">'; + $html = '<table class="table table-striped' . ($data ? ' data' : '') . '">'; $html .= '<thead><tr>'; foreach ($columns as $key => $column) { $html .= '<th class="column_' . $key . '">' . $column . '</th>'; @@ -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 '<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) { |