From 781d91c8878db0c136a5d99f80a9656fe6d1faab Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Tue, 4 Feb 2014 19:51:58 +1300 Subject: [PATCH] CRM-14137 alter report metadata in api result to be keyed as 'metadata' per discussions ---------------------------------------- * CRM-14137: Report_template.getrows api - allow retrieval of metadata at the top level http://issues.civicrm.org/jira/browse/CRM-14137 --- api/v3/ReportTemplate.php | 28 +++++++++++++-------- api/v3/examples/ReportTemplate/Getrows.php | 22 ++++++++++------ tests/phpunit/api/v3/ReportTemplateTest.php | 3 ++- 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/api/v3/ReportTemplate.php b/api/v3/ReportTemplate.php index 195b8eaf4b..f83a8f4e4c 100644 --- a/api/v3/ReportTemplate.php +++ b/api/v3/ReportTemplate.php @@ -88,8 +88,8 @@ function civicrm_api3_report_template_delete($params) { */ function civicrm_api3_report_template_getrows($params) { civicrm_api3_verify_one_mandatory($params, NULL, array('report_id', 'instance_id')); - list($rows, $instance, $labels) = _civicrm_api3_report_template_getrows($params); - return civicrm_api3_create_success($rows, $params, 'report_template', 'getrows', CRM_Core_DAO::$_nullObject, $labels); + list($rows, $instance, $metadata) = _civicrm_api3_report_template_getrows($params); + return civicrm_api3_create_success($rows, $params, 'report_template', 'getrows', CRM_Core_DAO::$_nullObject, $metadata); } function _civicrm_api3_report_template_getrows($params) { @@ -118,21 +118,29 @@ function _civicrm_api3_report_template_getrows($params) { $reportInstance->setOffsetValue($options['offset']); $reportInstance->beginPostProcessCommon(); $sql = $reportInstance->buildQuery(); - $rows = $metadata = array(); + $rows = $metadata = $requiredMetadata = array(); $reportInstance->buildRows($sql, $rows); - $metadata['title'] = $reportInstance->getTitle(); - foreach ($reportInstance->_columnHeaders as $key => $header) { - //would be better just to expect reports to provide titles but reports are not consistent - //NB I think these are already translated - $metadata['labels'][$key] = !empty($header['title']) ? $header['title'] : ''; + $requiredMetadata = array(); + if(isset($params['options']) && !empty($params['options']['metadata'])) { + $requiredMetadata = $params['options']['metadata']; + if(in_array('title', $requiredMetadata)) { + $metadata['metadata']['title'] = $reportInstance->getTitle(); + } + if(in_array('labels', $requiredMetadata)) { + foreach ($reportInstance->_columnHeaders as $key => $header) { + //would be better just to expect reports to provide titles but reports are not consistent so we anticipate empty + //NB I think these are already translated + $metadata['metadata']['labels'][$key] = !empty($header['title']) ? $header['title'] : ''; + } + } } return array($rows, $reportInstance, $metadata); } function civicrm_api3_report_template_getstatistics($params) { - list($rows, $reportInstance, $labels) = _civicrm_api3_report_template_getrows($params); + list($rows, $reportInstance, $metadata) = _civicrm_api3_report_template_getrows($params); $stats = $reportInstance->statistics($rows); - return civicrm_api3_create_success($stats, $params, 'report_template'); + return civicrm_api3_create_success($stats, $params, 'report_template', 'getstatistics', CRM_Core_DAO::$_nullObject, $metadata); } /** * Retrieve rows from a report template diff --git a/api/v3/examples/ReportTemplate/Getrows.php b/api/v3/examples/ReportTemplate/Getrows.php index 9b2fe93fff..2acc3e86a3 100644 --- a/api/v3/examples/ReportTemplate/Getrows.php +++ b/api/v3/examples/ReportTemplate/Getrows.php @@ -6,6 +6,12 @@ function report_template_getrows_example(){ $params = array( 'report_id' => 'contact/summary', + 'options' => array( + 'metadata' => array( + '0' => 'labels', + '1' => 'title', + ), + ), ); try{ @@ -47,13 +53,15 @@ function report_template_getrows_expectedresult(){ 'civicrm_country_name' => 'United States', ), ), - 'title' => '', - 'labels' => array( - 'civicrm_contact_sort_name' => 'Contact Name', - 'civicrm_contact_id' => 'Internal Contact ID', - 'civicrm_address_street_address' => 'Street Address', - 'civicrm_address_city' => 'City', - 'civicrm_country_name' => 'Country', + 'metadata' => array( + 'title' => 'ERROR: Title is not Set', + 'labels' => array( + 'civicrm_contact_sort_name' => 'Contact Name', + 'civicrm_contact_id' => 'Internal Contact ID', + 'civicrm_address_street_address' => 'Street Address', + 'civicrm_address_city' => 'City', + 'civicrm_country_name' => 'Country', + ), ), ); diff --git a/tests/phpunit/api/v3/ReportTemplateTest.php b/tests/phpunit/api/v3/ReportTemplateTest.php index 495edee312..36fbeb6116 100644 --- a/tests/phpunit/api/v3/ReportTemplateTest.php +++ b/tests/phpunit/api/v3/ReportTemplateTest.php @@ -117,8 +117,9 @@ class api_v3_ReportTemplateTest extends CiviUnitTestCase { $description = "Retrieve rows from a report template (optionally providing the instance_id)"; $result = $this->callAPIAndDocument('report_template', 'getrows', array( 'report_id' => 'contact/summary', + 'options' => array('metadata' => array('labels', 'title')) ), __FUNCTION__, __FILE__, $description, 'Getrows', 'getrows'); - $this->assertEquals('Contact Name', $result['labels']['civicrm_contact_sort_name']); + $this->assertEquals('Contact Name', $result['metadata']['labels']['civicrm_contact_sort_name']); //the second part of this test has been commented out because it relied on the db being reset to // it's base state -- 2.25.1