Merge pull request #8694 from eileenmcnaughton/unhurt-my-server
[civicrm-core.git] / CRM / Contact / BAO / GroupNesting.php
1 <?php
2 /*
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. |
10 | |
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. |
14 | |
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. |
19 | |
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 +--------------------------------------------------------------------+
27 */
28
29 /**
30 *
31 * @package CRM
32 * @copyright U.S. PIRG 2007
33 */
34 class CRM_Contact_BAO_GroupNesting extends CRM_Contact_DAO_GroupNesting {
35
36 /**
37 * Adds a new group nesting record.
38 *
39 * @param int $parentID
40 * Id of the group to add the child to.
41 * @param int $childID
42 * Id of the new child group.
43 *
44 * @return \CRM_Contact_DAO_GroupNesting
45 */
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)) {
51 $dao->save();
52 }
53 return $dao;
54 }
55
56 /**
57 * Removes a child group from it's parent.
58 *
59 * Does not delete child group, just the association between the two
60 *
61 * @param int $parentID
62 * The id of the group to remove the child from.
63 * @param int $childID
64 * The id of the child group being removed.
65 */
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)) {
71 $dao->delete();
72 }
73 }
74
75 /**
76 * Checks whether the association between parent and child is present.
77 *
78 * @param int $parentID
79 * The parent id of the association.
80 *
81 * @param int $childID
82 * The child id of the association.
83 *
84 * @return bool
85 * True if association is found, false otherwise.
86 */
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;
91 if ($dao->find()) {
92 return TRUE;
93 }
94 return FALSE;
95 }
96
97 /**
98 * Checks whether groupId has 1 or more parent groups.
99 *
100 * @param int $groupId
101 * The id of the group to check for parent groups.
102 *
103 * @return bool
104 * True if 1 or more parent groups are found, false otherwise.
105 */
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";
109 $dao->query($query);
110 if ($dao->fetch()) {
111 return TRUE;
112 }
113 return FALSE;
114 }
115
116 /**
117 * Returns array of group ids of child groups of the specified group.
118 *
119 * @param array $groupIds
120 * An array of valid group ids (passed by reference).
121 *
122 * @return array
123 * List of groupIds that represent the requested group and its children
124 */
125 public static function getChildGroupIds($groupIds) {
126 if (!is_array($groupIds)) {
127 $groupIds = array($groupIds);
128 }
129 $dao = new CRM_Contact_DAO_GroupNesting();
130 $query = "SELECT child_group_id FROM civicrm_group_nesting WHERE parent_group_id IN (" . implode(',', $groupIds) . ")";
131 $dao->query($query);
132 $childGroupIds = array();
133 while ($dao->fetch()) {
134 $childGroupIds[] = $dao->child_group_id;
135 }
136 return $childGroupIds;
137 }
138
139 /**
140 * Returns array of group ids of parent groups of the specified group.
141 *
142 * @param array $groupIds
143 * An array of valid group ids (passed by reference).
144 *
145 * @return array
146 * List of groupIds that represent the requested group and its parents
147 */
148 public static function getParentGroupIds($groupIds) {
149 if (!is_array($groupIds)) {
150 $groupIds = array($groupIds);
151 }
152 $dao = new CRM_Contact_DAO_GroupNesting();
153 $query = "SELECT parent_group_id FROM civicrm_group_nesting WHERE child_group_id IN (" . implode(',', $groupIds) . ")";
154 $dao->query($query);
155 $parentGroupIds = array();
156 while ($dao->fetch()) {
157 $parentGroupIds[] = $dao->parent_group_id;
158 }
159 return $parentGroupIds;
160 }
161
162 /**
163 * Returns array of group ids of descendent groups of the specified group.
164 *
165 * @param array $groupIds
166 * An array of valid group ids (passed by reference).
167 *
168 * @param bool $includeSelf
169 *
170 * @return array
171 * List of groupIds that represent the requested group and its descendents
172 */
173 public static function getDescendentGroupIds($groupIds, $includeSelf = TRUE) {
174 if (!is_array($groupIds)) {
175 $groupIds = array($groupIds);
176 }
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) . ")";
179 $dao->query($query);
180 $tmpGroupIds = array();
181 $childGroupIds = array();
182 if ($includeSelf) {
183 $childGroupIds = $groupIds;
184 }
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;
189 }
190 }
191 if (!empty($tmpGroupIds)) {
192 $newChildGroupIds = self::getDescendentGroupIds($tmpGroupIds);
193 $childGroupIds = array_merge($childGroupIds, $newChildGroupIds);
194 }
195 return $childGroupIds;
196 }
197
198 }