function wpcc_add_contact() { $cc = new ConstantContact(WPCC_APIKEY); try { $lists = $cc->listService->getLists(WPCC_ACCESS_TOKEN); } catch (CtctException $ex) { foreach ($ex->getErrors() as $error) { print_r($error); } if (!isset($lists)) { $lists = null; } } // check if the form was submitted if (isset($_POST['email']) && strlen($_POST['email']) > 1) { $action = "Getting Contact By Email Address"; try { // check to see if a contact with the email address already exists in the account $response = $cc->contactService->getContacts(WPCC_ACCESS_TOKEN, array("email" => $_POST['email'])); // create a new contact if one does not exist if (empty($response->results)) { $action = "Creating Contact"; $contact = new Contact(); $contact->addEmail($_POST['email']); $contact->addList('1818691277'); $contact->first_name = $_POST['first_name']; $contact->last_name = $_POST['last_name']; $address = new Address(); $address->address_type = "BUSINESS"; // PERSONAL, BUSINESS, UNKNOWN $address->line1 = $_POST['address_line_1']; $address->line2 = $_POST['address_line_2']; $address->city = $_POST['city']; $address->state_code = $_POST['state_code']; $address->postal_code = $_POST['postal_code']; $address->country_code = "US"; $contact->addAddress($address); $returnContact = $cc->contactService->addContact(WPCC_ACCESS_TOKEN, $contact); } else { $action = "Updating Contact"; $contact = $response->results[0]; if ($contact instanceof Contact) { $contact->addList('1818691277'); $contact->first_name = $_POST['first_name']; $contact->last_name = $_POST['last_name']; //If address exists, update that address. Else, create a new address object. if (count($contact->addresses) > 0) { $address = $contact->addresses[0]; } else { $address = new Address(); } $address->address_type = "BUSINESS"; // PERSONAL, BUSINESS, UNKNOWN $address->line1 = $_POST['address_line_1']; $address->line2 = $_POST['address_line_2']; $address->city = $_POST['city']; $address->state = ''; $address->state_code = $_POST['state_code']; $address->postal_code = $_POST['postal_code']; $address->country_code = $_POST['country_code']; $contact->addAddress($address); $customField_1 = new CustomField(); $customField_1->name = "CustomField1"; $customField_1->value = date('m/d/y', strtotime($_POST['dob'])); $contact->addCustomField($customField_1); /* * The third parameter of updateContact defaults to false, but if this were set to true it would tell * Constant Contact that this action is being performed by the contact themselves, and gives the ability to * opt contacts back in and trigger Welcome/Change-of-interest emails. * * See: http://developer.constantcontact.com/docs/contacts-api/contacts-index.html#opt_in */ $returnContact = $cc->contactService->updateContact(WPCC_ACCESS_TOKEN, $contact); } else { $e = new CtctException(); $e->setErrors(array("type", "Contact type not returned")); throw $e; } } // catch any exceptions thrown during the process and print the errors to screen } catch (CtctException $ex) { echo '<span class="label label-important">Error ' . $action . '</span>'; echo '<div class="container alert-error"><pre class="failure-pre">'; print_r($ex->getErrors()); echo '</pre></div>'; die; } } }
public function subscribe() { if (!$this->getRequest()->isAjax()) { //if not ajax, say go and eat your grass. exit("Action is not allowed!"); } $return = array('action' => '', 'message' => ''); $config = SiteConfig::current_site_config(); $cc = new ConstantContact($config->CcApiKey); $list = $this->getRequest()->postVar('list'); if ($config->CcDisplayZip) { $postcode = array('postal_code' => $this->getRequest()->postVar('postcode')); } else { $postcode = 0; } $email = $this->getRequest()->postVar('email'); $first_name = $this->getRequest()->postVar('first_name'); $last_name = $this->getRequest()->postVar('last_name'); if (empty($email) || empty($first_name) || empty($last_name) || empty($list)) { $return['action'] = 'E'; $return['message'] = $config->CcRequiredMessage ? $config->CcRequiredMessage : 'Required fields are missing.'; } else { try { // check to see if a contact with the email addess already exists in the account $response = $cc->getContactByEmail($config->CcAccessToken, $email); // create a new contact if one does not exist if (empty($response->results)) { $return['action'] = "C"; $contact = new Contact(); $contact->addEmail($email); if (!is_array($list)) { $contact->addList($list); } else { foreach ($list as $l) { $contact->addList($l); } } $contact->first_name = $first_name; $contact->last_name = $last_name; if ($postcode) { $contact->addAddress($postcode); } $returnContact = $cc->addContact($config->CcAccessToken, $contact); if (!empty($returnContact)) { $return['message'] = $config->CcAddedMessage; } // update the existing contact if address already existed } else { $return['action'] = "U"; $contact = $response->results[0]; if (!is_array($list)) { $contact->addList($list); } else { foreach ($list as $l) { $contact->addList($l); } } $contact->first_name = $first_name; $contact->last_name = $last_name; if ($postcode) { $contact->addAddress($postcode); } $returnContact = $cc->updateContact($config->CcAccessToken, $contact); if (!empty($returnContact)) { $return['message'] = $config->CcUpdatedMessage; } } // catch any exceptions thrown during the process and print the errors to screen } catch (CtctException $ex) { $ex = $ex->getErrors(); print_r($ex); $return['action'] = 'E'; $return['message'] = $config->CcErrorMessage; } } return json_encode($return); }