Merge pull request #2461 from eileenmcnaughton/CRM-14137-2
[civicrm-core.git] / api / v3 / ReportTemplate.php
index 37ffaeb965254375f793c0501a0203b57a92c316..f83a8f4e4cfc0daf372eaad0ee8c183983681829 100644 (file)
@@ -87,16 +87,21 @@ function civicrm_api3_report_template_delete($params) {
  * @access public
  */
 function civicrm_api3_report_template_getrows($params) {
-  list($rows, $instance) = _civicrm_api3_report_template_getrows($params);
-  return civicrm_api3_create_success($rows, $params, 'report_template');
+  civicrm_api3_verify_one_mandatory($params, NULL, array('report_id', 'instance_id'));
+  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) {
+  if(empty($params['report_id'])) {
+    $params['report_id'] = civicrm_api3('report_instance', 'getvalue', array('id' => $params['instance_id'], 'return' => 'report_id'));
+  }
+
   $class = civicrm_api3('option_value', 'getvalue', array(
-      'option_group_id' => 'report_template',
-      'return' => 'name',
-      'value' => $params['report_id'],
-  )
+    'option_group_id' => 'report_template',
+    'return' => 'name',
+    'value' => $params['report_id'],
+    )
   );
 
   $reportInstance = new $class();
@@ -108,17 +113,34 @@ function _civicrm_api3_report_template_getrows($params) {
   $reportInstance->preProcess();
   $reportInstance->setDefaultValues(FALSE);
   $reportInstance->setParams(array_merge($reportInstance->getDefaultValues(), $params));
+  $options = _civicrm_api3_get_options_from_params($params, TRUE,'report_template','get');
+  $reportInstance->setLimitValue($options['limit']);
+  $reportInstance->setOffsetValue($options['offset']);
   $reportInstance->beginPostProcessCommon();
   $sql = $reportInstance->buildQuery();
-  $rows = array();
+  $rows = $metadata = $requiredMetadata  = array();
   $reportInstance->buildRows($sql, $rows);
-  return array($rows, $reportInstance);
+  $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) = _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
@@ -130,7 +152,6 @@ function civicrm_api3_report_template_getstatistics($params) {
  */
 function _civicrm_api3_report_template_getrows_spec(&$params) {
   $params['report_id'] = array(
-    'api.required' => TRUE,
     'title' => 'Report ID - eg. member/lapse',
   );
 }