3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright U.S. PIRG Education Fund (c) 2007 |
7 | Licensed to CiviCRM under the Academic Free License version 3.0. |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
32 * @copyright U.S. PIRG 2007
34 class CRM_Contact_BAO_GroupNesting
extends CRM_Contact_DAO_GroupNesting
{
37 * Adds a new group nesting record.
39 * @param int $parentID
40 * Id of the group to add the child to.
42 * Id of the new child group.
44 * @return \CRM_Contact_DAO_GroupNesting
46 public static function add($parentID, $childID) {
47 $dao = new CRM_Contact_DAO_GroupNesting();
48 $dao->child_group_id
= $childID;
49 $dao->parent_group_id
= $parentID;
50 if (!$dao->find(TRUE)) {
57 * Removes a child group from it's parent.
59 * Does not delete child group, just the association between the two
61 * @param int $parentID
62 * The id of the group to remove the child from.
64 * The id of the child group being removed.
66 public static function remove($parentID, $childID) {
67 $dao = new CRM_Contact_DAO_GroupNesting();
68 $dao->child_group_id
= $childID;
69 $dao->parent_group_id
= $parentID;
70 if ($dao->find(TRUE)) {
76 * Checks whether the association between parent and child is present.
78 * @param int $parentID
79 * The parent id of the association.
82 * The child id of the association.
85 * True if association is found, false otherwise.
87 public static function isParentChild($parentID, $childID) {
88 $dao = new CRM_Contact_DAO_GroupNesting();
89 $dao->child_group_id
= $childID;
90 $dao->parent_group_id
= $parentID;
98 * Checks whether groupId has 1 or more parent groups.
100 * @param int $groupId
101 * The id of the group to check for parent groups.
104 * True if 1 or more parent groups are found, false otherwise.
106 public static function hasParentGroups($groupId) {
107 $dao = new CRM_Contact_DAO_GroupNesting();
108 $query = "SELECT parent_group_id FROM civicrm_group_nesting WHERE child_group_id = $groupId LIMIT 1";
117 * Returns array of group ids of child groups of the specified group.
119 * @param array $groupIds
120 * An array of valid group ids (passed by reference).
123 * List of groupIds that represent the requested group and its children
125 public static function getChildGroupIds($groupIds) {
126 if (!is_array($groupIds)) {
127 $groupIds = array($groupIds);
129 $dao = new CRM_Contact_DAO_GroupNesting();
130 $query = "SELECT child_group_id FROM civicrm_group_nesting WHERE parent_group_id IN (" . implode(',', $groupIds) . ")";
132 $childGroupIds = array();
133 while ($dao->fetch()) {
134 $childGroupIds[] = $dao->child_group_id
;
136 return $childGroupIds;
140 * Returns array of group ids of parent groups of the specified group.
142 * @param array $groupIds
143 * An array of valid group ids (passed by reference).
146 * List of groupIds that represent the requested group and its parents
148 public static function getParentGroupIds($groupIds) {
149 if (!is_array($groupIds)) {
150 $groupIds = array($groupIds);
152 $dao = new CRM_Contact_DAO_GroupNesting();
153 $query = "SELECT parent_group_id FROM civicrm_group_nesting WHERE child_group_id IN (" . implode(',', $groupIds) . ")";
155 $parentGroupIds = array();
156 while ($dao->fetch()) {
157 $parentGroupIds[] = $dao->parent_group_id
;
159 return $parentGroupIds;
163 * Returns array of group ids of descendent groups of the specified group.
165 * @param array $groupIds
166 * An array of valid group ids (passed by reference).
168 * @param bool $includeSelf
171 * List of groupIds that represent the requested group and its descendents
173 public static function getDescendentGroupIds($groupIds, $includeSelf = TRUE) {
174 if (!is_array($groupIds)) {
175 $groupIds = array($groupIds);
177 $dao = new CRM_Contact_DAO_GroupNesting();
178 $query = "SELECT child_group_id, parent_group_id FROM civicrm_group_nesting WHERE parent_group_id IN (" . implode(',', $groupIds) . ")";
180 $tmpGroupIds = array();
181 $childGroupIds = array();
183 $childGroupIds = $groupIds;
185 while ($dao->fetch()) {
186 // make sure we're not following any cyclical references
187 if (!array_key_exists($dao->parent_group_id
, $childGroupIds) && $dao->child_group_id
!= $groupIds[0]) {
188 $tmpGroupIds[] = $dao->child_group_id
;
191 if (!empty($tmpGroupIds)) {
192 $newChildGroupIds = self
::getDescendentGroupIds($tmpGroupIds);
193 $childGroupIds = array_merge($childGroupIds, $newChildGroupIds);
195 return $childGroupIds;