3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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
36 class CRM_Contact_BAO_GroupNesting
extends CRM_Contact_DAO_GroupNesting
implements Iterator
{
38 static $_sortOrder = 'ASC';
42 private $_parentStack = array();
44 private $_lastParentlessGroup;
46 private $_styleLabels;
48 private $_styleIndent;
50 private $_alreadyStyled = FALSE;
55 function __construct($styleLabels = FALSE, $styleIndent = " -- ") {
56 parent
::__construct();
57 $this->_styleLabels
= $styleLabels;
58 $this->_styleIndent
= $styleIndent;
64 function setSortOrder($sortOrder) {
68 if ($sortOrder != self
::$_sortOrder) {
69 self
::$_sortOrder = $sortOrder;
75 // spit out some error, someday
82 function getSortOrder() {
83 return self
::$_sortOrder;
89 function getCurrentNestingLevel() {
90 return count($this->_parentStack
);
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
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;
109 if ($this->_styleLabels
&&
111 !$this->_alreadyStyled
113 $styledGroup = clone($this->_current
);
114 $nestingLevel = $this->getCurrentNestingLevel();
116 while ($nestingLevel--) {
117 $indent .= $this->_styleIndent
;
119 $styledGroup->title
= $indent . $styledGroup->title
;
121 $this->_current
= &$styledGroup;
122 $this->_alreadyStyled
= TRUE;
124 return $this->_current
;
131 $group = &$this->_current
;
133 foreach ($this->_parentStack
as $parentGroup) {
134 $ids[] = $parentGroup->id
;
136 $key = implode('-', $ids);
137 if (strlen($key) > 0) {
145 * @return CRM_Contact_BAO_Group|null
148 $currentGroup = &$this->_current
;
149 $childGroup = $this->_getNextChildGroup($currentGroup);
151 $nextGroup = &$childGroup;
152 $this->_parentStack
[] = &$this->_current
;
155 $nextGroup = $this->_getNextSiblingGroup($currentGroup);
157 // no sibling, find an ancestor w/ a sibling
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) {
166 $nextGroup = $this->_getNextSiblingGroup($ancestor);
173 $this->_current
= &$nextGroup;
174 $this->_alreadyStyled
= FALSE;
182 if ($this->_current
) {
193 * @return CRM_Contact_BAO_Group|null
195 function _getNextParentlessGroup(&$group = NULL) {
196 $lastParentlessGroup = $this->_lastParentlessGroup
;
197 $nextGroup = new CRM_Contact_BAO_Group();
198 $nextGroup->order_by
= 'title ' . self
::$_sortOrder;
200 if ($group == NULL) {
206 while ($nextGroup->fetch()) {
207 if (!self
::hasParentGroups($nextGroup->id
) && $sawLast) {
210 elseif ($lastParentlessGroup->id
== $nextGroup->id
) {
218 * @param $parentGroup
221 * @return CRM_Contact_BAO_Group|null
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) {
237 while ($childGroup->fetch()) {
241 elseif ($currentGroup->id
=== $childGroup->id
) {
252 * @return CRM_Contact_BAO_Group|null
254 function _getNextSiblingGroup(&$group) {
255 $parentGroup = end($this->_parentStack
);
257 $nextGroup = $this->_getNextChildGroup($parentGroup, $group);
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.
267 $nextGroup = $this->_getNextParentlessGroup($group);
269 $this->_lastParentlessGroup
= $nextGroup;
277 * Adds a new child group identified by $childGroupId to the group
278 * identified by $groupId
283 * @internal param \The $groupId id of the group to add the child to
284 * @internal param \The $childGroupId id of the new child group
290 static function add($parentID, $childID) {
291 // TODO: Add checks here to make sure invalid nests can't be created
292 $dao = new CRM_Contact_DAO_GroupNesting();
293 $query = "REPLACE INTO civicrm_group_nesting (child_group_id, parent_group_id) VALUES ($childID,$parentID);";
298 * Removes a child group identified by $childGroupId from the group
299 * identified by $groupId; does not delete child group, just the
300 * association between the two
302 * @param $parentID The id of the group to remove the child from
303 * @param $childID The id of the child group being removed
309 static function remove($parentID, $childID) {
310 $dao = new CRM_Contact_DAO_GroupNesting();
311 $query = "DELETE FROM civicrm_group_nesting WHERE child_group_id = $childID AND parent_group_id = $parentID";
316 * Removes associations where a child group is identified by $childGroupId from the group
317 * identified by $groupId; does not delete child group, just the
318 * association between the two
320 * @param $childID The id of the child group being removed
322 * @internal param \The $parentID id of the group to remove the child from
327 static function removeAllParentForChild($childID) {
328 $dao = new CRM_Contact_DAO_GroupNesting();
329 $query = "DELETE FROM civicrm_group_nesting WHERE child_group_id = $childID";
334 * Returns true if the association between parent and child is present,
337 * @param $parentID The parent id of the association
338 * @param $childID The child id of the association
340 * @return boolean True if association is found, false otherwise.
344 static function isParentChild($parentID, $childID) {
345 $dao = new CRM_Contact_DAO_GroupNesting();
346 $query = "SELECT id FROM civicrm_group_nesting WHERE child_group_id = $childID AND parent_group_id = $parentID";
355 * Returns true if if the given groupId has 1 or more child groups,
358 * @param $groupId The id of the group to check for child groups
360 * @return boolean True if 1 or more child groups are found, false otherwise.
364 static function hasChildGroups($groupId) {
365 $dao = new CRM_Contact_DAO_GroupNesting();
366 $query = "SELECT child_group_id FROM civicrm_group_nesting WHERE parent_group_id = $groupId LIMIT 1";
367 //print $query . "\n<br><br>";
376 * Returns true if the given groupId has 1 or more parent groups,
379 * @param $groupId The id of the group to check for parent groups
381 * @return boolean True if 1 or more parent groups are found, false otherwise.
385 static function hasParentGroups($groupId) {
386 $dao = new CRM_Contact_DAO_GroupNesting();
387 $query = "SELECT parent_group_id FROM civicrm_group_nesting WHERE child_group_id = $groupId LIMIT 1";
396 * Returns true if checkGroupId is a parent of one of the groups in
397 * groupIds, false otherwise.
399 * @param $groupIds Array of group ids (or one group id) to serve as the starting point
400 * @param $checkGroupId The group id to check if it is a parent of the $groupIds group(s)
402 * @return boolean True if $checkGroupId points to a group that is a parent of one of the $groupIds groups, false otherwise.
406 static function isParentGroup($groupIds, $checkGroupId) {
407 if (!is_array($groupIds)) {
408 $groupIds = array($groupIds);
410 $dao = new CRM_Contact_DAO_GroupNesting();
411 $query = "SELECT parent_group_id FROM civicrm_group_nesting WHERE child_group_id IN (" . implode(',', $groupIds) . ")";
413 while ($dao->fetch()) {
414 $parentGroupId = $dao->parent_group_id
;
415 if ($parentGroupId == $checkGroupId) {
416 /* print "One of these: <pre>";
418 print "</pre> has groupId $checkGroupId as an ancestor.<br/>"; */
427 * Returns true if checkGroupId is a child of one of the groups in
428 * groupIds, false otherwise.
430 * @param $groupIds Array of group ids (or one group id) to serve as the starting point
431 * @param $checkGroupId The group id to check if it is a child of the $groupIds group(s)
433 * @return boolean True if $checkGroupId points to a group that is a child of one of the $groupIds groups, false otherwise.
437 static function isChildGroup($groupIds, $checkGroupId) {
439 if (!is_array($groupIds)) {
440 $groupIds = array($groupIds);
442 $dao = new CRM_Contact_DAO_GroupNesting();
443 $query = "SELECT child_group_id FROM civicrm_group_nesting WHERE parent_group_id IN (" . implode(',', $groupIds) . ")";
446 while ($dao->fetch()) {
447 $childGroupId = $dao->child_group_id
;
448 if ($childGroupId == $checkGroupId) {
449 /* print "One of these: <pre>";
451 print "</pre> has groupId $checkGroupId as a descendent.<br/><br/>"; */
460 * Returns true if checkGroupId is an ancestor of one of the groups in
461 * groupIds, false otherwise.
463 * @param $groupIds Array of group ids (or one group id) to serve as the starting point
464 * @param $checkGroupId The group id to check if it is an ancestor of the $groupIds group(s)
466 * @return boolean True if $checkGroupId points to a group that is an ancestor of one of the $groupIds groups, false otherwise.
470 static function isAncestorGroup($groupIds, $checkGroupId) {
471 if (!is_array($groupIds)) {
472 $groupIds = array($groupIds);
474 $dao = new CRM_Contact_DAO_GroupNesting();
475 $query = "SELECT parent_group_id FROM civicrm_group_nesting WHERE child_group_id IN (" . implode(',', $groupIds) . ")";
477 $nextGroupIds = array();
478 $gotAtLeastOneResult = FALSE;
479 while ($dao->fetch()) {
480 $gotAtLeastOneResult = TRUE;
481 $parentGroupId = $dao->parent_group_id
;
482 if ($parentGroupId == $checkGroupId) {
483 /* print "One of these: <pre>";
485 print "</pre> has groupId $checkGroupId as an ancestor.<br/>"; */
489 $nextGroupIds[] = $parentGroupId;
491 if ($gotAtLeastOneResult) {
492 return self
::isAncestorGroup($nextGroupIds, $checkGroupId);
500 * Returns true if checkGroupId is a descendent of one of the groups in
501 * groupIds, false otherwise.
503 * @param $groupIds Array of group ids (or one group id) to serve as the starting point
504 * @param $checkGroupId The group id to check if it is a descendent of the $groupIds group(s)
506 * @return boolean True if $checkGroupId points to a group that is a descendent of one of the $groupIds groups, false otherwise.
510 static function isDescendentGroup($groupIds, $checkGroupId) {
511 if (!is_array($groupIds)) {
512 $groupIds = array($groupIds);
514 $dao = new CRM_Contact_DAO_GroupNesting();
515 $query = "SELECT child_group_id FROM civicrm_group_nesting WHERE parent_group_id IN (" . implode(',', $groupIds) . ")";
517 $nextGroupIds = array();
518 $gotAtLeastOneResult = FALSE;
519 while ($dao->fetch()) {
520 $gotAtLeastOneResult = TRUE;
521 $childGroupId = $dao->child_group_id
;
522 if ($childGroupId == $checkGroupId) {
523 /* print "One of these: <pre>";
525 print "</pre> has groupId $checkGroupId as a descendent.<br/><br/>"; */
529 $nextGroupIds[] = $childGroupId;
531 if ($gotAtLeastOneResult) {
532 return self
::isDescendentGroup($nextGroupIds, $checkGroupId);
540 * Returns array of group ids of ancestor groups of the specified group.
542 * @param $groupIds An array of valid group ids (passed by reference)
544 * @param bool $includeSelf
546 * @return array $groupIdArray List of groupIds that represent the requested group and its ancestors@access public
548 static function getAncestorGroupIds($groupIds, $includeSelf = TRUE) {
549 if (!is_array($groupIds)) {
550 $groupIds = array($groupIds);
552 $dao = new CRM_Contact_DAO_GroupNesting();
553 $query = "SELECT parent_group_id, child_group_id
554 FROM civicrm_group_nesting
555 WHERE child_group_id IN (" . implode(',', $groupIds) . ")";
557 $tmpGroupIds = array();
558 $parentGroupIds = array();
560 $parentGroupIds = $groupIds;
562 while ($dao->fetch()) {
563 // make sure we're not following any cyclical references
564 if (!array_key_exists($dao->child_group_id
, $parentGroupIds) && $dao->parent_group_id
!= $groupIds[0]) {
565 $tmpGroupIds[] = $dao->parent_group_id
;
568 if (!empty($tmpGroupIds)) {
569 $newParentGroupIds = self
::getAncestorGroupIds($tmpGroupIds);
570 $parentGroupIds = array_merge($parentGroupIds, $newParentGroupIds);
572 return $parentGroupIds;
576 * Returns array of ancestor groups of the specified group.
578 * @param $groupIds An array of valid group ids (passed by reference)
580 * @param bool $includeSelf
581 * @return \An $groupArray List of ancestor groups@access public
583 static function getAncestorGroups($groupIds, $includeSelf = TRUE) {
584 $groupIds = self
::getAncestorGroupIds($groupIds, $includeSelf);
585 $params['id'] = $groupIds;
586 return CRM_Contact_BAO_Group
::getGroups($params);
590 * Returns array of group ids of child groups of the specified group.
592 * @param $groupIds An array of valid group ids (passed by reference)
594 * @return array $groupIdArray List of groupIds that represent the requested group and its children@access public
596 static function getChildGroupIds($groupIds) {
597 if (!is_array($groupIds)) {
598 $groupIds = array($groupIds);
600 $dao = new CRM_Contact_DAO_GroupNesting();
601 $query = "SELECT child_group_id FROM civicrm_group_nesting WHERE parent_group_id IN (" . implode(',', $groupIds) . ")";
603 $childGroupIds = array();
604 while ($dao->fetch()) {
605 $childGroupIds[] = $dao->child_group_id
;
607 return $childGroupIds;
611 * Returns array of group ids of parent groups of the specified group.
613 * @param $groupIds An array of valid group ids (passed by reference)
615 * @return array $groupIdArray List of groupIds that represent the requested group and its parents@access public
617 static function getParentGroupIds($groupIds) {
618 if (!is_array($groupIds)) {
619 $groupIds = array($groupIds);
621 $dao = new CRM_Contact_DAO_GroupNesting();
622 $query = "SELECT parent_group_id FROM civicrm_group_nesting WHERE child_group_id IN (" . implode(',', $groupIds) . ")";
624 $parentGroupIds = array();
625 while ($dao->fetch()) {
626 $parentGroupIds[] = $dao->parent_group_id
;
628 return $parentGroupIds;
632 * Returns array of group ids of descendent groups of the specified group.
634 * @param $groupIds An array of valid group ids (passed by reference)
636 * @param bool $includeSelf
637 * @return array $groupIdArray List of groupIds that represent the requested group and its descendents@access public
639 static function getDescendentGroupIds($groupIds, $includeSelf = TRUE) {
640 if (!is_array($groupIds)) {
641 $groupIds = array($groupIds);
643 $dao = new CRM_Contact_DAO_GroupNesting();
644 $query = "SELECT child_group_id, parent_group_id FROM civicrm_group_nesting WHERE parent_group_id IN (" . implode(',', $groupIds) . ")";
646 $tmpGroupIds = array();
647 $childGroupIds = array();
649 $childGroupIds = $groupIds;
651 while ($dao->fetch()) {
652 // make sure we're not following any cyclical references
653 if (!array_key_exists($dao->parent_group_id
, $childGroupIds) && $dao->child_group_id
!= $groupIds[0]) {
654 $tmpGroupIds[] = $dao->child_group_id
;
657 if (!empty($tmpGroupIds)) {
658 $newChildGroupIds = self
::getDescendentGroupIds($tmpGroupIds);
659 $childGroupIds = array_merge($childGroupIds, $newChildGroupIds);
661 return $childGroupIds;
665 * Returns array of descendent groups of the specified group.
667 * @param $groupIds An array of valid group ids (passed by reference)
669 * @param bool $includeSelf
670 * @return \An $groupArray List of descendent groups@access public
672 static function getDescendentGroups($groupIds, $includeSelf = TRUE) {
673 $groupIds = self
::getDescendentGroupIds($groupIds, $includeSelf);
674 $params['id'] = $groupIds;
675 return CRM_Contact_BAO_Group
::getGroups($params);
679 * Returns array of group ids of valid potential child groups of the specified group.
681 * @param $groupId The group id to get valid potential children for
683 * @return array $groupIdArray List of groupIds that represent the valid potential children of the group@access public
685 static function getPotentialChildGroupIds($groupId) {
686 $groups = CRM_Contact_BAO_Group
::getGroups();
687 $potentialChildGroupIds = array();
688 foreach ($groups as $group) {
689 $potentialChildGroupId = $group->id
;
690 // print "Checking if $potentialChildGroupId is a descendent/ancestor of $groupId<br/><br/>";
691 if (!self
::isDescendentGroup($groupId, $potentialChildGroupId) &&
692 !self
::isAncestorGroup($groupId, $potentialChildGroupId) &&
693 $potentialChildGroupId != $groupId
695 $potentialChildGroupIds[] = $potentialChildGroupId;
698 return $potentialChildGroupIds;
703 * @param $parentGroupId
707 static function getContainingGroups($contactId, $parentGroupId) {
708 $groups = CRM_Contact_BAO_Group
::getGroups();
709 $containingGroups = array();
710 foreach ($groups as $group) {
711 if (self
::isDescendentGroup($parentGroupId, $group->id
)) {
712 $members = CRM_Contact_BAO_Group
::getMember($group->id
);
713 if ($members[$contactId]) {
714 $containingGroups[] = $group->title
;
719 return $containingGroups;