From c58f66e0d926fe8a14f04a0f491715b975ccea09 Mon Sep 17 00:00:00 2001 From: Eileen Date: Thu, 5 Dec 2013 17:36:41 +1300 Subject: [PATCH] Add ReportTemplate.getrows api, changes to form class are adding getters & setters & also adding a function (open to renaming suggestions) for reports that want to override beginProcess to set some variables once ->_params has been run - since beginPostProcess is very 'formy' don't set qfkey if not in controller mode rename beginPostProcessNoController to beginPostProcessCommon which better reflects preProcessCommon, fix e-notices exposed by test add statistics api --- CRM/Report/Form.php | 81 +++++++++++++++++++++++++++++++++------ api/v3/ReportTemplate.php | 57 +++++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 12 deletions(-) diff --git a/CRM/Report/Form.php b/CRM/Report/Form.php index 11e0599744..309d5d649f 100644 --- a/CRM/Report/Form.php +++ b/CRM/Report/Form.php @@ -238,6 +238,14 @@ class CRM_Report_Form extends CRM_Core_Form { */ protected $_selectedTables; + /** + * outputmode e.g 'print', 'csv', 'pdf' + * @todo have declared this as public as fixing an e-Notice in a point release - would + * be better converted to protected in 4.5 + * @var string + */ + public $_outputMode; + public $_having = NULL; public $_select = NULL; public $_selectClauses = array(); @@ -249,6 +257,13 @@ class CRM_Report_Form extends CRM_Core_Form { public $_whereClauses = array(); public $_havingClauses = array(); + /** + * Is this being called without a form controller (ie. the report is being render outside the normal form + * - e.g the api is retrieving the rows + * @var boolean + */ + public $noController = FALSE; + /** * Variable to hold the currency alias */ @@ -318,18 +333,20 @@ class CRM_Report_Form extends CRM_Core_Form { CRM_Core_Region::instance('page-header')->add(array( 'markup' => sprintf('', htmlentities(get_class($this))), )); + if(!$this->noController) { + $this->setID($this->get('instanceId')); - $this->_id = $this->get('instanceId'); - if (!$this->_id) { - $this->_id = CRM_Report_Utils_Report::getInstanceID(); if (!$this->_id) { - $this->_id = CRM_Report_Utils_Report::getInstanceIDForPath(); + $this->setID(CRM_Report_Utils_Report::getInstanceID()); + if (!$this->_id) { + $this->setID( CRM_Report_Utils_Report::getInstanceIDForPath()); + } } - } - // set qfkey so that pager picks it up and use it in the "Next > Last >>" links. - // FIXME: Note setting it in $_GET doesn't work, since pager generates link based on QUERY_STRING - $_SERVER['QUERY_STRING'] .= "&qfKey={$this->controller->_key}"; + // set qfkey so that pager picks it up and use it in the "Next > Last >>" links. + // FIXME: Note setting it in $_GET doesn't work, since pager generates link based on QUERY_STRING + $_SERVER['QUERY_STRING'] .= "&qfKey={$this->controller->_key}"; + } if ($this->_id) { $this->assign('instanceId', $this->_id); @@ -368,7 +385,7 @@ class CRM_Report_Form extends CRM_Core_Form { // set the mode $this->assign('mode', 'instance'); } - else { + elseif (!$this->noController) { list($optionValueID, $optionValue) = CRM_Report_Utils_Report::getValueIDFromUrl(); $instanceCount = CRM_Report_Utils_Report::getInstanceCount($optionValue); if (($instanceCount > 0) && $optionValueID) { @@ -729,6 +746,37 @@ class CRM_Report_Form extends CRM_Core_Form { return FALSE; } + /** + * Setter for $_params + * @param array $params + */ + function setParams($params) { + $this->_params = $params; + } + + /** + * Setter for $_id + * @param integer $id + */ + function setID($instanceid) { + $this->_id = $instanceid; + } + + /** + * Setter for $_force + * @param boolean $force + */ + function setForce($isForce) { + $this->_force = $isForce; + } + /** + * Getter for $_defaultValues + * @return array $_defaultValues + */ + function getDefaultValues() { + return $this->_defaults; + } + function addColumns() { $options = array(); $colGroups = NULL; @@ -2031,19 +2079,20 @@ WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends) . "') AND } function beginPostProcess() { - $this->_params = $this->controller->exportValues($this->_name); + $this->setParams($this->controller->exportValues($this->_name)); if (empty($this->_params) && $this->_force ) { - $this->_params = $this->_formValues; + $this->setParams($this->_formValues); } // hack to fix params when submitted from dashboard, CRM-8532 // fields array is missing because form building etc is skipped // in dashboard mode for report + //@todo - this could be done in the dashboard no we have a setter if (!CRM_Utils_Array::value('fields', $this->_params) && !$this->_noFields) { - $this->_params = $this->_formValues; + $this->setParams($this->_formValues); } $this->_formValues = $this->_params; @@ -2056,6 +2105,14 @@ WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends) . "') AND $this->assign('updateReportButton', TRUE); } $this->processReportMode(); + $this->beginPostProcessCommon(); + } + + /** + * beginPostProcess function run in both report mode and non-report mode (api) + */ + function beginPostProcessCommon() { + } function buildQuery($applyLimit = TRUE) { diff --git a/api/v3/ReportTemplate.php b/api/v3/ReportTemplate.php index 5129dec728..37ffaeb965 100644 --- a/api/v3/ReportTemplate.php +++ b/api/v3/ReportTemplate.php @@ -78,6 +78,63 @@ function civicrm_api3_report_template_delete($params) { return civicrm_api3_option_value_delete($params); } +/** + * Retrieve rows from a report template + * + * @param array $params input parameters + * + * @return array details of found instances + * @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'); +} + +function _civicrm_api3_report_template_getrows($params) { + $class = civicrm_api3('option_value', 'getvalue', array( + 'option_group_id' => 'report_template', + 'return' => 'name', + 'value' => $params['report_id'], + ) + ); + + $reportInstance = new $class(); + if(!empty($params['instance_id'])) { + $reportInstance->setID($params['instance_id']); + } + $reportInstance->setParams($params); + $reportInstance->noController = TRUE; + $reportInstance->preProcess(); + $reportInstance->setDefaultValues(FALSE); + $reportInstance->setParams(array_merge($reportInstance->getDefaultValues(), $params)); + $reportInstance->beginPostProcessCommon(); + $sql = $reportInstance->buildQuery(); + $rows = array(); + $reportInstance->buildRows($sql, $rows); + return array($rows, $reportInstance); +} + +function civicrm_api3_report_template_getstatistics($params) { + list($rows, $reportInstance) = _civicrm_api3_report_template_getrows($params); + $stats = $reportInstance->statistics($rows); + return civicrm_api3_create_success($stats, $params, 'report_template'); +} +/** + * Retrieve rows from a report template + * + * @param array $params input parameters + * + * @return array details of found instances + * @access public + */ +function _civicrm_api3_report_template_getrows_spec(&$params) { + $params['report_id'] = array( + 'api.required' => TRUE, + 'title' => 'Report ID - eg. member/lapse', + ); +} + /* function civicrm_api3_report_template_getfields($params) { return civicrm_api3_create_success(array( -- 2.25.1