/** * Get the count of a members in a group with the specific status * * @param int $id group id * @param enum $status status of members in group * * @return int count of members in the group with above status * @access public */ static function memberCount($id, $status = 'Added', $countChildGroups = false) { require_once 'CRM/Contact/DAO/GroupContact.php'; $groupContact =& new CRM_Contact_DAO_GroupContact(); $groupIds = array($id); if ($countChildGroups) { require_once 'CRM/Contact/BAO/GroupNesting.php'; $groupIds = CRM_Contact_BAO_GroupNesting::getDescendentGroupIds($groupIds); } $count = 0; $contacts = self::getGroupContacts($id); foreach ($groupIds as $groupId) { $groupContacts = self::getGroupContacts($groupId); foreach ($groupContacts as $gcontact) { if ($groupId != $id) { // Loop through main group's contacts // and subtract from the count for each contact which // matches one in the present group, if it is not the // main group foreach ($contacts as $contact) { if ($contact['contact_id'] == $gcontact['contact_id']) { $count--; } } } } $groupContact->group_id = $groupId; if (isset($status)) { $groupContact->status = $status; } $count += $groupContact->count(); } return $count; }
/** * Returns array of contacts who are members of the specified group. * * @param CRM_Contact $group A valid group object (passed by reference) * @param array $returnProperties Which properties * should be included in the returned Contact object(s). If NULL, * the default set of contact properties will be * included. group_contact properties (such as 'status', * ' in_date', etc.) are included automatically.Note:Do not inclue * Id releted properties. * @param text $status A valid status value ('Added', 'Pending', 'Removed'). * @param text $sort Associative array of * one or more "property_name"=>"sort direction" * pairs which will control order of Contact objects returned. * @param Int $offset Starting row index. * @param Int $row_count Maximum number of rows to returns. * * * @return $contactArray Array of contacts who are members of the specified group * * @access public */ static function getGroupContacts(&$group, $returnProperties = null, $status = 'Added', $sort = null, $offset = null, $row_count = null, $includeChildGroups = false) { $groupDAO =& new CRM_Contact_DAO_Group(); $groupDAO->id = $group->id; if (!$groupDAO->find(true)) { return CRM_Core_Error::createError("Could not locate group with id: {$id}"); } // make sure user has got permission to view this group require_once 'CRM/Contact/BAO/Group.php'; if (!CRM_Contact_BAO_Group::checkPermission($groupDAO->id, $groupDAO->title)) { return CRM_Core_Error::createError("You do not have permission to access group with id: {$id}"); } $query = ''; if (empty($returnProperties)) { $query = "SELECT contact_a.id as contact_id,\n civicrm_email.email as email"; } else { $query = "SELECT contact_a.id as contact_id , {$grpStatus} as status,"; $query .= implode(',', $returnProperties); } $params = array(); if ($includeChildGroups) { require_once 'CRM/Contact/BAO/GroupNesting.php'; $groupIds = CRM_Contact_BAO_GroupNesting::getDescendentGroupIds(array($group->id)); } else { $groupIds = array($group->id); } foreach ($groupIds as $groupId) { $params[] = array('group', 'IN', array($group->id => true), 0, 0); } require_once 'CRM/Core/BAO/Email.php'; require_once 'CRM/Contact/BAO/Contact.php'; $tables = array(CRM_Core_BAO_Email::getTableName() => true, CRM_Contact_BAO_Contact::getTableName() => true); $inner = array(); $whereTables = array(); $where = CRM_Contact_BAO_Query::getWhereClause($params, null, $tables, $whereTables); $permission = CRM_Core_Permission::whereClause(CRM_Core_Permission::VIEW, $tables, $whereTables); $from = CRM_Contact_BAO_Query::fromClause($tables, $inner); $query .= " {$from} WHERE {$permission} AND {$where} "; if ($sort != null) { $order = array(); foreach ($sort as $key => $direction) { $order[] = " {$key} {$direction} "; } $query .= " ORDER BY " . implode(',', $order); } if (!is_null($offset) && !is_null($row_count)) { $query .= " LIMIT {$offset}, {$row_count}"; } $dao =& new CRM_Contact_DAO_Contact(); $dao->query($query); // this is quite inefficient, we need to change the return // values in docs $contactArray = array(); while ($dao->fetch()) { $contactArray[] = clone $dao; } return $contactArray; }
/** * Get the count of a members in a group with the specific status. * * @param int $id * Group id. * @param string $status * status of members in group * @param bool $countChildGroups * * @return int * count of members in the group with above status */ public static function memberCount($id, $status = 'Added', $countChildGroups = FALSE) { $groupContact = new CRM_Contact_DAO_GroupContact(); $groupIds = array($id); if ($countChildGroups) { $groupIds = CRM_Contact_BAO_GroupNesting::getDescendentGroupIds($groupIds); } $count = 0; $contacts = self::getGroupContacts($id); foreach ($groupIds as $groupId) { $groupContacts = self::getGroupContacts($groupId); foreach ($groupContacts as $gcontact) { if ($groupId != $id) { // Loop through main group's contacts // and subtract from the count for each contact which // matches one in the present group, if it is not the // main group foreach ($contacts as $contact) { if ($contact['contact_id'] == $gcontact['contact_id']) { $count--; } } } } $groupContact->group_id = $groupId; if (isset($status)) { $groupContact->status = $status; } $groupContact->_query['condition'] = 'WHERE contact_id NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1)'; $count += $groupContact->count(); } return $count; }