public function onAfterWrite() { parent::onAfterWrite(); if ($this->isChanged("ID") && $this->getFirstWrite() && $this->getSyncMailChimp()) { $apikey = SiteConfig::current_site_config()->getMCAPIKey(); $api = new MCAPI($apikey); $list = $this->getComponent("MCList"); if (!empty($list)) { // Limitited to 50 Bytes (Hopefully 50 Chars) $SegmentTitle = substr($this->Title, 0, 45); $api->listStaticSegmentAdd($list->ListID, $SegmentTitle); if ($api->errorCode) { SS_Log::log("API Call Failed: listStaticSegmentAdd(); | Error Code = " . $api->errorCode . " | Error Message = " . $api->errorMessage, SS_Log::ERR); } else { SS_Log::log("API Call Success: listStaticSegmentAdd();", SS_Log::NOTICE); // Make Second Call To Return MailChimp Segment ID $segments = $api->listStaticSegments($list->ListID); foreach ($segments as $segment) { // Make Sure We Capture the WHOLE Event ID! $id_chars = strlen((string) $this->EventID); $id = substr($segment['name'], 6, $id_chars); if ($id == $this->EventID) { SS_Log::log("This Event ID = " . $this->EventID . ", Static Segment Named " . $segment['name'] . " Relates to Event ID " . $id, SS_Log::NOTICE); SS_Log::log("We Have a Match", SS_Log::NOTICE); $this->setField("MCListSegmentID", $segment['id']); $this->write(); break; } } } } } // END: if($this->isChanged("ID")) { $this->setFirstWrite(false); }