/** * Provides group nesting record(s) given parent and/or child id. * * @param array $params an array containing at least child_group_id or parent_group_id * * @return array list of group nesting records */ function civicrm_group_nesting_get(&$params) { _civicrm_initialize(); if (!is_array($params)) { return civicrm_create_error('Params need to be of type array!'); } if (!array_key_exists('child_group_id', $params) && !array_key_exists('parent_group_id', $params)) { return civicrm_create_error(ts('At least one of child_group_id or parent_group_id is a required field')); } require_once 'CRM/Contact/DAO/GroupNesting.php'; $dao = new CRM_Contact_DAO_GroupNesting(); if (array_key_exists('child_group_id', $params)) { $dao->child_group_id = $params['child_group_id']; } if (array_key_exists('parent_group_id', $params)) { $dao->parent_group_id = $params['parent_group_id']; } $values = array(); if ($dao->find()) { while ($dao->fetch()) { $temp = array(); _civicrm_object_to_array($dao, $temp); $values[$dao->id] = $temp; } $values['is_error'] = 0; } else { return civicrm_create_error('No records found.'); } return $values; }
/** * Returns array of group ids of descendent groups of the specified group. * * @param array $groupIds * An array of valid group ids (passed by reference). * * @param bool $includeSelf * @return array * List of groupIds that represent the requested group and its descendents */ public static function getDescendentGroupIds($groupIds, $includeSelf = TRUE) { if (!is_array($groupIds)) { $groupIds = array($groupIds); } $dao = new CRM_Contact_DAO_GroupNesting(); $query = "SELECT child_group_id, parent_group_id FROM civicrm_group_nesting WHERE parent_group_id IN (" . implode(',', $groupIds) . ")"; $dao->query($query); $tmpGroupIds = array(); $childGroupIds = array(); if ($includeSelf) { $childGroupIds = $groupIds; } while ($dao->fetch()) { // make sure we're not following any cyclical references if (!array_key_exists($dao->parent_group_id, $childGroupIds) && $dao->child_group_id != $groupIds[0]) { $tmpGroupIds[] = $dao->child_group_id; } } if (!empty($tmpGroupIds)) { $newChildGroupIds = self::getDescendentGroupIds($tmpGroupIds); $childGroupIds = array_merge($childGroupIds, $newChildGroupIds); } return $childGroupIds; }