From: Coleman Watts Date: Fri, 24 Jul 2015 20:15:49 +0000 (-0400) Subject: CRM-16905 - Sort by count cannot be done with sql X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=1d19a8f2b71e134588d02081c60b70dd795c0b30;p=civicrm-core.git CRM-16905 - Sort by count cannot be done with sql --- diff --git a/CRM/Contact/BAO/Group.php b/CRM/Contact/BAO/Group.php index 7c268b630a..d63caf2650 100644 --- a/CRM/Contact/BAO/Group.php +++ b/CRM/Contact/BAO/Group.php @@ -795,6 +795,11 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group { $orderBy = ' ORDER BY groups.title asc'; if (!empty($params['sort'])) { $orderBy = ' ORDER BY ' . CRM_Utils_Type::escape($params['sort'], 'String'); + + // CRM-16905 - Sort by count cannot be done with sql + if (strpos($params['sort'], 'count') === 0) { + $orderBy = $limit = ''; + } } $select = $from = $where = ""; @@ -976,6 +981,17 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group { } } + // CRM-16905 - Sort by count cannot be done with sql + if (!empty($params['sort']) && strpos($params['sort'], 'count') === 0) { + usort($values, function($a, $b) { + return $a['count'] - $b['count']; + }); + if (strpos($params['sort'], 'desc')) { + $values = array_reverse($values, TRUE); + } + return array_slice($values, $params['offset'], $params['rowCount']); + } + return $values; }