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; }