public function onAfterWrite()
 {
     parent::onAfterWrite();
     // This is to ensure this only fires once on each write
     if ($this->getFirstWrite()) {
         // Get Array of updated fields
         $UpdatedDataFields = $this->owner->getChangedFields(true, 2);
         // Get HasManyList of this Members MCSubscriptions
         $subs = $this->owner->getComponents("MCSubscriptions");
         // If the Member Has One or More Subscriptions
         if (is_object($subs) && $subs->exists()) {
             // Foreach of This Members Subscription Objects
             foreach ($subs as $sub) {
                 SS_Log::log("Subscription ID = " . $sub->ID, SS_Log::NOTICE);
                 // Get DataList of MC List Field Mappings (Excluding LastVisited) Which Are On The Member Class && In A MCList Which Concerns This Member (i.e. One They Are Subscribed To)
                 // (as if LastVisited is the ONLY updated field it just represents a site login, not an actual manual MC data field update)
                 $dl = new DataList("MCListField");
                 $mappings = $dl->where("\"OnClass\" = 'Member' AND \"MCListID\" = '" . $sub->MCListID . "' AND \"SyncDirection\" IN ('Export','Both') AND \"FieldName\" != 'LastVisited'");
                 // Foreach Mapping Record
                 foreach ($mappings as $mapping) {
                     SS_Log::log("Mapping Field Name = " . $mapping->FieldName, SS_Log::NOTICE);
                     // If The Member FieldName is One of the Updated Fields
                     if (isset($UpdatedDataFields[$mapping->FieldName])) {
                         // Mark the Subscription as Being Updated
                         SS_Log::log("\$UpdatedDataFields['" . $mapping->FieldName . "'] Is Set, doing \$sub->write();", SS_Log::NOTICE);
                         $sub->setSyncMailChimp($this->getSyncMailChimp());
                         // Set MCSubscriber Sync to MailChimp Based on This (Member) Sync State
                         $sub->setForceAdditionalWrite(true);
                         $sub->write();
                         break;
                     }
                 }
             }
         } else {
             if (isset($UpdatedDataFields["ID"])) {
                 // Creation Write
                 // Add New or Link Existing Subscription Records
                 $subs = MCSubscription::get()->where("\"Email\" = '" . $this->owner->Email . "'");
                 if (is_object($subs) && $subs->exists()) {
                     // Link Each Subscriber Object to this Member
                     foreach ($subs as $sub) {
                         $sub->setForceAdditionalWrite(true);
                         $this->owner->getComponents("MCSubscriptions")->add($sub);
                     }
                 } else {
                     // Get the Default MailChimp List To Relate The New Subscription To
                     $MCList = MCList::get()->sort("\"SortOrder\" ASC")->first();
                     if (is_object($MCList) && $MCList->exists()) {
                         // Create New Subscriber Object and Link to New Member
                         $sub = new MCSubscription();
                         $sub->setField("FirstName", $this->owner->FirstName);
                         $sub->setField("Surname", $this->owner->Surname);
                         $sub->setField("Email", $this->owner->Email);
                         $sub->setField("MemberID", $this->owner->ID);
                         $sub->setField("MCListID", $MCList->ID);
                         $sub->write();
                         // Link the New Subscriber Object to this Member
                         $this->owner->getComponents("MCSubscriptions")->add($sub);
                     } else {
                         SS_Log::log("No MCList object available to add new MCSubscriptions to when saving Member #" . $this->owner->ID, SS_Log::NOTICE);
                     }
                 }
             }
         }
     }
 }
 public function UpdateLists()
 {
     $api = new MCAPI($this->apikey);
     $retval = $api->lists();
     if ($api->errorCode) {
         SS_Log::log("Unable to load lists()! Error Code = " . $api->errorCode . " Error Msg = " . $api->errorMessage, SS_Log::ERR);
         return false;
     } else {
         foreach ($retval['data'] as $list) {
             // Get DataList of All MCLists
             $dl = MCList::get();
             $l = $dl->where("ListID = '" . $list['id'] . "'")->first();
             // If the Current Iterations List Object Does Not Yet Exist, Create It
             if (!is_object($l)) {
                 $l = new MCList();
                 $l->setField("ListID", $list['id']);
             }
             // Populate/Overwrite the List Data
             $l->setField("Name", $list['name']);
             $l->setField("WebID", $list['web_id']);
             $l->setField("Subscribed", $list['stats']['member_count']);
             $l->setField("Unsubscribed", $list['stats']['unsubscribe_count']);
             $l->setField("Cleaned", $list['stats']['cleaned_count']);
             $l->write();
             // Add/Delete any New/Removed Merge Tags
             // (Newly Added Merge Tags Will Need Linking/Relating to the Appropriate DB Field Name
             // via Admin -> Setting -> MC Lists -> List Field Relationships)
             $retval = $api->listMergeVars($l->ListID);
             if ($api->errorCode) {
                 SS_Log::log("Unable to load listMergeVars()! Code = " . $api->errorCode . " Msg = " . $api->errorMessage, SS_Log::ERR);
                 return false;
             } else {
                 $currTags = array();
                 foreach ($retval as $mergeTagData) {
                     $currTags[] = $mergeTag = $mergeTagData['tag'];
                     $listField = $l->getComponents("MCListFields", "\"MergeTag\" = '" . $mergeTag . "'")->first();
                     if (empty($listField)) {
                         $lf = new MCListField();
                         $lf->setField("MergeTag", $mergeTag);
                         $lf->write();
                         $l->getComponents("MCListFields")->add($lf);
                     }
                 }
                 // Create DataList of All Existing MC List Fields Which Are No Longer Present In MailChimp (Old Merge Tags) and Delete Them
                 $dl = new DataList("MCListField");
                 $filter = "\"MCListID\" = '" . $l->ID . "' AND \"MergeTag\" NOT IN (" . $this->arrayToCSV($currTags) . ")";
                 SS_Log::log("Cleaning up historical merge fields. MCListField delete filter = '" . $filter . "'", SS_Log::NOTICE);
                 $dl->removeByFilter($filter);
             }
         }
     }
     return true;
 }