Skip to content

Commit

Permalink
group.update Parent omitted in params should do nothing + avoid recur…
Browse files Browse the repository at this point in the history
…sion
  • Loading branch information
samuelsov committed May 23, 2023
1 parent 5f76b65 commit 80db90a
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions CRM/Contact/BAO/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,10 @@ public function addSelectWhereClause() {
* The new group BAO (if created)
*/
public static function create(&$params) {

// only check if we need to remove parents if a params was provided
$parentsParamProvided = array_key_exists('parents', $params);

$params += [
'group_type' => NULL,
'parents' => NULL,
Expand Down Expand Up @@ -382,8 +386,12 @@ public static function create(&$params) {
$params['name'] = CRM_Utils_String::titleToVar($params['title']);
}

if (!CRM_Utils_System::isNull($params['parents'])) {
if ($parentsParamProvided) {
$params['parents'] = CRM_Utils_Array::convertCheckboxFormatToArray((array) $params['parents']);
// failsafe: forbid adding itself as parent
if (!empty($params['id']) && $key = array_search($params['id'], $params['parents'])) {
unset($params['parents'][$key]);
}
}

// convert params if array type
Expand Down Expand Up @@ -448,7 +456,7 @@ public static function create(&$params) {
}

// first deal with removed parents
if (array_key_exists('parents', $params) && !empty($currentParentGroupIDs)) {
if ($parentsParamProvided && !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'])) {
Expand All @@ -466,9 +474,10 @@ public static function create(&$params) {
}
}

// this is always required, since we don't know when a
// parent group is removed
CRM_Contact_BAO_GroupNestingCache::update();
// refresh cache if parents param was provided
if ($parentsParamProvided) {
CRM_Contact_BAO_GroupNestingCache::update();
}

// update group contact cache for all parent groups
$parentIds = CRM_Contact_BAO_GroupNesting::getParentGroupIds($group->id);
Expand Down

0 comments on commit 80db90a

Please sign in to comment.