Skip to content

Commit

Permalink
Merge pull request #2170 from eileenmcnaughton/CRM-13918
Browse files Browse the repository at this point in the history
CRM-13918 add report_template.getrows, report_template.getstatistics api functions
  • Loading branch information
deepak-srivastava committed Dec 16, 2013
2 parents 72984a5 + e2779f6 commit d194675
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 15 deletions.
81 changes: 69 additions & 12 deletions CRM/Report/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
*/
Expand Down Expand Up @@ -318,18 +333,20 @@ function preProcessCommon() {
CRM_Core_Region::instance('page-header')->add(array(
'markup' => sprintf('<!-- Report class: [%s] -->', 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);
Expand Down Expand Up @@ -368,7 +385,7 @@ function preProcessCommon() {
// 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) {
Expand Down Expand Up @@ -729,6 +746,37 @@ function getElementFromGroup($group, $grpFieldName) {
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;
Expand Down Expand Up @@ -2031,19 +2079,20 @@ function processReportMode() {
}

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;
Expand All @@ -2056,6 +2105,14 @@ function beginPostProcess() {
$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) {
Expand Down
4 changes: 3 additions & 1 deletion CRM/Report/Form/Contribute/Detail.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class CRM_Report_Form_Contribute_Detail extends CRM_Report_Form {
protected $_summary = NULL;
protected $_allBatches = NULL;

protected $_softFrom = NULL;

protected $_customGroupExtends = array( 'Contribution');

function __construct() {
Expand Down Expand Up @@ -540,7 +542,7 @@ function statistics(&$rows) {
);

// Stats for soft credits
if (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) != 'contributions_only') {
if ($this->_softFrom && CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) != 'contributions_only') {
$totalAmount = $average = array();
$count = 0;
$select = "
Expand Down
57 changes: 57 additions & 0 deletions api/v3/ReportTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
98 changes: 96 additions & 2 deletions tests/phpunit/api/v3/ReportTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@
*/

class api_v3_ReportTemplateTest extends CiviUnitTestCase {
protected $_apiversion;
protected $_apiversion = 3;
public $_eNoticeCompliant = TRUE;
function setUp() {
$this->_apiversion = 3;
parent::setUp();
$this->_sethtmlGlobals();
}

function tearDown() {}
Expand Down Expand Up @@ -109,4 +109,98 @@ public function testReportTemplate() {
WHERE name = "CRM_Report_Form_Examplez"
');
}

/**
*
*/
function testReportTemplateGetRowsContactSummary() {
$result = $this->callAPISuccess('report_template', 'getrows', array(
'report_id' => 'contact/summary',
));

//the second part of this test has been commented out because it relied on the db being reset to
// it's base state
//wasn't able to get that to work consistently
// however, when the db is in the base state the tests do pass
// and because the test covers 'all' contacts we can't create our own & assume the others don't exist
/*
$this->assertEquals(2, $result['count']);
$this->assertEquals('Default Organization', $result[0]['civicrm_contact_sort_name']);
$this->assertEquals('Second Domain', $result[1]['civicrm_contact_sort_name']);
$this->assertEquals('15 Main St', $result[1]['civicrm_address_street_address']);
*/
}

/**
* @dataProvider getReportTemplates
*/
function testReportTemplateGetRowsAllReports($reportID) {
if(stristr($reportID, 'has existing issues')) {
$this->markTestIncomplete($reportID);
}
$result = $this->callAPISuccess('report_template', 'getrows', array(
'report_id' => $reportID,
));
}

/**
* @dataProvider getReportTemplates
*/
function testReportTemplateGetStatisticsAllReports($reportID) {
if(stristr($reportID, 'has existing issues')) {
$this->markTestIncomplete($reportID);
}
if(in_array($reportID , array('contribute/softcredit', 'contribute/bookkeeping'))) {
$this->markTestIncomplete($reportID . " has non enotices when calling statistics fn");
}
$result = $this->callAPISuccess('report_template', 'getstatistics', array(
'report_id' => $reportID,
));
}

/**
* Data provider function for getting all templates, note that the function needs to
* be static so cannot use $this->callAPISuccess
*/
public static function getReportTemplates() {
$reportsToSkip = array(
'activity' => 'does not respect function signature on from clause',
'walklist' => 'Notice: Undefined index: type in CRM_Report_Form_Walklist_Walklist line 155.
(suspect the select function should be removed in favour of the parent (state province field)
also, type should be added to state province & others? & potentially getAddressColumns fn should be
used per other reports',
'contribute/repeat' => 'Reports with important functionality in postProcess are not callable via the api. For variable setting recommend beginPostProcessCommon, for temp table creation recommend From fn',
'contribute/organizationSummary' => 'Failure in api call for report_template getrows: Only variables should be assigned by reference line 381',
'contribute/householdSummary' => '(see contribute/repeat) Undefined property: CRM_Report_Form_Contribute_HouseholdSummary::$householdContact LINE 260, property should be declared on class, for api accessibility should be set in beginPreProcess common',
'contribute/topDonor' => 'construction of query in postprocess makes inaccessible ',
'contribute/sybunt' => 'e notice - (ui gives fatal error at civicrm/report/contribute/sybunt&reset=1&force=1
e-notice is on yid_valueContribute/Sybunt.php(214) because at the force url "yid_relative" not "yid_value" is defined',
'contribute/lybunt' => 'same as sybunt - fatals on force url & test identifies why',
'event/income' => 'I do no understant why but error is Call to undefined method CRM_Report_Form_Event_Income::from() in CRM/Report/Form.php on line 2120',
'contact/relationship' => '(see contribute/repeat), property declaration issue, Undefined property: CRM_Report_Form_Contact_Relationship::$relationType in /Contact/Relationship.php(486):',
'case/demographics' => 'Undefined index: operatorType Case/Demographics.php(319)',
'activitySummary' => 'Undefined index: group_bys_freq m/ActivitySummary.php(191)',
'event/incomesummary' => 'Undefined index: title, Report/Form/Event/IncomeCountSummary.php(187)',
'logging/contact/summary' => '(likely to be test releated) probably logging off Undefined index: Form/Contact/LoggingSummary.php(231): PHP',
'logging/contact/detail' => '(likely to be test releated) probably logging off DB Error: no such table',
'logging/contribute/summary' => '(likely to be test releated) probably logging off DB Error: no such table',
'logging/contribute/detail' => '(likely to be test releated) probably logging off DB Error: no such table',
'survey/detail' => '(likely to be test releated) Undefined index: CiviCampaign civicrm CRM/Core/Component.php(196)',
'contribute/history' => 'Declaration of CRM_Report_Form_Contribute_History::buildRows() should be compatible with CRM_Report_Form::buildRows($sql, &$rows)',
);

$reports = civicrm_api3('report_template', 'get', array('return' => 'value', 'options' => array('limit' => 500)));
foreach ($reports['values'] as $report) {
if(empty($reportsToSkip[$report['value']])) {
$reportTemplates[] = array($report['value']);
}
else {
$reportTemplates[] = array($report['value']. " has existing issues : " . $reportsToSkip[$report['value']]);
}
}



return $reportTemplates;
}
}

0 comments on commit d194675

Please sign in to comment.