/** * 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; }