From 272ebc151381c8303e8b8de6f9c507de853ae7b4 Mon Sep 17 00:00:00 2001 From: Samuel Vanhove Date: Thu, 11 May 2023 13:25:22 -0400 Subject: [PATCH] Following PR-25994 and PR-26074 --- CRM/Contact/BAO/Group.php | 19 +++++++++++++++++-- CRM/Group/Form/Edit.php | 14 -------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/CRM/Contact/BAO/Group.php b/CRM/Contact/BAO/Group.php index 89ec6bd5fc..138bc13988 100644 --- a/CRM/Contact/BAO/Group.php +++ b/CRM/Contact/BAO/Group.php @@ -357,8 +357,8 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group { } } - // dev/core#287 Disable child groups if all parents are disabled. if (!empty($params['id'])) { + // dev/core#287 Disable child groups if all parents are disabled. $allChildGroupIds = self::getChildGroupIds($params['id']); foreach ($allChildGroupIds as $childKey => $childValue) { $parentIds = CRM_Contact_BAO_GroupNesting::getParentGroupIds($childValue); @@ -370,7 +370,12 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group { self::setIsActive($childValue, (int) ($params['is_active'] ?? 1)); } } + + // get current parents for removal if not in the list anymore + $parents = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $params['id'], 'parents'); + $currentParentGroupIDs = explode(',', $parents); } + // form the name only if missing: CRM-627 $nameParam = $params['name'] ?? NULL; if (!$nameParam && empty($params['id'])) { @@ -442,7 +447,17 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group { $params['parents'] = [$domainGroupID]; } - // FIXME: Only allows adding parents, cannot remove them + // first deal with removed parents + if (!empty($params['id']) && !empty($currentParentGroupIDs)) { + foreach ($currentParentGroupIDs as $parentGroupId) { + // no more parents or not in the new list, let's remove + if (empty($params['parents']) || !in_array($parentGroupId, $params['parents'])) { + CRM_Contact_BAO_GroupNesting::remove($parentGroupId, $params['id']); + } + } + } + + // then add missing parents if (!CRM_Utils_System::isNull($params['parents'])) { foreach ($params['parents'] as $parentId) { if ($parentId && !CRM_Contact_BAO_GroupNesting::isParentChild($parentId, $group->id)) { diff --git a/CRM/Group/Form/Edit.php b/CRM/Group/Form/Edit.php index 28d07afd9d..25416f7151 100644 --- a/CRM/Group/Form/Edit.php +++ b/CRM/Group/Form/Edit.php @@ -369,16 +369,6 @@ WHERE title = %1 $group = CRM_Contact_BAO_Group::create($params); // Set the entity id so it is available to postProcess hook consumers $this->setEntityId($group->id); - //Remove any parent groups requested to be removed - if (!empty($this->_groupValues['parents'])) { - $parentGroupIds = explode(',', $this->_groupValues['parents']); - foreach ($parentGroupIds as $parentGroupId) { - if (isset($params["remove_parent_group_$parentGroupId"])) { - CRM_Contact_BAO_GroupNesting::remove($parentGroupId, $group->id); - $updateNestingCache = TRUE; - } - } - } CRM_Core_Session::setStatus(ts('The Group \'%1\' has been saved.', array(1 => $group->title)), ts('Group Saved'), 'success'); @@ -392,10 +382,6 @@ WHERE title = %1 } } - // update the nesting cache - if ($updateNestingCache) { - CRM_Contact_BAO_GroupNestingCache::update(); - } } /** -- 2.25.1