Skip to content

Commit

Permalink
Add Multi-dataset version of JSON and XML report formats.
Browse files Browse the repository at this point in the history
Add ability to specify which dataset(s) are used in CSV, JSON, and XML formats.
  • Loading branch information
jdorn committed Nov 4, 2013
1 parent 9adf72e commit 9918ac5
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 15 deletions.
4 changes: 4 additions & 0 deletions classes/headers/OptionsHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ class OptionsHeader extends HeaderBase {
'ttl'=>array(
'min'=>0,
'type'=>'number'
),
'default_dataset'=>array(
'type'=>'number',
'default'=>0
)
);

Expand Down
9 changes: 8 additions & 1 deletion classes/report_formats/CsvReportFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ public static function display(&$report, &$request) {
header("Pragma: no-cache");
header("Expires: 0");

$data = $report->renderReportPage('csv/report');
$i=0;
if(isset($_GET['dataset'])) $i = $_GET['dataset'];
elseif(isset($report->options['default_dataset'])) $i = $report->options['default_dataset'];
$i = intval($i);

$data = $report->renderReportPage('csv/report',array(
'dataset'=>$i
));

if(trim($data)) echo $data;
}
Expand Down
54 changes: 53 additions & 1 deletion classes/report_formats/JsonReportFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,59 @@ public static function display(&$report, &$request) {
header("Content-type: application/json");
header("Pragma: no-cache");
header("Expires: 0");

//run the report
$report->run();

if(!$report->options['DataSets']) return;

echo $report->renderReportPage('json/report');
$result = array();
if(isset($_GET['datasets'])) {
$datasets = $_GET['datasets'];
// If all the datasets should be included
if($datasets === 'all') {
$datasets = array_keys($report->options['DataSets']);
}
// If just a single dataset was specified, make it an array
else if(!is_array($datasets)) {
$datasets = explode(',',$datasets);
}

foreach($datasets as $i) {
$result[] = self::getDataSet($i, $report);
}
}
else {
$i=0;
if(isset($_GET['dataset'])) $i = $_GET['dataset'];
elseif(isset($report->options['default_dataset'])) $i = $report->options['default_dataset'];
$i = intval($i);

$dataset = self::getDataSet($i, $report);
$result = $dataset['rows'];
}

if(defined('JSON_PRETTY_PRINT')) {
echo json_encode($result,JSON_PRETTY_PRINT);
}
else {
echo json_encode($result);
}
}

public static function getDataSet($i, &$report) {
$dataset = array();
if(isset($report->options['DataSets'][$i]['title'])) {
$dataset['title'] = $report->options['DataSets'][$i]['title'];
}
$dataset['rows'] = array();
foreach($report->options['DataSets'][$i]['rows'] as $i=>$row) {
$tmp = array();
foreach($row['values'] as $key=>$value){
$tmp[$value->key] = $value->getValue();
}
$dataset['rows'][] = $tmp;
}
return $dataset;
}
}
29 changes: 28 additions & 1 deletion classes/report_formats/XmlReportFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,33 @@ public static function display(&$report, &$request) {
header("Pragma: no-cache");
header("Expires: 0");

echo $report->renderReportPage('xml/report');
$datasets = array();
$dataset_format = false;

if(isset($_GET['datasets'])) {
$dataset_format = true;
$datasets = $_GET['datasets'];
// If all the datasets should be included
if($datasets === 'all') {
$datasets = array_keys($report->options['DataSets']);
}
// If just a single dataset was specified, make it an array
else if(!is_array($datasets)) {
$datasets = explode(',',$datasets);
}
}
else {
$i=0;
if(isset($_GET['dataset'])) $i = $_GET['dataset'];
elseif(isset($report->options['default_dataset'])) $i = $report->options['default_dataset'];
$i = intval($i);

$datasets = array($i);
}

echo $report->renderReportPage('xml/report',array(
'datasets'=>$datasets,
'dataset_format'=>$dataset_format
));
}
}
4 changes: 2 additions & 2 deletions templates/default/csv/report.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% for value in DataSets[0].rows[0].values %}{% if not loop.first %},{% endif %}"{{value.key}}"{% endfor %}
{% for value in DataSets[dataset].rows[0].values %}{% if not loop.first %},{% endif %}"{{value.key}}"{% endfor %}

{% for row in DataSets[0].rows %}{% for value in row.values %}{% if not loop.first %},{% endif %}
{% for row in DataSets[dataset].rows %}{% for value in row.values %}{% if not loop.first %},{% endif %}
"{{ value.getValue()|replace({'"':'""'})|raw }}"{% endfor %}

{% endfor %}
9 changes: 0 additions & 9 deletions templates/default/json/report.twig

This file was deleted.

21 changes: 20 additions & 1 deletion templates/default/xml/report.twig
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
{% extends "xml/page.twig" %}

{% block content %}
{% if dataset_format %}
<Results>
{% for row in DataSets[0].rows %}
{% for dataset in datasets %}
<Dataset>
{% if DataSets[dataset].title %}<Title>{{ DataSets[dataset].title }}</Title>{% endif %}
<Rows>
{% for row in DataSets[dataset].rows %}
<Row>
{% for value in row.values %}
<{{value.keyCollapsed}}>{{ value.getValue()|raw }}</{{value.keyCollapsed}}>
{% endfor %}
</Row>
{% endfor %}
</Rows>
</Dataset>
{% endfor %}
</Results>
{% else %}
<Results>
{% for row in DataSets[datasets[0]].rows %}
<Row>
{% for value in row.values %}
<{{value.keyCollapsed}}>{{ value.getValue()|raw }}</{{value.keyCollapsed}}>
{% endfor %}
</Row>
{% endfor %}
</Results>
{% endif %}
{% endblock %}

0 comments on commit 9918ac5

Please sign in to comment.