From 36c1bb3f49659a6b54a2100d0b8c8996fa9eded5 Mon Sep 17 00:00:00 2001 From: Matthew Wire Date: Sat, 15 Aug 2020 15:26:33 +0100 Subject: [PATCH] Initial refactor of createProfileContact groups --- CRM/Contact/BAO/Contact.php | 35 ++++++++++++++++++++++++++++++-- CRM/Contact/BAO/GroupContact.php | 18 ++++++++++------ api/v3/GroupContact.php | 2 +- 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index d258641cb7..17163083f3 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -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) { diff --git a/CRM/Contact/BAO/GroupContact.php b/CRM/Contact/BAO/GroupContact.php index e2ae71125e..3f87cb4536 100644 --- a/CRM/Contact/BAO/GroupContact.php +++ b/CRM/Contact/BAO/GroupContact.php @@ -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 = []; diff --git a/api/v3/GroupContact.php b/api/v3/GroupContact.php index ef1121ccb9..e9b8e6de88 100644 --- a/api/v3/GroupContact.php +++ b/api/v3/GroupContact.php @@ -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); } -- 2.25.1