Skip to content

Commit

Permalink
Switch alphabetQuery to use new getSearchSQLParts() function
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwire committed Mar 6, 2019
1 parent adbc68a commit 3d5198a
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -4924,7 +4924,10 @@ public function searchQuery(
* @return CRM_Core_DAO
*/
public function alphabetQuery() {
$query = $this->getSearchSQL(NULL, NULL, NULL, FALSE, FALSE, TRUE);
$sqlParts = $this->getSearchSQLParts(NULL, NULL, NULL, FALSE, FALSE, TRUE);
$sqlParts['group_by'] = 'GROUP BY sort_name';
$sqlParts['order_by'] = 'ORDER BY sort_name asc';
$query = "{$sqlParts['select']} {$sqlParts['from']} {$sqlParts['where']} {$sqlParts['having']} {$sqlParts['group_by']} {$sqlParts['order_by']}";

$dao = CRM_Core_DAO::executeQuery($query);

Expand Down Expand Up @@ -6241,16 +6244,14 @@ public static function processSpecialFormValue(&$formValues, $specialFields, $ch
*
* @param string|CRM_Utils_Sort $sort
* The order by string.
* @param bool $sortByChar
* If true returns the distinct array of first characters for search results.
* @param null $sortOrder
* Who knows? Hu knows. He who knows Hu knows who.
* @param string $additionalFromClause
* Should be clause with proper joins, effective to reduce where clause load.
* @return array
* list(string $orderByClause, string $additionalFromClause).
*/
protected function prepareOrderBy($sort, $sortByChar, $sortOrder, $additionalFromClause) {
protected function prepareOrderBy($sort, $sortOrder, $additionalFromClause) {
$orderByArray = [];
$orderBy = '';

Expand Down Expand Up @@ -6286,9 +6287,6 @@ protected function prepareOrderBy($sort, $sortByChar, $sortOrder, $additionalFro
}
}
}
elseif ($sortByChar) {
$orderBy = " sort_name asc";
}
else {
$orderBy = " contact_a.sort_name ASC, contact_a.id";
}
Expand Down Expand Up @@ -6377,10 +6375,6 @@ protected function prepareOrderBy($sort, $sortByChar, $sortOrder, $additionalFro

// The above code relies on crazy brittle string manipulation of a peculiarly-encoded ORDER BY
// clause. But this magic helper which forgivingly reescapes ORDER BY.
// Note: $sortByChar implies that $order was hard-coded/trusted, so it can do funky things.
if ($sortByChar) {
return array(' ORDER BY ' . $order, $additionalFromClause);
}
if ($order) {
$order = CRM_Utils_Type::escape($order, 'MysqlOrderBy');
return array(' ORDER BY ' . $order, $additionalFromClause);
Expand Down Expand Up @@ -6729,14 +6723,14 @@ public function getSearchSQLParts($offset = 0, $rowCount = 0, $sort = NULL,

$order = $orderBy = '';
if (!$count) {
list($order, $additionalFromClause) = $this->prepareOrderBy($sort, $sortByChar, $sortOrder, $additionalFromClause);
if (!$sortByChar) {
list($order, $additionalFromClause) = $this->prepareOrderBy($sort, $sortOrder, $additionalFromClause);
}
}
// Two cases where we are disabling FGB (FULL_GROUP_BY_MODE):
// 1. Expecting the search query to return all the first single letter characters of contacts ONLY, but when FGB is enabled
// MySQL expect the columns present in GROUP BY, must be present in SELECT clause and that results into error, needless to have other columns.
// 2. When GROUP BY columns are present then disable FGB otherwise it demands to add ORDER BY columns in GROUP BY and eventually in SELECT
// Cases where we are disabling FGB (FULL_GROUP_BY_MODE):
// 1. When GROUP BY columns are present then disable FGB otherwise it demands to add ORDER BY columns in GROUP BY and eventually in SELECT
// clause. This will impact the search query output.
$disableFullGroupByMode = ($sortByChar || !empty($groupBy) || $groupContacts);
$disableFullGroupByMode = (!empty($groupBy) || $groupContacts);

if ($disableFullGroupByMode) {
CRM_Core_DAO::disableFullGroupByMode();
Expand Down

0 comments on commit 3d5198a

Please sign in to comment.