<?php /** This Example shows how to update a List Member's information using the MCAPI.php class and do some basic error checking. **/ require_once 'inc/MCAPI.class.php'; require_once 'inc/config.inc.php'; //contains apikey $api = new MCAPI($apikey); $merge_vars = array("FNAME" => 'Richard', "LNAME" => 'Wright'); $retval = $api->listUpdateMember($listId, $my_email, $merge_vars, 'html', false); if ($api->errorCode) { echo "Unable to update member info!\n"; echo "\tCode=" . $api->errorCode . "\n"; echo "\tMsg=" . $api->errorMessage . "\n"; } else { echo "Returned: " . $retval . "\n"; }
public function onAfterWrite() { parent::onAfterWrite(); // Define Related MCList Object $list = $this->getComponent("MCList"); // Store The True Changed Fields Array On First Write if ($this->getWriteCount() == 1) { $cf = $this->getChangedFields(false, 2); // Define Change Fields Array $this->setOriginalChangedFields($cf); // Store Original (First Write) Change Fields For Use On Second Write } else { if ($this->getWriteCount() > 1 && $this->getSyncMailChimp() && !empty($this->Email) && !empty($list->ID)) { $apikey = SiteConfig::current_site_config()->getMCAPIKey(); $api = new MCAPI($apikey); // Define The Change Fields Array Which We Stored On The First Write Iteration For Use On The Second Write (When Components Are Written) if ($this->getWriteCount() == 2) { $cf = $this->getOriginalChangedFields(); } $Class = array(); $Class["MCSubscription"] = $this; $where = "\"MCListID\" = '" . $this->MCListID . "' AND \"SyncDirection\" IN ('Export','Both')"; if (!empty($this->getComponent("Member")->ID)) { $Class['Member'] = $this->getComponent("Member"); SS_Log::log("Sub ID " . $this->ID . " Has A Related Member Object..", SS_Log::NOTICE); } else { // If No Related Member Object Only Deal With Subscription Record Merge Data $where .= " AND \"OnClass\" = 'MCSubscription'"; SS_Log::log("Sub ID " . $this->ID . " Has No Related Member Object..", SS_Log::NOTICE); } $dl = new DataList("MCListField"); $mappings = $dl->where($where); $merge_vars = array(); foreach ($mappings as $map) { $merge_vars[$map->MergeTag] = $Class[$map->OnClass]->getField($map->FieldName); } if (isset($cf['ID'])) { // If Adding a New Subscription $result = $api->listSubscribe($list->ListID, $this->Email, $merge_vars, 'html', $this->DoubleOptIn); // If Successfully Added a New Subscription Make a Second Call to Return the MailChimp Member (Web) && Email ID's if (empty($api->errorCode)) { SS_Log::log("API Call Success: listSubscribe(" . $list->ListID . ", " . $this->Email . "); for Subscription ID " . $this->ID, SS_Log::NOTICE); $retval = $api->listMemberInfo($list->ListID, $this->Email); if (empty($api->errorCode)) { SS_Log::log("API Call Success: listMemberInfo(" . $list->ListID . ", " . $this->Email . "); for Subscription ID " . $this->ID, SS_Log::NOTICE); SS_Log::log("Calling Additional write() for Subscription ID " . $this->ID . " to Save MailChimp Created Data", SS_Log::NOTICE); $this->setField("MCMemberID", $retval['data'][0]['web_id']); // Set The MailChimp Member (Web) ID for this Member (Which Is Static - Used For MC - Site Imports) $this->setField("MCEmailID", $retval['data'][0]['id']); // Set The MailChimp Email ID for this Member (Which Updates When E-mail Updates - Used For Site - MC Exports) $this->setWriteCount(); $this->write(); } else { SS_Log::log("API Call Failed: listMemberInfo(" . $list->ListID . ", " . $this->Email . "); for Subscription ID " . $this->ID . " | Error Code = " . $api->errorCode . " | Error Message = " . $api->errorMessage, SS_Log::ERR); } } else { SS_Log::log("API Call Failed: listSubscribe(" . $list->ListID . ", " . $this->Email . "); for Subscription ID " . $this->ID . " | Error Code = " . $api->errorCode . " | Error Message = " . $api->errorMessage, SS_Log::ERR); } } else { if (isset($cf['Subscribed']) && !empty($this->Subscribed)) { // If Just Re-Subscribed (This Will Replace Previous MC Record With New One Rather Than Re-Subscribing Existing) $result = $api->listSubscribe($list->ListID, $this->Email, $merge_vars, 'html', $this->DoubleOptIn); // Must use E-mail For Re-Subscription as listSubscribe() assumes a new user (it actually deletes the existing 'un-subscribed' MailChimp record for the provided e-mail and re-adds the user) if (empty($api->errorCode)) { SS_Log::log("API Call Success: listSubscribe(" . $list->ListID . ", " . $this->Email . "); for Subscription ID " . $this->ID, SS_Log::NOTICE); } else { SS_Log::log("API Call Failed: listSubscribe(" . $list->ListID . ", " . $this->Email . "); for Subscription ID " . $this->ID . " | Error Code = " . $api->errorCode . " | Error Message = " . $api->errorMessage, SS_Log::ERR); } } else { if (isset($cf['Subscribed']) && empty($this->Subscribed)) { // If Just Unsubscribed $result = $api->listUnsubscribe($list->ListID, $this->getMailChimpIdentifier()); if (empty($api->errorCode)) { SS_Log::log("API Call Success: listUnsubscribe(" . $list->ListID . ", " . $this->getMailChimpIdentifier() . "); for Subscription ID " . $this->ID, SS_Log::NOTICE); } else { SS_Log::log("API Call Failed: listUnsubscribe(" . $list->ListID . ", " . $this->getMailChimpIdentifier() . "); for Subscription ID " . $this->ID . " | Error Code = " . $api->errorCode . " | Error Message = " . $api->errorMessage, SS_Log::ERR); } } else { if (!empty($this->Subscribed)) { // If Updating an Existing Subscription (That Hasnt Already Unsubscribed) $result = $api->listUpdateMember($list->ListID, $this->getMailChimpIdentifier(), $merge_vars); // If Successfully Updated a Subscription Make a Second Call to Return the MailChimp Member Email ID if (empty($api->errorCode)) { SS_Log::log("API Call Success: listUpdateMember(" . $list->ListID . ", " . $this->getMailChimpIdentifier() . "); for Subscription ID " . $this->ID, SS_Log::NOTICE); if (isset($cf['Email'])) { $retval = $api->listMemberInfo($list->ListID, $this->Email); // Call Must Use Email As MCEmailID Will Be Outdated If Last Update Was An Email Change if (empty($api->errorCode)) { SS_Log::log("API Call Success: listMemberInfo(" . $list->ListID . ", " . $this->Email . "); for Subscription ID " . $this->ID, SS_Log::NOTICE); SS_Log::log("Calling Additional write() for Subscription ID " . $this->ID . " to Save Updated MailChimp Email ID", SS_Log::NOTICE); $this->setField("MCEmailID", $retval['data'][0]['id']); // Update The MailChimp Email ID for this Member (Which Updates When E-mail Updates) $this->setSyncMailChimp(false); $this->write(); } else { SS_Log::log("API Call Failed: listMemberInfo(" . $list->ListID . ", " . $this->Email . "); for Subscription ID " . $this->ID . " | Error Code = " . $api->errorCode . " | Error Message = " . $api->errorMessage, SS_Log::ERR); } } } else { SS_Log::log("API Call Failed: listUpdateMember(" . $list->ListID . ", " . $this->getMailChimpIdentifier() . "); for Subscription ID " . $this->ID . " | Error Code = " . $api->errorCode . " | Error Message = " . $api->errorMessage, SS_Log::ERR); } } else { SS_Log::log("No API Call Made: Record Must Be Marked As Unsubscribed.", SS_Log::NOTICE); } } } } } else { SS_Log::log("In >=2 Write But No MailChimp Sync Triggered? " . "Sub ID = '" . $this->ID . "' | " . "Write Count = '" . $this->getWriteCount() . "' | " . "Sync To MailChimp = '" . $this->getSyncMailChimp() . "' | " . "Subscriber E-mail = '" . $this->Email . "' | " . "Related MC List ID = '" . $list->ID . "'", SS_Log::WARN); } } $this->setWriteCount(); // If We Have Forced An Additional Write (Triggered When Saving Subscription Object Via Related Member Data Being Updated) if ($this->getForceAdditionalWrite()) { // Ensure that two complete (i.e. DataObject::write() doesnt decide // nothing has actually changed and fails to call onAfterWrite()) // writes occour on creation as the second is responsible for // syncing to MC. Setting a dummy field is a hack to fix broken write() // function when passed $forceWrite (Does Not Actually Force a Write!) $this->DummyField = time(); // Unset It First So We Don't Keep Forcing Additional Writes Causing An Infinite Loop $this->setForceAdditionalWrite(false); // Write The Object Once More (For Benefit Of Sync Logic On Second Write) $this->write(); } }
function addUserToList($user_id, $product_id) { require_once JPATH_COMPONENT . '/helpers/MCAPI.class.php'; $db = JFactory::getDBO(); $sql = "select p.mailchimpapi, p.mailchimplist, p.mailchimpregister, p.mailchimpgroupid, u.email, c.firstname, c.lastname from #__users u, #__digicom_products p, #__digicom_customers c where u.id=c.id and u.id=" . intval($user_id) . " and p.id=" . intval($product_id); $db->setQuery($sql); $db->query(); $result = $db->loadAssocList(); if (isset($result) && count($result) > 0) { $mc_username = $result["0"]["mailchimpapi"]; $mc_listid = $result["0"]["mailchimplist"]; $mc_autoregister = $result["0"]["mailchimpregister"] == 0 ? FALSE : TRUE; $mc_groupid = $result["0"]["mailchimpgroupid"]; $mc_email = $result["0"]["email"]; if (trim($mc_username) == "") { $sql = "select `mailchimpapi` from #__digicom_settings"; $db->setQuery($sql); $db->query(); $mc_username = $db->loadResult(); } if (trim($mc_listid) == "") { $sql = "select `mailchimplist` from #__digicom_settings"; $db->setQuery($sql); $db->query(); $mc_listid = $db->loadResult(); } if (trim($mc_username) != "" && trim($mc_listid) != "") { $api = new MCAPI($mc_username); $user_info = $api->listMemberInfo($mc_listid, $mc_email); if ($user_info === FALSE) { //add new user $mergeVars = array('FNAME' => $result["0"]["firstname"], 'LNAME' => $result["0"]["lastname"]); if (trim($mc_groupid) != "") { $mergeVars["INTERESTS"] = trim($mc_groupid); } $api->listSubscribe($mc_listid, $mc_email, $mergeVars, 'html', $mc_autoregister, true); } else { //already exist and update user group if (trim($mc_groupid) != "") { $user_group_string = $user_info["merges"]["INTERESTS"]; $user_group_array = explode(",", $user_group_string); $exist = FALSE; foreach ($user_group_array as $key => $group) { if (trim($group) == trim($mc_groupid)) { $exist = TRUE; } } if ($exist === FALSE) { $new_group_list = trim($user_group_string); if (trim($new_group_list) != "") { $new_group_list = trim($user_group_string) . ", " . trim($mc_groupid); } else { $new_group_list = trim($mc_groupid); } $mergeVars = array('INTERESTS' => $new_group_list); $name = ""; $groups = $api->listInterestGroupings($mc_listid); if (isset($groups) && count($groups) > 0) { foreach ($groups as $key_group => $group) { if (isset($group["groups"]) && count($group["groups"]) > 0) { foreach ($group["groups"] as $key_subgroup => $subgroup) { if (trim($subgroup["name"]) == trim($mc_groupid)) { $name = $group["name"]; break; } } } } } if (trim($name) != "") { $mergeVars = array('INTERESTS' => $new_group_list, 'GROUPINGS' => array(array('name' => $name, 'groups' => $new_group_list))); } $api->listUpdateMember($mc_listid, $mc_email, $mergeVars, 'html', false); } } } } } }