static function subscribeOrUnsubsribeToMailchimpList($groupDetails, $contactID, $action) { CRM_Mailchimp_Utils::checkDebug('Start-CRM_Mailchimp_Utils subscribeOrUnsubsribeToMailchimpList $groupDetails', $groupDetails); CRM_Mailchimp_Utils::checkDebug('Start-CRM_Mailchimp_Utils subscribeOrUnsubsribeToMailchimpList $contactID', $contactID); CRM_Mailchimp_Utils::checkDebug('Start-CRM_Mailchimp_Utils subscribeOrUnsubsribeToMailchimpList $action', $action); if (empty($groupDetails) || empty($contactID) || empty($action)) { return NULL; } // We need to get contact's email before subscribing in Mailchimp $contactParams = array('version' => 3, 'id' => $contactID); $contactResult = civicrm_api('Contact', 'get', $contactParams); // This is the primary email address of the contact $email = $contactResult['values'][$contactID]['email']; if (empty($email)) { // Its possible to have contacts in CiviCRM without email address // and add to group offline return; } // Optional merges for the email (FNAME, LNAME) $merge = array('FNAME' => $contactResult['values'][$contactID]['first_name'], 'LNAME' => $contactResult['values'][$contactID]['last_name']); $listID = $groupDetails['list_id']; $grouping_id = $groupDetails['grouping_id']; $group_id = $groupDetails['group_id']; if (!empty($grouping_id) and !empty($group_id)) { $merge_groups[$grouping_id] = array('id' => $groupDetails['grouping_id'], 'groups' => array()); $merge_groups[$grouping_id]['groups'][] = CRM_Mailchimp_Utils::getMCGroupName($listID, $grouping_id, $group_id); // remove the significant array indexes, in case Mailchimp cares. $merge['groupings'] = array_values($merge_groups); } // Send Mailchimp Lists API Call. $list = new Mailchimp_Lists(CRM_Mailchimp_Utils::mailchimp()); switch ($action) { case "subscribe": // http://apidocs.mailchimp.com/api/2.0/lists/subscribe.php try { $result = $list->subscribe($listID, array('email' => $email), $merge, $email_type = 'html', $double_optin = FALSE, $update_existing = FALSE, $replace_interests = TRUE, $send_welcome = FALSE); } catch (Exception $e) { // Don't display if the error is that we're already subscribed. $message = $e->getMessage(); if ($message !== $email . ' is already subscribed to the list.') { CRM_Core_Session::setStatus($message); } } break; case "unsubscribe": // https://apidocs.mailchimp.com/api/2.0/lists/unsubscribe.php try { $result = $list->unsubscribe($listID, array('email' => $email), $delete_member = false, $send_goodbye = false, $send_notify = false); } catch (Exception $e) { CRM_Core_Session::setStatus($e->getMessage()); } break; } CRM_Mailchimp_Utils::checkDebug('End-CRM_Mailchimp_Utils subscribeOrUnsubsribeToMailchimpList $groupDetails', $groupDetails); CRM_Mailchimp_Utils::checkDebug('End-CRM_Mailchimp_Utils subscribeOrUnsubsribeToMailchimpList $contactID', $contactID); CRM_Mailchimp_Utils::checkDebug('End-CRM_Mailchimp_Utils subscribeOrUnsubsribeToMailchimpList $action', $action); }
/** * Batch update Mailchimp with new contacts that need to be subscribed, or have changed data. * * This also does the clean-up tasks of removing the temporary tables. */ static function syncPushAdd(CRM_Queue_TaskContext $ctx, $listID) { CRM_Mailchimp_Utils::checkDebug('Start-CRM_Mailchimp_Form_Sync syncPushAdd $ctx= ', $ctx); CRM_Mailchimp_Utils::checkDebug('Start-CRM_Mailchimp_Form_Sync syncPushAdd $listID= ', $listID); // @todo take the remaining details from tmp_mailchimp_push_c // and construct a batchUpdate (do they need to be batched into 1000s? I can't recal). $dao = CRM_Core_DAO::executeQuery("SELECT * FROM tmp_mailchimp_push_c;"); $stats = array(); // Loop the $dao object to make a list of emails to subscribe/update $batch = array(); while ($dao->fetch()) { $merge = array('FNAME' => $dao->first_name, 'LNAME' => $dao->last_name); // set the groupings. $groupings = unserialize($dao->groupings); // this is a array(groupingid=>array(groupid=>bool membership)) $merge_groups = array(); foreach ($groupings as $grouping_id => $groups) { // CRM_Mailchimp_Utils::checkDebug('get groups $groups= ', $groups); $merge_groups[$grouping_id] = array('id' => $grouping_id, 'groups' => array()); CRM_Mailchimp_Utils::checkDebug('End-CRM_Mailchimp_Form_Sync $merge_groups[$grouping_id]', $merge_groups[$grouping_id]); foreach ($groups as $group_id => $is_member) { if ($is_member) { $merge_groups[$grouping_id]['groups'][] = CRM_Mailchimp_Utils::getMCGroupName($listID, $grouping_id, $group_id); } } } // remove the significant array indexes, in case Mailchimp cares. $merge['groupings'] = array_values($merge_groups); $batch[] = array('email' => array('email' => $dao->email), 'email_type' => 'html', 'merge_vars' => $merge); $stats[$listID]['added']++; } if (!$batch) { // Nothing to do return CRM_Queue_Task::TASK_SUCCESS; } // Log the batch subscribe details CRM_Core_Error::debug_var('Mailchimp syncPushAdd batchUnsubscribe $listID= ', $listID); CRM_Core_Error::debug_var('Mailchimp syncPushAdd batchSubscribe $batch= ', $batch); // Send Mailchimp Lists API Call. // http://apidocs.mailchimp.com/api/2.0/lists/batch-subscribe.php $list = new Mailchimp_Lists(CRM_Mailchimp_Utils::mailchimp()); $result = $list->batchSubscribe($listID, $batch, $double_optin = FALSE, $update = TRUE, $replace_interests = TRUE); // debug: file_put_contents(DRUPAL_ROOT . '/logs/' . date('Y-m-d-His') . '-MC-push.log', print_r($result,1)); CRM_Mailchimp_Utils::checkDebug('CRM_Mailchimp_Form_Sync syncPushAdd $result= ', $result); $get_GroupId = CRM_Mailchimp_Utils::getGroupsToSync(array(), $listID); CRM_Mailchimp_Utils::checkDebug('$get_GroupId= ', $get_GroupId); // @todo check result (keys: error_count, add_count, update_count) $stats[$listID]['group_id'] = array_keys($get_GroupId); $stats[$listID]['error_count'] = $result['error_count']; $stats[$listID]['error_details'] = $result['errors']; static::updatePushStats($stats); // Finally, finish up by removing the two temporary tables CRM_Core_DAO::executeQuery("DROP TABLE tmp_mailchimp_push_m;"); CRM_Core_DAO::executeQuery("DROP TABLE tmp_mailchimp_push_c;"); CRM_Mailchimp_Utils::checkDebug('get groupsss $grouping_id= ', $grouping_id); CRM_Mailchimp_Utils::checkDebug('get groupssss $group_id= ', $group_id); CRM_Mailchimp_Utils::checkDebug('End-CRM_Mailchimp_Form_Sync syncPushAdd $result= ', $result); CRM_Mailchimp_Utils::checkDebug('End-CRM_Mailchimp_Form_Sync syncPushAdd $ctx= ', $ctx); CRM_Mailchimp_Utils::checkDebug('End-CRM_Mailchimp_Form_Sync syncPushAdd $listID= ', $listID); return CRM_Queue_Task::TASK_SUCCESS; }