Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- /*
- * THIS CRON:
- *
- * 1. Syncs WHMCS/BuzzStream subscribers (non-clients) with these relevant Marketo lists:
- *
- * 1024: Subscribers | Free (New)
- * 1040: Subscribers | Paying
- * 1041: Subscribers | Former Paying
- *
- * The forms will aready put people on the first two lists, so this should be pretty good to start.
- *
- */
- // Include libraries
- require_once("header.for.crons.php"); // For errors and printing
- require_once($models.'class.core.php');
- require_once($models.'class.whmcs.php');
- require_once($models.'class.marketo.php'); // Kind of a hack, $marketo object defined within
- // Declare objects
- $whmcs = new NCWHMCS;
- $core = new NCCore;
- /*
- * Phase 1: Get contacts
- */
- echo "<pre><H2>GATHERING CONTACTS TO SYNC</H2>";
- echo "<pre><H3>GATHERING CLIENTS FROM WHMCS</H3>";
- // Mega array of contacts.
- $contactArr = array();
- // Add paying and former to the mega array.
- $allSubscriberServices = $whmcs->getChecklistSubscribers();
- $globalContactIncr = 0;
- foreach($allSubscriberServices AS $incr => $subscriber) {
- $globalContactIncr++;
- $contactArr[$globalContactIncr]["firstname"] = $subscriber["firstname"];
- $contactArr[$globalContactIncr]["lastname"] = $subscriber["lastname"];
- $contactArr[$globalContactIncr]["email"] = strtolower($subscriber["email"]);
- if($subscriber["domainstatus"] == "Active")
- $contactArr[$globalContactIncr]["status"] = "Paying";
- else
- $contactArr[$globalContactIncr]["status"] = "Former";
- }
- // Add free access to the mega array.
- $allFreeSubscribers = $whmcs->getChecklistFreeUsers();
- foreach($allFreeSubscribers AS $incr => $subscriber) {
- if($core->search2dArrayForValue($contactArr,$subscriber["email"]) < 1) {
- $globalContactIncr++;
- $contactArr[$globalContactIncr]["firstname"] = $subscriber["firstname"];
- $contactArr[$globalContactIncr]["lastname"] = $subscriber["lastname"];
- $contactArr[$globalContactIncr]["email"] = strtolower($subscriber["email"]);
- $contactArr[$globalContactIncr]["status"] = "Free";
- }
- }
- /*
- * Phase 2: Sync to Marketo
- */
- echo "<h2>SYNCING IT ALL TO MARKETO</H2>";
- echo "<h3>SCRUBING CLIENT DATA</H3>";
- // Step 1: Get existing Marketo Clients Lists to avoid unnecessary requests
- $freeMarketo = accessProtected($marketo->getLeadsByList("1024"),"data"); // Get free members. Paginates to 10 without extra vars.
- $payingMarketo = accessProtected($marketo->getLeadsByList("1040"),"data"); // Get paying members. Paginates to 10 without extra vars.
- $formerMarketo = accessProtected($marketo->getLeadsByList("1041"),"data"); // Get former members. Paginates to 10 without extra vars.
- // Step 2: Scrub our data
- // 2a: Free
- if(isset($freeMarketo) && isset($freeMarketo["result"])) {
- foreach($freeMarketo["result"] AS $incr => $alreadyMarkedFree) {
- $matchId = $core->search2dArrayForValue($contactArr,$alreadyMarkedFree["email"]);
- if($matchId > 0 && $contactArr[$matchId]["status"] == "Free") {
- echo $contactArr[$matchId]["email"] . " is already on the free list (".$contactArr[$matchId]["status"].").<br>";
- unset($contactArr[$matchId]);
- } elseif($matchId > 0) {
- $removal = $marketo->removeLeadsFromList("1024",array($alreadyMarkedFree["id"])); // Remove lead from wrong list
- }
- }
- }
- // 2b: Paying
- if(isset($payingMarketo) && isset($payingMarketo["result"])) {
- foreach($payingMarketo["result"] AS $incr => $alreadyMarkedPaying) {
- $matchId = $core->search2dArrayForValue($contactArr,$alreadyMarkedPaying["email"]);
- if($matchId > 0 && $contactArr[$matchId]["status"] == "Paying") {
- echo $contactArr[$matchId]["email"] . " is already on the paying list (".$contactArr[$matchId]["status"].").<br>";
- unset($contactArr[$matchId]);
- } elseif($matchId > 0) {
- $removal = $marketo->removeLeadsFromList("1040",array($alreadyMarkedPaying["id"])); // Remove lead from wrong list
- }
- }
- }
- // 2c: Former
- if(isset($formerMarketo) && isset($formerMarketo["result"])) {
- foreach($formerMarketo["result"] AS $incr => $alreadyMarkedFormer) {
- $matchId = $core->search2dArrayForValue($contactArr,$alreadyMarkedFormer["email"]);
- if($matchId > 0 && $contactArr[$matchId]["status"] == "Former") {
- echo $contactArr[$matchId]["email"] . " is already on the former list (".$contactArr[$matchId]["status"].").<br>";
- unset($contactArr[$matchId]);
- } elseif($matchId > 0) {
- $removal = $marketo->removeLeadsFromList("1041",array($alreadyMarkedFormer["id"])); // Remove lead from wrong list
- }
- }
- }
- echo "<h3>ADDING/ORGANIZING REMAINING CLIENTS IN MARKETO</H3>";
- // Step 3: Add people to the right lists.
- $groupEmailSignals = array("abuse","billing","support","sales","info","bill.com","bills","management","accounting","accounts");
- $counter = 0;
- foreach($contactArr AS $incr => $contactToSync) {
- $haltTheAdd = 0;
- if(!array_key_exists("firstname",$contactToSync)) $contactToSync["firstname"] = "";
- if(!array_key_exists("lastname",$contactToSync)) $contactToSync["lastname"] = "";
- // Skip crappy group email addresses.
- foreach($groupEmailSignals AS $incr2 => $signal) {
- if(stristr($contactToSync["email"],$signal)) $haltTheAdd = 1;
- }
- if($haltTheAdd == 0) {
- $counter++; // let's be nice to their rate limiting
- if($counter < 50) {
- // Determine the correct Marketo list ID
- if(trim($contactToSync["status"]) == "Free") $listId = 1024;
- elseif(trim($contactToSync["status"]) == "Paying") $listId = 1040;
- else $listId = 1041;
- $result = accessProtected($marketo->getLeadByFilterType("email",trim($contactToSync["email"])),"data");
- if(array_key_exists("result",$result) && $result["success"] == 1 && count($result["result"]) > 0) { // Already in Marketo - add to list
- // Add to list only - already in Marketo. If they're on the wrong list currently, Smart Campaigns will fix that.
- $result = $marketo->addLeadsToList($listId,$result["result"][0]["id"]);
- echo "Put " . $contactToSync["email"] . " on the " . $contactToSync["status"] . " list (" .$listId .") in Marketo!<br>";
- } elseif(isset($result["errors"])) {
- echo "<b>Error ". $result["errors"][0]["code"] .": </b>" .$result["errors"][0]["message"] ."<br>";
- } else {
- // Add to Marketo - then add to list
- if(isset($contactToSync["firstname"]) && isset($contactToSync["lastname"])) $result = accessProtected($marketo->createLeads(array(array("email"=>$contactToSync["email"],"firstName"=>$contactToSync["firstname"],"lastName"=>$contactToSync["lastname"]))),"data");
- else $result = accessProtected($marketo->createLeads(array(array("email"=>$contactToSync["email"]))),"data");
- // Then add to list
- if($result["success"] == 1 && array_key_exists("id",$result["result"][0])) $result = $marketo->addLeadsToList($listId,$result["result"][0]["id"]);
- echo "Added " . $contactToSync["email"] . " to Marketo and placed on the " . $contactToSync["status"] . " list (" .$listId .")!<br>";
- }
- } else {
- echo "Rate limit hit.<br>";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement