| 1 |
<?php |
| 2 |
// @codingStandardsIgnoreFile |
| 3 |
|
| 4 |
class AWeberEntry extends AWeberResponse { |
| 5 |
|
| 6 |
/** |
| 7 |
* @var array Holds list of data keys that are not publicly accessible |
| 8 |
*/ |
| 9 |
protected $_privateData = array( |
| 10 |
'resource_type_link', |
| 11 |
'http_etag', |
| 12 |
); |
| 13 |
|
| 14 |
/** |
| 15 |
* @var array Stores local modifications that have not been saved |
| 16 |
*/ |
| 17 |
protected $_localDiff = array(); |
| 18 |
|
| 19 |
/** |
| 20 |
* @var array Holds AWeberCollection objects already instantiated, keyed by |
| 21 |
* their resource name (plural) |
| 22 |
*/ |
| 23 |
protected $_collections = array(); |
| 24 |
|
| 25 |
/** |
| 26 |
* attrs |
| 27 |
* |
| 28 |
* Provides a simple array of all the available data (and collections) available |
| 29 |
* in this entry. |
| 30 |
* |
| 31 |
* @access public |
| 32 |
* @return array |
| 33 |
*/ |
| 34 |
public function attrs() { |
| 35 |
$attrs = array(); |
| 36 |
foreach ($this->data as $key => $value) { |
| 37 |
if (!in_array($key, $this->_privateData) && !strpos($key, 'collection_link')) { |
| 38 |
$attrs[$key] = $value; |
| 39 |
} |
| 40 |
} |
| 41 |
if (!empty(AWeberAPI::$_collectionMap[$this->type])) { |
| 42 |
foreach (AWeberAPI::$_collectionMap[$this->type] as $child) { |
| 43 |
$attrs[$child] = 'collection'; |
| 44 |
} |
| 45 |
} |
| 46 |
return $attrs; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* _type |
| 51 |
* |
| 52 |
* Used to pull the name of this resource from its resource_type_link |
| 53 |
* @access protected |
| 54 |
* @return String |
| 55 |
*/ |
| 56 |
protected function _type() { |
| 57 |
if (empty($this->type)) { |
| 58 |
$typeLink = $this->data['resource_type_link']; |
| 59 |
if (empty($typeLink)) return null; |
| 60 |
list($url, $type) = explode('#', $typeLink); |
| 61 |
$this->type = $type; |
| 62 |
} |
| 63 |
return $this->type; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* delete |
| 68 |
* |
| 69 |
* Delete this object from the AWeber system. May not be supported |
| 70 |
* by all entry types. |
| 71 |
* @access public |
| 72 |
* @return boolean Returns true if it is successfully deleted, false |
| 73 |
* if the delete request failed. |
| 74 |
*/ |
| 75 |
public function delete() { |
| 76 |
$this->adapter->request('DELETE', $this->url, array(), array('return' => 'status')); |
| 77 |
return true; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* move |
| 82 |
* |
| 83 |
* Invoke the API method to MOVE an entry resource to a different List. |
| 84 |
* |
| 85 |
* Note: Not all entry resources are eligible to be moved, please |
| 86 |
* refer to the AWeber API Reference Documentation at |
| 87 |
* https://labs.aweber.com/docs/reference/1.0 for more |
| 88 |
* details on which entry resources may be moved and if there |
| 89 |
* are any requirements for moving that resource. |
| 90 |
* |
| 91 |
* @access public |
| 92 |
* @param AWeberEntry(List) List to move Resource (this) too. |
| 93 |
* @return mixed AWeberEntry(Resource) Resource created on List ($list) |
| 94 |
* or False if resource was not created. |
| 95 |
*/ |
| 96 |
public function move($list, $last_followup_message_number_sent=NULL) { |
| 97 |
# Move Resource |
| 98 |
$params = array( |
| 99 |
'ws.op' => 'move', |
| 100 |
'list_link' => $list->self_link |
| 101 |
); |
| 102 |
if (isset($last_followup_message_number_sent)) { |
| 103 |
$params['last_followup_message_number_sent'] = $last_followup_message_number_sent; |
| 104 |
} |
| 105 |
|
| 106 |
$data = $this->adapter->request('POST', $this->url, $params, array('return' => 'headers')); |
| 107 |
|
| 108 |
# Return new Resource |
| 109 |
$url = $data['Location']; |
| 110 |
$resource_data = $this->adapter->request('GET', $url); |
| 111 |
return new AWeberEntry($resource_data, $url, $this->adapter); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* save |
| 116 |
* |
| 117 |
* Saves the current state of this object if it has been changed. |
| 118 |
* @access public |
| 119 |
* @return void |
| 120 |
*/ |
| 121 |
public function save() { |
| 122 |
if (!empty($this->_localDiff)) { |
| 123 |
$data = $this->adapter->request('PATCH', $this->url, $this->_localDiff, array('return' => 'status')); |
| 124 |
} |
| 125 |
$this->_localDiff = array(); |
| 126 |
return true; |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* __get |
| 132 |
* |
| 133 |
* Used to look up items in data, and special properties like type and |
| 134 |
* child collections dynamically. |
| 135 |
* |
| 136 |
* @param String $value Attribute being accessed |
| 137 |
* @access public |
| 138 |
* @throws AWeberResourceNotImplemented |
| 139 |
* @return mixed |
| 140 |
*/ |
| 141 |
public function __get($value) { |
| 142 |
if (in_array($value, $this->_privateData)) { |
| 143 |
return null; |
| 144 |
} |
| 145 |
if (!empty($this->data) && array_key_exists($value, $this->data)) { |
| 146 |
if (is_array($this->data[$value])) { |
| 147 |
$array = new AWeberEntryDataArray($this->data[$value], $value, $this); |
| 148 |
$this->data[$value] = $array; |
| 149 |
} |
| 150 |
return $this->data[$value]; |
| 151 |
} |
| 152 |
if ($value == 'type') return $this->_type(); |
| 153 |
if ($this->_isChildCollection($value)) { |
| 154 |
return $this->_getCollection($value); |
| 155 |
} |
| 156 |
throw new AWeberResourceNotImplemented($this, $value); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* __set |
| 161 |
* |
| 162 |
* If the key provided is part of the data array, then update it in the |
| 163 |
* data array. Otherwise, use the default __set() behavior. |
| 164 |
* |
| 165 |
* @param mixed $key Key of the attr being set |
| 166 |
* @param mixed $value Value being set to the $key attr |
| 167 |
* @access public |
| 168 |
*/ |
| 169 |
public function __set($key, $value) { |
| 170 |
if (array_key_exists($key, $this->data)) { |
| 171 |
$this->_localDiff[$key] = $value; |
| 172 |
return $this->data[$key] = $value; |
| 173 |
} else { |
| 174 |
return parent::__set($key, $value); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* findSubscribers |
| 180 |
* |
| 181 |
* Looks through all lists for subscribers |
| 182 |
* that match the given filter |
| 183 |
* @access public |
| 184 |
* @return AWeberCollection |
| 185 |
*/ |
| 186 |
public function findSubscribers($search_data) { |
| 187 |
$this->_methodFor(array('account')); |
| 188 |
$params = array_merge($search_data, array('ws.op' => 'findSubscribers')); |
| 189 |
$data = $this->adapter->request('GET', $this->url, $params); |
| 190 |
|
| 191 |
$ts_params = array_merge($params, array('ws.show' => 'total_size')); |
| 192 |
$total_size = $this->adapter->request('GET', $this->url, $ts_params, array('return' => 'integer')); |
| 193 |
|
| 194 |
# return collection |
| 195 |
$data['total_size'] = $total_size; |
| 196 |
$url = $this->url . '?'. http_build_query($params); |
| 197 |
return new AWeberCollection($data, $url, $this->adapter); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* getActivity |
| 202 |
* |
| 203 |
* Returns analytics activity for a given subscriber |
| 204 |
* @access public |
| 205 |
* @return AWeberCollection |
| 206 |
*/ |
| 207 |
public function getActivity() { |
| 208 |
$this->_methodFor(array('subscriber')); |
| 209 |
$params = array('ws.op' => 'getActivity'); |
| 210 |
$data = $this->adapter->request('GET', $this->url, $params); |
| 211 |
|
| 212 |
$ts_params = array_merge($params, array('ws.show' => 'total_size')); |
| 213 |
$total_size = $this->adapter->request('GET', $this->url, $ts_params, array('return' => 'integer')); |
| 214 |
|
| 215 |
# return collection |
| 216 |
$data['total_size'] = $total_size; |
| 217 |
$url = $this->url . '?'. http_build_query($params); |
| 218 |
return new AWeberCollection($data, $url, $this->adapter); |
| 219 |
} |
| 220 |
|
| 221 |
/** getParentEntry |
| 222 |
* |
| 223 |
* Gets an entry's parent entry |
| 224 |
* Returns NULL if no parent entry |
| 225 |
*/ |
| 226 |
public function getParentEntry(){ |
| 227 |
$url_parts = explode('/', $this->url); |
| 228 |
$size = count($url_parts); |
| 229 |
|
| 230 |
#Remove entry id and slash from end of url |
| 231 |
$url = substr($this->url, 0, -strlen($url_parts[$size-1])-1); |
| 232 |
|
| 233 |
#Remove collection name and slash from end of url |
| 234 |
$url = substr($url, 0, -strlen($url_parts[$size-2])-1); |
| 235 |
|
| 236 |
try { |
| 237 |
$data = $this->adapter->request('GET', $url); |
| 238 |
return new AWeberEntry($data, $url, $this->adapter); |
| 239 |
} catch (Exception $e) { |
| 240 |
return NULL; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* getWebForms |
| 246 |
* |
| 247 |
* Gets all web_forms for this account |
| 248 |
* @access public |
| 249 |
* @return array |
| 250 |
*/ |
| 251 |
public function getWebForms() { |
| 252 |
$this->_methodFor(array('account')); |
| 253 |
$data = $this->adapter->request('GET', $this->url.'?ws.op=getWebForms', array(), |
| 254 |
array('allow_empty' => true)); |
| 255 |
return $this->_parseNamedOperation($data); |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
/** |
| 260 |
* getWebFormSplitTests |
| 261 |
* |
| 262 |
* Gets all web_form split tests for this account |
| 263 |
* @access public |
| 264 |
* @return array |
| 265 |
*/ |
| 266 |
public function getWebFormSplitTests() { |
| 267 |
$this->_methodFor(array('account')); |
| 268 |
$data = $this->adapter->request('GET', $this->url.'?ws.op=getWebFormSplitTests', array(), |
| 269 |
array('allow_empty' => true)); |
| 270 |
return $this->_parseNamedOperation($data); |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* _parseNamedOperation |
| 275 |
* |
| 276 |
* Turns a dumb array of json into an array of Entries. This is NOT |
| 277 |
* a collection, but simply an array of entries, as returned from a |
| 278 |
* named operation. |
| 279 |
* |
| 280 |
* @param array $data |
| 281 |
* @access protected |
| 282 |
* @return array |
| 283 |
*/ |
| 284 |
protected function _parseNamedOperation($data) { |
| 285 |
$results = array(); |
| 286 |
foreach($data as $entryData) { |
| 287 |
$results[] = new AWeberEntry($entryData, str_replace($this->adapter->app->getBaseUri(), '', |
| 288 |
$entryData['self_link']), $this->adapter); |
| 289 |
} |
| 290 |
return $results; |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* _methodFor |
| 295 |
* |
| 296 |
* Raises exception if $this->type is not in array entryTypes. |
| 297 |
* Used to restrict methods to specific entry type(s). |
| 298 |
* @param mixed $entryTypes Array of entry types as strings, ie array('account') |
| 299 |
* @access protected |
| 300 |
* @return void |
| 301 |
*/ |
| 302 |
protected function _methodFor($entryTypes) { |
| 303 |
if (in_array($this->type, $entryTypes)) return true; |
| 304 |
throw new AWeberMethodNotImplemented($this); |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* _getCollection |
| 309 |
* |
| 310 |
* Returns the AWeberCollection object representing the given |
| 311 |
* collection name, relative to this entry. |
| 312 |
* |
| 313 |
* @param String $value The name of the sub-collection |
| 314 |
* @access protected |
| 315 |
* @return AWeberCollection |
| 316 |
*/ |
| 317 |
protected function _getCollection($value) { |
| 318 |
if (empty($this->_collections[$value])) { |
| 319 |
$url = "{$this->url}/{$value}"; |
| 320 |
$data = $this->adapter->request('GET', $url); |
| 321 |
$this->_collections[$value] = new AWeberCollection($data, $url, $this->adapter); |
| 322 |
} |
| 323 |
return $this->_collections[$value]; |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
/** |
| 328 |
* _isChildCollection |
| 329 |
* |
| 330 |
* Is the given name of a collection a child collection of this entry? |
| 331 |
* |
| 332 |
* @param String $value The name of the collection we are looking for |
| 333 |
* @access protected |
| 334 |
* @return boolean |
| 335 |
* @throws AWeberResourceNotImplemented |
| 336 |
*/ |
| 337 |
protected function _isChildCollection($value) { |
| 338 |
$this->_type(); |
| 339 |
if (!empty(AWeberAPI::$_collectionMap[$this->type]) && |
| 340 |
in_array($value, AWeberAPI::$_collectionMap[$this->type])) return true; |
| 341 |
return false; |
| 342 |
} |
| 343 |
|
| 344 |
} |
| 345 |
|