improve HTML output

master
Gilles Crettenand 11 years ago
parent 869b3d3ee0
commit b8ea65d4f6

@ -10,11 +10,36 @@ class Html extends Formatter {
));
}
protected function truncate($v, $ellipsis = ' [...]') {
$limit = 50;
if(strlen($v) > $limit) {
$v = substr($v, 0, $limit).$ellipsis;
}
return $v;
}
protected function formatValue($v) {
if(is_numeric($v)) {
return $v;
}
if(is_bool($v)) {
return '<span class="glyphicon glyphicon-'.($v ? 'ok' : 'remove').'"></span>';
}
if(is_string($v) && strpos($v, 'http') !== false) {
$url = $v;
$v = $this->truncate($v, '...');
return "<a href='$url' target='_blank'>$v</a>";
}
return $this->truncate(print_r($v, true));
}
protected function result($data) {
// the format is array('result' => array('function name' => DATA))
// so take the first element of the 'result' array
$func = key($data['result']);
$data = reset($data['result']);
$title = $func;
$first = reset($data);
$single = ! is_array($first);
@ -25,6 +50,8 @@ class Html extends Formatter {
} else {
$columns = array_keys($first);
$title .= ' ('.count($data).' results)';
$content .= '<tr>';
foreach($columns as $k) {
$content .= "<th>$k</th>";
@ -34,19 +61,13 @@ class Html extends Formatter {
$content .= '</thead><tbody>';
if($single) {
foreach($data as $k => $v) {
$content .= "<tr><th>$k</th><td>".print_r($v, true)."</td></tr>";
$content .= "<tr><th>$k</th><td>".$this->formatValue($v)."</td></tr>";
}
} else {
foreach($data as $row) {
$content .= '<tr>';
foreach($columns as $c) {
$v = print_r(isset($row[$c]) ? $row[$c] : '', true);
$limit = 50;
if(strlen($v) > $limit) {
$v = substr($v, 0, $limit).' [...]';
}
$content .= "<td>$v</td>";
$content .= '<td>'.$this->formatValue(isset($row[$c]) ? $row[$c] : '').'</td>';
}
$content .= '</tr>';
}
@ -54,7 +75,7 @@ class Html extends Formatter {
$content .= '</tbody></table>';
return array(
'title' => $func,
'title' => $title,
'content' => $content,
'status' => 'success',
);

Loading…
Cancel
Save