}
// 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
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)) {
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) {
* @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';
* Id of a particular group.
*
*
- * @return groupID
+ * @return int groupID
*/
public static function getGroupId($groupContactID) {
$dao = new CRM_Contact_DAO_GroupContact();
/**
* 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 = [];