Skip to content

Commit

Permalink
implement a basic generic table viewer
Browse files Browse the repository at this point in the history
Introduce a new function, renderTableViewer(), which renders a simple
HTML table from the given array of data rows and the declaration of
the table columns. Update renderCactiConfig() and renderMuninConfig()
to look at it in action.
  • Loading branch information
infrastation committed May 31, 2016
1 parent f72d89b commit f2c9e44
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 21 deletions.
34 changes: 13 additions & 21 deletions wwwroot/inc/interface-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -1054,21 +1054,15 @@ function renderMyQuickLinks ()

function renderCactiConfig()
{
$columns = array
(
array ('th_text' => 'base URL', 'row_key' => 'base_url'),
array ('th_text' => 'username', 'row_key' => 'username'),
array ('th_text' => 'graph(s)', 'row_key' => 'num_graphs', 'td_class' => 'tdright'),
);
$servers = getCactiServers();
startPortlet ('Cacti servers (' . count ($servers) . ')');
echo '<table cellspacing=0 cellpadding=5 align=center class=widetable>';
echo '<tr>' .
'<th>base URL</th>' .
'<th>username</th>' .
'<th>graph(s)</th>' .
'</tr>';
foreach ($servers as $server)
echo '<tr align=left valign=top>' .
'<td>' . stringForTD ($server['base_url']) . '</td>' .
'<td>' . stringForTD ($server['username']) . '</td>' .
"<td class=tdright>${server['num_graphs']}</td>" .
'</tr>';
echo '</table>';
renderTableViewer ($columns, $servers);
finishPortlet();
}

Expand Down Expand Up @@ -1120,16 +1114,14 @@ function printNewItemTR ()

function renderMuninConfig()
{
$columns = array
(
array ('th_text' => 'base URL', 'row_key' => 'base_url', 'td_maxlen' => 150),
array ('th_text' => 'graph(s)', 'row_key' => 'num_graphs', 'td_class' => 'tdright'),
);
$servers = getMuninServers();
startPortlet ('Munin servers (' . count ($servers) . ')');
echo '<table cellspacing=0 cellpadding=5 align=center class=widetable>';
echo '<tr><th>base URL</th><th>graph(s)</th></tr>';
foreach ($servers as $server)
{
echo '<tr align=left valign=top><td>' . stringForTD ($server['base_url']) . '</td>';
echo "<td class=tdright>${server['num_graphs']}</td></tr>";
}
echo '</table>';
renderTableViewer ($columns, $servers);
finishPortlet();
}

Expand Down
58 changes: 58 additions & 0 deletions wwwroot/inc/interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6399,4 +6399,62 @@ function printNewitemTR ($column)
echo '</table>';
}

// Each table column descriptor is an array that contains at least the "row_key"
// key, which tells where the data for that column is in each row. It may also
// contain additional keys below:
//
// th_text -- text for the TH (not HTML escaped!)
// th_class -- CSS class for the TH
// td_class -- CSS class for the TD
// td_escape -- whether to do HTML escaping (defaults to TRUE)
// td_maxlen -- cutoff margin for escaping (defaults to 0)
function renderTableViewer ($columns, $rows)
{
$header_row = FALSE;
foreach ($columns as $col)
{
if (! array_key_exists ('row_key', $col))
throw new InvalidArgException ('columns', '(array)', '\'row_key\' is not set for a column');
if (array_key_exists ('th_text', $col))
$header_row = TRUE;
}
$tclass = $header_row ? 'zebra' : 'zebra0';
echo "<table cellspacing=0 cellpadding=5 align=center class='widetable ${tclass}'>";
if ($header_row)
{
echo '<tr>';
foreach ($columns as $col)
if (! array_key_exists ('th_text', $col))
echo '<th>&nbsp;</th>';
else
{
echo '<th';
if (array_key_exists ('th_class', $col))
echo ' class=' . $col['th_class'];
echo '>' . $col['th_text'] . '</th>';
}
echo '</tr>';
}
foreach ($rows as $row)
{
echo '<tr align=left valign=top>';
foreach ($columns as $col)
if (! array_key_exists ($col['row_key'], $row))
echo '<td class=trerror>data error</td>';
else
{
echo '<td';
if (array_key_exists ('td_class', $col))
echo ' class=' . $col['td_class'];
echo '>';
$text = $row[$col['row_key']];
if (array_fetch ($col, 'td_escape', TRUE))
$text = stringForTD ($text, array_fetch ($col, 'td_maxlen', 0));
echo $text . '</td>';
}
echo '</tr>';
}
echo '</table>';
}

?>

0 comments on commit f2c9e44

Please sign in to comment.