Merge pull request #3679 from yashodha/CRM-14951
[civicrm-core.git] / api / v3 / ReportTemplate.php
index b52cefa15fe461d2c70c576992cfa097aa6e9533..ad618ac654d699c7b80b58a5d3d9ab6ef35acf11 100644 (file)
@@ -5,7 +5,9 @@
  *
  * FIXME This is a bare-minimum placeholder
  *
- * @param  array  $ params input parameters
+ * @param $params
+ *
+ * @internal param $array $ params input parameters
  *
  * {@example OptionValueGet.php 0}
  * @example OptionValueGet.php
@@ -29,6 +31,8 @@ function civicrm_api3_report_template_get($params) {
  *
  * {@example OptionValueCreate.php}
  *
+ * @param $params
+ *
  * @return array of newly created option_value property values.
  * {@getfields OptionValue_create}
  * @access public
@@ -87,11 +91,23 @@ 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);
 }
 
+/**
+ * @param $params
+ *
+ * @return array
+ * @throws API_Exception
+ * @throws CiviCRM_API3_Exception
+ */
 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',
@@ -113,15 +129,34 @@ function _civicrm_api3_report_template_getrows($params) {
   $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);
 }
 
+/**
+ * @param $params
+ *
+ * @return array
+ */
 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
@@ -133,7 +168,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',
   );
 }