PageRenderTime 70ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/builder/subscription/aweber/aweber_entry.php

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