csrest_clients.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. <?php
  2. require_once dirname(__FILE__).'/class/base_classes.php';
  3. /**
  4. * Class to access a clients resources from the create send API.
  5. * This class includes functions to create and edit clients,
  6. * along with accessing lists of client specific resources e.g campaigns
  7. * @author tobyb
  8. *
  9. */
  10. class CS_REST_Clients extends CS_REST_Wrapper_Base {
  11. /**
  12. * The base route of the clients resource.
  13. * @var string
  14. * @access private
  15. */
  16. var $_clients_base_route;
  17. /**
  18. * Constructor.
  19. * @param $client_id string The client id to access (Ignored for create requests)
  20. * @param $auth_details array Authentication details to use for API calls.
  21. * This array must take one of the following forms:
  22. * If using OAuth to authenticate:
  23. * array(
  24. * 'access_token' => 'your access token',
  25. * 'refresh_token' => 'your refresh token')
  26. *
  27. * Or if using an API key:
  28. * array('api_key' => 'your api key')
  29. * @param $protocol string The protocol to use for requests (http|https)
  30. * @param $debug_level int The level of debugging required CS_REST_LOG_NONE | CS_REST_LOG_ERROR | CS_REST_LOG_WARNING | CS_REST_LOG_VERBOSE
  31. * @param $host string The host to send API requests to. There is no need to change this
  32. * @param $log CS_REST_Log The logger to use. Used for dependency injection
  33. * @param $serialiser The serialiser to use. Used for dependency injection
  34. * @param $transport The transport to use. Used for dependency injection
  35. * @access public
  36. */
  37. function __construct(
  38. $client_id,
  39. $auth_details,
  40. $protocol = 'https',
  41. $debug_level = CS_REST_LOG_NONE,
  42. $host = 'api.createsend.com',
  43. $log = NULL,
  44. $serialiser = NULL,
  45. $transport = NULL) {
  46. parent::__construct($auth_details, $protocol, $debug_level, $host, $log, $serialiser, $transport);
  47. $this->set_client_id($client_id);
  48. }
  49. /**
  50. * Change the client id used for calls after construction
  51. * @param $client_id
  52. * @access public
  53. */
  54. function set_client_id($client_id) {
  55. $this->_clients_base_route = $this->_base_route.'clients/'.$client_id.'/';
  56. }
  57. /**
  58. * Gets a list of sent campaigns for the current client
  59. * @access public
  60. * @return CS_REST_Wrapper_Result A successful response will be an object of the form
  61. * array(
  62. * {
  63. * 'WebVersionURL' => The web version url of the campaign
  64. * 'WebVersionTextURL' => The web version url of the text version of the campaign
  65. * 'CampaignID' => The id of the campaign
  66. * 'Subject' => The campaign subject
  67. * 'Name' => The name of the campaign
  68. * 'FromName' => The from name for the campaign
  69. * 'FromEmail' => The from email address for the campaign
  70. * 'ReplyTo' => The reply to email address for the campaign
  71. * 'SentDate' => The sent data of the campaign
  72. * 'TotalRecipients' => The number of recipients of the campaign
  73. * }
  74. * )
  75. */
  76. function get_campaigns() {
  77. return $this->get_request($this->_clients_base_route.'campaigns.json');
  78. }
  79. /**
  80. * Gets a list of scheduled campaigns for the current client
  81. * @access public
  82. * @return CS_REST_Wrapper_Result A successful response will be an object of the form
  83. * array(
  84. * {
  85. * 'CampaignID' => The id of the campaign
  86. * 'Name' => The name of the campaign
  87. * 'Subject' => The subject of the campaign
  88. * 'FromName' => The from name for the campaign
  89. * 'FromEmail' => The from email address for the campaign
  90. * 'ReplyTo' => The reply to email address for the campaign
  91. * 'DateCreated' => The date the campaign was created
  92. * 'PreviewURL' => The preview url of the campaign
  93. * 'PreviewTextURL' => The preview url of the text version of the campaign
  94. * 'DateScheduled' => The date the campaign is scheduled to be sent
  95. * 'ScheduledTimeZone' => The time zone in which the campaign is scheduled to be sent at 'DateScheduled'
  96. * }
  97. * )
  98. */
  99. function get_scheduled() {
  100. return $this->get_request($this->_clients_base_route.'scheduled.json');
  101. }
  102. /**
  103. * Gets a list of draft campaigns for the current client
  104. * @access public
  105. * @return CS_REST_Wrapper_Result A successful response will be an object of the form
  106. * array(
  107. * {
  108. * 'CampaignID' => The id of the campaign
  109. * 'Name' => The name of the campaign
  110. * 'Subject' => The subject of the campaign
  111. * 'FromName' => The from name for the campaign
  112. * 'FromEmail' => The from email address for the campaign
  113. * 'ReplyTo' => The reply to email address for the campaign
  114. * 'DateCreated' => The date the campaign was created
  115. * 'PreviewURL' => The preview url of the draft campaign
  116. * 'PreviewTextURL' => The preview url of the text version of the campaign
  117. * }
  118. * )
  119. */
  120. function get_drafts() {
  121. return $this->get_request($this->_clients_base_route.'drafts.json');
  122. }
  123. /**
  124. * Gets all subscriber lists the current client has created
  125. * @access public
  126. * @return CS_REST_Wrapper_Result A successful response will be an object of the form
  127. * array(
  128. * {
  129. * 'ListID' => The id of the list
  130. * 'Name' => The name of the list
  131. * }
  132. * )
  133. */
  134. function get_lists() {
  135. return $this->get_request($this->_clients_base_route.'lists.json');
  136. }
  137. /**
  138. * Gets the lists across a client to which a subscriber with a particular
  139. * email address belongs.
  140. * @param string $email_address Subscriber's email address.
  141. * @access public
  142. * @return CS_REST_Wrapper_Result A successful response will be an object of the form
  143. * array(
  144. * {
  145. * 'ListID' => The id of the list
  146. * 'ListName' => The name of the list
  147. * 'SubscriberState' => The state of the subscriber in the list
  148. * 'DateSubscriberAdded' => The date the subscriber was added
  149. * }
  150. * )
  151. */
  152. function get_lists_for_email($email_address) {
  153. return $this->get_request($this->_clients_base_route .
  154. 'listsforemail.json?email='.urlencode($email_address));
  155. }
  156. /**
  157. * Gets all list segments the current client has created
  158. * @access public
  159. * @return CS_REST_Wrapper_Result A successful response will be an object of the form
  160. * array(
  161. * {
  162. * 'ListID' => The id of the list owning this segment
  163. * 'SegmentID' => The id of this segment
  164. * 'Title' => The title of this segment
  165. * }
  166. * )
  167. */
  168. function get_segments() {
  169. return $this->get_request($this->_clients_base_route.'segments.json');
  170. }
  171. /**
  172. * Gets all email addresses on the current client's suppression list
  173. * @param int $page_number The page number to get
  174. * @param int $page_size The number of records per page
  175. * @param string $order_field The field to order the record set by ('EMAIL', 'DATE')
  176. * @param string $order_direction The direction to order the record set ('ASC', 'DESC')
  177. * @access public
  178. * @return CS_REST_Wrapper_Result A successful response will be an object of the form
  179. * {
  180. * 'ResultsOrderedBy' => The field the results are ordered by
  181. * 'OrderDirection' => The order direction
  182. * 'PageNumber' => The page number for the result set
  183. * 'PageSize' => The page size used
  184. * 'RecordsOnThisPage' => The number of records returned
  185. * 'TotalNumberOfRecords' => The total number of records available
  186. * 'NumberOfPages' => The total number of pages for this collection
  187. * 'Results' => array(
  188. * {
  189. * 'EmailAddress' => The suppressed email address
  190. * 'Date' => The date the email was suppressed
  191. * 'State' => The state of the suppressed email
  192. * }
  193. * )
  194. * }
  195. */
  196. function get_suppressionlist($page_number = NULL, $page_size = NULL, $order_field = NULL,
  197. $order_direction = NULL) {
  198. return $this->get_request_paged($this->_clients_base_route.'suppressionlist.json',
  199. $page_number, $page_size, $order_field, $order_direction, '?');
  200. }
  201. /**
  202. * Adds email addresses to a client's suppression list.
  203. * @param array<string> $emails The email addresses to suppress.
  204. * @access public
  205. */
  206. function suppress($emails) {
  207. $data = array('EmailAddresses' => $emails);
  208. return $this->post_request($this->_clients_base_route.'suppress.json', $data);
  209. }
  210. /**
  211. * Unsuppresses an email address by removing it from the the client's
  212. * suppression list.
  213. * @param string $email The email address to be unsuppressed
  214. * @access public
  215. */
  216. function unsuppress($email) {
  217. return $this->put_request($this->_clients_base_route.'unsuppress.json?email=' . urlencode($email), '');
  218. }
  219. /**
  220. * Gets all templates the current client has access to
  221. * @access public
  222. * @return CS_REST_Wrapper_Result A successful response will be an object of the form
  223. * array(
  224. * {
  225. * 'TemplateID' => The id of the template
  226. * 'Name' => The name of the template
  227. * 'PreviewURL' => The url to preview the template from
  228. * 'ScreenshotURL' => The url of the template screenshot
  229. * }
  230. * )
  231. */
  232. function get_templates() {
  233. return $this->get_request($this->_clients_base_route.'templates.json');
  234. }
  235. /**
  236. * Gets all templates the current client has access to
  237. * @access public
  238. * @return CS_REST_Wrapper_Result A successful response will be an object of the form
  239. * {
  240. * 'ApiKey' => The clients API Key, THIS IS NOT THE CLIENT ID
  241. * 'BasicDetails' =>
  242. * {
  243. * 'ClientID' => The id of the client
  244. * 'CompanyName' => The company name of the client
  245. * 'ContactName' => The contact name of the client
  246. * 'EmailAddress' => The clients contact email address
  247. * 'Country' => The clients country
  248. * 'TimeZone' => The clients timezone
  249. * }
  250. * 'BillingDetails' =>
  251. * If on monthly billing
  252. * {
  253. * 'CurrentTier' => The current monthly tier the client sits in
  254. * 'CurrentMonthlyRate' => The current pricing rate the client pays per month
  255. * 'MarkupPercentage' => The percentage markup applied to the base rates
  256. * 'Currency' => The currency paid in
  257. * 'ClientPays' => Whether the client pays for themselves,
  258. * 'MonthlyScheme' => Basic or Unlimited
  259. * }
  260. * If paying per campaign
  261. * {
  262. * 'CanPurchaseCredits' => Whether the client can purchase credits
  263. * 'Credits' => The number of credits belonging to the client
  264. * 'BaseDeliveryFee' => The base fee payable per campaign
  265. * 'BaseRatePerRecipient' => The base fee payable per campaign recipient
  266. * 'BaseDesignSpamTestRate' => The base fee payable per design and spam test
  267. * 'MarkupOnDelivery' => The markup applied per campaign
  268. * 'MarkupPerRecipient' => The markup applied per campaign recipient
  269. * 'MarkupOnDesignSpamTest' => The markup applied per design and spam test
  270. * 'Currency' => The currency fees are paid in
  271. * 'ClientPays' => Whether client client pays for themselves
  272. * }
  273. * }
  274. */
  275. function get() {
  276. return $this->get_request(trim($this->_clients_base_route, '/').'.json');
  277. }
  278. /**
  279. * Deletes an existing client from the system
  280. * @access public
  281. * @return CS_REST_Wrapper_Result A successful response will be empty
  282. */
  283. function delete() {
  284. return $this->delete_request(trim($this->_clients_base_route, '/').'.json');
  285. }
  286. /**
  287. * Creates a new client based on the provided information
  288. * @param array $client Basic information of the new client.
  289. * This should be an array of the form
  290. * array(
  291. * 'CompanyName' => The company name of the client
  292. * 'Country' => The clients country
  293. * 'TimeZone' => The clients timezone
  294. * )
  295. * @access public
  296. * @return CS_REST_Wrapper_Result A successful response will be the ID of the newly created client
  297. */
  298. function create($client) {
  299. if(isset($client['ContactName'])) {
  300. trigger_error('[DEPRECATION] Use Person->add to set name on a new person in a client. For now, we will create a default person with the name provided.', E_USER_NOTICE);
  301. }
  302. if(isset($client['EmailAddress'])) {
  303. trigger_error('[DEPRECATION] Use Person->add to set email on a new person in a client. For now, we will create a default person with the email provided.', E_USER_NOTICE);
  304. }
  305. return $this->post_request($this->_base_route.'clients.json', $client);
  306. }
  307. /**
  308. * Updates the basic information for a client
  309. * @param array $client_basics Basic information of the client.
  310. * This should be an array of the form
  311. * array(
  312. * 'CompanyName' => The company name of the client
  313. * 'Country' => The clients country
  314. * 'TimeZone' => The clients timezone
  315. * )
  316. * @access public
  317. * @return CS_REST_Wrapper_Result A successful response will be empty
  318. */
  319. function set_basics($client_basics) {
  320. if(isset($client['ContactName'])) {
  321. trigger_error('[DEPRECATION] Use person->update to set name on a particular person in a client. For now, we will update the default person with the name provided.', E_USER_NOTICE);
  322. }
  323. if(isset($client['EmailAddress'])) {
  324. trigger_error('[DEPRECATION] Use person->update to set email on a particular person in a client. For now, we will update the default person with the email address provided.', E_USER_NOTICE);
  325. }
  326. return $this->put_request($this->_clients_base_route.'setbasics.json', $client_basics);
  327. }
  328. /**
  329. * Updates the billing details of the current client, setting the client to the payg billing model
  330. * For clients not set to pay themselves then all fields below ClientPays are ignored
  331. * All Markup fields are optional
  332. * @param array $client_billing Payg billing details of the client.
  333. * This should be an array of the form
  334. * array(
  335. * 'Currency' => The currency fees are paid in
  336. * 'ClientPays' => Whether client client pays for themselves
  337. * 'MarkupPercentage' => Can be used to set the percentage markup for all unset fees
  338. * 'CanPurchaseCredits' => Whether the client can purchase credits
  339. * 'MarkupOnDelivery' => The markup applied per campaign
  340. * 'MarkupPerRecipient' => The markup applied per campaign recipient
  341. * 'MarkupOnDesignSpamTest' => The markup applied per design and spam test
  342. * )
  343. * @access public
  344. * @return CS_REST_Wrapper_Result A successful response will be empty
  345. */
  346. function set_payg_billing($client_billing) {
  347. return $this->put_request($this->_clients_base_route.'setpaygbilling.json', $client_billing);
  348. }
  349. /**
  350. * Updates the billing details of the current client, setting the client to the monthly billing model
  351. * For clients not set to pay themselves then the markup percentage field is ignored
  352. * @param array $client_billing Payg billing details of the client.
  353. * This should be an array of the form
  354. * array(
  355. * 'Currency' => The currency fees are paid in
  356. * 'ClientPays' => Whether client client pays for themselves
  357. * 'MarkupPercentage' => Sets the percentage markup used for all monthly tiers
  358. * )
  359. * @access public
  360. * @return CS_REST_Wrapper_Result A successful response will be empty
  361. */
  362. function set_monthly_billing($client_billing) {
  363. return $this->put_request($this->_clients_base_route.'setmonthlybilling.json', $client_billing);
  364. }
  365. /**
  366. * Transfer credits to or from this client.
  367. *
  368. * @param array $transfer_data Details for the credit transfer. This array
  369. * should be of the form:
  370. * array(
  371. * 'Credits' => An in representing the number of credits to transfer.
  372. * This value may be either positive if you want to allocate credits
  373. * from your account to the client, or negative if you want to
  374. * deduct credits from the client back into your account.
  375. * 'CanUseMyCreditsWhenTheyRunOut' => A boolean value which if set
  376. * to true, will allow the client to continue sending using your
  377. * credits or payment details once they run out of credits, and if
  378. * set to false, will prevent the client from using your credits to
  379. * continue sending until you allocate more credits to them.
  380. * )
  381. * @access public
  382. * @return CS_REST_Wrapper_Result A successful response will be an object
  383. * of the form:
  384. * {
  385. * 'AccountCredits' => Integer representing credits in your account now
  386. * 'ClientCredits' => Integer representing credits in this client's
  387. * account now
  388. * }
  389. */
  390. function transfer_credits($transfer_data) {
  391. return $this->post_request($this->_clients_base_route.'credits.json',
  392. $transfer_data);
  393. }
  394. /**
  395. * returns the people associated with this client.
  396. * @return CS_REST_Wrapper_Result A successful response will be an object of the form
  397. * array({
  398. * 'EmailAddress' => the email address of the person
  399. * 'Name' => the name of the person
  400. * 'AccessLevel' => the access level of the person
  401. * 'Status' => the status of the person
  402. * })
  403. */
  404. function get_people() {
  405. return $this->get_request($this->_clients_base_route.'people.json');
  406. }
  407. /**
  408. * retrieves the email address of the primary contact for this client
  409. * @return CS_REST_Wrapper_Result a successful response will be an array in the form:
  410. * array('EmailAddress'=> email address of primary contact)
  411. */
  412. function get_primary_contact() {
  413. return $this->get_request($this->_clients_base_route.'primarycontact.json');
  414. }
  415. /**
  416. * assigns the primary contact for this client to the person with the specified email address
  417. * @param string $emailAddress the email address of the person designated to be the primary contact
  418. * @return CS_REST_Wrapper_Result a successful response will be an array in the form:
  419. * array('EmailAddress'=> email address of primary contact)
  420. */
  421. function set_primary_contact($emailAddress) {
  422. return $this->put_request($this->_clients_base_route.'primarycontact.json?email=' . urlencode($emailAddress), '');
  423. }
  424. }