Initial refactor of createProfileContact groups
authorMatthew Wire <mjw@mjwconsult.co.uk>
Sat, 15 Aug 2020 14:26:33 +0000 (15:26 +0100)
committerMatthew Wire <mjw@mjwconsult.co.uk>
Tue, 13 Oct 2020 08:34:32 +0000 (09:34 +0100)
CRM/Contact/BAO/Contact.php
CRM/Contact/BAO/GroupContact.php
api/v3/GroupContact.php

index d258641cb70ae7fe95619484b4f3ca979f12402d..17163083f3fe22f309db9005b863903d9c767da5 100644 (file)
@@ -2029,6 +2029,8 @@ ORDER BY civicrm_email.is_primary DESC";
     }
 
     // Process group and tag
+    // @todo Contact::create also calls addContactsToGroup/removeContactsToGroup
+    //   Remove from here and use the existing functionality in Contact::create
     if (isset($params['group'])) {
       $method = 'Admin';
       // this for sure means we are coming in via profile since i added it to fix
@@ -2036,7 +2038,35 @@ ORDER BY civicrm_email.is_primary DESC";
       if ($visibility) {
         $method = 'Web';
       }
-      CRM_Contact_BAO_GroupContact::create($params['group'], $contactID, $visibility, $method);
+      $groupParams = $params['group'] ?? [];
+      $contactIds = [$contactID];
+      $contactGroup = [];
+
+      if ($contactID) {
+        $contactGroupList = CRM_Contact_BAO_GroupContact::getContactGroup($contactID, 'Added',
+          NULL, FALSE, $visibility
+        );
+        if (is_array($contactGroupList)) {
+          foreach ($contactGroupList as $key) {
+            $groupId = $key['group_id'];
+            $contactGroup[$groupId] = $groupId;
+          }
+        }
+      }
+      // get the list of all the groups
+      $allGroup = CRM_Contact_BAO_GroupContact::getGroupList(0, $visibility);
+
+      // check which values has to be add/remove contact from group
+      foreach ($allGroup as $key => $varValue) {
+        if (!empty($groupParams[$key]) && !array_key_exists($key, $contactGroup)) {
+          // add contact to group
+          CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $key, $method);
+        }
+        elseif (empty($groupParams[$key]) && array_key_exists($key, $contactGroup)) {
+          // remove contact from group
+          CRM_Contact_BAO_GroupContact::removeContactsFromGroup($contactIds, $key, $method);
+        }
+      }
     }
 
     if (!empty($fields['tag']) && array_key_exists('tag', $params)) {
@@ -2045,7 +2075,8 @@ ORDER BY civicrm_email.is_primary DESC";
       CRM_Core_BAO_EntityTag::create($tags, 'civicrm_contact', $contactID);
     }
 
-    //to add profile in default group
+    // to add profile in default group
+    // @todo merge this with code above which also calls addContactsToGroup
     if (is_array($addToGroupID)) {
       $contactIds = [$contactID];
       foreach ($addToGroupID as $groupId) {
index e2ae71125ee19ef44d498ea96921361b387564e2..3f87cb4536439b6ffad37bd409633d4729d7db28 100644 (file)
@@ -33,7 +33,7 @@ class CRM_Contact_BAO_GroupContact extends CRM_Contact_DAO_GroupContact {
    * @param array $params
    *   (reference ) an assoc array of name/value pairs.
    *
-   * @return CRM_Contact_BAO_Group
+   * @return CRM_Contact_BAO_GroupContact
    */
   public static function add($params) {
     $hook = empty($params['id']) ? 'create' : 'edit';
@@ -473,7 +473,7 @@ SELECT    *
    *   Id of a particular group.
    *
    *
-   * @return groupID
+   * @return int groupID
    */
   public static function getGroupId($groupContactID) {
     $dao = new CRM_Contact_DAO_GroupContact();
@@ -485,19 +485,25 @@ SELECT    *
   /**
    * Creates / removes contacts from the groups
    *
-   * FIXME: Nonstandard create function; only called from CRM_Contact_BAO_Contact::createProfileContact
-   *
    * @param array $params
    *   Name/value pairs.
    * @param int $contactId
    *   Contact id.
-   *
    * @param bool $ignorePermission
    *   if ignorePermission is true we are coming in via profile mean $method = 'Web'
-   *
    * @param string $method
+   *
+   * @return CRM_Contact_BAO_GroupContact|void
    */
   public static function create($params, $contactId, $ignorePermission = FALSE, $method = 'Admin') {
+    if (empty($contactId)) {
+      return self::add($params);
+    }
+
+    // @fixme create was only called from CRM_Contact_BAO_Contact::createProfileContact
+    //   Now it's not called from anywhere so we can remove the below code after some time
+    CRM_Core_Error::deprecatedFunctionWarning('Use the GroupContact API');
+
     $contactIds = [$contactId];
     $contactGroup = [];
 
index ef1121ccb9f96766a9d49bd2e13c00a62767e079..e9b8e6de88bbb494b1d5a73a27022d9d74e5ab63 100644 (file)
@@ -116,7 +116,7 @@ function civicrm_api3_group_contact_create($params) {
       $params['contact_id'] = $info['values'][$params['id']]['contact_id'];
     }
   }
-  $action = CRM_Utils_Array::value('status', $params, 'Added');
+  $action = $params['status'] ?? 'Added';
   return _civicrm_api3_group_contact_common($params, $action);
 }