function buildQuickForm() { // Contact $this->add('hidden', 'contact_id', ts('Contact id'), '', true); // get the list of all the groups $allGroups = CRM_Core_PseudoConstant::group(); // Arrange groups into hierarchical listing (child groups follow their parents and have indentation spacing in title) $groupHierarchy = CRM_Contact_BAO_Group::getGroupsHierarchy($allGroups, NULL, ' ', TRUE); // get the list of groups contact is currently in ("Added") or unsubscribed ("Removed"). $currentGroups = CRM_Contact_BAO_GroupContact::getGroupList($this->_contactId); // Remove current groups from drowdown options ($groupSelect) if (is_array($currentGroups)) { // Compare array keys, since the array values (group title) in $groupList may have extra spaces for indenting child groups $groupSelect = array_diff_key($groupHierarchy, $currentGroups); } else { $groupSelect = $groupHierarchy; } $groupSelect = array('' => ts('- select group -')) + $groupSelect; if (count($groupSelect) > 1) { $session = CRM_Core_Session::singleton(); $msg = ts('Add to a group'); $this->add('select', 'group_id', $msg, $groupSelect, false); $this->addButtons(array(array('type' => 'submit', 'name' => ts('Opslaan / Volgende'), 'isDefault' => TRUE))); // export form elements $this->assign('elementNames', $this->getRenderableElementNames()); } parent::buildQuickForm(); }
/** * Build the form object. * * @return void */ public function buildQuickForm() { // get the list of all the groups if ($this->_context == 'user') { $onlyPublicGroups = CRM_Utils_Request::retrieve('onlyPublicGroups', 'Boolean', $this, FALSE); $allGroups = CRM_Core_PseudoConstant::staticGroup($onlyPublicGroups); } else { $allGroups = CRM_Core_PseudoConstant::group(); } // Arrange groups into hierarchical listing (child groups follow their parents and have indentation spacing in title) $groupHierarchy = CRM_Contact_BAO_Group::getGroupsHierarchy($allGroups, NULL, ' ', TRUE); // get the list of groups contact is currently in ("Added") or unsubscribed ("Removed"). $currentGroups = CRM_Contact_BAO_GroupContact::getGroupList($this->_contactId); // Remove current groups from drowdown options ($groupSelect) if (is_array($currentGroups)) { // Compare array keys, since the array values (group title) in $groupList may have extra spaces for indenting child groups $groupSelect = array_diff_key($groupHierarchy, $currentGroups); } else { $groupSelect = $groupHierarchy; } $groupSelect = array('' => ts('- select group -')) + $groupSelect; if (count($groupSelect) > 1) { $session = CRM_Core_Session::singleton(); // user dashboard if (strstr($session->readUserContext(), 'user')) { $msg = ts('Join a Group'); } else { $msg = ts('Add to a group'); } $this->add('select', 'group_id', '', $groupSelect, TRUE, array('class' => 'crm-select2 crm-action-menu action-icon-plus', 'placeholder' => $msg)); $this->addButtons(array(array('type' => 'next', 'name' => ts('Add'), 'isDefault' => TRUE))); } }
/** * Takes an associative array and creates / removes * contacts from the groups * * * @param array $params * (reference ) an assoc array of name/value pairs. * @param array $contactId * Contact id. * * @param bool $visibility * @param string $method * * @return void */ public static function create(&$params, $contactId, $visibility = FALSE, $method = 'Admin') { $contactIds = array(); $contactIds[] = $contactId; //if $visibility is true we are coming in via profile mean $method = 'Web' $ignorePermission = FALSE; if ($visibility) { $ignorePermission = TRUE; } if ($contactId) { $contactGroupList =& CRM_Contact_BAO_GroupContact::getContactGroup($contactId, 'Added', NULL, FALSE, $ignorePermission); 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); // this fix is done to prevent warning generated by array_key_exits incase of empty array is given as input if (!is_array($params)) { $params = array(); } // this fix is done to prevent warning generated by array_key_exits incase of empty array is given as input if (!isset($contactGroup) || !is_array($contactGroup)) { $contactGroup = array(); } // check which values has to be add/remove contact from group foreach ($allGroup as $key => $varValue) { if (!empty($params[$key]) && !array_key_exists($key, $contactGroup)) { // add contact to group CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $key, $method); } elseif (empty($params[$key]) && array_key_exists($key, $contactGroup)) { // remove contact from group CRM_Contact_BAO_GroupContact::removeContactsFromGroup($contactIds, $key, $method); } } }
/** * Method to get groups * * @return array * @access protected */ protected function getGroups() { return array('' => ts('-- please select --')) + CRM_Contact_BAO_GroupContact::getGroupList(); }
/** * Function to build the array for display the profile fields * * @param array $params key value. * @param int $gid profile Id * @param array $groupTitle Profile Group Title. * @param array $values formatted array of key value * * @return None * @access public */ function displayProfile(&$params, $gid, &$groupTitle, &$values) { if ($gid) { require_once 'CRM/Core/BAO/UFGroup.php'; require_once 'CRM/Profile/Form.php'; require_once 'CRM/Event/PseudoConstant.php'; $session = CRM_Core_Session::singleton(); $contactID = $session->get('userID'); if ($contactID) { if (CRM_Core_BAO_UFGroup::filterUFGroups($gid, $contactID)) { $fields = CRM_Core_BAO_UFGroup::getFields($gid, false, CRM_Core_Action::VIEW); } } else { $fields = CRM_Core_BAO_UFGroup::getFields($gid, false, CRM_Core_Action::ADD); } if (is_array($fields)) { // unset any email-* fields since we already collect it, CRM-2888 foreach (array_keys($fields) as $fieldName) { if (substr($fieldName, 0, 6) == 'email-') { unset($fields[$fieldName]); } } } foreach ($fields as $v) { if (CRM_Utils_Array::value('groupTitle', $v)) { $groupTitle['groupTitle'] = $v["groupTitle"]; break; } } $config = CRM_Core_Config::singleton(); require_once 'CRM/Core/PseudoConstant.php'; $locationTypes = $imProviders = array(); $locationTypes = CRM_Core_PseudoConstant::locationType(); $imProviders = CRM_Core_PseudoConstant::IMProvider(); //start of code to set the default values foreach ($fields as $name => $field) { $index = $field['title']; $customFieldName = null; if ($name === 'organization_name') { $values[$index] = $params[$name]; } if ('state_province' == substr($name, 0, 14)) { if ($params[$name]) { $values[$index] = CRM_Core_PseudoConstant::stateProvince($params[$name]); } else { $values[$index] = ''; } } else { if ('country' == substr($name, 0, 7)) { if ($params[$name]) { $values[$index] = CRM_Core_PseudoConstant::country($params[$name]); } else { $values[$index] = ''; } } else { if ('county' == substr($name, 0, 6)) { if ($params[$name]) { $values[$index] = CRM_Core_PseudoConstant::county($params[$name]); } else { $values[$index] = ''; } } else { if ('gender' == substr($name, 0, 6)) { $gender = CRM_Core_PseudoConstant::gender(); $values[$index] = $gender[$params[$name]]; } else { if ('individual_prefix' == substr($name, 0, 17)) { $prefix = CRM_Core_PseudoConstant::individualPrefix(); $values[$index] = $prefix[$params[$name]]; } else { if ('individual_suffix' == substr($name, 0, 17)) { $suffix = CRM_Core_PseudoConstant::individualSuffix(); $values[$index] = $suffix[$params[$name]]; } else { if (in_array($name, array('addressee', 'email_greeting', 'postal_greeting'))) { $filterCondition = array('greeting_type' => $name); $greeting =& CRM_Core_PseudoConstant::greeting($filterCondition); $values[$index] = $greeting[$params[$name]]; } else { if ($name === 'preferred_communication_method') { $communicationFields = CRM_Core_PseudoConstant::pcm(); $pref = array(); $compref = array(); $pref = $params[$name]; if (is_array($pref)) { foreach ($pref as $k => $v) { if ($v) { $compref[] = $communicationFields[$k]; } } } $values[$index] = implode(",", $compref); } else { if ($name == 'group') { require_once 'CRM/Contact/BAO/GroupContact.php'; $groups = CRM_Contact_BAO_GroupContact::getGroupList(); $title = array(); foreach ($params[$name] as $gId => $dontCare) { if ($dontCare) { $title[] = $groups[$gId]; } } $values[$index] = implode(', ', $title); } else { if ($name == 'tag') { require_once 'CRM/Core/BAO/EntityTag.php'; $entityTags = $params[$name]; $allTags =& CRM_Core_PseudoConstant::tag(); $title = array(); if (is_array($entityTags)) { foreach ($entityTags as $tagId => $dontCare) { $title[] = $allTags[$tagId]; } } $values[$index] = implode(', ', $title); } else { if ('participant_role_id' == $name) { $roles = CRM_Event_PseudoConstant::participantRole(); $values[$index] = $roles[$params[$name]]; } else { if ('participant_status_id' == $name) { $status = CRM_Event_PseudoConstant::participantStatus(); $values[$index] = $status[$params[$name]]; } else { if (strpos($name, '-') !== false) { list($fieldName, $id) = CRM_Utils_System::explode('-', $name, 2); $detailName = str_replace(' ', '_', $name); if (in_array($fieldName, array('state_province', 'country', 'county'))) { $values[$index] = $params[$detailName]; $idx = $detailName . '_id'; $values[$index] = $params[$idx]; } else { if ($fieldName == 'im') { $providerName = null; if ($providerId = $detailName . '-provider_id') { $providerName = CRM_Utils_Array::value($params[$providerId], $imProviders); } if ($providerName) { $values[$index] = $params[$detailName] . " (" . $providerName . ")"; } else { $values[$index] = $params[$detailName]; } } else { $values[$index] = $params[$detailName]; } } } else { if (substr($name, 0, 7) === 'do_not_' or substr($name, 0, 3) === 'is_') { if ($params[$name]) { $values[$index] = '[ x ]'; } } else { require_once 'CRM/Core/BAO/CustomField.php'; if ($cfID = CRM_Core_BAO_CustomField::getKeyID($name)) { $query = "\nSELECT html_type, data_type\nFROM civicrm_custom_field\nWHERE id = {$cfID}\n"; $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); $dao->fetch(); $htmlType = $dao->html_type; $dataType = $dao->data_type; if ($htmlType == 'File') { //$fileURL = CRM_Core_BAO_CustomField::getFileURL( $contactID, $cfID ); //$params[$index] = $values[$index] = $fileURL['file_url']; $values[$index] = $params[$index]; } else { if ($dao->data_type == 'Int' || $dao->data_type == 'Boolean') { $customVal = (int) $params[$name]; } else { if ($dao->data_type == 'Float') { $customVal = (double) $params[$name]; } else { if ($dao->data_type == 'Date') { $date = CRM_Utils_Date::format($params[$name], null, 'invalidDate'); if ($date != 'invalidDate') { $customVal = $date; } } else { $customVal = $params[$name]; } } } //take the custom field options $returnProperties = array($name => 1); require_once 'CRM/Contact/BAO/Query.php'; $query = new CRM_Contact_BAO_Query($params, $returnProperties, $fields); $options =& $query->_options; $displayValue = CRM_Core_BAO_CustomField::getDisplayValue($customVal, $cfID, $options); //Hack since we dont have function to check empty. //FIXME in 2.3 using crmIsEmptyArray() $customValue = true; if (is_array($customVal) && is_array($displayValue)) { $customValue = array_diff($customVal, $displayValue); } //use difference of arrays if (empty($customValue) || !$customValue) { $values[$index] = ''; } else { $values[$index] = $displayValue; } if (CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $cfID, 'is_search_range')) { $customFieldName = "{$name}_from"; } } } else { if ($name == 'home_URL' && !empty($params[$name])) { $url = CRM_Utils_System::fixURL($params[$name]); $values[$index] = "<a href=\"{$url}\">{$params[$name]}</a>"; } else { if (in_array($name, array('birth_date', 'deceased_date', 'participant_register_date'))) { require_once 'CRM/Utils/Date.php'; $values[$index] = CRM_Utils_Date::customFormat(CRM_Utils_Date::format($params[$name])); } else { $values[$index] = $params[$name]; } } } } } } } } } } } } } } } } } } } }
/** * Function to build the form * * @return None * @access public */ public function buildQuickForm() { // get the list of all the groups if ($this->_context == 'user') { $onlyPublicGroups = CRM_Utils_Request::retrieve('onlyPublicGroups', 'Boolean', $this, false); $allGroups = CRM_Core_PseudoConstant::staticGroup($onlyPublicGroups); } else { $allGroups = CRM_Core_PseudoConstant::group(); } // get the list of groups for the contact $currentGroups = CRM_Contact_BAO_GroupContact::getGroupList($this->_contactId); if (is_array($currentGroups)) { $groupList = array_diff($allGroups, $currentGroups); } else { $groupList = $allGroups; } $groupList[''] = ts('- select group -'); asort($groupList); if (count($groupList) > 1) { $session = CRM_Core_Session::singleton(); // user dashboard if (strstr($session->readUserContext(), 'user')) { $msg = ts('Join a Group'); } else { $msg = ts('Add to a group'); } $this->add('select', 'group_id', $msg, $groupList, true); $this->addButtons(array(array('type' => 'next', 'name' => ts('Add'), 'isDefault' => true))); } }
/** * takes an associative array and creates / removes * contacts from the groups * * * @param array $params (reference ) an assoc array of name/value pairs * @param array $contactId contact id * * @return none * @access public * @static */ static function create(&$params, $contactId, $visibility = false, $method = 'Admin') { $contactIds = array(); $contactIds[] = $contactId; if ($contactId) { $contactGroupList =& CRM_Contact_BAO_GroupContact::getContactGroup($contactId, 'Added'); 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); // this fix is done to prevent warning generated by array_key_exits incase of empty array is given as input if (!is_array($params)) { $params = array(); } // this fix is done to prevent warning generated by array_key_exits incase of empty array is given as input if (!isset($contactGroup) || !is_array($contactGroup)) { $contactGroup = array(); } // check which values has to be add/remove contact from group foreach ($allGroup as $key => $varValue) { if (CRM_Utils_Array::value($key, $params) && !array_key_exists($key, $contactGroup)) { // add contact to group CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $key, $method); } else { if (!CRM_Utils_Array::value($key, $params) && array_key_exists($key, $contactGroup)) { // remove contact from group CRM_Contact_BAO_GroupContact::removeContactsFromGroup($contactIds, $key, $method); } } } }
/** * Function to build the array for display the profile fields * * @param array $params key value. * @param int $gid profile Id * @param array $groupTitle Profile Group Title. * @param array $values formatted array of key value * * @param array $profileFields * * @return void * @access public * @static */ static function displayProfile(&$params, $gid, &$groupTitle, &$values, &$profileFields = array()) { if ($gid) { $config = CRM_Core_Config::singleton(); $session = CRM_Core_Session::singleton(); $contactID = $session->get('userID'); if ($contactID) { if (CRM_Core_BAO_UFGroup::filterUFGroups($gid, $contactID)) { $fields = CRM_Core_BAO_UFGroup::getFields($gid, FALSE, CRM_Core_Action::VIEW); } } else { $fields = CRM_Core_BAO_UFGroup::getFields($gid, FALSE, CRM_Core_Action::ADD); } foreach ($fields as $v) { if (!empty($v['groupTitle'])) { $groupTitle['groupTitle'] = $v['groupTitle']; break; } } $customVal = ''; $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id'); //start of code to set the default values foreach ($fields as $name => $field) { $skip = FALSE; // skip fields that should not be displayed separately if ($field['skipDisplay']) { continue; } $index = $field['title']; if ($name === 'organization_name') { $values[$index] = $params[$name]; } if ('state_province' == substr($name, 0, 14)) { if ($params[$name]) { $values[$index] = CRM_Core_PseudoConstant::stateProvince($params[$name]); } else { $values[$index] = ''; } } elseif ('date' == substr($name, -4)) { $values[$index] = CRM_Utils_Date::customFormat(CRM_Utils_Date::processDate($params[$name]), $config->dateformatFull); } elseif ('country' == substr($name, 0, 7)) { if ($params[$name]) { $values[$index] = CRM_Core_PseudoConstant::country($params[$name]); } else { $values[$index] = ''; } } elseif ('county' == substr($name, 0, 6)) { if ($params[$name]) { $values[$index] = CRM_Core_PseudoConstant::county($params[$name]); } else { $values[$index] = ''; } } elseif (in_array(substr($name, 0, -3), array('gender', 'prefix', 'suffix', 'communication_style'))) { $values[$index] = CRM_Core_PseudoConstant::getLabel('CRM_Contact_DAO_Contact', $name, $params[$name]); } elseif (in_array($name, array('addressee', 'email_greeting', 'postal_greeting'))) { $filterCondition = array('greeting_type' => $name); $greeting = CRM_Core_PseudoConstant::greeting($filterCondition); $values[$index] = $greeting[$params[$name]]; } elseif ($name === 'preferred_communication_method') { $communicationFields = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method'); $compref = array(); $pref = $params[$name]; if (is_array($pref)) { foreach ($pref as $k => $v) { if ($v) { $compref[] = $communicationFields[$k]; } } } $values[$index] = implode(',', $compref); } elseif ($name == 'contact_sub_type') { $values[$index] = implode(', ', $params[$name]); } elseif ($name == 'group') { $groups = CRM_Contact_BAO_GroupContact::getGroupList(); $title = array(); foreach ($params[$name] as $gId => $dontCare) { if ($dontCare) { $title[] = $groups[$gId]; } } $values[$index] = implode(', ', $title); } elseif ($name == 'tag') { $entityTags = $params[$name]; $allTags = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE)); $title = array(); if (is_array($entityTags)) { foreach ($entityTags as $tagId => $dontCare) { $title[] = $allTags[$tagId]; } } $values[$index] = implode(', ', $title); } elseif ('participant_role_id' == $name or 'participant_role' == $name) { $roles = CRM_Event_PseudoConstant::participantRole(); $values[$index] = $roles[$params[$name]]; } elseif ('participant_status_id' == $name or 'participant_status' == $name) { $status = CRM_Event_PseudoConstant::participantStatus(); $values[$index] = $status[$params[$name]]; } elseif (substr($name, -11) == 'campaign_id') { $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns($params[$name]); $values[$index] = CRM_Utils_Array::value($params[$name], $campaigns); } elseif (strpos($name, '-') !== FALSE) { list($fieldName, $id) = CRM_Utils_System::explode('-', $name, 2); $detailName = str_replace(' ', '_', $name); if (in_array($fieldName, array('state_province', 'country', 'county'))) { $values[$index] = $params[$detailName]; $idx = $detailName . '_id'; $values[$index] = $params[$idx]; } elseif ($fieldName == 'im') { $providerName = NULL; if ($providerId = $detailName . '-provider_id') { $providerName = CRM_Utils_Array::value($params[$providerId], $imProviders); } if ($providerName) { $values[$index] = $params[$detailName] . " (" . $providerName . ")"; } else { $values[$index] = $params[$detailName]; } } elseif ($fieldName == 'phone') { $phoneExtField = str_replace('phone', 'phone_ext', $detailName); if (isset($params[$phoneExtField])) { $values[$index] = $params[$detailName] . " (" . $params[$phoneExtField] . ")"; } else { $values[$index] = $params[$detailName]; } } else { $values[$index] = $params[$detailName]; } } else { if (substr($name, 0, 7) === 'do_not_' or substr($name, 0, 3) === 'is_') { if ($params[$name]) { $values[$index] = '[ x ]'; } } else { if ($cfID = CRM_Core_BAO_CustomField::getKeyID($name)) { $query = "\nSELECT html_type, data_type\nFROM civicrm_custom_field\nWHERE id = {$cfID}\n"; $dao = CRM_Core_DAO::executeQuery($query); $dao->fetch(); $htmlType = $dao->html_type; if ($htmlType == 'File') { $values[$index] = $params[$index]; } else { if ($dao->data_type == 'Int' || $dao->data_type == 'Boolean') { $v = $params[$name]; if (!CRM_Utils_System::isNull($v)) { $customVal = (int) $v; } } elseif ($dao->data_type == 'Float') { $customVal = (double) $params[$name]; } elseif ($dao->data_type == 'Date') { //@todo note the currently we are using default date time formatting. Since you can select/set // different date and time format specific to custom field we should consider fixing this // sometime in the future $customVal = $displayValue = CRM_Utils_Date::customFormat(CRM_Utils_Date::processDate($params[$name]), $config->dateformatFull); if (!empty($params[$name . '_time'])) { $customVal = $displayValue = CRM_Utils_Date::customFormat(CRM_Utils_Date::processDate($params[$name], $params[$name . '_time']), $config->dateformatDatetime); } $skip = TRUE; } else { $customVal = $params[$name]; } //take the custom field options $returnProperties = array($name => 1); $query = new CRM_Contact_BAO_Query($params, $returnProperties, $fields); $options =& $query->_options; if (!$skip) { $displayValue = CRM_Core_BAO_CustomField::getDisplayValue($customVal, $cfID, $options); } //Hack since we dont have function to check empty. //FIXME in 2.3 using crmIsEmptyArray() $customValue = TRUE; if (is_array($customVal) && is_array($displayValue)) { $customValue = array_diff($customVal, $displayValue); } //use difference of arrays if (empty($customValue) || !$customValue) { $values[$index] = ''; } else { $values[$index] = $displayValue; } } } elseif ($name == 'home_URL' && !empty($params[$name])) { $url = CRM_Utils_System::fixURL($params[$name]); $values[$index] = "<a href=\"{$url}\">{$params[$name]}</a>"; } elseif (in_array($name, array('birth_date', 'deceased_date', 'participant_register_date'))) { $values[$index] = CRM_Utils_Date::customFormat(CRM_Utils_Date::format($params[$name])); } else { $values[$index] = $params[$name]; } } } $profileFields[$name] = $field; } } }
/** * Implementation of hook_civicrm_post * * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_post */ function mailchimp_civicrm_post($op, $objectName, $objectId, &$objectRef) { /***** NO BULK EMAILS (User Opt Out) *****/ if ($objectName == 'Individual' || $objectName == 'Organization' || $objectName == 'Household') { // Contact Edited if ($op == 'edit' || $op == 'create') { if ($objectRef->is_opt_out == 1) { $action = 'unsubscribe'; } else { $action = 'subscribe'; } // Get all groups, the contact is subscribed to $civiGroups = CRM_Contact_BAO_GroupContact::getGroupList($objectId); $civiGroups = array_keys($civiGroups); if (empty($civiGroups)) { return; } // Get mailchimp details $groups = CRM_Mailchimp_Utils::getGroupsToSync($civiGroups); if (!empty($groups)) { // Loop through all groups and unsubscribe the email address from mailchimp foreach ($groups as $groupId => $groupDetails) { CRM_Mailchimp_Utils::subscribeOrUnsubsribeToMailchimpList($groupDetails, $objectId, $action); } } } } /***** Contacts added/removed/deleted from CiviCRM group *****/ if ($objectName == 'GroupContact') { // FIXME: Dirty hack to skip hook require_once 'CRM/Core/Session.php'; $session = CRM_Core_Session::singleton(); $skipPostHook = $session->get('skipPostHook'); // Added/Removed/Deleted - This works for both bulk action and individual add/remove/delete if (($op == 'create' || $op == 'edit' || $op == 'delete') && empty($skipPostHook)) { // Decide mailchimp action based on $op // Add / Rejoin Group if ($op == 'create' || $op == 'edit') { $action = 'subscribe'; } elseif ($op == 'delete') { $action = 'unsubscribe'; } // Get mailchimp details for the group $groups = CRM_Mailchimp_Utils::getGroupsToSync(array($objectId)); // Proceed only if the group is configured with mailing list/groups if (!empty($groups[$objectId])) { // Loop through all contacts added/removed from the group foreach ($objectRef as $contactId) { // Subscribe/Unsubscribe in Mailchimp CRM_Mailchimp_Utils::subscribeOrUnsubsribeToMailchimpList($groups[$objectId], $contactId, $action); } } } } }
/** * Implementation of hook_civicrm_post * * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_post */ function mailchimp_civicrm_post($op, $objectName, $objectId, &$objectRef) { if (!CRM_Mailchimp_Utils::$post_hook_enabled) { // Post hook is disabled at this point in the running. return; } /***** NO BULK EMAILS (User Opt Out) *****/ if ($objectName == 'Individual' || $objectName == 'Organization' || $objectName == 'Household') { // Contact Edited // @todo artfulrobot: I don't understand the cases this is dealing with. // Perhaps it was trying to check that if someone's been // marked as 'opt out' then they're unsubscribed from all // mailings. I could not follow the logic though - // without tests in place I thought it was better // disabled. if (FALSE) { if ($op == 'edit' || $op == 'create') { if ($objectRef->is_opt_out == 1) { $action = 'unsubscribe'; } else { $action = 'subscribe'; } // Get all groups, the contact is subscribed to $civiGroups = CRM_Contact_BAO_GroupContact::getGroupList($objectId); $civiGroups = array_keys($civiGroups); if (empty($civiGroups)) { return; } // Get mailchimp details $groups = CRM_Mailchimp_Utils::getGroupsToSync($civiGroups); if (!empty($groups)) { // Loop through all groups and unsubscribe the email address from mailchimp foreach ($groups as $groupId => $groupDetails) { // method removed. CRM_Mailchimp_Utils::subscribeOrUnsubsribeToMailchimpList($groupDetails, $objectId, $action); } } } } } /***** Contacts added/removed/deleted from CiviCRM group *****/ if ($objectName == 'GroupContact') { // Determine if the action being taken needs to affect Mailchimp at all. if ($op == 'view') { // Nothing changed; nothing to do. return; } // Get mailchimp details for the group. // $objectId here means CiviCRM group Id. $groups = CRM_Mailchimp_Utils::getGroupsToSync(array($objectId)); if (empty($groups[$objectId])) { // This group has nothing to do with Mailchimp. return; } // The updates we need to make can be complex. // If someone left/joined a group synced as the membership group for a // Mailchimp list, then that's a subscribe/unsubscribe option. // If however it was a group synced to an interest in Mailchimp, then // the join/leave on the CiviCRM side only means updating interests on the // Mailchimp side, not a subscribe/unsubscribe. // There is also the case that somone's been put into an interest group, but // is not in the membership group, which should not result in them being // subscribed at MC. if ($groups[$objectId]['interest_id']) { // This is a change to an interest grouping. // We only need update Mailchimp about this if the contact is in the // membership group. $list_id = $groups[$objectId]['list_id']; // find membership group, then find out if the contact is in that group. $membership_group_details = CRM_Mailchimp_Utils::getGroupsToSync(array(), $list_id, TRUE); $result = civicrm_api3('Contact', 'getsingle', ['return' => 'group', 'contact_id' => $objectRef[0]]); if (!CRM_Mailchimp_Utils::getGroupIds($result['groups'], $membership_group_details)) { // This contact is not in the membership group, so don't bother telling // Mailchimp about a change in their interests. return; } } // Finally this hook is useful for small changes only; if you just added // thousands of people to a group then this is NOT the way to tell Mailchimp // about it as it would require thousands of separate API calls. This would // probably cause big problems (like hitting the API rate limits, or // crashing CiviCRM due to PHP max execution times etc.). Such updates must // happen in the more controlled bulk update (push). if (count($objectRef) > 1) { // Limit application to one contact only. CRM_Core_Session::setStatus(ts('You have made a bulk update that means CiviCRM contacts and Mailchimp are no longer in sync. You should do an "Update Mailchimp from CiviCRM" sync to ensure the changes you have made are applied at Mailchimp.'), ts('Update Mailchimp from CiviCRM required.')); return; } // Trigger mini sync for this person and this list. $sync = new CRM_Mailchimp_Sync($groups[$objectId]['list_id']); $sync->updateMailchimpFromCiviSingleContact($objectRef[0]); } }
/** * Function to build the form * * @return None * @access public */ function buildQuickForm() { // get the list of all the groups $allGroups = CRM_Core_PseudoConstant::group(); // get the list of groups for the contact $currentGroups = CRM_Contact_BAO_GroupContact::getGroupList($this->_contactId); if (is_array($currentGroups)) { $groupList = array_diff($allGroups, $currentGroups); } else { $groupList = $allGroups; } $groupList[''] = ts('- select group -'); asort($groupList); if (count($groupList) > 1) { $this->addElement('select', 'group_id', ts('Add to a group'), $groupList); $this->addRule('group_id', ts('Please select the group.'), 'required'); $this->addButtons(array(array('type' => 'next', 'name' => ts('Add'), 'isDefault' => true))); } }
/** * Method to get groups * * @return array * @access protected */ protected function getGroups() { return CRM_Contact_BAO_GroupContact::getGroupList(); }