}
$query = "
- SELECT groups.*, createdBy.sort_name as created_by, IF(groups.saved_search_id, COUNT(smart_contacts.id), COUNT(contacts.id)) as `count` {$select}
+ SELECT groups.*, createdBy.sort_name as created_by {$select}
FROM civicrm_group groups
- LEFT JOIN civicrm_group_contact contacts
- ON contacts.group_id = groups.id AND contacts.status = 'Added'
- LEFT JOIN civicrm_group_contact_cache smart_contacts
- ON smart_contacts.group_id = groups.id
LEFT JOIN civicrm_contact createdBy
ON createdBy.id = groups.created_id
{$from}
$links = self::actionLinks();
$allTypes = CRM_Core_OptionGroup::values('group_type');
- $values = array();
+ $values = $groupsToCount = array();
$visibility = CRM_Core_SelectValues::ufVisibility();
if ($permission) {
$newLinks = $links;
- $values[$object->id] = array('class' => array());
+ $values[$object->id] = array(
+ 'class' => array(),
+ 'count' => '0',
+ );
CRM_Core_DAO::storeValues($object, $values[$object->id]);
if ($object->saved_search_id) {
$values[$object->id]['title'] .= ' (' . ts('Smart Group') . ')';
$values[$object->id]['visibility'] = $visibility[$values[$object->id]['visibility']];
- $values[$object->id]['count'] = $object->count;
+ $groupsToCount[$object->saved_search_id ? 'civicrm_group_contact_cache' : 'civicrm_group_contact'][] = $object->id;
if (isset($values[$object->id]['group_type'])) {
$groupTypes = explode(CRM_Core_DAO::VALUE_SEPARATOR,
}
}
+ // Get group counts
+ foreach ($groupsToCount as $table => $groups) {
+ $dao = CRM_Core_DAO::executeQuery("SELECT group_id, COUNT(id) as `count` FROM $table WHERE group_id IN (" . implode(',', $groups) . ") GROUP BY group_id");
+ while($dao->fetch()) {
+ $values[$dao->group_id]['count'] = $dao->count;
+ }
+ }
+
return $values;
}