function updateSettings($variables) { if (!isset($variables["mailchimp_secure"])) { $variables["mailchimp_secure"] = 0; } /** * Check for a valid api key. */ if ($variables["apikey_changed"] == "1" && $variables["mailchimp_apikey"] != "") { include_once "include/MCAPI.class.php"; $api = new MCAPI($variables["mailchimp_apikey"]); $api->ping(); if ($api->errorCode) { unset($variables["mailchimp_apikey"]); $this->updateErrorMessage = "Unable to change the MailChimp apikey: " . $api->errorMessage . " (" . $api->errorCode . ")"; return $variables; } //end if } //end if /** * Check for valid list id */ if ($variables["apilist_changed"] == "1" && $variables["mailchimp_list_id"] != "") { include_once "include/MCAPI.class.php"; /** * Check to see if api is already defined (from a possible api key check) * If not, define it and check the key/connection */ if (!isset($api)) { $api = new MCAPI($variables["mailchimp_apikey"]); $api->ping(); if ($api->errorCode) { unset($variables["mailchimp_list_id"]); $this->updateErrorMessage = "Unable to change the MailChimp list id: " . $api->errorMessage . " (" . $api->errorCode . ")"; return $variables; } //end if } //end if /** * Look up the lists */ $lists = $api->lists(); if ($api->errorCode) { unset($variables["mailchimp_list_id"]); $this->updateErrorMessage = "Unable to change the MailChimp list id: " . $api->errorMessage . " (" . $api->errorCode . ")"; return $variables; } else { /** * Check to see if list id is valid */ $validId = false; foreach ($lists as $list) { if ($list["id"] == $variables["mailchimp_list_id"]) { $validId = true; break; } } //endif if (!$validId) { unset($variables["mailchimp_list_id"]); $this->updateErrorMessage = "Unable to change the MailChimp list id: the id does not match a valid id on the account."; return $variables; } else { /** * Check to see if the list has a uuid. */ $hasUuid = false; $hasCompany = false; $hasType = false; $mergeVars = $api->listMergeVars($variables["mailchimp_list_id"]); if ($api->errorCode) { unset($variables["mailchimp_list_id"]); $this->updateErrorMessage = "Unable to change the MailChimp list id: " . $api->errorMessage . " (" . $api->errorCode . ")"; return $variables; } //end if $req = array(); foreach ($mergeVars as $mergeVar) { switch ($mergeVar["tag"]) { case "UUID": $hasUuid = true; break; case "COMPANY": $hasCompany = true; break; case "TYPE": $hasType = true; break; } //end switch } //end foreach /** * If it doesn't have a uuid field, create it. */ if (!$hasUuid) { $req = array("req" => true, "public" => false, "field_type" => "text"); $api->listMergeVarAdd($variables["mailchimp_list_id"], "UUID", "phpBMS unique user id", $req); if ($api->errorCode) { unset($variables["mailchimp_list_id"]); $this->updateErrorMessage = "Unable to change the MailChimp list id: " . $api->errorMessage . " (" . $api->errorCode . ")"; return $variables; } //end if } //end if /** * If it doesn't have a company field, create it. */ if (!$hasCompany) { $req = array("req" => false, "public" => true, "field_type" => "text"); $api->listMergeVarAdd($variables["mailchimp_list_id"], "COMPANY", "Company", $req); if ($api->errorCode) { unset($variables["mailchimp_list_id"]); $this->updateErrorMessage = "Unable to change the MailChimp list id: " . $api->errorMessage . " (" . $api->errorCode . ")"; return $variables; } //end if } //end if /** * If it doesn't have a type field, create it. */ if (!$hasType) { $req = array("req" => false, "public" => true, "field_type" => "text"); $api->listMergeVarAdd($variables["mailchimp_list_id"], "TYPE", "Type", $req); if ($api->errorCode) { unset($variables["mailchimp_list_id"]); $this->updateErrorMessage = "Unable to change the MailChimp list id: " . $api->errorMessage . " (" . $api->errorCode . ")"; return $variables; } //end if } //end if /** * If the date list id has changed, the last sync date must be reset: */ $variables["mailchimp_last_sync_date"] = ""; } //end if } //end if } //end if return $variables; }