Skip to content

Commit

Permalink
Merge pull request #168 from Kajakaran/webhook_api
Browse files Browse the repository at this point in the history
Resubscribe if address is unsubscribed in mailchimp and added extra condition to avoid server crash
  • Loading branch information
veda-consulting committed Nov 18, 2015
2 parents de56e62 + 363464a commit 9be2ef6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
12 changes: 11 additions & 1 deletion CRM/Mailchimp/Form/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
17 changes: 15 additions & 2 deletions CRM/Mailchimp/Form/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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));
Expand Down
8 changes: 8 additions & 0 deletions api/v3/Mailchimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<author>Deepak Srivastava, Kajan, Parvez Saleh and Rich Lott (Artful Robot)</author>
<email>[email protected]</email>
</maintainer>
<releaseDate>2015-10-20</releaseDate>
<version>1.8.3</version>
<releaseDate>2015-11-18</releaseDate>
<version>1.8.4</version>
<develStage>stable</develStage>
<compatibility>
<ver>4.4</ver>
Expand Down

0 comments on commit 9be2ef6

Please sign in to comment.