From 363464ade813fcdb701c1afe699c1b8478504f1d Mon Sep 17 00:00:00 2001 From: Kajan Date: Wed, 18 Nov 2015 13:07:08 +0000 Subject: [PATCH] Resubsribe if address is unsubscribed in mailchimp and added extra msg to avoid server crash --- CRM/Mailchimp/Form/Setting.php | 12 +++++++++++- CRM/Mailchimp/Form/Sync.php | 17 +++++++++++++++-- api/v3/Mailchimp.php | 8 ++++++++ info.xml | 4 ++-- 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/CRM/Mailchimp/Form/Setting.php b/CRM/Mailchimp/Form/Setting.php index 51c0c4f..d8604b0 100644 --- a/CRM/Mailchimp/Form/Setting.php +++ b/CRM/Mailchimp/Form/Setting.php @@ -63,7 +63,17 @@ public function buildQuickForm() { 'name' => ts('Save & Test'), ), ); - + $groups = CRM_Mailchimp_Utils::getGroupsToSync(array(), null, $membership_only = TRUE); + foreach ($groups as $group_id => $details) { + $list = new Mailchimp_Lists(CRM_Mailchimp_Utils::mailchimp()); + $webhookoutput = $list->webhooks($details['list_id']); + if($webhookoutput[0]['sources']['api'] == 1) { + CRM_Mailchimp_Utils::checkDebug('CRM_Mailchimp_Form_Setting - API is set in Webhook setting for listID', $details['list_id']); + $listID = $details['list_id']; + CRM_Core_Session::setStatus(ts('API is set in Webhook setting for listID %1', array(1 => $listID)), ts('Error'), 'error'); + break; + } + } // Add the Buttons. $this->addButtons($buttons); } diff --git a/CRM/Mailchimp/Form/Sync.php b/CRM/Mailchimp/Form/Sync.php index 424b038..bc89e29 100644 --- a/CRM/Mailchimp/Form/Sync.php +++ b/CRM/Mailchimp/Form/Sync.php @@ -312,7 +312,7 @@ static function syncPushAdd(CRM_Queue_TaskContext $ctx, $listID) { // 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); + $batch[$dao->email] = array('email' => array('email' => $dao->email), 'email_type' => 'html', 'merge_vars' => $merge); $stats[$listID]['added']++; } if (!$batch) { @@ -333,7 +333,20 @@ static function syncPushAdd(CRM_Queue_TaskContext $ctx, $listID) { $batchResult[$id] = $list->batchSubscribe( $listID, $batch, $double_optin=FALSE, $update=TRUE, $replace_interests=TRUE); $result['error_count'] += $batchResult[$id]['error_count']; // @TODO: updating stats for errors, create sql error "Data too long for column 'value'" (for long array) - $result['errors'] = array_merge($result['errors'], $batchResult[$id]['errors']); + if ($batchResult[$id]['errors']) { + foreach ($batchResult[$id]['errors'] as $errorDetails){ + // Resubscribe if email address is reported as unsubscribed + // they want to resubscribe. + if ($errorDetails['code'] == 212) { + $unsubscribedEmail = $errorDetails['email']['email']; + $list->subscribe( $listID, $batch[$unsubscribedEmail]['email'], $batch[$unsubscribedEmail]['merge_vars'], $batch[$unsubscribedEmail]['email_type'], FALSE, TRUE, FALSE, FALSE); + $result['error_count'] -= 1; + } + else { + $result['errors'][] = $errorDetails; + } + } + } CRM_Mailchimp_Utils::checkDebug('CRM_Mailchimp_Form_Sync syncPushAdd $result= ', $batchResult[$id]); } // debug: file_put_contents(DRUPAL_ROOT . '/logs/' . date('Y-m-d-His') . '-MC-push.log', print_r($result,1)); diff --git a/api/v3/Mailchimp.php b/api/v3/Mailchimp.php index d90da89..dd0bce0 100644 --- a/api/v3/Mailchimp.php +++ b/api/v3/Mailchimp.php @@ -178,6 +178,14 @@ function civicrm_api3_mailchimp_getcivicrmgroupmailchimpsettings($params) { * @throws API_Exception */ function civicrm_api3_mailchimp_sync($params) { + $groups = CRM_Mailchimp_Utils::getGroupsToSync(array(), null, $membership_only = TRUE); + foreach ($groups as $group_id => $details) { + $list = new Mailchimp_Lists(CRM_Mailchimp_Utils::mailchimp()); + $webhookoutput = $list->webhooks($details['list_id']); + if($webhookoutput[0]['sources']['api'] == 1) { + return civicrm_api3_create_error('civicrm_api3_mailchimp_sync - API is set in Webhook setting for listID '.$details['list_id'].' Please uncheck API' ); + } + } $result = $pullResult = array(); // Do pull first from mailchimp to CiviCRM diff --git a/info.xml b/info.xml index 33ab218..9dd77cd 100644 --- a/info.xml +++ b/info.xml @@ -8,8 +8,8 @@ Deepak Srivastava, Kajan, Parvez Saleh and Rich Lott (Artful Robot) support@vedaconsulting.co.uk - 2015-10-20 - 1.8.3 + 2015-11-18 + 1.8.4 stable 4.4