csrest_segments.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. require_once dirname(__FILE__).'/class/base_classes.php';
  3. /**
  4. * Class to access a segments resources from the create send API.
  5. * This class includes functions to create and edits segments
  6. * along with accessing the subscribers of a specific segment
  7. * @author tobyb
  8. *
  9. */
  10. class CS_REST_Segments extends CS_REST_Wrapper_Base {
  11. /**
  12. * The base route of the lists resource.
  13. * @var string
  14. * @access private
  15. */
  16. var $_segments_base_route;
  17. /**
  18. * Constructor.
  19. * @param $segment_id string The segment 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. $segment_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_segment_id($segment_id);
  48. }
  49. /**
  50. * Change the segment id used for calls after construction
  51. * @param $segment_id
  52. * @access public
  53. */
  54. function set_segment_id($segment_id) {
  55. $this->_segments_base_route = $this->_base_route.'segments/'.$segment_id;
  56. }
  57. /**
  58. * Creates a new segment on the given list with the provided details
  59. * @param int $list_id The list on which to create the segment
  60. * @param $segment_details The details of the new segment
  61. * This should be an array of the form
  62. * array(
  63. * 'Title' => The title of the new segment
  64. * 'RuleGroups' => array(
  65. * array(
  66. * 'Rules' => array(
  67. * array(
  68. * 'RuleType' => The subject of this rule
  69. * 'Clause' => The specific clauses for this rule
  70. * )
  71. * )
  72. * )
  73. * )
  74. * )
  75. * @return CS_REST_Wrapper_Result A successful response will be the ID of the newly created segment
  76. */
  77. function create($list_id, $segment_details) {
  78. return $this->post_request($this->_base_route.'segments/'.$list_id.'.json', $segment_details);
  79. }
  80. /**
  81. * Updates the current segment with the provided details. Calls to this route will clear any existing rules
  82. * @param $segment_details The new details for the segment
  83. * This should be an array of the form
  84. * array(
  85. * 'Title' => The title of the new segment
  86. * 'RuleGroups' => array(
  87. * array(
  88. * 'Rules' => array(
  89. * array(
  90. * 'RuleType' => The subject of this rule
  91. * 'Clause' => The specific clauses for this rule
  92. * )
  93. * )
  94. * )
  95. * )
  96. * )
  97. * @return CS_REST_Wrapper_Result A successful response will be empty
  98. */
  99. function update($segment_details) {
  100. return $this->put_request($this->_segments_base_route.'.json', $segment_details);
  101. }
  102. /**
  103. * Adds the given rule to the current segment
  104. * @param $rule The rule to add to the segment
  105. * This should be an array of the form
  106. * array(
  107. * 'Rules' => array(
  108. * array(
  109. * 'RuleType' => The subject of this rule
  110. * 'Clause' => The specific clauses for this rule
  111. * )
  112. * )
  113. * )
  114. * @return CS_REST_Wrapper_Result A successful response will be empty
  115. */
  116. function add_rulegroup($rulegroup) {
  117. return $this->post_request($this->_segments_base_route.'/rules.json', $rulegroup);
  118. }
  119. /**
  120. * Gets the details of the current segment
  121. * @access public
  122. * @return CS_REST_Wrapper_Result A successful response will be an object of the form
  123. * {
  124. * 'ActiveSubscribers' => The number of active subscribers in this segment
  125. * 'Rules' => array(
  126. * {
  127. * 'Subject' => The subject of the rule
  128. * 'Clauses' => array<string> The clauses making up this segment rule
  129. * }
  130. * ),
  131. * 'ListID' => The ID of the list on which this segment is applied
  132. * 'SegmentID' => The ID of this segment
  133. * 'Title' => The title of this segment
  134. * }
  135. */
  136. function get() {
  137. return $this->get_request($this->_segments_base_route.'.json');
  138. }
  139. /**
  140. * Deletes an existing segment from the system
  141. * @access public
  142. * @return CS_REST_Wrapper_Result A successful response will be empty
  143. */
  144. function delete() {
  145. return $this->delete_request($this->_segments_base_route.'.json');
  146. }
  147. /**
  148. * Deletes all rules for the current segment
  149. * @access public
  150. * @return CS_REST_Wrapper_Result A successful response will be empty
  151. */
  152. function clear_rules() {
  153. return $this->delete_request($this->_segments_base_route.'/rules.json');
  154. }
  155. /**
  156. * Gets a paged collection of subscribers which fall into the given segment
  157. * @param string $subscribed_since The date to start getting subscribers from
  158. * @param int $page_number The page number to get
  159. * @param int $page_size The number of records per page
  160. * @param string $order_field The field to order the record set by ('EMAIL', 'NAME', 'DATE')
  161. * @param string $order_direction The direction to order the record set ('ASC', 'DESC')
  162. * @access public
  163. * @return CS_REST_Wrapper_Result A successful response will be an object of the form
  164. * {
  165. * 'ResultsOrderedBy' => The field the results are ordered by
  166. * 'OrderDirection' => The order direction
  167. * 'PageNumber' => The page number for the result set
  168. * 'PageSize' => The page size used
  169. * 'RecordsOnThisPage' => The number of records returned
  170. * 'TotalNumberOfRecords' => The total number of records available
  171. * 'NumberOfPages' => The total number of pages for this collection
  172. * 'Results' => array(
  173. * {
  174. * 'EmailAddress' => The email address of the subscriber
  175. * 'Name' => The name of the subscriber
  176. * 'Date' => The date that the subscriber was added to the list
  177. * 'State' => The current state of the subscriber, will be 'Active'
  178. * 'CustomFields' => array (
  179. * {
  180. * 'Key' => The personalisation tag of the custom field
  181. * 'Value' => The value of the custom field for this subscriber
  182. * }
  183. * )
  184. * }
  185. * )
  186. * }
  187. */
  188. function get_subscribers($subscribed_since = '', $page_number = NULL,
  189. $page_size = NULL, $order_field = NULL, $order_direction = NULL) {
  190. return $this->get_request_paged($this->_segments_base_route.'/active.json?date='.urlencode($subscribed_since),
  191. $page_number, $page_size, $order_field, $order_direction);
  192. }
  193. }