CRM-21707 use serialisation metadata from api basic_create_fallback.
[civicrm-core.git] / CRM / Contact / BAO / Group.php
index 9aa7704fb6f60ac09ef36e07925b13c74dfbd4d1..05e78491fc624e56b81cd5395d625a65f28546b7 100644 (file)
@@ -356,16 +356,13 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
       $params['name'] = CRM_Utils_String::titleToVar($params['title']);
     }
 
+    if (!empty($params['parents'])) {
+      $params['parents'] = CRM_Utils_Array::convertCheckboxFormatToArray((array) $params['parents']);
+    }
+
     // convert params if array type
     if (isset($params['group_type'])) {
-      if (is_array($params['group_type'])) {
-        $params['group_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
-            CRM_Utils_Array::convertCheckboxFormatToArray($params['group_type'])
-          ) . CRM_Core_DAO::VALUE_SEPARATOR;
-      }
-      else {
-        $params['group_type'] = CRM_Core_DAO::VALUE_SEPARATOR . $params['group_type'] . CRM_Core_DAO::VALUE_SEPARATOR;
-      }
+      $params['group_type'] = CRM_Utils_Array::convertCheckboxFormatToArray((array) $params['group_type']);
     }
     else {
       $params['group_type'] = NULL;
@@ -391,17 +388,8 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
       }
     }
     $group = new CRM_Contact_BAO_Group();
-    $group->copyValues($params);
-    //@todo very hacky fix for the fact this function wants to receive 'parents' as an array further down but
-    // needs it as a separated string for the DB. Preferred approaches are having the copyParams or save fn
-    // use metadata to translate the array to the appropriate DB type or altering the param in the api layer,
-    // or at least altering the param in same section as 'group_type' rather than repeating here. However, further down
-    // we need the $params one to be in it's original form & we are not sure what test coverage we have on that
-    if (isset($group->parents) && is_array($group->parents)) {
-      $group->parents = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
-          array_keys($group->parents)
-        ) . CRM_Core_DAO::VALUE_SEPARATOR;
-    }
+    $group->copyValues($params, TRUE);
+
     if (empty($params['id']) &&
       !$nameParam
     ) {
@@ -437,14 +425,11 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
       ) {
         // if no parent present and the group doesn't already have any parents,
         // make sure site group goes as parent
-        $params['parents'] = array($domainGroupID => 1);
-      }
-      elseif (array_key_exists('parents', $params) && !is_array($params['parents'])) {
-        $params['parents'] = array($params['parents'] => 1);
+        $params['parents'] = array($domainGroupID);
       }
 
       if (!empty($params['parents'])) {
-        foreach ($params['parents'] as $parentId => $dnc) {
+        foreach ($params['parents'] as $parentId) {
           if ($parentId && !CRM_Contact_BAO_GroupNesting::isParentChild($parentId, $group->id)) {
             CRM_Contact_BAO_GroupNesting::add($parentId, $group->id);
           }