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;
108 public function current() {
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
;
130 public function key() {
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
147 public function next() {
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;
181 public function valid() {
182 if ($this->_current
) {
193 * @return CRM_Contact_BAO_Group|null
195 public 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 public 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 public 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
280 * @param int $parentID id of the group to add the child to
281 * @param int $childID id of the new child group
286 public static function add($parentID, $childID) {
287 // TODO: Add checks here to make sure invalid nests can't be created
288 $dao = new CRM_Contact_DAO_GroupNesting();
289 $query = "REPLACE INTO civicrm_group_nesting (child_group_id, parent_group_id) VALUES ($childID,$parentID);";
294 * Removes a child group identified by $childGroupId from the group
295 * identified by $groupId; does not delete child group, just the
296 * association between the two
298 * @param $parentID The id of the group to remove the child from
299 * @param $childID The id of the child group being removed
304 public static function remove($parentID, $childID) {
305 $dao = new CRM_Contact_DAO_GroupNesting();
306 $query = "DELETE FROM civicrm_group_nesting WHERE child_group_id = $childID AND parent_group_id = $parentID";
311 * Removes associations where a child group is identified by $childGroupId from the group
312 * identified by $groupId; does not delete child group, just the
313 * association between the two
315 * @param int $childID The id of the child group being removed
320 public static function removeAllParentForChild($childID) {
321 $dao = new CRM_Contact_DAO_GroupNesting();
322 $query = "DELETE FROM civicrm_group_nesting WHERE child_group_id = $childID";
327 * Returns true if the association between parent and child is present,
330 * @param $parentID The parent id of the association
331 * @param $childID The child id of the association
333 * @return boolean True if association is found, false otherwise.
336 public static function isParentChild($parentID, $childID) {
337 $dao = new CRM_Contact_DAO_GroupNesting();
338 $query = "SELECT id FROM civicrm_group_nesting WHERE child_group_id = $childID AND parent_group_id = $parentID";
347 * Returns true if if the given groupId has 1 or more child groups,
350 * @param $groupId The id of the group to check for child groups
352 * @return boolean True if 1 or more child groups are found, false otherwise.
355 public static function hasChildGroups($groupId) {
356 $dao = new CRM_Contact_DAO_GroupNesting();
357 $query = "SELECT child_group_id FROM civicrm_group_nesting WHERE parent_group_id = $groupId LIMIT 1";
358 //print $query . "\n<br><br>";
367 * Returns true if the given groupId has 1 or more parent groups,
370 * @param $groupId The id of the group to check for parent groups
372 * @return boolean True if 1 or more parent groups are found, false otherwise.
375 public static function hasParentGroups($groupId) {
376 $dao = new CRM_Contact_DAO_GroupNesting();
377 $query = "SELECT parent_group_id FROM civicrm_group_nesting WHERE child_group_id = $groupId LIMIT 1";
386 * Returns true if checkGroupId is a parent of one of the groups in
387 * groupIds, false otherwise.
389 * @param $groupIds Array of group ids (or one group id) to serve as the starting point
390 * @param $checkGroupId The group id to check if it is a parent of the $groupIds group(s)
392 * @return boolean True if $checkGroupId points to a group that is a parent of one of the $groupIds groups, false otherwise.
395 public static function isParentGroup($groupIds, $checkGroupId) {
396 if (!is_array($groupIds)) {
397 $groupIds = array($groupIds);
399 $dao = new CRM_Contact_DAO_GroupNesting();
400 $query = "SELECT parent_group_id FROM civicrm_group_nesting WHERE child_group_id IN (" . implode(',', $groupIds) . ")";
402 while ($dao->fetch()) {
403 $parentGroupId = $dao->parent_group_id
;
404 if ($parentGroupId == $checkGroupId) {
405 /* print "One of these: <pre>";
407 print "</pre> has groupId $checkGroupId as an ancestor.<br/>"; */
416 * Returns true if checkGroupId is a child of one of the groups in
417 * groupIds, false otherwise.
419 * @param $groupIds Array of group ids (or one group id) to serve as the starting point
420 * @param $checkGroupId The group id to check if it is a child of the $groupIds group(s)
422 * @return boolean True if $checkGroupId points to a group that is a child of one of the $groupIds groups, false otherwise.
425 public static function isChildGroup($groupIds, $checkGroupId) {
427 if (!is_array($groupIds)) {
428 $groupIds = array($groupIds);
430 $dao = new CRM_Contact_DAO_GroupNesting();
431 $query = "SELECT child_group_id FROM civicrm_group_nesting WHERE parent_group_id IN (" . implode(',', $groupIds) . ")";
434 while ($dao->fetch()) {
435 $childGroupId = $dao->child_group_id
;
436 if ($childGroupId == $checkGroupId) {
437 /* print "One of these: <pre>";
439 print "</pre> has groupId $checkGroupId as a descendent.<br/><br/>"; */
448 * Returns true if checkGroupId is an ancestor of one of the groups in
449 * groupIds, false otherwise.
451 * @param $groupIds Array of group ids (or one group id) to serve as the starting point
452 * @param $checkGroupId The group id to check if it is an ancestor of the $groupIds group(s)
454 * @return boolean True if $checkGroupId points to a group that is an ancestor of one of the $groupIds groups, false otherwise.
457 public static function isAncestorGroup($groupIds, $checkGroupId) {
458 if (!is_array($groupIds)) {
459 $groupIds = array($groupIds);
461 $dao = new CRM_Contact_DAO_GroupNesting();
462 $query = "SELECT parent_group_id FROM civicrm_group_nesting WHERE child_group_id IN (" . implode(',', $groupIds) . ")";
464 $nextGroupIds = array();
465 $gotAtLeastOneResult = FALSE;
466 while ($dao->fetch()) {
467 $gotAtLeastOneResult = TRUE;
468 $parentGroupId = $dao->parent_group_id
;
469 if ($parentGroupId == $checkGroupId) {
470 /* print "One of these: <pre>";
472 print "</pre> has groupId $checkGroupId as an ancestor.<br/>"; */
476 $nextGroupIds[] = $parentGroupId;
478 if ($gotAtLeastOneResult) {
479 return self
::isAncestorGroup($nextGroupIds, $checkGroupId);
487 * Returns true if checkGroupId is a descendent of one of the groups in
488 * groupIds, false otherwise.
490 * @param $groupIds Array of group ids (or one group id) to serve as the starting point
491 * @param $checkGroupId The group id to check if it is a descendent of the $groupIds group(s)
493 * @return boolean True if $checkGroupId points to a group that is a descendent of one of the $groupIds groups, false otherwise.
496 public static function isDescendentGroup($groupIds, $checkGroupId) {
497 if (!is_array($groupIds)) {
498 $groupIds = array($groupIds);
500 $dao = new CRM_Contact_DAO_GroupNesting();
501 $query = "SELECT child_group_id FROM civicrm_group_nesting WHERE parent_group_id IN (" . implode(',', $groupIds) . ")";
503 $nextGroupIds = array();
504 $gotAtLeastOneResult = FALSE;
505 while ($dao->fetch()) {
506 $gotAtLeastOneResult = TRUE;
507 $childGroupId = $dao->child_group_id
;
508 if ($childGroupId == $checkGroupId) {
509 /* print "One of these: <pre>";
511 print "</pre> has groupId $checkGroupId as a descendent.<br/><br/>"; */
515 $nextGroupIds[] = $childGroupId;
517 if ($gotAtLeastOneResult) {
518 return self
::isDescendentGroup($nextGroupIds, $checkGroupId);
526 * Returns array of group ids of ancestor groups of the specified group.
528 * @param $groupIds An array of valid group ids (passed by reference)
530 * @param bool $includeSelf
532 * @return array $groupIdArray List of groupIds that represent the requested group and its ancestors@access public
534 public static function getAncestorGroupIds($groupIds, $includeSelf = TRUE) {
535 if (!is_array($groupIds)) {
536 $groupIds = array($groupIds);
538 $dao = new CRM_Contact_DAO_GroupNesting();
539 $query = "SELECT parent_group_id, child_group_id
540 FROM civicrm_group_nesting
541 WHERE child_group_id IN (" . implode(',', $groupIds) . ")";
543 $tmpGroupIds = array();
544 $parentGroupIds = array();
546 $parentGroupIds = $groupIds;
548 while ($dao->fetch()) {
549 // make sure we're not following any cyclical references
550 if (!array_key_exists($dao->child_group_id
, $parentGroupIds) && $dao->parent_group_id
!= $groupIds[0]) {
551 $tmpGroupIds[] = $dao->parent_group_id
;
554 if (!empty($tmpGroupIds)) {
555 $newParentGroupIds = self
::getAncestorGroupIds($tmpGroupIds);
556 $parentGroupIds = array_merge($parentGroupIds, $newParentGroupIds);
558 return $parentGroupIds;
562 * Returns array of ancestor groups of the specified group.
564 * @param $groupIds An array of valid group ids (passed by reference)
566 * @param bool $includeSelf
567 * @return \An $groupArray List of ancestor groups@access public
569 public static function getAncestorGroups($groupIds, $includeSelf = TRUE) {
570 $groupIds = self
::getAncestorGroupIds($groupIds, $includeSelf);
571 $params['id'] = $groupIds;
572 return CRM_Contact_BAO_Group
::getGroups($params);
576 * Returns array of group ids of child groups of the specified group.
578 * @param $groupIds An array of valid group ids (passed by reference)
580 * @return array $groupIdArray List of groupIds that represent the requested group and its children@access public
582 public static function getChildGroupIds($groupIds) {
583 if (!is_array($groupIds)) {
584 $groupIds = array($groupIds);
586 $dao = new CRM_Contact_DAO_GroupNesting();
587 $query = "SELECT child_group_id FROM civicrm_group_nesting WHERE parent_group_id IN (" . implode(',', $groupIds) . ")";
589 $childGroupIds = array();
590 while ($dao->fetch()) {
591 $childGroupIds[] = $dao->child_group_id
;
593 return $childGroupIds;
597 * Returns array of group ids of parent groups of the specified group.
599 * @param $groupIds An array of valid group ids (passed by reference)
601 * @return array $groupIdArray List of groupIds that represent the requested group and its parents@access public
603 public static function getParentGroupIds($groupIds) {
604 if (!is_array($groupIds)) {
605 $groupIds = array($groupIds);
607 $dao = new CRM_Contact_DAO_GroupNesting();
608 $query = "SELECT parent_group_id FROM civicrm_group_nesting WHERE child_group_id IN (" . implode(',', $groupIds) . ")";
610 $parentGroupIds = array();
611 while ($dao->fetch()) {
612 $parentGroupIds[] = $dao->parent_group_id
;
614 return $parentGroupIds;
618 * Returns array of group ids of descendent groups of the specified group.
620 * @param $groupIds An array of valid group ids (passed by reference)
622 * @param bool $includeSelf
623 * @return array $groupIdArray List of groupIds that represent the requested group and its descendents@access public
625 public static function getDescendentGroupIds($groupIds, $includeSelf = TRUE) {
626 if (!is_array($groupIds)) {
627 $groupIds = array($groupIds);
629 $dao = new CRM_Contact_DAO_GroupNesting();
630 $query = "SELECT child_group_id, parent_group_id FROM civicrm_group_nesting WHERE parent_group_id IN (" . implode(',', $groupIds) . ")";
632 $tmpGroupIds = array();
633 $childGroupIds = array();
635 $childGroupIds = $groupIds;
637 while ($dao->fetch()) {
638 // make sure we're not following any cyclical references
639 if (!array_key_exists($dao->parent_group_id
, $childGroupIds) && $dao->child_group_id
!= $groupIds[0]) {
640 $tmpGroupIds[] = $dao->child_group_id
;
643 if (!empty($tmpGroupIds)) {
644 $newChildGroupIds = self
::getDescendentGroupIds($tmpGroupIds);
645 $childGroupIds = array_merge($childGroupIds, $newChildGroupIds);
647 return $childGroupIds;
651 * Returns array of descendent groups of the specified group.
653 * @param $groupIds An array of valid group ids (passed by reference)
655 * @param bool $includeSelf
656 * @return \An $groupArray List of descendent groups@access public
658 public static function getDescendentGroups($groupIds, $includeSelf = TRUE) {
659 $groupIds = self
::getDescendentGroupIds($groupIds, $includeSelf);
660 $params['id'] = $groupIds;
661 return CRM_Contact_BAO_Group
::getGroups($params);
665 * Returns array of group ids of valid potential child groups of the specified group.
667 * @param $groupId The group id to get valid potential children for
669 * @return array $groupIdArray List of groupIds that represent the valid potential children of the group@access public
671 public static function getPotentialChildGroupIds($groupId) {
672 $groups = CRM_Contact_BAO_Group
::getGroups();
673 $potentialChildGroupIds = array();
674 foreach ($groups as $group) {
675 $potentialChildGroupId = $group->id
;
676 // print "Checking if $potentialChildGroupId is a descendent/ancestor of $groupId<br/><br/>";
677 if (!self
::isDescendentGroup($groupId, $potentialChildGroupId) &&
678 !self
::isAncestorGroup($groupId, $potentialChildGroupId) &&
679 $potentialChildGroupId != $groupId
681 $potentialChildGroupIds[] = $potentialChildGroupId;
684 return $potentialChildGroupIds;
688 * @param int $contactId
689 * @param int $parentGroupId
693 public static function getContainingGroups($contactId, $parentGroupId) {
694 $groups = CRM_Contact_BAO_Group
::getGroups();
695 $containingGroups = array();
696 foreach ($groups as $group) {
697 if (self
::isDescendentGroup($parentGroupId, $group->id
)) {
698 $members = CRM_Contact_BAO_Group
::getMember($group->id
);
699 if ($members[$contactId]) {
700 $containingGroups[] = $group->title
;
705 return $containingGroups;