Source of file ConstantContact.php
Size: 4,510 Bytes - Last Modified: 2021-12-24T05:18:43+00:00
/var/www/docs.ssmods.com/process/src/src/ConstantContact.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 | <?php namespace IQnection\ConstantContact; use SilverStripe\ORM\DataExtension; use SilverStripe\ORM\ArrayList; use SilverStripe\View\ArrayData; use SilverStrpie\Forms; use SilverStripe\SiteConfig\SiteConfig; use Ctct\ConstantContact as CC; use Ctct\Components\Contacts\Contact; use Ctct\Exceptions\CtctException; class ConstantContact { // Enter your Constant Contact APIKEY and ACCESS_TOKEN protected $ApiKey; protected $ApiToken; public function __construct($ApiKey=null,$ApiToken=null) { $siteConfig = SiteConfig::current_site_config(); $this->setApiKey( ($ApiKey) ? $ApiKey : $siteConfig->ConstantContactApiKey ); $this->setApiToken( ($ApiToken) ? $ApiToken : $siteConfig->ConstantContactApiToken ); } public function setApiKey($key) { $this->ApiKey = $key; } public function setApiToken($token) { $this->ApiToken = $token; } public function addContact($Email, $list_id=false, $FirstName = null, $LastName = null) { $errors = array(); $cc = new CC($this->ApiKey); // attempt to fetch lists in the account, catching any exceptions try { $lists = $cc->listService->getLists($this->ApiToken); } catch (CtctException $ex) { foreach ($ex->getErrors() as $error) { $errors[] = $error; } } // get list from the params, or just the first ACTIVE list if no param was passed $the_list = false; foreach ($lists as $list) { if (!$the_list && $list->status == 'ACTIVE') { $the_list = $list; } if ($list_id == $list->id) { $the_list = $list; break; } } if ($the_list) { try { $response = $cc->contactService->getContacts($this->ApiToken, array('email' => $Email)); //If the contact doesn't exist, add. If it does, update. if (empty($response->results)) { $contact = new Contact(); $contact->addEmail($Email); $contact->addList($the_list->id); $contact->first_name = $FirstName; $contact->last_name = $LastName; /* * The third parameter of addContact 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->addContact($this->ApiToken, $contact); return $returnContact; } else { // update contact $contact = $response->results[0]; if ($contact instanceof Contact) { $contact->addList($the_list->id); if ($FirstName) { $contact->first_name = $FirstName; } if ($LastName) { $contact->last_name = $LastName; } /* * 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($this->ApiToken, $contact); return $returnContact; } } } catch (CtctException $ex) { $errors[] = $ex->getErrors(); } } return $errors; } public function getLists() { $errors = array(); $cc = new CC($this->ApiKey); // attempt to fetch lists in the account, catching any exceptions try { $lists = $cc->listService->getLists($this->ApiToken); } catch (CtctException $ex) { foreach ($ex->getErrors() as $error) { $errors[] = $error; } return $errors; } return $lists; } } |