From 0ad26017aa0b9f75d76476026203ef04e4102b2c Mon Sep 17 00:00:00 2001 From: "deb.monish" Date: Thu, 22 Feb 2018 13:43:41 +0530 Subject: [PATCH] CRM-21789: 'Find Case' group by issue --- CRM/Contact/BAO/Query.php | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 5435aa3c2f..31a97f9d05 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -4703,10 +4703,11 @@ civicrm_relationship.is_permission_a_b = 0 * * @param array $selectClauses * @param array $groupBy - Columns already included in GROUP By clause. + * @param string $aggregateFunction * * @return string */ - public static function appendAnyValueToSelect($selectClauses, $groupBy) { + public static function appendAnyValueToSelect($selectClauses, $groupBy, $aggregateFunction = 'ANY_VALUE') { if (!CRM_Utils_SQL::disableFullGroupByMode()) { $groupBy = array_map('trim', (array) $groupBy); $aggregateFunctions = '/(ROUND|AVG|COUNT|GROUP_CONCAT|SUM|MAX|MIN)\(/i'; @@ -4714,7 +4715,9 @@ civicrm_relationship.is_permission_a_b = 0 list($selectColumn, $alias) = array_pad(preg_split('/ as /i', $val), 2, NULL); // append ANY_VALUE() keyword if (!in_array($selectColumn, $groupBy) && preg_match($aggregateFunctions, trim($selectColumn)) !== 1) { - $val = str_replace($selectColumn, "ANY_VALUE({$selectColumn})", $val); + $val = ($aggregateFunction == 'GROUP_CONCAT') ? + str_replace($selectColumn, "$aggregateFunction(DISTINCT {$selectColumn})", $val) : + str_replace($selectColumn, "$aggregateFunction({$selectColumn})", $val); } } } @@ -4732,9 +4735,9 @@ civicrm_relationship.is_permission_a_b = 0 * */ public static function getGroupByFromOrderBy(&$groupBy, $orderBys) { - if (CRM_Utils_SQL::disableFullGroupByMode()) { + if (!CRM_Utils_SQL::disableFullGroupByMode()) { foreach ($orderBys as $orderBy) { - $orderBy = str_replace(array(' DESC', ' ASC'), '', $orderBy); // remove sort syntax from ORDER BY clauses if present + $orderBy = str_replace(array(' DESC', ' ASC', '`'), '', $orderBy); // remove sort syntax from ORDER BY clauses if present // if ORDER BY column is not present in GROUP BY then append it to end if (preg_match('/(MAX|MIN)\(/i', trim($orderBy)) !== 1 && !strstr($groupBy, $orderBy)) { $groupBy .= ", {$orderBy}"; @@ -4856,21 +4859,19 @@ civicrm_relationship.is_permission_a_b = 0 } // building the query string - $groupBy = $groupByCol = NULL; + $groupBy = $groupByCols = NULL; if (!$count) { if (isset($this->_groupByComponentClause)) { $groupBy = $this->_groupByComponentClause; - $groupCols = preg_replace('/^GROUP BY /', '', trim($this->_groupByComponentClause)); - $groupByCol = explode(', ', $groupCols); + $groupByCols = preg_replace('/^GROUP BY /', '', trim($this->_groupByComponentClause)); + $groupByCols = explode(', ', $groupByCols); } elseif ($this->_useGroupBy) { - $groupByCol = 'contact_a.id'; - $groupBy = ' GROUP BY contact_a.id'; + $groupByCols = array('contact_a.id'); } } if ($this->_mode & CRM_Contact_BAO_Query::MODE_ACTIVITY && (!$count)) { - $groupByCol = 'civicrm_activity.id'; - $groupBy = 'GROUP BY civicrm_activity.id '; + $groupByCols = array('civicrm_activity.id'); } $order = $orderBy = $limit = ''; @@ -4892,8 +4893,14 @@ civicrm_relationship.is_permission_a_b = 0 list($select, $from, $where, $having) = $this->query($count, $sortByChar, $groupContacts, $onlyDeleted); - if (!empty($groupByCol)) { - $groupBy = self::getGroupByFromSelectColumns($this->_select, $groupByCol); + if (!empty($groupByCols)) { + $select = self::appendAnyValueToSelect($this->_select, $groupByCols, 'GROUP_CONCAT'); + $groupBy = " GROUP BY " . implode(', ', $groupByCols); + if (!empty($order)) { + // retrieve order by columns from ORDER BY clause + $orderBys = explode(",", str_replace('ORDER BY ', '', $order)); + self::getGroupByFromOrderBy($groupBy, $orderBys); + } } if ($additionalWhereClause) { @@ -6375,7 +6382,7 @@ AND displayRelType.is_active = 1 $orderByArray = array("UPPER(LEFT(contact_a.sort_name, 1)) asc"); } else { - $order = " ORDER BY contact_a.sort_name asc, contact_a.id"; + $order = " ORDER BY contact_a.sort_name ASC, contact_a.id"; } } if (!$order && empty($orderByArray)) { -- 2.25.1