Created
November 14, 2014 14:30
-
-
Save chadhutchins/15269c14d82b8bd4acca to your computer and use it in GitHub Desktop.
LoveTEFL SugarChimp Webhook Receiver Changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
echo "SugarChimp Webhook Receiver is setup properly!"; | |
require_once('modules/SugarChimp/includes/classes/SugarChimp/Helper.php'); | |
require_once('modules/SugarChimp/includes/classes/SugarChimp/FieldMap.php'); | |
require_once('modules/SugarChimp/includes/classes/SugarChimp/Activity.php'); | |
// could probably use a class to route the webhook stuff below | |
// but it's not really necessary at this point | |
// the webhook implementations are pretty straightforward | |
class Mailchimp_Webhooks_Receiver {} | |
$type = $_POST['type']; | |
$data = $_POST['data']; | |
// lookup target list by provided mailchimp list id $data['list_id'] | |
if (empty($data['list_id'])) | |
{ | |
SugarChimp_Helper::log('error',"Mailchimp_Webhooks_Receiver cannot process webhook, no list id is provided {$type} ".print_r($data,true)); | |
return; | |
} | |
global $current_user; | |
$current_user = BeanFactory::getBean('Users'); | |
$current_user->getSystemUser(); | |
$target_list = BeanFactory::newBean('ProspectLists'); | |
$target_list->retrieve_by_string_fields(array('mailchimp_list_name_c' => $data['list_id'])); | |
if (empty($target_list) || empty($target_list->id)) | |
{ | |
SugarChimp_Helper::log('warning',"Mailchimp_Webhooks_Receiver cannot process webhook because a related list was not found {$type} ".print_r($data,true)); | |
return; | |
} | |
$default_module = $target_list->mailchimp_default_module_c; | |
if (empty($default_module)) | |
{ | |
// set to default, default module | |
$default_module = SugarChimp_FieldMap::get_default_module(); | |
} | |
else | |
{ | |
// make sure it's in supported modules array, if not set to default, default module | |
$supported_modules = SugarChimp_FieldMap::get_supported_modules(); | |
if (!in_array($default_module,$supported_modules) === true) | |
{ | |
// if it's not, set it to the default | |
$default_module = SugarChimp_FieldMap::get_default_module(); | |
} | |
} | |
switch ($type) | |
{ | |
/* | |
case "campaign": | |
SugarChimp_Helper::log('debug','Mailchimp_Webhooks_Receiver::campaign start'); | |
// the method fires when a campaign has been successfully sent | |
// we need to fire the initial campaign import job | |
if (empty($data['id'])) | |
{ | |
// campaign id is required | |
SugarChimp_Helper::log('error','Mailchimp_Webhooks_Receiver::campaign id (campaign id) is required, but it was empty'); | |
break; | |
} | |
if (empty($data['status'])) | |
{ | |
// status is required | |
SugarChimp_Helper::log('error','Mailchimp_Webhooks_Receiver::campaign status is required, but it was empty'); | |
break; | |
} | |
if (empty($data['list_id'])) | |
{ | |
// list_id is required | |
SugarChimp_Helper::log('error','Mailchimp_Webhooks_Receiver::list id is required, but it was empty'); | |
break; | |
} | |
$status = $data['status']; | |
$mailchimp_campaign_id = $data['id']; | |
$mailchimp_list_id = $data['list_id']; | |
// add it to the tracking table | |
// the activity data will be downloaded once it's turn is up | |
// this is governed by the MailchimpActivityToSugarCRM | |
SugarChimp_Activity::track_campaign_activity($mailchimp_list_id,$mailchimp_campaign_id, date("Y-m-d H:i:s"),false,true,false); | |
break; | |
*/ | |
/* | |
case "subscribe": | |
SugarChimp_Helper::log('debug','Mailchimp_Webhooks_Receiver::subscribe start'); | |
// this method runs when a person subscribes to a list in mailchimp | |
// sugarchimp will try to find existing contacts in CRM or create it based on the default_module | |
// it will then update the records with the data provided from mailchimp | |
// and lastly it will add the records to the target list | |
// if email already exists in Sugar, make sure it is marked as optin and valid | |
SugarChimp_Helper::mark_email_valid($data['email']); | |
SugarChimp_Helper::mark_email_optin($data['email']); | |
// attempt to lookup crm record(s) with $data['email'] | |
$beans = SugarChimp_Helper::find_beans_by_email($data['email']); | |
// if no beans, nothing matches on the sugar side, so create the record based on target list default modue | |
if (empty($beans)) | |
{ | |
$beans = array(); | |
$beans []= SugarChimp_Helper::get_empty_bean($default_module); | |
} | |
// disable logic hooks for the beans | |
SugarChimp_Helper::disable_logic_hooks($beans); | |
SugarChimp_Helper::disable_logic_hooks(array($target_list)); | |
// create/update new bean with data from mailchimp | |
$result = SugarChimp_Helper::update_beans_from_mailchimp($data['list_id'],$beans,$data['merges']); | |
if ($result !== true) | |
{ | |
SugarChimp_Helper::log('error','Mailchimp_Webhooks_Receiver::subscribe update_beans_from_mailchimp failed'); | |
break; | |
} | |
// relate the record to the target list | |
$result = SugarChimp_Helper::add_beans_to_list($target_list,$beans); | |
if ($result !== true) | |
{ | |
SugarChimp_Helper::log('error','Mailchimp_Webhooks_Receiver::subscribe update_beans_from_mailchimp failed'); | |
break; | |
} | |
// enable logic hooks for the beans | |
SugarChimp_Helper::enable_logic_hooks($beans); | |
SugarChimp_Helper::enable_logic_hooks(array($target_list)); | |
SugarChimp_Helper::log('debug','Mailchimp_Webhooks_Receiver::subscribe finish'); | |
break; | |
*/ | |
case "unsubscribe": | |
global $db; | |
SugarChimp_Helper::log('debug','Mailchimp_Webhooks_Receiver::unsubscribe start'); | |
// this method runs when a person unsubscribes from a list in mailchimp | |
// sugarchimp will try to find existing contacts in CRM | |
// if they exist, it will then remove those beans from the list | |
// attempt to lookup crm record with $data['email'] | |
$beans = SugarChimp_Helper::find_beans_by_email($data['email']); | |
$optout_email = true; | |
if (!empty($data['reason']) && $data['reason']!='manual') | |
{ | |
// flag to later mark the email as invalid instead of opted out | |
$optout_email = false; | |
} | |
// if we found beans, remove them from target list | |
if (!empty($beans)) | |
{ | |
// disable logic hooks for the beans and target list | |
SugarChimp_Helper::disable_logic_hooks($beans); | |
SugarChimp_Helper::disable_logic_hooks(array($target_list)); | |
// get all synced lists for beans | |
$lists = SugarChimp_Helper::get_lists_for_beans($beans); | |
// check the globaloptout setting to see what to do | |
$globaloptout = SugarChimp_Setting::retrieve('globaloptout'); | |
if (empty($globaloptout)) | |
{ | |
// global opt out is disabled and set to false, this is default behavior | |
SugarChimp_Helper::log('debug','Mailchimp_Webhooks_Receiver::optout global opt out is disabled'); | |
// only opt out email in sugar if email address is synced to exactly 1 list | |
if (count($lists)===1) | |
{ | |
if ($optout_email === true) | |
{ | |
SugarChimp_Helper::log('error','Mailchimp_Webhooks_Receiver::optout attempting to optout '.$data['email']); | |
$marked = SugarChimp_Helper::mark_email_optout($data['email']); | |
} | |
else | |
{ | |
SugarChimp_Helper::log('error','Mailchimp_Webhooks_Receiver::optout attempting to mark invalid '.$data['email']); | |
$marked = SugarChimp_Helper::mark_email_invalid($data['email']); | |
} | |
if ($marked !== true) | |
{ | |
SugarChimp_Helper::log('error','Mailchimp_Webhooks_Receiver::optout '.$data['email'].' failed to be opted out. marked: '.print_r($marked,true)); | |
} | |
else | |
{ | |
SugarChimp_Helper::log('debug','Mailchimp_Webhooks_Receiver::optout '.$data['email'].' was opted out.'); | |
} | |
} | |
} | |
else | |
{ | |
// global opt out is enabled and set to true | |
SugarChimp_Helper::log('debug','Mailchimp_Webhooks_Receiver::optout global opt out is enabled'); | |
// opt out the email address in Sugar | |
if ($optout_email === true) | |
{ | |
SugarChimp_Helper::log('error','Mailchimp_Webhooks_Receiver::optout attempting to optout '.$data['email']); | |
$marked = SugarChimp_Helper::mark_email_optout($data['email']); | |
} | |
else | |
{ | |
SugarChimp_Helper::log('error','Mailchimp_Webhooks_Receiver::optout attempting to mark invalid '.$data['email']); | |
$marked = SugarChimp_Helper::mark_email_invalid($data['email']); | |
} | |
if ($marked !== true) | |
{ | |
SugarChimp_Helper::log('error','Mailchimp_Webhooks_Receiver::optout '.$data['email'].' failed to be opted out. marked: '.print_r($marked,true)); | |
} | |
else | |
{ | |
SugarChimp_Helper::log('debug','Mailchimp_Webhooks_Receiver::optout '.$data['email'].' was opted out.'); | |
} | |
// remove from all related synced lists in Sugar, this will trigger removal from MC lists as well | |
if (count($lists)>1) | |
{ | |
// remove the original target list from the list, the beans will be removed from it later | |
unset($lists[$target_list->id]); | |
// we want the logic hooks to fire for the beans when they are removed from the list | |
SugarChimp_Helper::enable_logic_hooks($beans); | |
foreach ($lists as $list) | |
{ | |
// remove beans from the list | |
SugarChimp_Helper::remove_beans_from_list($list,$beans); | |
} | |
// re-disable the hooks | |
SugarChimp_Helper::disable_logic_hooks($beans); | |
} | |
} | |
// remove beans from the list | |
SugarChimp_Helper::remove_beans_from_list($target_list,$beans); | |
// enable logic hooks for the beans and target list | |
SugarChimp_Helper::enable_logic_hooks($beans); | |
SugarChimp_Helper::enable_logic_hooks(array($target_list)); | |
} | |
SugarChimp_Helper::log('debug','Mailchimp_Webhooks_Receiver::unsubscribe finish'); | |
break; | |
/* | |
case "profile": | |
SugarChimp_Helper::log('debug','Mailchimp_Webhooks_Receiver::profile start'); | |
// this method runs when a person updates their profile in mailchimp | |
// sugarchimp will try to find existing contacts in CRM or create it based on the default_module | |
// it will then update the records with the data provided from mailchimp | |
// and lastly it will add the records to the target list | |
// ideally if they're updating, the creating new records and attaching them to target lists isn't necessary | |
// but this is an extra step by going ahead and correcting the out of sync data by doing this here | |
// attempt to lookup crm record(s) with $data['email'] | |
$beans = SugarChimp_Helper::find_beans_by_email($data['email']); | |
// if no beans, nothing matches on the sugar side, so create the record based on target list default modue | |
if (empty($beans)) | |
{ | |
$beans = array(); | |
$beans []= SugarChimp_Helper::get_empty_bean($default_module); | |
} | |
// disable logic hooks for the beans | |
SugarChimp_Helper::disable_logic_hooks($beans); | |
SugarChimp_Helper::disable_logic_hooks(array($target_list)); | |
// create/update new bean with data from mailchimp | |
$result = SugarChimp_Helper::update_beans_from_mailchimp($data['list_id'],$beans,$data['merges']); | |
if ($result !== true) | |
{ | |
SugarChimp_Helper::log('error','Mailchimp_Webhooks_Receiver::profile update_beans_from_mailchimp failed'); | |
break; | |
} | |
// relate the record to the target list | |
$result = SugarChimp_Helper::add_beans_to_list($target_list,$beans); | |
if ($result !== true) | |
{ | |
SugarChimp_Helper::log('error','Mailchimp_Webhooks_Receiver::profile update_beans_from_mailchimp failed'); | |
break; | |
} | |
// enable logic hooks for the beans | |
SugarChimp_Helper::enable_logic_hooks($beans); | |
SugarChimp_Helper::enable_logic_hooks(array($target_list)); | |
SugarChimp_Helper::log('debug','Mailchimp_Webhooks_Receiver::profile finish'); | |
break; | |
*/ | |
/* | |
case "upemail": | |
SugarChimp_Helper::log('debug','Mailchimp_Webhooks_Receiver::upemail start'); | |
// this method runs when a person updates their email address in mailchimp | |
// sugarchimp will try to find existing contacts in CRM or create it based on the default_module | |
// it will then update the records with the data provided from mailchimp | |
// and lastly it will add the records to the target list | |
// attempt to lookup crm record(s) with $data['old_email'] | |
$beans = SugarChimp_Helper::find_beans_by_email($data['old_email']); | |
// if no beans, nothing matches on the sugar side, so create the record based on target list default modue | |
if (empty($beans)) | |
{ | |
$beans = array(); | |
$beans []= SugarChimp_Helper::get_empty_bean($default_module); | |
} | |
// create/update new bean with data from mailchimp | |
$result = SugarChimp_Helper::update_beans_from_mailchimp($data['list_id'],$beans,$data['merges']); | |
if ($result !== true) | |
{ | |
SugarChimp_Helper::log('error','Mailchimp_Webhooks_Receiver::upemail update_beans_from_mailchimp failed'); | |
break; | |
} | |
// relate the record to the target list | |
$result = SugarChimp_Helper::add_beans_to_list($target_list,$beans); | |
if ($result !== true) | |
{ | |
SugarChimp_Helper::log('error','Mailchimp_Webhooks_Receiver::upemail update_beans_from_mailchimp failed'); | |
break; | |
} | |
// todo: right now it just does a standard update and sets the new email address to email1. | |
// to support custom modules and email fields check the field map for fields of type 'email' | |
SugarChimp_Helper::log('debug','Mailchimp_Webhooks_Receiver::upemail finish'); | |
break; | |
*/ | |
case "cleaned": | |
global $db; | |
SugarChimp_Helper::log('debug','Mailchimp_Webhooks_Receiver::clean start'); | |
// this method runs when a person is cleaned from a list in mailchimp | |
// sugarchimp will try to find existing contacts in CRM | |
// if they exist, it will then remove those beans from the list | |
// attempt to lookup crm record with $data['email'] | |
$beans = SugarChimp_Helper::find_beans_by_email($data['email']); | |
// if we found beans, remove them from target list | |
if (!empty($beans)) | |
{ | |
// disable logic hooks for the beans and target list | |
SugarChimp_Helper::disable_logic_hooks($beans); | |
SugarChimp_Helper::disable_logic_hooks(array($target_list)); | |
// get all synced lists for beans | |
$lists = SugarChimp_Helper::get_lists_for_beans($beans); | |
// check the globaloptout setting to see what to do | |
$globaloptout = SugarChimp_Setting::retrieve('globaloptout'); | |
if (empty($globaloptout)) | |
{ | |
// global opt out is disabled and set to false, this is default behavior | |
SugarChimp_Helper::log('debug','Mailchimp_Webhooks_Receiver::clean global opt out is disabled'); | |
// only opt out email in sugar if email address is synced to exactly 1 list | |
if (count($lists)===1) | |
{ | |
SugarChimp_Helper::log('error','Mailchimp_Webhooks_Receiver::clean attempting to mark invalid '.$data['email']); | |
$marked = SugarChimp_Helper::mark_email_invalid($data['email']); | |
if ($marked !== true) | |
{ | |
SugarChimp_Helper::log('error','Mailchimp_Webhooks_Receiver::clean '.$data['email'].' failed to be marked invalid. marked: '.print_r($marked,true)); | |
} | |
else | |
{ | |
SugarChimp_Helper::log('debug','Mailchimp_Webhooks_Receiver::clean '.$data['email'].' was marked invalid.'); | |
} | |
} | |
} | |
else | |
{ | |
// global opt out is enabled and set to true | |
SugarChimp_Helper::log('debug','Mailchimp_Webhooks_Receiver::clean global opt out is enabled'); | |
// opt out the email address in Sugar | |
SugarChimp_Helper::log('error','Mailchimp_Webhooks_Receiver::clean attempting to mark invalid '.$data['email']); | |
$marked = SugarChimp_Helper::mark_email_invalid($data['email']); | |
if ($marked !== true) | |
{ | |
SugarChimp_Helper::log('error','Mailchimp_Webhooks_Receiver::clean '.$data['email'].' failed to be marked invalid. marked: '.print_r($marked,true)); | |
} | |
else | |
{ | |
SugarChimp_Helper::log('debug','Mailchimp_Webhooks_Receiver::clean '.$data['email'].' was marked invalid.'); | |
} | |
// remove from all related synced lists in Sugar, this will trigger removal from MC lists as well | |
if (count($lists)>1) | |
{ | |
// remove the original target list from the list, the beans will be removed from it later | |
unset($lists[$target_list->id]); | |
// we want the logic hooks to fire for the beans when they are removed from the list | |
SugarChimp_Helper::enable_logic_hooks($beans); | |
foreach ($lists as $list) | |
{ | |
// remove beans from the list | |
SugarChimp_Helper::remove_beans_from_list($list,$beans); | |
} | |
// re-disable the hooks | |
SugarChimp_Helper::disable_logic_hooks($beans); | |
} | |
} | |
// remove beans from the list | |
SugarChimp_Helper::remove_beans_from_list($target_list,$beans); | |
// enable logic hooks for the beans and target list | |
SugarChimp_Helper::enable_logic_hooks($beans); | |
SugarChimp_Helper::enable_logic_hooks(array($target_list)); | |
} | |
SugarChimp_Helper::log('debug','Mailchimp_Webhooks_Receiver::clean finish'); | |
break; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment