Created
February 22, 2010 16:10
-
-
Save dogmatic69/311202 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ContactComponent extends Object { | |
/** | |
* components being used here | |
*/ | |
var $components = array(); | |
var $errors = null; | |
var $setup = array( | |
'uri' => array( | |
'scheme' => 'https', | |
'host' => 'api.constantcontact.com', | |
'path' => '/ws/customers/{username}/contacts' | |
), | |
'auth' => array( | |
'method' => 'Basic', | |
'user' => '{api_key}%{username}', | |
'pass' => '{password}' | |
), | |
'username' => '{username}' | |
); | |
// define the lists you have created here. | |
var $lists = array( | |
'default' => '1' | |
); | |
/** | |
* Controllers initialize function. | |
*/ | |
function initialize(&$controller, $settings = array()) { | |
$this->Controller = &$controller; | |
$settings = array_merge(array(), (array)$settings); | |
} | |
/** | |
* Find by email. | |
* | |
* This is used to find users in the constant contact api. | |
* http://developer.constantcontact.com/doc/manageContacts#search_contact_by_email | |
* | |
* @param string $email the email address of the user you want to find | |
*/ | |
function findContact($email = null){ | |
if (!$email) { | |
$this->errors[] = 'No email passed'; | |
return false; | |
} | |
$request = array( | |
'method' => 'GET', | |
'uri' => $this->setup['uri'], | |
'auth' => $this->setup['auth'] | |
); | |
$request['uri']['query'] = 'email='.urlencode($email); | |
if ($array = $this->__runRequest($request)) { | |
return $this->__formatSingleContact($array); | |
} | |
return false; | |
} | |
/** | |
* get all users | |
* | |
* just used to see whats there atm | |
* | |
* @return na | |
*/ | |
function listUsers(){ | |
$request = array( | |
'method' => 'GET', | |
'uri' => $this->setup['uri'], | |
'auth' => $this->setup['auth'] | |
); | |
App::import('Core', 'HttpSocket'); | |
$this->HttpSocket = new HttpSocket(); | |
$users = $this->HttpSocket->request($request); | |
var_dump($users); | |
exit; | |
} | |
/** | |
* http://developer.constantcontact.com/doc/manageContacts#update_contact_info | |
*/ | |
function updateContact($data = null){ | |
if (!$data || !isset($data['email']) || empty($data['email'])) { | |
$this->errors[] ='No one to update'; | |
return false; | |
} | |
$request = array( | |
'method' => 'GET', | |
'uri' => $this->setup['uri'], | |
'auth' => $this->setup['auth'] | |
); | |
$request['uri']['query'] = 'email='.urlencode($data['email']); | |
$_data = $this->__runRequest($request); | |
if (!$_data && !isset($_data['Feed']['Entry']['id'])) { | |
$this->errors[] = 'something broke trying to upload'; | |
return false; | |
} | |
$link = explode('/', $_data['Feed']['Entry']['id']); | |
if (!isset($link[7])) { | |
$this->errors[] = 'can not determin the user'; | |
return false; | |
} | |
$request = array( | |
'method' => 'PUT', | |
'uri' => $this->setup['uri'], | |
'auth' => $this->setup['auth'] | |
); | |
$request['uri']['path'] .= '/'.$link[7]; | |
//$rawXml = $this->__runRequest($request, true); | |
//$_data = $this->__parseXml($rawXml); | |
$return = "<?xml version='1.0' encoding='UTF-8'?>".' | |
<entry xmlns="http://www.w3.org/2005/Atom"> | |
<link href="/ws/customers/'.$this->setup['username'].'/contacts/'.$link[7].'" rel="edit" /> | |
<id>http://api.constantcontact.com/ws/customers/'.$this->setup['username'].'/contacts/'.$link[7].'</id> | |
<title type="text">Contact: '.$data['email'].'</title> | |
<updated>2010-02-05T02:25:45.782Z</updated> | |
<author> | |
<name>Constant Contact</name> | |
</author> | |
<content type="application/vnd.ctct+xml"> | |
<Contact xmlns="http://ws.constantcontact.com/ns/1.0/" id="http://api.constantcontact.com/ws/customers/'.$this->setup['username'].'/contacts/'.$link[7].'"> | |
<Status>Active</Status> | |
<EmailAddress>'.$data['email'].'</EmailAddress> | |
<EmailType>HTML</EmailType> | |
<Name>'.$data['last_name'].' '.$data['first_name'].'</Name> | |
<FirstName>'.$data['first_name'].'</FirstName> | |
<MiddleName></MiddleName> | |
<LastName>'.$data['last_name'].'</LastName> | |
<JobTitle></JobTitle> | |
<CompanyName></CompanyName> | |
<HomePhone>'.$data['phone'].'</HomePhone> | |
<WorkPhone></WorkPhone> | |
<Addr1>'.$data['address'].'</Addr1> | |
<Addr2>'.$data['address_line_2'].'</Addr2> | |
<Addr3></Addr3> | |
<City>'.$data['city'].'</City> | |
<StateCode></StateCode> | |
<StateName>'.$data['state'].'</StateName> | |
<CountryCode></CountryCode> | |
<CountryName></CountryName> | |
<PostalCode>'.$data['zip'].'</PostalCode> | |
<SubPostalCode></SubPostalCode> | |
<Note></Note> | |
<CustomField1>'.$data['coupon_code'].'</CustomField1> | |
<CustomField2></CustomField2> | |
<CustomField3></CustomField3> | |
<CustomField4></CustomField4> | |
<CustomField5></CustomField5> | |
<CustomField6></CustomField6> | |
<CustomField7></CustomField7> | |
<CustomField8></CustomField8> | |
<CustomField9></CustomField9> | |
<CustomField10></CustomField10> | |
<CustomField11></CustomField11> | |
<CustomField12></CustomField12> | |
<CustomField13></CustomField13> | |
<CustomField14></CustomField14> | |
<CustomField15></CustomField15> | |
<ContactLists> | |
<ContactList id="http://api.constantcontact.com/ws/customers/'.$this->setup['username'].'/lists/1"> | |
<link xmlns="http://www.w3.org/2005/Atom" href="/ws/customers/'.$this->setup['username'].'/lists/1" rel="self" /> | |
<OptInSource>ACTION_BY_CONTACT</OptInSource> | |
<OptInTime>2010-02-04T22:30:55.356Z</OptInTime> | |
</ContactList> | |
<ContactList id="http://api.constantcontact.com/ws/customers/'.$this->setup['username'].'/lists/'.$this->lists[$data['birthday']['month']].'"> | |
<link xmlns="http://www.w3.org/2005/Atom" href="/ws/customers/'.$this->setup['username'].'/lists/'.$this->lists[$data['birthday']['month']].'" rel="self" /> | |
<OptInSource>ACTION_BY_CONTACT</OptInSource> | |
<OptInTime>2010-02-04T22:30:55.356Z</OptInTime> | |
</ContactList> | |
</ContactLists> | |
<Confirmed>false</Confirmed> | |
<InsertTime>2010-02-04T22:30:55.283Z</InsertTime> | |
<LastUpdateTime>2010-02-05T02:25:45.782Z</LastUpdateTime> | |
</Contact> | |
</content> | |
<source> | |
<id>http://api.constantcontact.com/ws/customers/'.$this->setup['username'].'/contacts</id> | |
<title type="text">Contacts for Customer: '.$this->setup['username'].'</title> | |
<link href="contacts" /> | |
<link href="contacts" rel="self" /> | |
<author> | |
<name>'.$this->setup['username'].'</name> | |
</author> | |
<updated>2010-02-05T02:35:49.190Z</updated> | |
</source> | |
</entry>'; | |
App::import('Core', 'HttpSocket'); | |
$this->HttpSocket = new HttpSocket(); | |
$request = $this->setup; | |
$request['method'] = 'PUT'; | |
$request['uri']['path'] .= '/'.$link[7]; | |
$request['body'] = $return; | |
$request['header']['Content-Type'] = 'application/atom+xml'; | |
$_data = $this->HttpSocket->request($request); | |
if (isset($this->HttpSocket->response['status']['code']) && $this->HttpSocket->response['status']['code'] == 204) { | |
return true; | |
} | |
return false; | |
} | |
/** | |
* http://developer.constantcontact.com/doc/manageSubscriptions#add_remove_contact_from_list | |
*/ | |
function createContact($data = null){ | |
if (!$data) { | |
$this->errors[] = 'Nothing to save'; | |
} | |
$xml = '<entry xmlns="http://www.w3.org/2005/Atom">'. | |
'<id>data:,</id>'. | |
'<title/>'. | |
'<author/>'. | |
'<updated>'.date('Y-m-d').'</updated>'. | |
'<content type="application/vnd.ctct+xml">'. | |
'<Contact xmlns="http://ws.constantcontact.com/ns/1.0/">'. | |
'<EmailAddress>'.$data['email'].'</EmailAddress>'. | |
'<Name>'.$data['last_name'].', '.$data['first_name'].'</Name>'. | |
'<FirstName>'.$data['first_name'].'</FirstName>'. | |
'<LastName>'.$data['last_name'].'</LastName>'. | |
'<Addr1>'.$data['address'].'</Addr1>'. | |
'<Addr2>'.$data['address_line_2'].'</Addr2>'. | |
'<City>'.$data['city'].'</City>'. | |
'<CustomField1>'.$data['coupon_code'].'</CustomField1>'. | |
'<StateName>'.$data['state'].'</StateName>'. | |
'<HomePhone>'.$data['phone'].'</HomePhone>'. | |
'<EmailType>HTML</EmailType>'. | |
'<OptInSource>ACTION_BY_CONTACT</OptInSource>'. | |
'<ContactLists>'. | |
'<ContactList id="http://api.constantcontact.com/ws/customers/'.$this->setup['username'].'/lists/1" />'. | |
'<ContactList id="http://api.constantcontact.com/ws/customers/'.$this->setup['username'].'/lists/'.$this->lists[$data['birthday']['month']].'" />'. | |
'</ContactLists>'. | |
'</Contact>'. | |
'</content>'. | |
'</entry>'; | |
return $this->__sendRequest($xml); | |
} | |
/** | |
* http://developer.constantcontact.com/doc/manageSubscriptions#add_remove_contact_from_list | |
*/ | |
function removeContact(){ | |
} | |
function __sendRequest($xml = null){ | |
if (!$xml) { | |
$this->errors[] = 'No xml to send'; | |
return false; | |
} | |
App::import('Core', 'HttpSocket'); | |
$this->HttpSocket = new HttpSocket(); | |
$request = $this->setup; | |
$request['method'] = 'POST'; | |
$request['body'] = $xml; | |
$request['header']['Content-Type'] = 'application/atom+xml'; | |
return $this->HttpSocket->request($request); | |
} | |
/** | |
* Runs a request to the api. | |
* | |
* This will take the params for the request and then return data. | |
* It can either be raw xml if you pass true as the second param, or the | |
* default is to return an array | |
* | |
* @param array $request the request array | |
* @param bool $raw true for returning xml | |
* @return mixed array || xml | |
*/ | |
function __runRequest($request = null, $raw = false){ | |
if (!$request) { | |
$this->errors[] = 'Nothing set for request'; | |
return false; | |
} | |
App::import('Core', 'HttpSocket'); | |
$this->HttpSocket = new HttpSocket(); | |
$data = $this->HttpSocket->request($request); | |
if (!$data) { | |
$this->errors[] = 'Nothing Found'; | |
} | |
if ($raw) { | |
return $data; | |
} | |
return $this->__parseXml($data); | |
} | |
/** | |
* Converting xml. | |
* | |
* Convert the xml recived into an array | |
* | |
* @param string $data the xml data from constant contacts | |
* @return array the formatted array. | |
*/ | |
function __parseXml($data = null){ | |
if (!$data) { | |
$this->errors[] = 'No xml available'; | |
} | |
App::import('Core', 'Xml'); | |
$xml = new Xml($data); | |
$xmlAsArray = Set::reverse($xml); | |
return $xml->toArray(); | |
} | |
/** | |
* formats the array. | |
* | |
* gets a cake formatted array from the contacts array | |
* | |
* @param mixed $data | |
* @return | |
*/ | |
function __formatSingleContact($data = null){ | |
if (!$data) { | |
$this->errors[] ='No data to format'; | |
return false; | |
} | |
if (isset($data['Feed']['Entry']['Link']['href'])) { | |
$link = explode('/', $data['Feed']['Entry']['id']); | |
if (isset($link[7])) { | |
$return['User']['contact_id'] = $link[7]; | |
} | |
$request = array( | |
'method' => 'GET', | |
'uri' => $this->setup['uri'], | |
'auth' => $this->setup['auth'] | |
); | |
$request['uri']['path'] .= '/'.$return['User']['contact_id']; | |
$data = $this->__runRequest($request); | |
} | |
if (isset($data['Entry']['updated'])) { | |
$dateParts = explode('T', $data['Entry']['updated']); | |
$return['User']['cotact_modified'] = $dateParts[0]; | |
$time = explode('.', $dateParts[1]); | |
$return['User']['cotact_modified'] .= ' '.$time[0]; | |
} | |
if (!isset($data['Entry']['Content']['Contact']) || empty($data['Entry']['Content']['Contact'])) { | |
$this->errors[] = 'No contact details'; | |
return false; | |
} | |
$contact = $data['Entry']['Content']['Contact']; | |
$return['Subscriber']['contact_status'] = $contact['Status']; | |
$return['Subscriber']['contact_confirmed'] = $contact['Confirmed']; | |
$return['Subscriber']['email'] = $contact['EmailAddress']; | |
$return['Subscriber']['send_as'] = $contact['EmailType']; | |
$return['Subscriber']['first_name'] = $contact['FirstName']; | |
$return['Subscriber']['last_name'] = $contact['LastName']; | |
$return['Subscriber']['coupon_code'] = $contact['CustomField1']; | |
return $return; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment