PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/php/WufooApiWrapper.php

https://github.com/Fourshift/Wufoo-API-Wrappers
PHP | 270 lines | 118 code | 27 blank | 125 comment | 4 complexity | ca97827cc34868f5a4e64fa7537601c8 MD5 | raw file
  1. <?php
  2. require_once('WufooApiWrapperBase.php');
  3. require_once('WufooValueObjects.php');
  4. /**
  5. * API Main.
  6. * @author Timothy S Sabat
  7. */
  8. class WufooApiWrapper extends WufooApiWrapperBase {
  9. protected $apiKey;
  10. protected $subdomain;
  11. protected $domain = 'wufoo.com';
  12. protected $curl;
  13. /**
  14. * Constructor. Ignore the domain, as this is needed only for local testing
  15. *
  16. * @param string $apiKey
  17. * @param string $subdomain
  18. * @param string $domain
  19. * @author Timothy S Sabat
  20. */
  21. public function __construct($apiKey, $subdomain, $domain = 'wufoo.com') {
  22. $this->apiKey = $apiKey;
  23. $this->subdomain = $subdomain;
  24. $this->domain = $domain;
  25. }
  26. /* -------------------------------
  27. PUBLIC GET CALLS
  28. ------------------------------- */
  29. /**
  30. * Gets all permitted users. See http://wufoo.com/docs/api/v3/users/
  31. *
  32. * @return array of User Value Objects by Hash
  33. * @author Timothy S Sabat
  34. */
  35. public function getUsers() {
  36. $url = $this->getFullUrl('users');
  37. return $this->getHelper($url, 'User', 'Users');
  38. }
  39. /**
  40. * Gets all forms permitted to user.
  41. *
  42. * @param string $formIdentifier can be the url or hash. Remember, the URL changes with the form title, so it's best to use the hash.
  43. * @return array of Form Value Objects by hash
  44. * @author Timothy S Sabat
  45. */
  46. public function getForms($formIdentifier = null) {
  47. $url = ($formIdentifier) ? $this->getFullUrl('forms/'.$formIdentifier) : $this->getFullUrl('forms');
  48. return $this->getHelper($url, 'Form', 'Forms');
  49. }
  50. /**
  51. * Gets all fields for a given form or report by url or hash. Remember, the URL changes with the form/report title, so it's best to use the hash.
  52. *
  53. * @param string $formIdentifier A URL or Hash
  54. * @param string $from can be left as 'forms'. The call getReportFields uses this parameter.
  55. * @return WufooFieldCollection Value Object
  56. * @author Timothy S Sabat
  57. */
  58. public function getFields($formIdentifier, $from = 'forms') {
  59. $url = $this->getFullUrl($from.'/'.$formIdentifier.'/fields');
  60. $this->curl = new WufooCurl();
  61. $fields = json_decode($this->curl->getAuthenticated($url, $this->apiKey));
  62. $fieldHelper = new WufooFieldCollection();
  63. foreach ($fields->Fields as $field) {
  64. $fieldHelper->Fields[$field->ID] = new WufooField($field);
  65. $fieldHelper->Hash[$field->ID] = $field;
  66. if ($field->SubFields) {
  67. foreach ($field->SubFields as $subfield) {
  68. $fieldHelper->Hash[$subfield->ID] = $subfield;
  69. }
  70. }
  71. }
  72. return $fieldHelper;
  73. }
  74. /**
  75. * Gets all entries from a given form or report by url or hash. Remember, the URL changes with the form/report title, so it's best to use the hash.
  76. *
  77. * @param string $identifier a URL or Hash
  78. * @param string $from can be left as 'forms'. The call getReportFields uses this parameter.
  79. * @param string $getArgs a URL encoded string to filter entries.
  80. * @param string $index determines the key of the return hash
  81. * @return array of Form/Report Value Objects by hash
  82. * @author Timothy S Sabat
  83. */
  84. public function getEntries($identifier, $from = 'forms', $getArgs = '', $index = 'EntryId') {
  85. $url = $this->getFullUrl($from.'/'.$identifier.'/entries');
  86. $url.= ($getArgs) ? '?'. ltrim($getArgs, '?') : '';
  87. return $this->getHelper($url, 'Entry', 'Entries', $index);
  88. }
  89. /**
  90. * Gets entry count for a given form or report by url or hash. Remember, the URL changes with the form/report title, so it's best to use the hash.
  91. *
  92. * @param string $identifier a URL or Hash
  93. * @param string $from can be left as 'forms'. The call getReportFields uses this parameter.
  94. * @param string $getArgs a URL encoded string to filter entries.
  95. * @return int entry count
  96. * @author Timothy S Sabat
  97. */
  98. public function getEntryCount($identifier, $from = 'forms', $getArgs = '') {
  99. $url = $this->getFullUrl($from.'/'.$identifier.'/entries/count');
  100. $url.= ($getArgs) ? '?'. ltrim($getArgs, '?') : '';
  101. $this->curl = new WufooCurl();
  102. $countObject = json_decode($this->curl->getAuthenticated($url, $this->apiKey));
  103. return $countObject->EntryCount;
  104. }
  105. /**
  106. * Gets the entry count for a specific day.
  107. *
  108. *
  109. * @param string $identifier a URL or Hash
  110. * @return int today's entry count
  111. * @author Baylor Rae'
  112. */
  113. public function getEntryCountToday($identifier) {
  114. $url = $this->getFullUrl($from.'/'.$identifier) . '?includeTodayCount=true';
  115. $this->curl = new WufooCurl();
  116. $countObject = json_decode($this->curl->getAuthenticated($url, $this->apiKey));
  117. $ret = 0;
  118. if (isset($countObject->EntryCountToday)) {
  119. $ret = $countObject->EntryCountToday;
  120. } elseif (isset($countObject->Forms[0]->EntryCountToday)) {
  121. $ret = $countObject->Forms[0]->EntryCountToday;
  122. }
  123. return $ret;
  124. }
  125. /**
  126. * Gets all reports permitted to user.
  127. *
  128. * @param string $reportIdentifier can be the url or hash. Remember, the URL changes with the report title, so it's best to use the hash.
  129. * @return array of Report Value Objects by hash.
  130. * @author Timothy S Sabat
  131. */
  132. public function getReports($reportIdentifier) {
  133. $url = ($reportIdentifier) ? $this->getFullUrl('reports/'.$reportIdentifier) : $this->getFullUrl('reports');
  134. return $this->getHelper($url, 'Report', 'Reports');
  135. }
  136. /**
  137. * Gets all widgets permitted to user.
  138. *
  139. * @param string string $reportIdentifier can be the url or hash. Remember, the URL changes with the report title, so it's best to use the hash.
  140. * @return array of Widget Value Objects by hash.
  141. * @author Timothy S Sabat
  142. */
  143. public function getWidgets($reportIdentifier) {
  144. $url = $this->getFullUrl('reports/'.$reportIdentifier.'/widgets');
  145. return $this->getHelper($url, 'Widget', 'Widgets');
  146. }
  147. /**
  148. * Gets all fields for a given report by url or hash. Notice this is a facade for getFields() call.
  149. *
  150. * @param string $reportIdentifier can be the url or hash. Remember, the URL changes with the report title, so it's best to use the hash.
  151. * @return array of Field Value Objects by hash.
  152. * @author Timothy S Sabat
  153. */
  154. public function getReportFields($reportIdentifier) {
  155. return $this->getFields($reportIdentifier, 'reports');
  156. }
  157. /**
  158. * Gets all entries for a given report by url or hash. Notice this is a facade for getFields() call.
  159. *
  160. * @param string $reportIdentifier can be the url or hash. Remember, the URL changes with the report title, so it's best to use the hash.
  161. * @param string $getArgs a URL encoded string to filter entries.
  162. * @return array of Entry Value Objects by EntryId.
  163. * @author Timothy S Sabat
  164. */
  165. public function getReportEntries($reportIdentifier, $getArgs = '') {
  166. return $this->getEntries($reportIdentifier, 'reports', $getArgs);
  167. }
  168. /**
  169. * Gets entry count for a given report by url or hash. Notice this is a facade for getEntryCount.
  170. *
  171. * @param string $reportIdentifier can be the url or hash. Remember, the URL changes with the report title, so it's best to use the hash.
  172. * @param string $getArgs a URL encoded string to filter entries.
  173. * @return array of Entry Value Objects by EntryId.
  174. * @author Timothy S Sabat
  175. */
  176. public function getReportEntryCount($reportIdentifier, $getArgs = '') {
  177. return $this->getEntryCount($reportIdentifier, 'reports', $getArgs);
  178. }
  179. /**
  180. * Gets comments for a given form and (optionally) entry.
  181. *
  182. * @param string $formIdentifier
  183. * @param string $entryId (optional). If provided, narrows the filter to the entry id.
  184. * @return array of Comment Value Objects by EntryId
  185. * @author Timothy S Sabat
  186. */
  187. public function getComments($formIdentifier, $entryId = null) {
  188. if ($entryId) {
  189. $url = $this->getFullUrl('forms/'.$formIdentifier.'/comments/'.$entryId);
  190. } else {
  191. $url = $this->getFullUrl('forms/'.$formIdentifier.'/comments');
  192. }
  193. return $this->getHelper($url, 'Comment', 'Comments', 'CommentId');
  194. }
  195. /**
  196. * submits an entry to your form
  197. *
  198. * @param string $formIdentifier a URL or Hash
  199. * @param string $wufooSubmitFields an associative array containing array('FieldX' => Y)
  200. * @return an object containing info about your submission or submission failure
  201. * @author Timothy S Sabat
  202. */
  203. public function entryPost($formIdentifier, $wufooSubmitFields) {
  204. $url = $this->getFullUrl('forms/'.$formIdentifier.'/entries');
  205. $postParams = array();
  206. foreach ($wufooSubmitFields as $field) {
  207. $postParams[$field->getId()] = $field->getValue();
  208. }
  209. $curl = new WufooCurl();
  210. $response = $curl->postAuthenticated($postParams, $url, $this->apiKey);
  211. return new PostResponse($response);
  212. }
  213. /**
  214. * returns an API key for a given email, password,
  215. *
  216. * @param string $email
  217. * @param string $password
  218. * @param string $subdomain
  219. * @return void
  220. * @author Timothy S Sabat
  221. */
  222. public function getLogin($email, $password, $integrationKey, $subdomain = '') {
  223. $args = array('email' => $email, 'password' => $password, 'integrationKey' => $integrationKey, 'subdomain' => $subdomain);
  224. $url = 'http://wufoo.com/api/v3/login/';
  225. $response = $curl->postAuthenticated($args, $url, null);
  226. return new PostResponse($response);
  227. }
  228. public function webHookPut($formIdentifier, $webHookUrl, $handshakeKey, $metadata = false) {
  229. $url = $this->getFullUrl('forms/'.$formIdentifier.'/webhooks');
  230. String::debugShout(__LINE__, __FILE__, array($url));
  231. $this->curl = new WufooCurl();
  232. $args = array('url' => $webHookUrl, 'handshakeKey' => $handshakeKey, 'metadata' => $metadata);
  233. $result = json_decode($this->curl->putAuthenticated($args, $url, $this->apiKey));
  234. return new WebHookResponse($result->WebHookPutResult->Hash);
  235. }
  236. public function webHookDelete($formIdentifier, $hash) {
  237. $url = $this->getFullUrl('forms/'.$formIdentifier.'/webhooks');
  238. $this->curl = new WufooCurl();
  239. $result = json_decode($this->curl->deleteAuthenticated(array('hash' => $hash), $url, $this->apiKey));
  240. return new WebHookResponse($result->WebHookDeleteResult->Hash);
  241. }
  242. }
  243. ?>