CC_Contact

Class that is used for ConstantConact CRUD management.

Defined (1)

The class is defined in the following location(s).

/api/cc_class.php  
  1. class CC_Contact extends CC_Utility { 
  2.  
  3. /** 
  4. * Method that checks if a subscriber already exist 
  5. * @param string $email 
  6. * @return string|int subscriber`s id if it exists or false if it doesn't 
  7. */ 
  8. public function subscriberExists($email = '') { 
  9. $call = $this->apiPath.'/contacts?email='.rawurlencode( $email ); 
  10. $return = $this->doServerCall($call); 
  11. $xml = simplexml_load_string($return); 
  12. $id = $xml->entry->id; 
  13. if($id) { return $id; } 
  14. else { return false; } 
  15.  
  16. /** 
  17. * Method that retrieves from Constant Contact a collection with all the Subscribers 
  18. * If email parameter is mentioned then only mentioned contact is retrieved. 
  19. * @param string $email 
  20. * @return array Bi-Dimenstional array with information about contacts. 
  21. */ 
  22. public function getSubscribers($email = '', $page = '') { 
  23. $contacts = array(); 
  24. $contacts['items'] = array(); 
  25.  
  26. if (! empty($email)) { 
  27. $call = $this->apiPath.'/contacts?email='.rawurlencode( $email ); 
  28. } else { 
  29. if (! empty($page)) { 
  30. $call = $this->apiPath.$page; 
  31. } else { 
  32. $call = $this->apiPath.'/contacts'; 
  33.  
  34. $return = $this->doServerCall($call); 
  35. $parsedReturn = simplexml_load_string($return); 
  36. // We parse here the link array to establish which are the next page and previous page 
  37. foreach ($parsedReturn->link as $item) { 
  38. $attributes = $item->Attributes(); 
  39.  
  40. if (! empty($attributes['rel']) && $attributes['rel'] == 'next') { 
  41. $tmp = explode($this->login, $attributes['href']); 
  42. $contacts['next'] = $tmp[1]; 
  43. if (! empty($attributes['rel']) && $attributes['rel'] == 'first') { 
  44. $tmp = explode($this->login, $attributes['href']); 
  45. $contacts['first'] = $tmp[1]; 
  46. if (! empty($attributes['rel']) && $attributes['rel'] == 'current') { 
  47. $tmp = explode($this->login, $attributes['href']); 
  48. $contacts['current'] = $tmp[1]; 
  49.  
  50. foreach ($parsedReturn->entry as $item) { 
  51. $tmp = array(); 
  52. $tmp['id'] = (string) $item->id; 
  53. $tmp['title'] = (string) $item->title; 
  54. $tmp['status'] = (string) $item->content->Contact->Status; 
  55. $tmp['EmailAddress'] = (string) $item->content->Contact->EmailAddress; 
  56. $tmp['EmailType'] = (string) $item->content->Contact->EmailType; 
  57. $tmp['Name'] = (string) $item->content->Contact->Name; 
  58. $contacts['items'][] = $tmp; 
  59.  
  60. return $contacts; 
  61.  
  62. /** 
  63. * Retrieves all the details for a specific contact identified by $email. 
  64. * @param string $email 
  65. * @return false|array with all information about the contact, or false if contact doesn't exist 
  66. */ 
  67. public function getSubscriberDetails($email) { 
  68. $contact = $this->getSubscribers($email); 
  69. if ( empty( $contact['items'] ) ) { 
  70. return false; 
  71. $fullContact = array(); 
  72. $call = str_replace('http://', 'https://', $contact['items'][0]['id']); 
  73. // Convert id URI to BASIC compliant 
  74. $return = $this->doServerCall($call); 
  75. $parsedReturn = simplexml_load_string($return); 
  76. $fullContact['id'] = $parsedReturn->id; 
  77. $fullContact['email_address'] = $parsedReturn->content->Contact->EmailAddress; 
  78. $fullContact['first_name'] = $parsedReturn->content->Contact->FirstName; 
  79. $fullContact['last_name'] = $parsedReturn->content->Contact->LastName; 
  80. $fullContact['middle_name'] = $parsedReturn->content->Contact->MiddleName; 
  81. $fullContact['company_name'] = $parsedReturn->content->Contact->CompanyName; 
  82. $fullContact['job_title'] = $parsedReturn->content->Contact->JobTitle; 
  83. $fullContact['home_number'] = $parsedReturn->content->Contact->HomePhone; 
  84. $fullContact['work_number'] = $parsedReturn->content->Contact->WorkPhone; 
  85. $fullContact['address_line_1'] = $parsedReturn->content->Contact->Addr1; 
  86. $fullContact['address_line_2'] = $parsedReturn->content->Contact->Addr2; 
  87. $fullContact['address_line_3'] = $parsedReturn->content->Contact->Addr3; 
  88. $fullContact['city_name'] = (string) $parsedReturn->content->Contact->City; 
  89. $fullContact['state_code'] = (string) $parsedReturn->content->Contact->StateCode; 
  90. $fullContact['state_name'] = (string) $parsedReturn->content->Contact->StateName; 
  91. $fullContact['country_code'] = $parsedReturn->content->Contact->CountryCode; 
  92. $fullContact['zip_code'] = $parsedReturn->content->Contact->PostalCode; 
  93. $fullContact['sub_zip_code'] = $parsedReturn->content->Contact->SubPostalCode; 
  94. $fullContact['custom_field_1'] = $parsedReturn->content->Contact->CustomField1; 
  95. $fullContact['custom_field_2'] = $parsedReturn->content->Contact->CustomField2; 
  96. $fullContact['custom_field_3'] = $parsedReturn->content->Contact->CustomField3; 
  97. $fullContact['custom_field_4'] = $parsedReturn->content->Contact->CustomField4; 
  98. $fullContact['custom_field_5'] = $parsedReturn->content->Contact->CustomField5; 
  99. $fullContact['custom_field_6'] = $parsedReturn->content->Contact->CustomField6; 
  100. $fullContact['custom_field_7'] = $parsedReturn->content->Contact->CustomField7; 
  101. $fullContact['custom_field_8'] = $parsedReturn->content->Contact->CustomField8; 
  102. $fullContact['custom_field_9'] = $parsedReturn->content->Contact->CustomField9; 
  103. $fullContact['custom_field_10'] = $parsedReturn->content->Contact->CustomField10; 
  104. $fullContact['custom_field_11'] = $parsedReturn->content->Contact->CustomField11; 
  105. $fullContact['custom_field_12'] = $parsedReturn->content->Contact->CustomField12; 
  106. $fullContact['custom_field_13'] = $parsedReturn->content->Contact->CustomField13; 
  107. $fullContact['custom_field_14'] = $parsedReturn->content->Contact->CustomField14; 
  108. $fullContact['custom_field_15'] = $parsedReturn->content->Contact->CustomField15; 
  109. $fullContact['notes'] = $parsedReturn->content->Contact->Note; 
  110. $fullContact['mail_type'] = $parsedReturn->content->Contact->EmailType; 
  111. $fullContact['status'] = $parsedReturn->content->Contact->Status; 
  112. $fullContact['lists'] = array(); 
  113.  
  114. if ($parsedReturn->content->Contact->ContactLists->ContactList) { 
  115. foreach ($parsedReturn->content->Contact->ContactLists->ContactList as $item) { 
  116. $fullContact['lists'][] = trim((string) $item->Attributes()); 
  117.  
  118. return $fullContact; 
  119.  
  120. /** 
  121. * Method that modifies a contact State to DO NOT MAIL 
  122. * @param string $email - contact email address 
  123. * @return TRUE in case of success or FALSE otherwise 
  124. */ 
  125. public function deleteSubscriber($email) { 
  126. if ( empty($email)) { return false; } 
  127. $contact = $this->getSubscribers($email); 
  128. $id = $contact['items'][0]['id']; 
  129. $return = $this->doServerCall($id, '', 'DELETE'); 
  130. if (! empty($return)) { return false; } 
  131. return true; 
  132.  
  133. /** 
  134. * Method that modifies a contact State to REMOVED 
  135. * @param string $email - contact email address 
  136. * @return TRUE in case of success or FALSE otherwise 
  137. */ 
  138. public function removeSubscriber($email) { 
  139. $contact = $this->getSubscriberDetails($email); 
  140. $contact['lists'] = array(); 
  141. $xml = $this->createContactXML($contact['id'], $contact); 
  142. if ($this->editSubscriber($contact['id'], $xml)) { 
  143. return true; 
  144. } else { 
  145. return false; 
  146.  
  147. /** 
  148. * Upload a new contact to Constant Contact server 
  149. * @param strong $contactXML - formatted XML with contact information 
  150. * @return TRUE in case of success or FALSE otherwise 
  151. */ 
  152. public function addSubscriber($contactXML) { 
  153. $call = $this->apiPath.'/contacts'; 
  154. $return = $this->doServerCall($call, $contactXML, 'POST'); 
  155. $parsedReturn = simplexml_load_string($return); 
  156.  
  157. if ($return) { 
  158. return true; 
  159. } else { 
  160. $xml = simplexml_load_string($contactXML); 
  161. $emailAddress = $xml->content->Contact->EmailAddress; 
  162. if ($this->subscriberExists($emailAddress)) { 
  163. $this->lastError = 'This contact already exists. <a href="edit_contact.php?email='.$emailAddress.'">Click here</a> to edit the contact details.'; 
  164. } else { $this->lastError = 'An Error Occurred'; } 
  165. return false; 
  166.  
  167. /** 
  168. * Modifies a contact 
  169. * @param string $contactUrl - identifies the url for the modified contact 
  170. * @param string $contactXML - formed XML with contact information 
  171. * @return TRUE in case of success or FALSE otherwise 
  172. */ 
  173. public function editSubscriber($contactUrl, $contactXML) { 
  174. $return = $this->doServerCall($contactUrl, $contactXML, 'PUT'); 
  175. if (! empty($return)) { 
  176. if (strpos($return, '<') !== false) { 
  177. $parsedReturn = simplexml_load_string($return); 
  178. if (! empty($parsedReturn->message)) { 
  179. $this->lastError = $parsedReturn->message; 
  180. } else { 
  181. $this->lastError = $parsedReturn->message; 
  182. return false; 
  183. return true; 
  184.  
  185. /** 
  186. * Method that compose the needed XML format for a contact 
  187. * @param string $id 
  188. * @param array $params 
  189. * @return string Formed XML 
  190. */ 
  191. public function createContactXML($id, $params = array()) { 
  192. if ( empty($id)) { 
  193. $id = "urn:uuid:E8553C09F4xcvxCCC53F481214230867087"; 
  194.  
  195. $update_date = date("Y-m-d").'T'.date("H:i:s").'+01:00'; 
  196. $xml_string = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><entry xmlns='http://www.w3.org/2005/Atom'></entry>"; 
  197. $xml_object = simplexml_load_string($xml_string); 
  198. $title_node = $xml_object->addChild("title", htmlspecialchars(("TitleNode"), ENT_QUOTES, 'UTF-8')); 
  199. $updated_node = $xml_object->addChild("updated", htmlspecialchars(($update_date), ENT_QUOTES, 'UTF-8')); 
  200. $author_node = $xml_object->addChild("author"); 
  201. $author_name = $author_node->addChild("name", ("CTCT Samples")); 
  202. $id_node = $xml_object->addChild("id", htmlspecialchars(($id), ENT_QUOTES, 'UTF-8')); 
  203. $summary_node = $xml_object->addChild("summary", htmlspecialchars(("Customer document"), ENT_QUOTES, 'UTF-8')); 
  204. $summary_node->addAttribute("type", "text"); 
  205. $content_node = $xml_object->addChild("content"); 
  206. $content_node->addAttribute("type", "application/vnd.ctct+xml"); 
  207. $contact_node = $content_node->addChild("Contact", htmlspecialchars(("Customer document"), ENT_QUOTES, 'UTF-8')); 
  208. $contact_node->addAttribute("xmlns", "http://ws.constantcontact.com/ns/1.0/"); 
  209. $email_node = $contact_node->addChild("EmailAddress", htmlspecialchars((@$params['email_address']), ENT_QUOTES, 'UTF-8')); 
  210. $fname_node = $contact_node->addChild("FirstName", urldecode(htmlspecialchars((@$params['first_name']), ENT_QUOTES, 'UTF-8'))); 
  211. $lname_node = $contact_node->addChild("LastName", urldecode(htmlspecialchars((@$params['last_name']), ENT_QUOTES, 'UTF-8'))); 
  212. $lname_node = $contact_node->addChild("MiddleName", urldecode(htmlspecialchars((@$params['middle_name']), ENT_QUOTES, 'UTF-8'))); 
  213. $lname_node = $contact_node->addChild("CompanyName", urldecode(htmlspecialchars((@$params['company_name']), ENT_QUOTES, 'UTF-8'))); 
  214. $lname_node = $contact_node->addChild("JobTitle", urldecode(htmlspecialchars((@$params['job_title']), ENT_QUOTES, 'UTF-8'))); 
  215.  
  216. if (@$params['status'] == 'Do Not Mail') { 
  217. $this->actionBy = 'ACTION_BY_CONTACT'; 
  218.  
  219. $optin_node = $contact_node->addChild("OptInSource", htmlspecialchars($this->actionBy)); 
  220. $hn_node = $contact_node->addChild("HomePhone", htmlspecialchars(@$params['home_number'], ENT_QUOTES, 'UTF-8')); 
  221. $wn_node = $contact_node->addChild("WorkPhone", htmlspecialchars(@$params['work_number'], ENT_QUOTES, 'UTF-8')); 
  222. $ad1_node = $contact_node->addChild("Addr1", htmlspecialchars(@$params['address_line_1'], ENT_QUOTES, 'UTF-8')); 
  223. $ad2_node = $contact_node->addChild("Addr2", htmlspecialchars(@$params['address_line_2'], ENT_QUOTES, 'UTF-8')); 
  224. $ad3_node = $contact_node->addChild("Addr3", htmlspecialchars(@$params['address_line_3'], ENT_QUOTES, 'UTF-8')); 
  225. $city_node = $contact_node->addChild("City", htmlspecialchars(@$params['city_name'], ENT_QUOTES, 'UTF-8')); 
  226. $state_node = $contact_node->addChild("StateCode", htmlspecialchars(@$params['state_code'], ENT_QUOTES, 'UTF-8')); 
  227. $state_name = $contact_node->addChild("StateName", htmlspecialchars(@$params['state_name'], ENT_QUOTES, 'UTF-8')); 
  228. $ctry_node = $contact_node->addChild("CountryCode", htmlspecialchars(@$params['country_code'], ENT_QUOTES, 'UTF-8')); 
  229. $zip_node = $contact_node->addChild("PostalCode", htmlspecialchars(@$params['zip_code'], ENT_QUOTES, 'UTF-8')); 
  230. $subzip_node = $contact_node->addChild("SubPostalCode", htmlspecialchars(@$params['sub_zip_code'], ENT_QUOTES, 'UTF-8')); 
  231. $note_node = $contact_node->addChild("Note", htmlspecialchars(@$params['notes'], ENT_QUOTES, 'UTF-8')); 
  232. $emailtype_node = $contact_node->addChild("EmailType", htmlspecialchars(@$params['mail_type'], ENT_QUOTES, 'UTF-8')); 
  233.  
  234.  
  235. // Modified by ZK for Version 2.0.2 
  236. for ($i = 1; $i < 16; $i++) { 
  237. if (!empty($params['custom_field_'.$i]) && $params['custom_field_'.$i] !== 0) { 
  238. $content = htmlspecialchars((string)$params['custom_field_'.$i], ENT_QUOTES, 'UTF-8'); 
  239. $content = substr($content, 0, 50); 
  240. $content = preg_replace('/(.*)&([^;]+)?$/ism', '$1', $content); // This prevents unencoded endings 
  241. $contact_node->addChild("CustomField".$i, $content); 
  242. // End modification 
  243.  
  244. $contactlists_node = $contact_node->addChild("ContactLists"); 
  245. if ($params['lists']) { 
  246. foreach ($params['lists'] as $tmp) { 
  247. $contactlist_node = $contactlists_node->addChild("ContactList"); 
  248. $contactlist_node->addAttribute("id", $tmp); 
  249.  
  250. $entry = $xml_object->asXML(); 
  251. return $entry; 
  252.