Skip to content

Commit

Permalink
Fix bug in Chart header with negative numbers.
Browse files Browse the repository at this point in the history
Add basic dashboard functionality. Fixes jdorn#114
  • Loading branch information
jdorn committed Jan 26, 2014
1 parent d5eaaba commit 912b680
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 7 deletions.
2 changes: 1 addition & 1 deletion classes/headers/ChartHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ protected static function getRowInfo(&$rows, $params, $num, &$report) {
$val->datatype = 'date';
}
elseif($types[$key] === 'number') {
$val->setValue(round(floatval(preg_replace('/[^0-9\.]*/','',$val->getValue())),6));
$val->setValue(round(floatval(preg_replace('/[^-0-9\.]*/','',$val->getValue())),6));
$val->datatype = 'number';
}
else {
Expand Down
8 changes: 5 additions & 3 deletions classes/report_formats/HtmlReportFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ public static function display(&$report, &$request) {
}

try {
$html = $report->renderReportPage($template);
$additional_vars = array();
if(isset($request->query['no_charts'])) $additional_vars['no_charts'] = true;

$html = $report->renderReportPage($template,$additional_vars);
echo $html;
}
catch(Exception $e) {

catch(Exception $e) {
if(isset($request->query['content_only'])) {
$template = 'html/blank_page';
}
Expand Down
3 changes: 3 additions & 0 deletions config/config.php.sample
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ return array(
//the root directory of all your reports
//reports can be organized in subdirectories
'reportDir' => 'sample_reports',

//the root directory of all dashboards
'dashboardDir' => 'sample_dashboards',

//the directory where things will be cached
//this is relative to the project root by default, but can be set to an absolute path too
Expand Down
8 changes: 8 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
PhpReports::listReports();
});

Flight::route('/dashboards',function() {
PhpReports::listDashboards();
});

Flight::route('/dashboard/@name',function($name) {
PhpReports::displayDashboard($name);
});

//JSON list of reports (used for typeahead search)
Flight::route('/report-list-json',function() {
header("Content-Type: application/json");
Expand Down
42 changes: 42 additions & 0 deletions lib/PhpReports/PhpReports.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,48 @@ public static function listReports() {
$start = microtime(true);
echo self::render('html/report_list',$template_vars);
}

public static function listDashboards() {
$dashboards = self::getDashboards();

uasort($dashboards,function($a,$b) {
return strcmp($a['title'],$b['title']);
});

echo self::render('html/dashboard_list',array(
'dashboards'=>$dashboards
));
}

public static function displayDashboard($dashboard) {
$content = self::getDashboard($dashboard);

echo self::render('html/dashboard',array(
'dashboard'=>$content
));
}

public static function getDashboards() {
$dashboards = glob(PhpReports::$config['dashboardDir'].'/*.json');

$ret = array();
foreach($dashboards as $key=>$value) {
$name = basename($value,'.json');
$ret[$name] = self::getDashboard($name);
}

return $ret;
}

public static function getDashboard($dashboard) {
$file = PhpReports::$config['dashboardDir'].'/'.$dashboard.'.json';
if(!file_exists($file)) {
throw "Unknown dashboard - ".$dashboard;
}

return json_decode(file_get_contents($file),true);
}

public static function getRecentReports() {
$recently_run = FileSystemCache::retrieve(FileSystemCache::generateCacheKey('recently_run'));
$recent = array();
Expand Down
27 changes: 27 additions & 0 deletions sample_dashboards/timezones.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"title": "Timezone Dashboard",
"description": "Shows off the dashboard feature of Php Reports",
"reports": [
{
"report": "php/timezones.php",
"title": "ACST Timezone",
"macros": {
"region": "acst"
},
"format": "html",
"newRow": false,
"style": "max-height: 400px; overflow-y: auto; overflow-x: hidden;",
"class": "col-md-6"
},
{
"report": "php/timezones.php",
"title": "CDT Timezone",
"macros": {
"region": "cdt"
},
"format": "chart",
"newRow": false,
"class": "col-md-6"
}
]
}
1 change: 1 addition & 0 deletions sample_reports/php/timezones.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// options: ["cdt","acst","pdt"],
// default: "acst"
// }
// CHART: {type: "LineChart"}

$timezone_abbreviations = DateTimeZone::listAbbreviations();

Expand Down
104 changes: 104 additions & 0 deletions templates/default/html/dashboard.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
{% extends "html/page.twig" %}

{% block title %}Dashboard List{% endblock %}

{% block header %}
<h2>{{dashboard.title}}</h2>
{% if dashboard.description %}<p>{{dashboard.description|raw}}</p>{% endif %}
{% endblock %}

{% block content %}
<div id='reports_holder' style='margin-top: 20px;'>

</div>
{% endblock %}


{% block javascripts %}
{{ parent() }}
<script type='text/javascript' src='{{base}}/public/js/jquery.dataTables.min.js'></script>
<script type="text/javascript" src="{{base}}/public/js/moment-2.0.min.js"></script>
<script type="text/javascript" src="{{base}}/public/js/daterangepicker-1.2.js"></script>
<script type="text/javascript" src="{{base}}/public/js/bootstrap-datepicker.js"></script>
<script type="text/javascript" src="{{base}}/public/js/bootstrap-multiselect.js"></script>
<script type="text/javascript" src="{{base}}/public/js/prettify.js"></script>
<script type="text/javascript" src="{{base}}/public/js/lang-sql.js"></script>
<script type="text/javascript" src="{{base}}/public/js/jquery.browser.js"></script>
<script type="text/javascript" src="{{base}}/public/js/jquery.iframe-auto-height.plugin.1.9.3.js"></script>
<script type="text/javascript">
$("#content").on('click','a[data-role="button"]',function(e) {
e.preventDefault();
});
</script>
<script>
var dashboard = {{ dashboard|json_encode|raw }};
var last_row = $("<div class='row'>").appendTo($("#reports_holder"));
var first = true;
$.each(dashboard.reports, function(i,report) {
if(!first && report.newRow) {
last_row = $("<div class='row'>").appendTo($("#reports_holder"));
}
var container = $("<div>").appendTo(last_row);
if(report.class) container.addClass(report.class);
var report_data = {
macros: report.macros || {},
report: report.report
};
if(report.title) {
container.append("<div style='float: right; line-height: 30px;'><a href='{{base}}/report/html/?"+$.param(report_data)+"'>View full report</a></div>");
container.append($("<h2>").text(report.title).css({
marginTop: 0,
paddingTop: 0
}));
}
else {
container.append("<p><a href='{{base}}/report/html/?"+$.param(report_data)+"'>View full report</a></p>");
}
if(report.description) {
container.append($("<p>").text(report.description));
}
var holder;
if(!report.format || report.format === 'html' || report.format === 'table') {
holder = $("<div>").appendTo(container);
var report_url = "{{base}}/report/html";
// Loading message
holder.append("<p>Loading...</p>");
report_data.content_only = true;
report_data.no_charts = true;
$.get(report_url,report_data,function(response) {
holder.empty().html(response);
});
if(report.style) holder.attr('style',report.style);
}
else {
holder = $("<iframe>").appendTo(container).css({
border: 0,
width: '100%'
}).iframeAutoHeight();
holder.attr('src',"{{base}}/report/"+report.format+"?"+$.param(report_data));
if(report.style) holder.attr('style',holder.attr('style')+';'+report.style);
}
});
</script>
{% endblock %}
19 changes: 19 additions & 0 deletions templates/default/html/dashboard_list.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% extends "html/page.twig" %}

{% block title %}Dashboard List{% endblock %}

{% set breadcrumb = {"Dashboard List": true} %}

{% block header %}
<h2>Dashboards</h2>
{% endblock %}

{% block content %}
<div class="row">
<ul>
{% for key, dashboard in dashboards %}
<li><a href='{{base}}/dashboard/{{key}}'>{{dashboard.title}}</a></li>
{% endfor %}
</ul>
</div>
{% endblock %}
6 changes: 3 additions & 3 deletions templates/default/html/report_content.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% block chart_area %}
{% if has_charts %}
<iframe src="{{base}}/report/chart/?{{report_querystring}}" id='chart_container' class="auto-height" scrolling="no" style='width:100%;' frameborder="0"></iframe>
{% if has_charts and not no_charts %}
<iframe src="{{base}}/report/chart/?{{report_querystring}}" id='chart_container' class="auto-height" scrolling="no" style='width:100%;' frameborder="0"></iframe>
<script>
$(function() {
$('iframe#chart_container').iframeAutoHeight();
Expand All @@ -14,7 +14,7 @@
{% if not nodata %}
<script type='text/javascript'>
$(function() {
{% for dataset in DataSets %}
{% for dataset in DataSets %}
{% if not dataset.vertical %}
$('#result_table_{{loop.index}}').dataTable({
bPaginate: false,
Expand Down

0 comments on commit 912b680

Please sign in to comment.