From 7628764a08bbcb353205272d22f04a218412566e Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 11 Jul 2019 01:24:10 +1200 Subject: [PATCH] Move group by to processor --- CRM/Export/BAO/Export.php | 52 ------------------------------ CRM/Export/BAO/ExportProcessor.php | 48 ++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 53 deletions(-) diff --git a/CRM/Export/BAO/Export.php b/CRM/Export/BAO/Export.php index c1f78bf179..0513b39162 100644 --- a/CRM/Export/BAO/Export.php +++ b/CRM/Export/BAO/Export.php @@ -75,54 +75,6 @@ class CRM_Export_BAO_Export { return $component; } - /** - * Get Query Group By Clause - * @param \CRM_Export_BAO_ExportProcessor $processor - * Export Mode - * @param object $query - * CRM_Contact_BAO_Query - * - * @return string - * Group By Clause - */ - public static function getGroupBy($processor, $query) { - $groupBy = NULL; - $returnProperties = $processor->getReturnProperties(); - $exportMode = $processor->getExportMode(); - $queryMode = $processor->getQueryMode(); - if (!empty($returnProperties['tags']) || !empty($returnProperties['groups']) || - CRM_Utils_Array::value('notes', $returnProperties) || - // CRM-9552 - ($queryMode & CRM_Contact_BAO_Query::MODE_CONTACTS && $query->_useGroupBy) - ) { - $groupBy = "contact_a.id"; - } - - switch ($exportMode) { - case CRM_Export_Form_Select::CONTRIBUTE_EXPORT: - $groupBy = 'civicrm_contribution.id'; - if (CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled()) { - // especial group by when soft credit columns are included - $groupBy = ['contribution_search_scredit_combined.id', 'contribution_search_scredit_combined.scredit_id']; - } - break; - - case CRM_Export_Form_Select::EVENT_EXPORT: - $groupBy = 'civicrm_participant.id'; - break; - - case CRM_Export_Form_Select::MEMBER_EXPORT: - $groupBy = "civicrm_membership.id"; - break; - } - - if ($queryMode & CRM_Contact_BAO_Query::MODE_ACTIVITY) { - $groupBy = "civicrm_activity.id "; - } - - return $groupBy ? ' GROUP BY ' . implode(', ', (array) $groupBy) : ''; - } - /** * Get the list the export fields. * @@ -206,10 +158,6 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c // This perhaps only needs calling when $mergeSameHousehold == 1 self::buildRelatedContactArray($selectAll, $ids, $processor, $componentTable); - $groupBy = self::getGroupBy($processor, $query); - - $queryString .= $groupBy; - if ($order) { // always add contact_a.id to the ORDER clause // so the order is deterministic diff --git a/CRM/Export/BAO/ExportProcessor.php b/CRM/Export/BAO/ExportProcessor.php index 6898793312..9c81f1689c 100644 --- a/CRM/Export/BAO/ExportProcessor.php +++ b/CRM/Export/BAO/ExportProcessor.php @@ -741,7 +741,9 @@ class CRM_Export_BAO_ExportProcessor { else { $where .= " AND " . implode(' AND ', $whereClauses); } - return [$query, "$select $from $where $having"]; + + $groupBy = $this->getGroupBy($query); + return [$query, "$select $from $where $having $groupBy"]; } /** @@ -1675,6 +1677,50 @@ class CRM_Export_BAO_ExportProcessor { } return $returnProperties; } + /** + * @param object $query + * CRM_Contact_BAO_Query + * + * @return string + * Group By Clause + */ + public function getGroupBy($query) { + $groupBy = NULL; + $returnProperties = $this->getReturnProperties(); + $exportMode = $this->getExportMode(); + $queryMode = $this->getQueryMode(); + if (!empty($returnProperties['tags']) || !empty($returnProperties['groups']) || + CRM_Utils_Array::value('notes', $returnProperties) || + // CRM-9552 + ($queryMode & CRM_Contact_BAO_Query::MODE_CONTACTS && $query->_useGroupBy) + ) { + $groupBy = "contact_a.id"; + } + + switch ($exportMode) { + case CRM_Export_Form_Select::CONTRIBUTE_EXPORT: + $groupBy = 'civicrm_contribution.id'; + if (CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled()) { + // especial group by when soft credit columns are included + $groupBy = ['contribution_search_scredit_combined.id', 'contribution_search_scredit_combined.scredit_id']; + } + break; + + case CRM_Export_Form_Select::EVENT_EXPORT: + $groupBy = 'civicrm_participant.id'; + break; + + case CRM_Export_Form_Select::MEMBER_EXPORT: + $groupBy = "civicrm_membership.id"; + break; + } + + if ($queryMode & CRM_Contact_BAO_Query::MODE_ACTIVITY) { + $groupBy = "civicrm_activity.id "; + } + + return $groupBy ? ' GROUP BY ' . implode(', ', (array) $groupBy) : ''; + } /** * @param int $contactId -- 2.25.1