Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 TO |
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 | * $Id$ | |
34 | * | |
35 | */ | |
36 | class CRM_Contact_BAO_GroupNesting extends CRM_Contact_DAO_GroupNesting implements Iterator { | |
37 | ||
38 | static $_sortOrder = 'ASC'; | |
39 | ||
40 | private $_current; | |
41 | ||
42 | private $_parentStack = array(); | |
43 | ||
44 | private $_lastParentlessGroup; | |
45 | ||
46 | private $_styleLabels; | |
47 | ||
48 | private $_styleIndent; | |
49 | ||
50 | private $_alreadyStyled = FALSE; | |
51 | ||
52 | /** | |
100fef9d | 53 | * Class constructor |
6a488035 TO |
54 | */ |
55 | function __construct($styleLabels = FALSE, $styleIndent = " -- ") { | |
56 | parent::__construct(); | |
57 | $this->_styleLabels = $styleLabels; | |
58 | $this->_styleIndent = $styleIndent; | |
59 | } | |
60 | ||
86538308 EM |
61 | /** |
62 | * @param $sortOrder | |
63 | */ | |
6a488035 TO |
64 | function setSortOrder($sortOrder) { |
65 | switch ($sortOrder) { | |
66 | case 'ASC': | |
67 | case 'DESC': | |
68 | if ($sortOrder != self::$_sortOrder) { | |
69 | self::$_sortOrder = $sortOrder; | |
70 | $this->rewind(); | |
71 | } | |
72 | break; | |
73 | ||
74 | default: | |
75 | // spit out some error, someday | |
76 | } | |
77 | } | |
78 | ||
86538308 EM |
79 | /** |
80 | * @return string | |
81 | */ | |
6a488035 TO |
82 | function getSortOrder() { |
83 | return self::$_sortOrder; | |
84 | } | |
85 | ||
86538308 EM |
86 | /** |
87 | * @return int | |
88 | */ | |
6a488035 TO |
89 | function getCurrentNestingLevel() { |
90 | return count($this->_parentStack); | |
91 | } | |
92 | ||
93 | /** | |
94 | * Go back to the first element in the group nesting graph, | |
95 | * which is the first group (according to _sortOrder) that | |
96 | * has no parent groups | |
97 | */ | |
98 | function rewind() { | |
99 | $this->_parentStack = array(); | |
100 | // calling _getNextParentlessGroup w/ no arguments | |
101 | // makes it return the first parentless group | |
102 | $firstGroup = $this->_getNextParentlessGroup(); | |
103 | $this->_current = $firstGroup; | |
104 | $this->_lastParentlessGroup = $firstGroup; | |
105 | $this->_alreadyStyled = FALSE; | |
106 | } | |
107 | ||
108 | function current() { | |
109 | if ($this->_styleLabels && | |
110 | $this->valid() && | |
111 | !$this->_alreadyStyled | |
112 | ) { | |
113 | $styledGroup = clone($this->_current); | |
114 | $nestingLevel = $this->getCurrentNestingLevel(); | |
115 | $indent = ''; | |
116 | while ($nestingLevel--) { | |
117 | $indent .= $this->_styleIndent; | |
118 | } | |
119 | $styledGroup->title = $indent . $styledGroup->title; | |
120 | ||
121 | $this->_current = &$styledGroup; | |
122 | $this->_alreadyStyled = TRUE; | |
123 | } | |
124 | return $this->_current; | |
125 | } | |
126 | ||
86538308 EM |
127 | /** |
128 | * @return string | |
129 | */ | |
6a488035 TO |
130 | function key() { |
131 | $group = &$this->_current; | |
132 | $ids = array(); | |
133 | foreach ($this->_parentStack as $parentGroup) { | |
134 | $ids[] = $parentGroup->id; | |
135 | } | |
136 | $key = implode('-', $ids); | |
137 | if (strlen($key) > 0) { | |
138 | $key .= '-'; | |
139 | } | |
140 | $key .= $group->id; | |
141 | return $key; | |
142 | } | |
143 | ||
86538308 EM |
144 | /** |
145 | * @return CRM_Contact_BAO_Group|null | |
146 | */ | |
6a488035 TO |
147 | function next() { |
148 | $currentGroup = &$this->_current; | |
149 | $childGroup = $this->_getNextChildGroup($currentGroup); | |
150 | if ($childGroup) { | |
151 | $nextGroup = &$childGroup; | |
152 | $this->_parentStack[] = &$this->_current; | |
153 | } | |
154 | else { | |
155 | $nextGroup = $this->_getNextSiblingGroup($currentGroup); | |
156 | if (!$nextGroup) { | |
157 | // no sibling, find an ancestor w/ a sibling | |
158 | for (;; ) { | |
159 | // since we pop this array everytime, we should be | |
160 | // reasonably safe from infinite loops, I think :) | |
161 | $ancestor = array_pop($this->_parentStack); | |
162 | $this->_current = &$ancestor; | |
163 | if ($ancestor == NULL) { | |
164 | break; | |
165 | } | |
166 | $nextGroup = $this->_getNextSiblingGroup($ancestor); | |
167 | if ($nextGroup) { | |
168 | break; | |
169 | } | |
170 | } | |
171 | } | |
172 | } | |
173 | $this->_current = &$nextGroup; | |
174 | $this->_alreadyStyled = FALSE; | |
175 | return $nextGroup; | |
176 | } | |
177 | ||
86538308 EM |
178 | /** |
179 | * @return bool | |
180 | */ | |
6a488035 TO |
181 | function valid() { |
182 | if ($this->_current) { | |
183 | return TRUE; | |
184 | } | |
185 | else { | |
186 | return FALSE; | |
187 | } | |
188 | } | |
189 | ||
86538308 EM |
190 | /** |
191 | * @param null $group | |
192 | * | |
193 | * @return CRM_Contact_BAO_Group|null | |
194 | */ | |
6a488035 TO |
195 | function _getNextParentlessGroup(&$group = NULL) { |
196 | $lastParentlessGroup = $this->_lastParentlessGroup; | |
197 | $nextGroup = new CRM_Contact_BAO_Group(); | |
198 | $nextGroup->order_by = 'title ' . self::$_sortOrder; | |
199 | $nextGroup->find(); | |
200 | if ($group == NULL) { | |
201 | $sawLast = TRUE; | |
202 | } | |
203 | else { | |
204 | $sawLast = FALSE; | |
205 | } | |
206 | while ($nextGroup->fetch()) { | |
207 | if (!self::hasParentGroups($nextGroup->id) && $sawLast) { | |
208 | return $nextGroup; | |
209 | } | |
210 | elseif ($lastParentlessGroup->id == $nextGroup->id) { | |
211 | $sawLast = TRUE; | |
212 | } | |
213 | } | |
214 | return NULL; | |
215 | } | |
216 | ||
86538308 EM |
217 | /** |
218 | * @param $parentGroup | |
219 | * @param null $group | |
220 | * | |
221 | * @return CRM_Contact_BAO_Group|null | |
222 | */ | |
6a488035 TO |
223 | function _getNextChildGroup(&$parentGroup, &$group = NULL) { |
224 | $children = self::getChildGroupIds($parentGroup->id); | |
225 | if (count($children) > 0) { | |
226 | // we have child groups, so get the first one based on _sortOrder | |
227 | $childGroup = new CRM_Contact_BAO_Group(); | |
228 | $cgQuery = "SELECT * FROM civicrm_group WHERE id IN (" . implode(',', $children) . ") ORDER BY title " . self::$_sortOrder; | |
229 | $childGroup->query($cgQuery); | |
230 | $currentGroup = &$this->_current; | |
231 | if ($group == NULL) { | |
232 | $sawLast = TRUE; | |
233 | } | |
234 | else { | |
235 | $sawLast = FALSE; | |
236 | } | |
237 | while ($childGroup->fetch()) { | |
238 | if ($sawLast) { | |
239 | return $childGroup; | |
240 | } | |
241 | elseif ($currentGroup->id === $childGroup->id) { | |
242 | $sawLast = TRUE; | |
243 | } | |
244 | } | |
245 | } | |
246 | return NULL; | |
247 | } | |
248 | ||
86538308 EM |
249 | /** |
250 | * @param $group | |
251 | * | |
252 | * @return CRM_Contact_BAO_Group|null | |
253 | */ | |
6a488035 TO |
254 | function _getNextSiblingGroup(&$group) { |
255 | $parentGroup = end($this->_parentStack); | |
256 | if ($parentGroup) { | |
257 | $nextGroup = $this->_getNextChildGroup($parentGroup, $group); | |
258 | return $nextGroup; | |
259 | } | |
260 | else { | |
261 | /* if we get here, it could be because we're out of siblings | |
262 | * (in which case we return null) or because we're at the | |
263 | * top level groups which do not have parents but may still | |
264 | * have siblings, so check for that first. | |
265 | */ | |
266 | ||
267 | $nextGroup = $this->_getNextParentlessGroup($group); | |
268 | if ($nextGroup) { | |
269 | $this->_lastParentlessGroup = $nextGroup; | |
270 | return $nextGroup; | |
271 | } | |
272 | return NULL; | |
273 | } | |
274 | } | |
275 | ||
276 | /** | |
277 | * Adds a new child group identified by $childGroupId to the group | |
278 | * identified by $groupId | |
279 | * | |
c490a46a CW |
280 | * @param int $parentID id of the group to add the child to |
281 | * @param int $childID id of the new child group | |
fd31fa4c | 282 | * |
6a488035 | 283 | * |
c490a46a | 284 | * @return void |
6a488035 TO |
285 | * @access public |
286 | */ | |
287 | static function add($parentID, $childID) { | |
288 | // TODO: Add checks here to make sure invalid nests can't be created | |
289 | $dao = new CRM_Contact_DAO_GroupNesting(); | |
290 | $query = "REPLACE INTO civicrm_group_nesting (child_group_id, parent_group_id) VALUES ($childID,$parentID);"; | |
291 | $dao->query($query); | |
292 | } | |
293 | ||
294 | /** | |
295 | * Removes a child group identified by $childGroupId from the group | |
296 | * identified by $groupId; does not delete child group, just the | |
297 | * association between the two | |
298 | * | |
299 | * @param $parentID The id of the group to remove the child from | |
300 | * @param $childID The id of the child group being removed | |
301 | * | |
302 | * @return void | |
303 | * | |
304 | * @access public | |
305 | */ | |
306 | static function remove($parentID, $childID) { | |
307 | $dao = new CRM_Contact_DAO_GroupNesting(); | |
308 | $query = "DELETE FROM civicrm_group_nesting WHERE child_group_id = $childID AND parent_group_id = $parentID"; | |
309 | $dao->query($query); | |
310 | } | |
311 | ||
312 | /** | |
313 | * Removes associations where a child group is identified by $childGroupId from the group | |
314 | * identified by $groupId; does not delete child group, just the | |
315 | * association between the two | |
316 | * | |
c490a46a | 317 | * @param int $childID The id of the child group being removed |
6a488035 TO |
318 | * |
319 | * @return void | |
320 | * | |
321 | * @access public | |
322 | */ | |
323 | static function removeAllParentForChild($childID) { | |
324 | $dao = new CRM_Contact_DAO_GroupNesting(); | |
325 | $query = "DELETE FROM civicrm_group_nesting WHERE child_group_id = $childID"; | |
326 | $dao->query($query); | |
327 | } | |
328 | ||
329 | /** | |
330 | * Returns true if the association between parent and child is present, | |
331 | * false otherwise. | |
332 | * | |
333 | * @param $parentID The parent id of the association | |
334 | * @param $childID The child id of the association | |
335 | * | |
336 | * @return boolean True if association is found, false otherwise. | |
337 | * | |
338 | * @access public | |
339 | */ | |
340 | static function isParentChild($parentID, $childID) { | |
341 | $dao = new CRM_Contact_DAO_GroupNesting(); | |
342 | $query = "SELECT id FROM civicrm_group_nesting WHERE child_group_id = $childID AND parent_group_id = $parentID"; | |
343 | $dao->query($query); | |
344 | if ($dao->fetch()) { | |
345 | return TRUE; | |
346 | } | |
347 | return FALSE; | |
348 | } | |
349 | ||
350 | /** | |
351 | * Returns true if if the given groupId has 1 or more child groups, | |
352 | * false otherwise. | |
353 | * | |
354 | * @param $groupId The id of the group to check for child groups | |
355 | * | |
356 | * @return boolean True if 1 or more child groups are found, false otherwise. | |
357 | * | |
358 | * @access public | |
359 | */ | |
360 | static function hasChildGroups($groupId) { | |
361 | $dao = new CRM_Contact_DAO_GroupNesting(); | |
362 | $query = "SELECT child_group_id FROM civicrm_group_nesting WHERE parent_group_id = $groupId LIMIT 1"; | |
363 | //print $query . "\n<br><br>"; | |
364 | $dao->query($query); | |
365 | if ($dao->fetch()) { | |
366 | return TRUE; | |
367 | } | |
368 | return FALSE; | |
369 | } | |
370 | ||
371 | /** | |
372 | * Returns true if the given groupId has 1 or more parent groups, | |
373 | * false otherwise. | |
374 | * | |
375 | * @param $groupId The id of the group to check for parent groups | |
376 | * | |
377 | * @return boolean True if 1 or more parent groups are found, false otherwise. | |
378 | * | |
379 | * @access public | |
380 | */ | |
381 | static function hasParentGroups($groupId) { | |
382 | $dao = new CRM_Contact_DAO_GroupNesting(); | |
383 | $query = "SELECT parent_group_id FROM civicrm_group_nesting WHERE child_group_id = $groupId LIMIT 1"; | |
384 | $dao->query($query); | |
385 | if ($dao->fetch()) { | |
386 | return TRUE; | |
387 | } | |
388 | return FALSE; | |
389 | } | |
390 | ||
391 | /** | |
392 | * Returns true if checkGroupId is a parent of one of the groups in | |
393 | * groupIds, false otherwise. | |
394 | * | |
395 | * @param $groupIds Array of group ids (or one group id) to serve as the starting point | |
396 | * @param $checkGroupId The group id to check if it is a parent of the $groupIds group(s) | |
397 | * | |
398 | * @return boolean True if $checkGroupId points to a group that is a parent of one of the $groupIds groups, false otherwise. | |
399 | * | |
400 | * @access public | |
401 | */ | |
402 | static function isParentGroup($groupIds, $checkGroupId) { | |
403 | if (!is_array($groupIds)) { | |
404 | $groupIds = array($groupIds); | |
405 | } | |
406 | $dao = new CRM_Contact_DAO_GroupNesting(); | |
407 | $query = "SELECT parent_group_id FROM civicrm_group_nesting WHERE child_group_id IN (" . implode(',', $groupIds) . ")"; | |
408 | $dao->query($query); | |
409 | while ($dao->fetch()) { | |
410 | $parentGroupId = $dao->parent_group_id; | |
411 | if ($parentGroupId == $checkGroupId) { | |
412 | /* print "One of these: <pre>"; | |
413 | print_r($groupIds); | |
414 | print "</pre> has groupId $checkGroupId as an ancestor.<br/>"; */ | |
415 | ||
416 | return TRUE; | |
417 | } | |
418 | } | |
419 | return FALSE; | |
420 | } | |
421 | ||
422 | /** | |
423 | * Returns true if checkGroupId is a child of one of the groups in | |
424 | * groupIds, false otherwise. | |
425 | * | |
426 | * @param $groupIds Array of group ids (or one group id) to serve as the starting point | |
427 | * @param $checkGroupId The group id to check if it is a child of the $groupIds group(s) | |
428 | * | |
429 | * @return boolean True if $checkGroupId points to a group that is a child of one of the $groupIds groups, false otherwise. | |
430 | * | |
431 | * @access public | |
432 | */ | |
433 | static function isChildGroup($groupIds, $checkGroupId) { | |
434 | ||
435 | if (!is_array($groupIds)) { | |
436 | $groupIds = array($groupIds); | |
437 | } | |
438 | $dao = new CRM_Contact_DAO_GroupNesting(); | |
439 | $query = "SELECT child_group_id FROM civicrm_group_nesting WHERE parent_group_id IN (" . implode(',', $groupIds) . ")"; | |
440 | //print $query; | |
441 | $dao->query($query); | |
442 | while ($dao->fetch()) { | |
443 | $childGroupId = $dao->child_group_id; | |
444 | if ($childGroupId == $checkGroupId) { | |
445 | /* print "One of these: <pre>"; | |
446 | print_r($groupIds); | |
447 | print "</pre> has groupId $checkGroupId as a descendent.<br/><br/>"; */ | |
448 | ||
449 | return TRUE; | |
450 | } | |
451 | } | |
452 | return FALSE; | |
453 | } | |
454 | ||
455 | /** | |
456 | * Returns true if checkGroupId is an ancestor of one of the groups in | |
457 | * groupIds, false otherwise. | |
458 | * | |
459 | * @param $groupIds Array of group ids (or one group id) to serve as the starting point | |
460 | * @param $checkGroupId The group id to check if it is an ancestor of the $groupIds group(s) | |
461 | * | |
462 | * @return boolean True if $checkGroupId points to a group that is an ancestor of one of the $groupIds groups, false otherwise. | |
463 | * | |
464 | * @access public | |
465 | */ | |
466 | static function isAncestorGroup($groupIds, $checkGroupId) { | |
467 | if (!is_array($groupIds)) { | |
468 | $groupIds = array($groupIds); | |
469 | } | |
470 | $dao = new CRM_Contact_DAO_GroupNesting(); | |
471 | $query = "SELECT parent_group_id FROM civicrm_group_nesting WHERE child_group_id IN (" . implode(',', $groupIds) . ")"; | |
472 | $dao->query($query); | |
473 | $nextGroupIds = array(); | |
474 | $gotAtLeastOneResult = FALSE; | |
475 | while ($dao->fetch()) { | |
476 | $gotAtLeastOneResult = TRUE; | |
477 | $parentGroupId = $dao->parent_group_id; | |
478 | if ($parentGroupId == $checkGroupId) { | |
479 | /* print "One of these: <pre>"; | |
480 | print_r($groupIds); | |
481 | print "</pre> has groupId $checkGroupId as an ancestor.<br/>"; */ | |
482 | ||
483 | return TRUE; | |
484 | } | |
485 | $nextGroupIds[] = $parentGroupId; | |
486 | } | |
487 | if ($gotAtLeastOneResult) { | |
488 | return self::isAncestorGroup($nextGroupIds, $checkGroupId); | |
489 | } | |
490 | else { | |
491 | return FALSE; | |
492 | } | |
493 | } | |
494 | ||
495 | /** | |
496 | * Returns true if checkGroupId is a descendent of one of the groups in | |
497 | * groupIds, false otherwise. | |
498 | * | |
499 | * @param $groupIds Array of group ids (or one group id) to serve as the starting point | |
500 | * @param $checkGroupId The group id to check if it is a descendent of the $groupIds group(s) | |
501 | * | |
502 | * @return boolean True if $checkGroupId points to a group that is a descendent of one of the $groupIds groups, false otherwise. | |
503 | * | |
504 | * @access public | |
505 | */ | |
506 | static function isDescendentGroup($groupIds, $checkGroupId) { | |
507 | if (!is_array($groupIds)) { | |
508 | $groupIds = array($groupIds); | |
509 | } | |
510 | $dao = new CRM_Contact_DAO_GroupNesting(); | |
511 | $query = "SELECT child_group_id FROM civicrm_group_nesting WHERE parent_group_id IN (" . implode(',', $groupIds) . ")"; | |
512 | $dao->query($query); | |
513 | $nextGroupIds = array(); | |
514 | $gotAtLeastOneResult = FALSE; | |
515 | while ($dao->fetch()) { | |
516 | $gotAtLeastOneResult = TRUE; | |
517 | $childGroupId = $dao->child_group_id; | |
518 | if ($childGroupId == $checkGroupId) { | |
519 | /* print "One of these: <pre>"; | |
520 | print_r($groupIds); | |
521 | print "</pre> has groupId $checkGroupId as a descendent.<br/><br/>"; */ | |
522 | ||
523 | return TRUE; | |
524 | } | |
525 | $nextGroupIds[] = $childGroupId; | |
526 | } | |
527 | if ($gotAtLeastOneResult) { | |
528 | return self::isDescendentGroup($nextGroupIds, $checkGroupId); | |
529 | } | |
530 | else { | |
531 | return FALSE; | |
532 | } | |
533 | } | |
534 | ||
535 | /** | |
536 | * Returns array of group ids of ancestor groups of the specified group. | |
537 | * | |
538 | * @param $groupIds An array of valid group ids (passed by reference) | |
539 | * | |
2a6da8d7 | 540 | * @param bool $includeSelf |
6a488035 | 541 | * |
2a6da8d7 | 542 | * @return array $groupIdArray List of groupIds that represent the requested group and its ancestors@access public |
6a488035 TO |
543 | */ |
544 | static function getAncestorGroupIds($groupIds, $includeSelf = TRUE) { | |
545 | if (!is_array($groupIds)) { | |
546 | $groupIds = array($groupIds); | |
547 | } | |
548 | $dao = new CRM_Contact_DAO_GroupNesting(); | |
549 | $query = "SELECT parent_group_id, child_group_id | |
550 | FROM civicrm_group_nesting | |
551 | WHERE child_group_id IN (" . implode(',', $groupIds) . ")"; | |
552 | $dao->query($query); | |
553 | $tmpGroupIds = array(); | |
554 | $parentGroupIds = array(); | |
555 | if ($includeSelf) { | |
556 | $parentGroupIds = $groupIds; | |
557 | } | |
558 | while ($dao->fetch()) { | |
559 | // make sure we're not following any cyclical references | |
560 | if (!array_key_exists($dao->child_group_id, $parentGroupIds) && $dao->parent_group_id != $groupIds[0]) { | |
561 | $tmpGroupIds[] = $dao->parent_group_id; | |
562 | } | |
563 | } | |
564 | if (!empty($tmpGroupIds)) { | |
565 | $newParentGroupIds = self::getAncestorGroupIds($tmpGroupIds); | |
566 | $parentGroupIds = array_merge($parentGroupIds, $newParentGroupIds); | |
567 | } | |
568 | return $parentGroupIds; | |
569 | } | |
570 | ||
571 | /** | |
572 | * Returns array of ancestor groups of the specified group. | |
573 | * | |
574 | * @param $groupIds An array of valid group ids (passed by reference) | |
575 | * | |
2a6da8d7 EM |
576 | * @param bool $includeSelf |
577 | * @return \An $groupArray List of ancestor groups@access public | |
6a488035 TO |
578 | */ |
579 | static function getAncestorGroups($groupIds, $includeSelf = TRUE) { | |
580 | $groupIds = self::getAncestorGroupIds($groupIds, $includeSelf); | |
581 | $params['id'] = $groupIds; | |
582 | return CRM_Contact_BAO_Group::getGroups($params); | |
583 | } | |
584 | ||
585 | /** | |
586 | * Returns array of group ids of child groups of the specified group. | |
587 | * | |
588 | * @param $groupIds An array of valid group ids (passed by reference) | |
589 | * | |
2a6da8d7 | 590 | * @return array $groupIdArray List of groupIds that represent the requested group and its children@access public |
6a488035 TO |
591 | */ |
592 | static function getChildGroupIds($groupIds) { | |
593 | if (!is_array($groupIds)) { | |
594 | $groupIds = array($groupIds); | |
595 | } | |
596 | $dao = new CRM_Contact_DAO_GroupNesting(); | |
597 | $query = "SELECT child_group_id FROM civicrm_group_nesting WHERE parent_group_id IN (" . implode(',', $groupIds) . ")"; | |
598 | $dao->query($query); | |
599 | $childGroupIds = array(); | |
600 | while ($dao->fetch()) { | |
601 | $childGroupIds[] = $dao->child_group_id; | |
602 | } | |
603 | return $childGroupIds; | |
604 | } | |
605 | ||
606 | /** | |
607 | * Returns array of group ids of parent groups of the specified group. | |
608 | * | |
609 | * @param $groupIds An array of valid group ids (passed by reference) | |
610 | * | |
77b97be7 | 611 | * @return array $groupIdArray List of groupIds that represent the requested group and its parents@access public |
6a488035 TO |
612 | */ |
613 | static function getParentGroupIds($groupIds) { | |
614 | if (!is_array($groupIds)) { | |
615 | $groupIds = array($groupIds); | |
616 | } | |
617 | $dao = new CRM_Contact_DAO_GroupNesting(); | |
618 | $query = "SELECT parent_group_id FROM civicrm_group_nesting WHERE child_group_id IN (" . implode(',', $groupIds) . ")"; | |
619 | $dao->query($query); | |
620 | $parentGroupIds = array(); | |
621 | while ($dao->fetch()) { | |
622 | $parentGroupIds[] = $dao->parent_group_id; | |
623 | } | |
624 | return $parentGroupIds; | |
625 | } | |
626 | ||
627 | /** | |
628 | * Returns array of group ids of descendent groups of the specified group. | |
629 | * | |
630 | * @param $groupIds An array of valid group ids (passed by reference) | |
631 | * | |
2a6da8d7 EM |
632 | * @param bool $includeSelf |
633 | * @return array $groupIdArray List of groupIds that represent the requested group and its descendents@access public | |
6a488035 TO |
634 | */ |
635 | static function getDescendentGroupIds($groupIds, $includeSelf = TRUE) { | |
636 | if (!is_array($groupIds)) { | |
637 | $groupIds = array($groupIds); | |
638 | } | |
639 | $dao = new CRM_Contact_DAO_GroupNesting(); | |
640 | $query = "SELECT child_group_id, parent_group_id FROM civicrm_group_nesting WHERE parent_group_id IN (" . implode(',', $groupIds) . ")"; | |
641 | $dao->query($query); | |
642 | $tmpGroupIds = array(); | |
643 | $childGroupIds = array(); | |
644 | if ($includeSelf) { | |
645 | $childGroupIds = $groupIds; | |
646 | } | |
647 | while ($dao->fetch()) { | |
648 | // make sure we're not following any cyclical references | |
649 | if (!array_key_exists($dao->parent_group_id, $childGroupIds) && $dao->child_group_id != $groupIds[0]) { | |
650 | $tmpGroupIds[] = $dao->child_group_id; | |
651 | } | |
652 | } | |
653 | if (!empty($tmpGroupIds)) { | |
654 | $newChildGroupIds = self::getDescendentGroupIds($tmpGroupIds); | |
655 | $childGroupIds = array_merge($childGroupIds, $newChildGroupIds); | |
656 | } | |
657 | return $childGroupIds; | |
658 | } | |
659 | ||
660 | /** | |
661 | * Returns array of descendent groups of the specified group. | |
662 | * | |
663 | * @param $groupIds An array of valid group ids (passed by reference) | |
664 | * | |
2a6da8d7 EM |
665 | * @param bool $includeSelf |
666 | * @return \An $groupArray List of descendent groups@access public | |
6a488035 TO |
667 | */ |
668 | static function getDescendentGroups($groupIds, $includeSelf = TRUE) { | |
669 | $groupIds = self::getDescendentGroupIds($groupIds, $includeSelf); | |
670 | $params['id'] = $groupIds; | |
671 | return CRM_Contact_BAO_Group::getGroups($params); | |
672 | } | |
673 | ||
674 | /** | |
675 | * Returns array of group ids of valid potential child groups of the specified group. | |
676 | * | |
677 | * @param $groupId The group id to get valid potential children for | |
678 | * | |
2a6da8d7 | 679 | * @return array $groupIdArray List of groupIds that represent the valid potential children of the group@access public |
6a488035 TO |
680 | */ |
681 | static function getPotentialChildGroupIds($groupId) { | |
682 | $groups = CRM_Contact_BAO_Group::getGroups(); | |
683 | $potentialChildGroupIds = array(); | |
684 | foreach ($groups as $group) { | |
685 | $potentialChildGroupId = $group->id; | |
686 | // print "Checking if $potentialChildGroupId is a descendent/ancestor of $groupId<br/><br/>"; | |
687 | if (!self::isDescendentGroup($groupId, $potentialChildGroupId) && | |
688 | !self::isAncestorGroup($groupId, $potentialChildGroupId) && | |
689 | $potentialChildGroupId != $groupId | |
690 | ) { | |
691 | $potentialChildGroupIds[] = $potentialChildGroupId; | |
692 | } | |
693 | } | |
694 | return $potentialChildGroupIds; | |
695 | } | |
696 | ||
86538308 | 697 | /** |
100fef9d CW |
698 | * @param int $contactId |
699 | * @param int $parentGroupId | |
86538308 EM |
700 | * |
701 | * @return array | |
702 | */ | |
6a488035 TO |
703 | static function getContainingGroups($contactId, $parentGroupId) { |
704 | $groups = CRM_Contact_BAO_Group::getGroups(); | |
705 | $containingGroups = array(); | |
706 | foreach ($groups as $group) { | |
707 | if (self::isDescendentGroup($parentGroupId, $group->id)) { | |
708 | $members = CRM_Contact_BAO_Group::getMember($group->id); | |
709 | if ($members[$contactId]) { | |
710 | $containingGroups[] = $group->title; | |
711 | } | |
712 | } | |
713 | } | |
714 | ||
715 | return $containingGroups; | |
716 | } | |
717 | } | |
718 |