-
Notifications
You must be signed in to change notification settings - Fork 106
/
csrest_segments.php
210 lines (197 loc) · 9.17 KB
/
csrest_segments.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
require_once dirname(__FILE__).'/class/base_classes.php';
/**
* Class to access a segments resources from the create send API.
* This class includes functions to create and edits segments
* along with accessing the subscribers of a specific segment
* @author tobyb
*
*/
if (!class_exists('CS_REST_Segments')) {
class CS_REST_Segments extends CS_REST_Wrapper_Base {
/**
* The base route of the lists resource.
* @var string
* @access private
*/
var $_segments_base_route;
/**
* Constructor.
* @param $segment_id string The segment id to access (Ignored for create requests)
* @param $auth_details array Authentication details to use for API calls.
* This array must take one of the following forms:
* If using OAuth to authenticate:
* array(
* 'access_token' => 'your access token',
* 'refresh_token' => 'your refresh token')
*
* Or if using an API key:
* array('api_key' => 'your api key')
* @param $protocol string The protocol to use for requests (http|https)
* @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
* @param $host string The host to send API requests to. There is no need to change this
* @param $log CS_REST_Log The logger to use. Used for dependency injection
* @param $serialiser The serialiser to use. Used for dependency injection
* @param $transport The transport to use. Used for dependency injection
* @access public
*/
function __construct (
$segment_id,
$auth_details,
$protocol = 'https',
$debug_level = CS_REST_LOG_NONE,
$host = 'api.createsend.com',
$log = NULL,
$serialiser = NULL,
$transport = NULL) {
parent::__construct($auth_details, $protocol, $debug_level, $host, $log, $serialiser, $transport);
$this->set_segment_id($segment_id);
}
/**
* Change the segment id used for calls after construction
* @param $segment_id
* @access public
*/
function set_segment_id($segment_id) {
$this->_segments_base_route = $this->_base_route.'segments/'.$segment_id;
}
/**
* Creates a new segment on the given list with the provided details
* @param int $list_id The list on which to create the segment
* @param $segment_details The details of the new segment
* This should be an array of the form
* array(
* 'Title' => The title of the new segment
* 'RuleGroups' => array(
* array(
* 'Rules' => array(
* array(
* 'RuleType' => The subject of this rule
* 'Clause' => The specific clauses for this rule
* )
* )
* )
* )
* )
* @return CS_REST_Wrapper_Result A successful response will be the ID of the newly created segment
*/
function create($list_id, $segment_details) {
return $this->post_request($this->_base_route.'segments/'.$list_id.'.json', $segment_details);
}
/**
* Updates the current segment with the provided details. Calls to this route will clear any existing rules
* @param $segment_details The new details for the segment
* This should be an array of the form
* array(
* 'Title' => The title of the new segment
* 'RuleGroups' => array(
* array(
* 'Rules' => array(
* array(
* 'RuleType' => The subject of this rule
* 'Clause' => The specific clauses for this rule
* )
* )
* )
* )
* )
* @return CS_REST_Wrapper_Result A successful response will be empty
*/
function update($segment_details) {
return $this->put_request($this->_segments_base_route.'.json', $segment_details);
}
/**
* Adds the given rule to the current segment
* @param $rule The rule to add to the segment
* This should be an array of the form
* array(
* 'Rules' => array(
* array(
* 'RuleType' => The subject of this rule
* 'Clause' => The specific clauses for this rule
* )
* )
* )
* @return CS_REST_Wrapper_Result A successful response will be empty
*/
function add_rulegroup($rulegroup) {
return $this->post_request($this->_segments_base_route.'/rules.json', $rulegroup);
}
/**
* Gets the details of the current segment
* @access public
* @return CS_REST_Wrapper_Result A successful response will be an object of the form
* {
* 'ActiveSubscribers' => The number of active subscribers in this segment
* 'Rules' => array(
* {
* 'Subject' => The subject of the rule
* 'Clauses' => array<string> The clauses making up this segment rule
* }
* ),
* 'ListID' => The ID of the list on which this segment is applied
* 'SegmentID' => The ID of this segment
* 'Title' => The title of this segment
* }
*/
function get() {
return $this->get_request($this->_segments_base_route.'.json');
}
/**
* Deletes an existing segment from the system
* @access public
* @return CS_REST_Wrapper_Result A successful response will be empty
*/
function delete() {
return $this->delete_request($this->_segments_base_route.'.json');
}
/**
* Deletes all rules for the current segment
* @access public
* @return CS_REST_Wrapper_Result A successful response will be empty
*/
function clear_rules() {
return $this->delete_request($this->_segments_base_route.'/rules.json');
}
/**
* Gets a paged collection of subscribers which fall into the given segment
* @param string $subscribed_since The date to start getting subscribers from
* @param int $page_number The page number to get
* @param int $page_size The number of records per page
* @param string $order_field The field to order the record set by ('EMAIL', 'NAME', 'DATE')
* @param string $order_direction The direction to order the record set ('ASC', 'DESC')
* @param boolean $include_tracking_pref Whether or not to return the ConsentToTrack value (true, false)
* @access public
* @return CS_REST_Wrapper_Result A successful response will be an object of the form
* {
* 'ResultsOrderedBy' => The field the results are ordered by
* 'OrderDirection' => The order direction
* 'PageNumber' => The page number for the result set
* 'PageSize' => The page size used
* 'RecordsOnThisPage' => The number of records returned
* 'TotalNumberOfRecords' => The total number of records available
* 'NumberOfPages' => The total number of pages for this collection
* 'Results' => array(
* {
* 'EmailAddress' => The email address of the subscriber
* 'Name' => The name of the subscriber
* 'Date' => The date when the status of subscriber last changed.
* 'ListJoinedDate' => The date the subscriber was first added to the list
* 'State' => The current state of the subscriber, will be 'Active'
* 'CustomFields' => array (
* {
* 'Key' => The personalisation tag of the custom field
* 'Value' => The value of the custom field for this subscriber
* }
* )
* }
* )
* }
*/
function get_subscribers($subscribed_since = '', $page_number = NULL,
$page_size = NULL, $order_field = NULL, $order_direction = NULL, $include_tracking_pref = NULL) {
return $this->get_request_paged($this->_segments_base_route.'/active.json?date='.urlencode($subscribed_since),
$page_number, $page_size, $order_field, $order_direction, $include_tracking_pref);
}
}
}