aweber_entry.php
10.7 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<?php
class AWeberEntry extends AWeberResponse {
/**
* @var array Holds list of data keys that are not publicly accessible
*/
protected $_privateData = array(
'resource_type_link',
'http_etag',
);
/**
* @var array Stores local modifications that have not been saved
*/
protected $_localDiff = array();
/**
* @var array Holds AWeberCollection objects already instantiated, keyed by
* their resource name (plural)
*/
protected $_collections = array();
/**
* attrs
*
* Provides a simple array of all the available data (and collections) available
* in this entry.
*
* @access public
* @return array
*/
public function attrs() {
$attrs = array();
foreach ($this->data as $key => $value) {
if (!in_array($key, $this->_privateData) && !strpos($key, 'collection_link')) {
$attrs[$key] = $value;
}
}
if (!empty(AWeberAPI::$_collectionMap[$this->type])) {
foreach (AWeberAPI::$_collectionMap[$this->type] as $child) {
$attrs[$child] = 'collection';
}
}
return $attrs;
}
/**
* _type
*
* Used to pull the name of this resource from its resource_type_link
* @access protected
* @return String
*/
protected function _type() {
if (empty($this->type)) {
$typeLink = $this->data['resource_type_link'];
if (empty($typeLink)) return null;
list($url, $type) = explode('#', $typeLink);
$this->type = $type;
}
return $this->type;
}
/**
* delete
*
* Delete this object from the AWeber system. May not be supported
* by all entry types.
* @access public
* @return boolean Returns true if it is successfully deleted, false
* if the delete request failed.
*/
public function delete() {
$this->adapter->request('DELETE', $this->url, array(), array('return' => 'status'));
return true;
}
/**
* move
*
* Invoke the API method to MOVE an entry resource to a different List.
*
* Note: Not all entry resources are eligible to be moved, please
* refer to the AWeber API Reference Documentation at
* https://labs.aweber.com/docs/reference/1.0 for more
* details on which entry resources may be moved and if there
* are any requirements for moving that resource.
*
* @access public
* @param AWeberEntry(List) List to move Resource (this) too.
* @return mixed AWeberEntry(Resource) Resource created on List ($list)
* or False if resource was not created.
*/
public function move($list, $last_followup_message_number_sent=NULL) {
# Move Resource
$params = array(
'ws.op' => 'move',
'list_link' => $list->self_link
);
if (isset($last_followup_message_number_sent)) {
$params['last_followup_message_number_sent'] = $last_followup_message_number_sent;
}
$data = $this->adapter->request('POST', $this->url, $params, array('return' => 'headers'));
# Return new Resource
$url = $data['Location'];
$resource_data = $this->adapter->request('GET', $url);
return new AWeberEntry($resource_data, $url, $this->adapter);
}
/**
* save
*
* Saves the current state of this object if it has been changed.
* @access public
* @return void
*/
public function save() {
if (!empty($this->_localDiff)) {
$data = $this->adapter->request('PATCH', $this->url, $this->_localDiff, array('return' => 'status'));
}
$this->_localDiff = array();
return true;
}
/**
* __get
*
* Used to look up items in data, and special properties like type and
* child collections dynamically.
*
* @param String $value Attribute being accessed
* @access public
* @throws AWeberResourceNotImplemented
* @return mixed
*/
public function __get($value) {
if (in_array($value, $this->_privateData)) {
return null;
}
if (!empty($this->data) && array_key_exists($value, $this->data)) {
if (is_array($this->data[$value])) {
$array = new AWeberEntryDataArray($this->data[$value], $value, $this);
$this->data[$value] = $array;
}
return $this->data[$value];
}
if ($value == 'type') return $this->_type();
if ($this->_isChildCollection($value)) {
return $this->_getCollection($value);
}
throw new AWeberResourceNotImplemented($this, $value);
}
/**
* __set
*
* If the key provided is part of the data array, then update it in the
* data array. Otherwise, use the default __set() behavior.
*
* @param mixed $key Key of the attr being set
* @param mixed $value Value being set to the $key attr
* @access public
*/
public function __set($key, $value) {
if (array_key_exists($key, $this->data)) {
$this->_localDiff[$key] = $value;
return $this->data[$key] = $value;
} else {
return parent::__set($key, $value);
}
}
/**
* findSubscribers
*
* Looks through all lists for subscribers
* that match the given filter
* @access public
* @return AWeberCollection
*/
public function findSubscribers($search_data) {
$this->_methodFor(array('account'));
$params = array_merge($search_data, array('ws.op' => 'findSubscribers'));
$data = $this->adapter->request('GET', $this->url, $params);
$ts_params = array_merge($params, array('ws.show' => 'total_size'));
$total_size = $this->adapter->request('GET', $this->url, $ts_params, array('return' => 'integer'));
# return collection
$data['total_size'] = $total_size;
$url = $this->url . '?'. http_build_query($params);
return new AWeberCollection($data, $url, $this->adapter);
}
/**
* getActivity
*
* Returns analytics activity for a given subscriber
* @access public
* @return AWeberCollection
*/
public function getActivity() {
$this->_methodFor(array('subscriber'));
$params = array('ws.op' => 'getActivity');
$data = $this->adapter->request('GET', $this->url, $params);
$ts_params = array_merge($params, array('ws.show' => 'total_size'));
$total_size = $this->adapter->request('GET', $this->url, $ts_params, array('return' => 'integer'));
# return collection
$data['total_size'] = $total_size;
$url = $this->url . '?'. http_build_query($params);
return new AWeberCollection($data, $url, $this->adapter);
}
/** getParentEntry
*
* Gets an entry's parent entry
* Returns NULL if no parent entry
*/
public function getParentEntry(){
$url_parts = explode('/', $this->url);
$size = count($url_parts);
#Remove entry id and slash from end of url
$url = substr($this->url, 0, -strlen($url_parts[$size-1])-1);
#Remove collection name and slash from end of url
$url = substr($url, 0, -strlen($url_parts[$size-2])-1);
try {
$data = $this->adapter->request('GET', $url);
return new AWeberEntry($data, $url, $this->adapter);
} catch (Exception $e) {
return NULL;
}
}
/**
* getWebForms
*
* Gets all web_forms for this account
* @access public
* @return array
*/
public function getWebForms() {
$this->_methodFor(array('account'));
$data = $this->adapter->request('GET', $this->url.'?ws.op=getWebForms', array(),
array('allow_empty' => true));
return $this->_parseNamedOperation($data);
}
/**
* getWebFormSplitTests
*
* Gets all web_form split tests for this account
* @access public
* @return array
*/
public function getWebFormSplitTests() {
$this->_methodFor(array('account'));
$data = $this->adapter->request('GET', $this->url.'?ws.op=getWebFormSplitTests', array(),
array('allow_empty' => true));
return $this->_parseNamedOperation($data);
}
/**
* _parseNamedOperation
*
* Turns a dumb array of json into an array of Entries. This is NOT
* a collection, but simply an array of entries, as returned from a
* named operation.
*
* @param array $data
* @access protected
* @return array
*/
protected function _parseNamedOperation($data) {
$results = array();
foreach($data as $entryData) {
$results[] = new AWeberEntry($entryData, str_replace($this->adapter->app->getBaseUri(), '',
$entryData['self_link']), $this->adapter);
}
return $results;
}
/**
* _methodFor
*
* Raises exception if $this->type is not in array entryTypes.
* Used to restrict methods to specific entry type(s).
* @param mixed $entryTypes Array of entry types as strings, ie array('account')
* @access protected
* @return void
*/
protected function _methodFor($entryTypes) {
if (in_array($this->type, $entryTypes)) return true;
throw new AWeberMethodNotImplemented($this);
}
/**
* _getCollection
*
* Returns the AWeberCollection object representing the given
* collection name, relative to this entry.
*
* @param String $value The name of the sub-collection
* @access protected
* @return AWeberCollection
*/
protected function _getCollection($value) {
if (empty($this->_collections[$value])) {
$url = "{$this->url}/{$value}";
$data = $this->adapter->request('GET', $url);
$this->_collections[$value] = new AWeberCollection($data, $url, $this->adapter);
}
return $this->_collections[$value];
}
/**
* _isChildCollection
*
* Is the given name of a collection a child collection of this entry?
*
* @param String $value The name of the collection we are looking for
* @access protected
* @return boolean
* @throws AWeberResourceNotImplemented
*/
protected function _isChildCollection($value) {
$this->_type();
if (!empty(AWeberAPI::$_collectionMap[$this->type]) &&
in_array($value, AWeberAPI::$_collectionMap[$this->type])) return true;
return false;
}
}