From c3fdd2b7defaafe9f1c17d3f593e766e630a69f5 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 2 Apr 2018 20:46:09 +1200 Subject: [PATCH] Fix fatal on topDonor report --- CRM/Report/Form.php | 60 ++++++++++----------- CRM/Report/Form/Contribute/TopDonor.php | 21 ++------ tests/phpunit/api/v3/ReportTemplateTest.php | 1 - 3 files changed, 33 insertions(+), 49 deletions(-) diff --git a/CRM/Report/Form.php b/CRM/Report/Form.php index d9de18c085..c95b5dffd2 100644 --- a/CRM/Report/Form.php +++ b/CRM/Report/Form.php @@ -1393,7 +1393,7 @@ class CRM_Report_Form extends CRM_Core_Form { foreach ($this->_columns as $tableName => $table) { if (array_key_exists('group_bys', $table)) { foreach ($table['group_bys'] as $fieldName => $field) { - if (!empty($field)) { + if (!empty($field) && empty($field['no_display'])) { $options[$field['title']] = $fieldName; if (!empty($field['frequency'])) { $freqElements[$field['title']] = $fieldName; @@ -5191,45 +5191,45 @@ LEFT JOIN civicrm_contact {$field['alias']} ON {$field['alias']}.id = {$this->_a */ protected function storeGroupByArray() { - if (CRM_Utils_Array::value('group_bys', $this->_params) && - is_array($this->_params['group_bys']) && - !empty($this->_params['group_bys']) - ) { - foreach ($this->_columns as $tableName => $table) { - $table = $this->_columns[$tableName]; - if (array_key_exists('group_bys', $table)) { - foreach ($table['group_bys'] as $fieldName => $fieldData) { - $field = $this->_columns[$tableName]['metadata'][$fieldName]; - if (!empty($this->_params['group_bys'][$fieldName])) { - if (!empty($field['chart'])) { - $this->assign('chartSupported', TRUE); - } + if (!CRM_Utils_Array::value('group_bys', $this->_params) + || !is_array($this->_params['group_bys'])) { + $this->_params['group_bys'] = []; + } - if (!empty($table['group_bys'][$fieldName]['frequency']) && - !empty($this->_params['group_bys_freq'][$fieldName]) - ) { + foreach ($this->_columns as $tableName => $table) { + $table = $this->_columns[$tableName]; + if (array_key_exists('group_bys', $table)) { + foreach ($table['group_bys'] as $fieldName => $fieldData) { + $field = $this->_columns[$tableName]['metadata'][$fieldName]; + if (!empty($this->_params['group_bys'][$fieldName]) || !empty($fieldData['required'])) { + if (!empty($field['chart'])) { + $this->assign('chartSupported', TRUE); + } - switch ($this->_params['group_bys_freq'][$fieldName]) { - case 'FISCALYEAR': - $this->_groupByArray[$tableName . '_' . $fieldName . '_start'] = self::fiscalYearOffset($field['dbAlias']); + if (!empty($table['group_bys'][$fieldName]['frequency']) && + !empty($this->_params['group_bys_freq'][$fieldName]) + ) { - case 'YEAR': - $this->_groupByArray[$tableName . '_' . $fieldName . '_start'] = " {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})"; + switch ($this->_params['group_bys_freq'][$fieldName]) { + case 'FISCALYEAR': + $this->_groupByArray[$tableName . '_' . $fieldName . '_start'] = self::fiscalYearOffset($field['dbAlias']); - default: - $this->_groupByArray[$tableName . '_' . $fieldName . '_start'] = "EXTRACT(YEAR_{$this->_params['group_bys_freq'][$fieldName]} FROM {$field['dbAlias']})"; + case 'YEAR': + $this->_groupByArray[$tableName . '_' . $fieldName . '_start'] = " {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})"; + + default: + $this->_groupByArray[$tableName . '_' . $fieldName . '_start'] = "EXTRACT(YEAR_{$this->_params['group_bys_freq'][$fieldName]} FROM {$field['dbAlias']})"; - } } - else { - if (!in_array($field['dbAlias'], $this->_groupByArray)) { - $this->_groupByArray[$tableName . '_' . $fieldName] = $field['dbAlias']; - } + } + else { + if (!in_array($field['dbAlias'], $this->_groupByArray)) { + $this->_groupByArray[$tableName . '_' . $fieldName] = $field['dbAlias']; } } } - } + } } } diff --git a/CRM/Report/Form/Contribute/TopDonor.php b/CRM/Report/Form/Contribute/TopDonor.php index ce0bb788f6..4be5c28e8b 100644 --- a/CRM/Report/Form/Contribute/TopDonor.php +++ b/CRM/Report/Form/Contribute/TopDonor.php @@ -104,12 +104,13 @@ class CRM_Report_Form_Contribute_TopDonor extends CRM_Report_Form { ), ), 'filters' => $this->getBasicContactFilters(), + 'group_bys' => ['contact_contact_id' => ['name' => 'id', 'required' => 1, 'no_display' => 1]], ), 'civicrm_line_item' => array( 'dao' => 'CRM_Price_DAO_LineItem', ), ); - $this->_columns += $this->getAddressColumns(); + $this->_columns += $this->getAddressColumns(['group_by' => FALSE]); $this->_columns += array( 'civicrm_contribution' => array( 'dao' => 'CRM_Contribute_DAO_Contribution', @@ -159,6 +160,7 @@ class CRM_Report_Form_Contribute_TopDonor extends CRM_Report_Form { 'default' => array(1), ), ), + 'group_bys' => ['contribution_currency' => ['name' => 'currency', 'required' => 1, 'no_display' => 1]], ), 'civicrm_financial_trxn' => array( 'dao' => 'CRM_Financial_DAO_FinancialTrxn', @@ -208,19 +210,6 @@ class CRM_Report_Form_Contribute_TopDonor extends CRM_Report_Form { parent::__construct(); } - public function preProcess() { - parent::preProcess(); - } - - /** - * Select only contact ID when adding to group. - * - * @todo consider moving that to parent to support AddToGroup in general. - */ - public function select() { - parent::select(); - } - /** * @param $fields * @param $files @@ -319,10 +308,6 @@ class CRM_Report_Form_Contribute_TopDonor extends CRM_Report_Form { } } - public function groupBy() { - $this->_groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, array("{$this->_aliases['civicrm_contact']}.id", "{$this->_aliases['civicrm_contribution']}.currency")); - } - /** * Build output rows. * diff --git a/tests/phpunit/api/v3/ReportTemplateTest.php b/tests/phpunit/api/v3/ReportTemplateTest.php index d37c6095fa..8cc5b6f6d8 100644 --- a/tests/phpunit/api/v3/ReportTemplateTest.php +++ b/tests/phpunit/api/v3/ReportTemplateTest.php @@ -188,7 +188,6 @@ class api_v3_ReportTemplateTest extends CiviUnitTestCase { public static function getReportTemplates() { $reportsToSkip = array( 'activity' => 'does not respect function signature on from clause', - 'contribute/topDonor' => 'construction of query in postProcess makes inaccessible ', 'event/income' => 'I do no understand why but error is Call to undefined method CRM_Report_Form_Event_Income::from() in CRM/Report/Form.php on line 2120', 'logging/contact/summary' => '(likely to be test related) probably logging off Undefined index: Form/Contact/LoggingSummary.php(231): PHP', 'logging/contribute/summary' => '(likely to be test related) probably logging off DB Error: no such table', -- 2.25.1