Skip to content

Commit

Permalink
Improved look of the text report format.
Browse files Browse the repository at this point in the history
Fixed bug with variables in Mysql reports.
  • Loading branch information
jdorn committed Jun 1, 2012
1 parent 45059d7 commit 4d64c61
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
65 changes: 64 additions & 1 deletion classes/report_formats/TextReportFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,69 @@ public static function display(&$report, &$request) {
header("Pragma: no-cache");
header("Expires: 0");

PhpReports::renderPage($page_template,'text/page');
/**
* This code taken from Stack Overflow answer by ehudokai
* http://stackoverflow.com/a/4597190
*/

//first get your sizes
$sizes = array();
$first_row = $report->options['Rows'][0];
foreach($first_row['values'] as $key=>$value){
$key = $value['key'];
$value = $value['raw_value'];

//initialize to the size of the column name
$sizes[$key] = strlen($key);
}
foreach($report->options['Rows'] as $row) {
foreach($row['values'] as $key=>$value){
$key = $value['key'];
$value = $value['raw_value'];

$length = strlen($value);
if($length > $sizes[$key]) $sizes[$key] = $length; // get largest result size
}
}

//top of output
foreach($sizes as $length){
echo "+".str_pad("",$length+2,"-");
}
echo "+\n";

// column names
foreach($first_row['values'] as $key=>$value){
$key = $value['key'];
$value = $value['raw_value'];

echo "| ";
echo str_pad($key,$sizes[$key]+1);
}
echo "|\n";

//line under column names
foreach($sizes as $length){
echo "+".str_pad("",$length+2,"-");
}
echo "+\n";

//output data
foreach($report->options['Rows'] as $row) {
foreach($row['values'] as $key=>$value){
$key = $value['key'];
$value = $value['raw_value'];

echo "| ";
echo str_pad($value,$sizes[$key]+1);
}
echo "|\n";
}

//bottom of output
foreach($sizes as $length){
echo "+".str_pad("",$length+2,"-");
}
echo "+\n";
}
}
2 changes: 1 addition & 1 deletion classes/report_types/MysqlReportType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static function init(&$report) {
if(!isset($report->macros['host'])) $report->macros['host'] = $mysql['host'];

//replace shorthand {host} with {{host}} in query
$report->raw_query = preg_replace('/([^\{])\{host\}([^\}])/','$1{{host}}$3',$report->raw_query);
$report->raw_query = preg_replace('/([^\{])\{([^\{\}]+)\}([^\}])/','$1{{$2}}$3',$report->raw_query);

//if there are any included reports, add the report sql to the top
if(isset($report->options['Includes'])) {
Expand Down

0 comments on commit 4d64c61

Please sign in to comment.