From ea5f60133b65d0623c8bef56a632cc94d05fc04d Mon Sep 17 00:00:00 2001 From: Edsel Date: Fri, 21 Jul 2017 15:37:50 +0530 Subject: [PATCH] CRM-20934 Filtered active parent groups --- CRM/Contact/BAO/Group.php | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/CRM/Contact/BAO/Group.php b/CRM/Contact/BAO/Group.php index 6ebf3b8b96..1a7b71e40d 100644 --- a/CRM/Contact/BAO/Group.php +++ b/CRM/Contact/BAO/Group.php @@ -1075,7 +1075,7 @@ WHERE id IN $groupIdString if ($parents) { // group can have > 1 parent so parents may be comma separated list (eg. '1,2,5'). We just grab and match on 1st parent. $parentArray = explode(',', $parents); - $parent = $parentArray[0]; + $parent = self::filterActiveGroups($parentArray); $args[2] = array($parent, 'Integer'); $query .= " AND SUBSTRING_INDEX(parents, ',', 1) = %2"; } @@ -1091,7 +1091,7 @@ WHERE id IN $groupIdString while ($dao->fetch()) { if ($dao->parents) { $parentArray = explode(',', $dao->parents); - $parent = $parentArray[0]; + $parent = self::filterActiveGroups($parentArray); $tree[$parent][] = array( 'id' => $dao->id, 'title' => $dao->title, @@ -1378,4 +1378,27 @@ WHERE {$whereClause}"; return $childGroupIDs; } + /** + * Check parent groups and filter out the disabled ones. + * + * @param array $parentArray + * Array of group Ids. + * + * @return int + */ + public static function filterActiveGroups($parentArray) { + if (count($parentArray) > 1) { + foreach ($parentArray as $key => $groupId) { + $isActive = civicrm_api3('Group', 'getvalue', array( + 'id' => $groupId, + 'return' => 'is_active', + )); + if (!$isActive) { + unset($parentArray[$key]); + } + } + } + return reset($parentArray); + } + } -- 2.25.1