3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 public function __construct($styleLabels = FALSE, $styleIndent = " -- ") {
56 parent
::__construct();
57 $this->_styleLabels
= $styleLabels;
58 $this->_styleIndent
= $styleIndent;
64 public function setSortOrder($sortOrder) {
68 if ($sortOrder != self
::$_sortOrder) {
69 self
::$_sortOrder = $sortOrder;
75 // spit out some error, someday
82 public function getSortOrder() {
83 return self
::$_sortOrder;
89 public 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
98 public 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;
111 public function current() {
112 if ($this->_styleLabels
&&
114 !$this->_alreadyStyled
116 $styledGroup = clone($this->_current
);
117 $nestingLevel = $this->getCurrentNestingLevel();
119 while ($nestingLevel--) {
120 $indent .= $this->_styleIndent
;
122 $styledGroup->title
= $indent . $styledGroup->title
;
124 $this->_current
= &$styledGroup;
125 $this->_alreadyStyled
= TRUE;
127 return $this->_current
;
133 public function key() {
134 $group = &$this->_current
;
136 foreach ($this->_parentStack
as $parentGroup) {
137 $ids[] = $parentGroup->id
;
139 $key = implode('-', $ids);
140 if (strlen($key) > 0) {
148 * @return CRM_Contact_BAO_Group|null
150 public function next() {
151 $currentGroup = &$this->_current
;
152 $childGroup = $this->_getNextChildGroup($currentGroup);
154 $nextGroup = &$childGroup;
155 $this->_parentStack
[] = &$this->_current
;
158 $nextGroup = $this->_getNextSiblingGroup($currentGroup);
160 // no sibling, find an ancestor w/ a sibling
162 // since we pop this array everytime, we should be
163 // reasonably safe from infinite loops, I think :)
164 $ancestor = array_pop($this->_parentStack
);
165 $this->_current
= &$ancestor;
166 if ($ancestor == NULL) {
169 $nextGroup = $this->_getNextSiblingGroup($ancestor);
176 $this->_current
= &$nextGroup;
177 $this->_alreadyStyled
= FALSE;
184 public function valid() {
185 if ($this->_current
) {
196 * @return CRM_Contact_BAO_Group|null
198 public function _getNextParentlessGroup(&$group = NULL) {
199 $lastParentlessGroup = $this->_lastParentlessGroup
;
200 $nextGroup = new CRM_Contact_BAO_Group();
201 $nextGroup->order_by
= 'title ' . self
::$_sortOrder;
203 if ($group == NULL) {
209 while ($nextGroup->fetch()) {
210 if (!self
::hasParentGroups($nextGroup->id
) && $sawLast) {
213 elseif ($lastParentlessGroup->id
== $nextGroup->id
) {
221 * @param $parentGroup
224 * @return CRM_Contact_BAO_Group|null
226 public function _getNextChildGroup(&$parentGroup, &$group = NULL) {
227 $children = self
::getChildGroupIds($parentGroup->id
);
228 if (count($children) > 0) {
229 // we have child groups, so get the first one based on _sortOrder
230 $childGroup = new CRM_Contact_BAO_Group();
231 $cgQuery = "SELECT * FROM civicrm_group WHERE id IN (" . implode(',', $children) . ") ORDER BY title " . self
::$_sortOrder;
232 $childGroup->query($cgQuery);
233 $currentGroup = &$this->_current
;
234 if ($group == NULL) {
240 while ($childGroup->fetch()) {
244 elseif ($currentGroup->id
=== $childGroup->id
) {
255 * @return CRM_Contact_BAO_Group|null
257 public function _getNextSiblingGroup(&$group) {
258 $parentGroup = end($this->_parentStack
);
260 $nextGroup = $this->_getNextChildGroup($parentGroup, $group);
264 /* if we get here, it could be because we're out of siblings
265 * (in which case we return null) or because we're at the
266 * top level groups which do not have parents but may still
267 * have siblings, so check for that first.
270 $nextGroup = $this->_getNextParentlessGroup($group);
272 $this->_lastParentlessGroup
= $nextGroup;
280 * Adds a new child group identified by $childGroupId to the group
281 * identified by $groupId
283 * @param int $parentID
284 * Id of the group to add the child to.
285 * @param int $childID
286 * Id of the new child group.
291 public static function add($parentID, $childID) {
292 // TODO: Add checks here to make sure invalid nests can't be created
293 $dao = new CRM_Contact_DAO_GroupNesting();
294 $query = "REPLACE INTO civicrm_group_nesting (child_group_id, parent_group_id) VALUES ($childID,$parentID);";
299 * Removes a child group identified by $childGroupId from the group
300 * identified by $groupId; does not delete child group, just the
301 * association between the two
304 * The id of the group to remove the child from.
306 * The id of the child group being removed.
310 public static function remove($parentID, $childID) {
311 $dao = new CRM_Contact_DAO_GroupNesting();
312 $query = "DELETE FROM civicrm_group_nesting WHERE child_group_id = $childID AND parent_group_id = $parentID";
317 * Removes associations where a child group is identified by $childGroupId from the group
318 * identified by $groupId; does not delete child group, just the
319 * association between the two
321 * @param int $childID
322 * The id of the child group being removed.
326 public static function removeAllParentForChild($childID) {
327 $dao = new CRM_Contact_DAO_GroupNesting();
328 $query = "DELETE FROM civicrm_group_nesting WHERE child_group_id = $childID";
333 * Returns true if the association between parent and child is present,
337 * The parent id of the association.
339 * The child id of the association.
342 * True if association is found, false otherwise.
344 public 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,
359 * The id of the group to check for child groups.
362 * True if 1 or more child groups are found, false otherwise.
364 public 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,
380 * The id of the group to check for parent groups.
383 * True if 1 or more parent groups are found, false otherwise.
385 public 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 array $groupIds
400 * of group ids (or one group id) to serve as the starting point.
401 * @param $checkGroupId
402 * The group id to check if it is a parent of the $groupIds group(s).
405 * True if $checkGroupId points to a group that is a parent of one of the $groupIds groups, false otherwise.
407 public static function isParentGroup($groupIds, $checkGroupId) {
408 if (!is_array($groupIds)) {
409 $groupIds = array($groupIds);
411 $dao = new CRM_Contact_DAO_GroupNesting();
412 $query = "SELECT parent_group_id FROM civicrm_group_nesting WHERE child_group_id IN (" . implode(',', $groupIds) . ")";
414 while ($dao->fetch()) {
415 $parentGroupId = $dao->parent_group_id
;
416 if ($parentGroupId == $checkGroupId) {
417 /* print "One of these: <pre>";
419 print "</pre> has groupId $checkGroupId as an ancestor.<br/>"; */
428 * Returns true if checkGroupId is a child of one of the groups in
429 * groupIds, false otherwise.
431 * @param array $groupIds
432 * of group ids (or one group id) to serve as the starting point.
433 * @param $checkGroupId
434 * The group id to check if it is a child of the $groupIds group(s).
437 * True if $checkGroupId points to a group that is a child of one of the $groupIds groups, false otherwise.
439 public static function isChildGroup($groupIds, $checkGroupId) {
441 if (!is_array($groupIds)) {
442 $groupIds = array($groupIds);
444 $dao = new CRM_Contact_DAO_GroupNesting();
445 $query = "SELECT child_group_id FROM civicrm_group_nesting WHERE parent_group_id IN (" . implode(',', $groupIds) . ")";
448 while ($dao->fetch()) {
449 $childGroupId = $dao->child_group_id
;
450 if ($childGroupId == $checkGroupId) {
451 /* print "One of these: <pre>";
453 print "</pre> has groupId $checkGroupId as a descendent.<br/><br/>"; */
462 * Returns true if checkGroupId is an ancestor of one of the groups in
463 * groupIds, false otherwise.
465 * @param array $groupIds
466 * of group ids (or one group id) to serve as the starting point.
467 * @param $checkGroupId
468 * The group id to check if it is an ancestor of the $groupIds group(s).
471 * True if $checkGroupId points to a group that is an ancestor of one of the $groupIds groups, false otherwise.
473 public static function isAncestorGroup($groupIds, $checkGroupId) {
474 if (!is_array($groupIds)) {
475 $groupIds = array($groupIds);
477 $dao = new CRM_Contact_DAO_GroupNesting();
478 $query = "SELECT parent_group_id FROM civicrm_group_nesting WHERE child_group_id IN (" . implode(',', $groupIds) . ")";
480 $nextGroupIds = array();
481 $gotAtLeastOneResult = FALSE;
482 while ($dao->fetch()) {
483 $gotAtLeastOneResult = TRUE;
484 $parentGroupId = $dao->parent_group_id
;
485 if ($parentGroupId == $checkGroupId) {
486 /* print "One of these: <pre>";
488 print "</pre> has groupId $checkGroupId as an ancestor.<br/>"; */
492 $nextGroupIds[] = $parentGroupId;
494 if ($gotAtLeastOneResult) {
495 return self
::isAncestorGroup($nextGroupIds, $checkGroupId);
503 * Returns true if checkGroupId is a descendent of one of the groups in
504 * groupIds, false otherwise.
506 * @param array $groupIds
507 * of group ids (or one group id) to serve as the starting point.
508 * @param $checkGroupId
509 * The group id to check if it is a descendent of the $groupIds group(s).
512 * True if $checkGroupId points to a group that is a descendent of one of the $groupIds groups, false otherwise.
514 public static function isDescendentGroup($groupIds, $checkGroupId) {
515 if (!is_array($groupIds)) {
516 $groupIds = array($groupIds);
518 $dao = new CRM_Contact_DAO_GroupNesting();
519 $query = "SELECT child_group_id FROM civicrm_group_nesting WHERE parent_group_id IN (" . implode(',', $groupIds) . ")";
521 $nextGroupIds = array();
522 $gotAtLeastOneResult = FALSE;
523 while ($dao->fetch()) {
524 $gotAtLeastOneResult = TRUE;
525 $childGroupId = $dao->child_group_id
;
526 if ($childGroupId == $checkGroupId) {
527 /* print "One of these: <pre>";
529 print "</pre> has groupId $checkGroupId as a descendent.<br/><br/>"; */
533 $nextGroupIds[] = $childGroupId;
535 if ($gotAtLeastOneResult) {
536 return self
::isDescendentGroup($nextGroupIds, $checkGroupId);
544 * Returns array of group ids of ancestor groups of the specified group.
546 * @param array $groupIds
547 * An array of valid group ids (passed by reference).
549 * @param bool $includeSelf
552 * List of groupIds that represent the requested group and its ancestors@access public
554 public static function getAncestorGroupIds($groupIds, $includeSelf = TRUE) {
555 if (!is_array($groupIds)) {
556 $groupIds = array($groupIds);
558 $dao = new CRM_Contact_DAO_GroupNesting();
559 $query = "SELECT parent_group_id, child_group_id
560 FROM civicrm_group_nesting
561 WHERE child_group_id IN (" . implode(',', $groupIds) . ")";
563 $tmpGroupIds = array();
564 $parentGroupIds = array();
566 $parentGroupIds = $groupIds;
568 while ($dao->fetch()) {
569 // make sure we're not following any cyclical references
570 if (!array_key_exists($dao->child_group_id
, $parentGroupIds) && $dao->parent_group_id
!= $groupIds[0]) {
571 $tmpGroupIds[] = $dao->parent_group_id
;
574 if (!empty($tmpGroupIds)) {
575 $newParentGroupIds = self
::getAncestorGroupIds($tmpGroupIds);
576 $parentGroupIds = array_merge($parentGroupIds, $newParentGroupIds);
578 return $parentGroupIds;
582 * Returns array of ancestor groups of the specified group.
584 * @param array $groupIds
585 * An array of valid group ids (passed by reference).
587 * @param bool $includeSelf
589 * List of ancestor groups@access public
591 public static function getAncestorGroups($groupIds, $includeSelf = TRUE) {
592 $groupIds = self
::getAncestorGroupIds($groupIds, $includeSelf);
593 $params['id'] = $groupIds;
594 return CRM_Contact_BAO_Group
::getGroups($params);
598 * Returns array of group ids of child groups of the specified group.
600 * @param array $groupIds
601 * An array of valid group ids (passed by reference).
604 * List of groupIds that represent the requested group and its children@access public
606 public static function getChildGroupIds($groupIds) {
607 if (!is_array($groupIds)) {
608 $groupIds = array($groupIds);
610 $dao = new CRM_Contact_DAO_GroupNesting();
611 $query = "SELECT child_group_id FROM civicrm_group_nesting WHERE parent_group_id IN (" . implode(',', $groupIds) . ")";
613 $childGroupIds = array();
614 while ($dao->fetch()) {
615 $childGroupIds[] = $dao->child_group_id
;
617 return $childGroupIds;
621 * Returns array of group ids of parent groups of the specified group.
623 * @param array $groupIds
624 * An array of valid group ids (passed by reference).
627 * List of groupIds that represent the requested group and its parents@access public
629 public static function getParentGroupIds($groupIds) {
630 if (!is_array($groupIds)) {
631 $groupIds = array($groupIds);
633 $dao = new CRM_Contact_DAO_GroupNesting();
634 $query = "SELECT parent_group_id FROM civicrm_group_nesting WHERE child_group_id IN (" . implode(',', $groupIds) . ")";
636 $parentGroupIds = array();
637 while ($dao->fetch()) {
638 $parentGroupIds[] = $dao->parent_group_id
;
640 return $parentGroupIds;
644 * Returns array of group ids of descendent groups of the specified group.
646 * @param array $groupIds
647 * An array of valid group ids (passed by reference).
649 * @param bool $includeSelf
651 * List of groupIds that represent the requested group and its descendents@access public
653 public static function getDescendentGroupIds($groupIds, $includeSelf = TRUE) {
654 if (!is_array($groupIds)) {
655 $groupIds = array($groupIds);
657 $dao = new CRM_Contact_DAO_GroupNesting();
658 $query = "SELECT child_group_id, parent_group_id FROM civicrm_group_nesting WHERE parent_group_id IN (" . implode(',', $groupIds) . ")";
660 $tmpGroupIds = array();
661 $childGroupIds = array();
663 $childGroupIds = $groupIds;
665 while ($dao->fetch()) {
666 // make sure we're not following any cyclical references
667 if (!array_key_exists($dao->parent_group_id
, $childGroupIds) && $dao->child_group_id
!= $groupIds[0]) {
668 $tmpGroupIds[] = $dao->child_group_id
;
671 if (!empty($tmpGroupIds)) {
672 $newChildGroupIds = self
::getDescendentGroupIds($tmpGroupIds);
673 $childGroupIds = array_merge($childGroupIds, $newChildGroupIds);
675 return $childGroupIds;
679 * Returns array of descendent groups of the specified group.
681 * @param array $groupIds
682 * An array of valid group ids
684 * @param bool $includeSelf
686 * List of descendent groups@access public
688 public static function getDescendentGroups($groupIds, $includeSelf = TRUE) {
689 $groupIds = self
::getDescendentGroupIds($groupIds, $includeSelf);
690 $params['id'] = $groupIds;
691 return CRM_Contact_BAO_Group
::getGroups($params);
695 * Returns array of group ids of valid potential child groups of the specified group.
698 * The group id to get valid potential children for.
701 * List of groupIds that represent the valid potential children of the group@access public
703 public static function getPotentialChildGroupIds($groupId) {
704 $groups = CRM_Contact_BAO_Group
::getGroups();
705 $potentialChildGroupIds = array();
706 foreach ($groups as $group) {
707 $potentialChildGroupId = $group->id
;
708 // print "Checking if $potentialChildGroupId is a descendent/ancestor of $groupId<br/><br/>";
709 if (!self
::isDescendentGroup($groupId, $potentialChildGroupId) &&
710 !self
::isAncestorGroup($groupId, $potentialChildGroupId) &&
711 $potentialChildGroupId != $groupId
713 $potentialChildGroupIds[] = $potentialChildGroupId;
716 return $potentialChildGroupIds;
720 * @param int $contactId
721 * @param int $parentGroupId
725 public static function getContainingGroups($contactId, $parentGroupId) {
726 $groups = CRM_Contact_BAO_Group
::getGroups();
727 $containingGroups = array();
728 foreach ($groups as $group) {
729 if (self
::isDescendentGroup($parentGroupId, $group->id
)) {
730 $members = CRM_Contact_BAO_Group
::getMember($group->id
);
731 if ($members[$contactId]) {
732 $containingGroups[] = $group->title
;
737 return $containingGroups;