From 9918ac5df094da34a1d3ed28510e412d8364f74c Mon Sep 17 00:00:00 2001 From: Jeremy Dorn Date: Sun, 3 Nov 2013 16:16:57 -0800 Subject: [PATCH] Add Multi-dataset version of JSON and XML report formats. Add ability to specify which dataset(s) are used in CSV, JSON, and XML formats. --- classes/headers/OptionsHeader.php | 4 ++ classes/report_formats/CsvReportFormat.php | 9 +++- classes/report_formats/JsonReportFormat.php | 54 ++++++++++++++++++++- classes/report_formats/XmlReportFormat.php | 29 ++++++++++- templates/default/csv/report.twig | 4 +- templates/default/json/report.twig | 9 ---- templates/default/xml/report.twig | 21 +++++++- 7 files changed, 115 insertions(+), 15 deletions(-) delete mode 100644 templates/default/json/report.twig diff --git a/classes/headers/OptionsHeader.php b/classes/headers/OptionsHeader.php index 9fed64d6..5785ce2a 100644 --- a/classes/headers/OptionsHeader.php +++ b/classes/headers/OptionsHeader.php @@ -65,6 +65,10 @@ class OptionsHeader extends HeaderBase { 'ttl'=>array( 'min'=>0, 'type'=>'number' + ), + 'default_dataset'=>array( + 'type'=>'number', + 'default'=>0 ) ); diff --git a/classes/report_formats/CsvReportFormat.php b/classes/report_formats/CsvReportFormat.php index 6484412d..61976b5a 100644 --- a/classes/report_formats/CsvReportFormat.php +++ b/classes/report_formats/CsvReportFormat.php @@ -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; } diff --git a/classes/report_formats/JsonReportFormat.php b/classes/report_formats/JsonReportFormat.php index 60808e18..adc4c062 100644 --- a/classes/report_formats/JsonReportFormat.php +++ b/classes/report_formats/JsonReportFormat.php @@ -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; } } diff --git a/classes/report_formats/XmlReportFormat.php b/classes/report_formats/XmlReportFormat.php index 8bd572bb..54e869ec 100644 --- a/classes/report_formats/XmlReportFormat.php +++ b/classes/report_formats/XmlReportFormat.php @@ -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 + )); } } diff --git a/templates/default/csv/report.twig b/templates/default/csv/report.twig index 7739fa75..1500d1dc 100644 --- a/templates/default/csv/report.twig +++ b/templates/default/csv/report.twig @@ -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 %} diff --git a/templates/default/json/report.twig b/templates/default/json/report.twig deleted file mode 100644 index 54200942..00000000 --- a/templates/default/json/report.twig +++ /dev/null @@ -1,9 +0,0 @@ -[{% for row in DataSets[0].rows %}{% if not loop.first %},{% endif %} - - {{'{'}}{% for value in row.values %}{% if not loop.first %},{% endif %} - - "{{value.key}}":"{{ value.getValue()|replace({'\\':'\\\\','"':'\\"' })|raw }}"{% endfor %} - - }{% endfor %} - -] diff --git a/templates/default/xml/report.twig b/templates/default/xml/report.twig index b5540598..8a332155 100644 --- a/templates/default/xml/report.twig +++ b/templates/default/xml/report.twig @@ -1,8 +1,26 @@ {% extends "xml/page.twig" %} {% block content %} +{% if dataset_format %} -{% for row in DataSets[0].rows %} +{% for dataset in datasets %} + + {% if DataSets[dataset].title %}{{ DataSets[dataset].title }}{% endif %} + + {% for row in DataSets[dataset].rows %} + + {% for value in row.values %} + <{{value.keyCollapsed}}>{{ value.getValue()|raw }} + {% endfor %} + + {% endfor %} + + +{% endfor %} + +{% else %} + +{% for row in DataSets[datasets[0]].rows %} {% for value in row.values %} <{{value.keyCollapsed}}>{{ value.getValue()|raw }} @@ -10,4 +28,5 @@ {% endfor %} +{% endif %} {% endblock %}