PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/StreamingController.php

http://forceworkbench.googlecode.com/
PHP | 211 lines | 181 code | 29 blank | 1 comment | 26 complexity | c593f768835be88fe8ca1e98f62218b8 MD5 | raw file
  1. <?php
  2. require_once 'restclient/RestClient.php';
  3. class StreamingController {
  4. private $restBaseUrl;
  5. private $restApi;
  6. private $selectedTopic;
  7. private $pushTopics;
  8. private $infos;
  9. private $errors;
  10. private $enabled;
  11. private $isAjax;
  12. function __construct() {
  13. $this->restApi = WorkbenchContext::get()->getRestDataConnection();
  14. $this->infos = array();
  15. $this->errors = array();
  16. $this->enabled = true;
  17. $this->isAjax = false;
  18. $this->restBaseUrl = "/services/data/v" . WorkbenchContext::get()->getApiVersion();
  19. if (get_magic_quotes_gpc()) {
  20. foreach ($_REQUEST as $fieldName => &$r) {
  21. if (strpos($fieldName, "pushTopicDmlForm_") > -1) {
  22. $r =& stripslashes($r);
  23. }
  24. }
  25. }
  26. $this->selectedTopic = new PushTopic(
  27. isset($_REQUEST['pushTopicDmlForm_Id']) ? $_REQUEST['pushTopicDmlForm_Id'] : null,
  28. isset($_REQUEST['pushTopicDmlForm_Name']) ? $_REQUEST['pushTopicDmlForm_Name'] : null,
  29. isset($_REQUEST['pushTopicDmlForm_ApiVersion']) ? $_REQUEST['pushTopicDmlForm_ApiVersion'] : null,
  30. isset($_REQUEST['pushTopicDmlForm_Query']) ? $_REQUEST['pushTopicDmlForm_Query'] : null);
  31. if (isset($_REQUEST['PUSH_TOPIC_DML'])) {
  32. $this->isAjax = true;
  33. if ($_REQUEST['PUSH_TOPIC_DML'] == "SAVE") {
  34. $this->save();
  35. } else if ($_REQUEST['PUSH_TOPIC_DML'] == "DELETE") {
  36. $this->delete();
  37. }
  38. }
  39. $this->refresh();
  40. }
  41. private function refresh() {
  42. $pushTopicSoql = "SELECT Id, Name, Query, ApiVersion FROM PushTopic";
  43. $url = $this->restBaseUrl . "/query?" . http_build_query(array("q" => $pushTopicSoql));
  44. try {
  45. $queryResponse = $this->restApi->send("GET", $url, null, null, false);
  46. if (strpos($queryResponse->header, "200 OK") === false) {
  47. $this->errors[] = "Could not load Push Topics. Ensure the both the REST and Streaming APIs are enabled for this organization.";
  48. $this->enabled = false;
  49. return;
  50. }
  51. $this->pushTopics = json_decode($queryResponse->body)->records;
  52. } catch (Exception $e) {
  53. $this->errors[] = "Unknown Error Fetching Push Topics:\n" . $e->getMessage();
  54. }
  55. }
  56. private function save() {
  57. if ($this->selectedTopic->Id != null && $this->selectedTopic->Id != "undefined") {
  58. $this->dml("PATCH", "Updated", "Updating", "/" . $this->selectedTopic->Id, $this->selectedTopic->toJson(false));
  59. } else {
  60. $this->dml("POST", "Created", "Creating", "", $this->selectedTopic->toJson(false));
  61. }
  62. }
  63. private function delete() {
  64. $this->dml("DELETE", "Deleted", "Deleting", "/".$this->selectedTopic->Id, null);
  65. }
  66. private function dml($method, $opPastLabel, $opProgLabel, $urlTail, $data) {
  67. $headers = array("Content-Type: application/json");
  68. $url = $this->restBaseUrl . "/sobjects/PushTopic" . $urlTail;
  69. try {
  70. $response = $this->restApi->send($method, $url, $headers, $data, false);
  71. } catch (Exception $e) {
  72. $this->errors[] = "Unknown Error $opProgLabel Push Topic\n:" . $e->getMessage();
  73. return;
  74. }
  75. if (strpos($response->header, "201 Created") > 0 || strpos($response->header, "204 No Content") > 0) {
  76. $this->infos[] = "Push Topic $opPastLabel Successfully";
  77. } else {
  78. $body = json_decode($response->body);
  79. $this->errors[] = "Error $opProgLabel Push Topic:\n" . $body[0]->message;
  80. }
  81. }
  82. function getMessages() {
  83. $messages = "";
  84. ob_start();
  85. if (count($this->errors) > 0) displayError($this->errors);
  86. if (count($this->infos) > 0) displayInfo($this->infos);
  87. $messages .= ob_get_clean();
  88. $messages .= "<div id='partialSavedTopic' style='display:none;'>";
  89. if (count($this->errors) > 0) {
  90. $messages .= $this->selectedTopic->toJson();
  91. }
  92. $messages .= "</div>";
  93. return $messages;
  94. }
  95. function getPushTopicOptions() {
  96. $options = "";
  97. $options .= "<option></option>\n";
  98. if (!$this->isEnabled()) {
  99. return $options;
  100. }
  101. $selected = count($this->pushTopics) == 0 ? "selected='selected'" : "";
  102. $options .= "<option value='". PushTopic::template()->toJson() . "' $selected>--Create New--</option>\n";
  103. foreach($this->pushTopics as $topic) {
  104. $topic->Name = htmlspecialchars($topic->Name, ENT_QUOTES);
  105. $topic->Query = htmlspecialchars($topic->Query, ENT_QUOTES);
  106. $topic->ApiVersion = strpos($topic->ApiVersion, ".") === false ? $topic->ApiVersion.".0" : $topic->ApiVersion;
  107. $selected = $topic->Name == $this->selectedTopic->Name ? "selected='selected'" : "";
  108. $options .= "<option value='". json_encode($topic) . "' $selected>" . $topic->Name . "</option>\n";
  109. }
  110. return $options;
  111. }
  112. function getApiVersionOptions() {
  113. $options = "";
  114. foreach($GLOBALS['API_VERSIONS'] as $v) {
  115. $options .= "<option value='$v'>$v</option>\n";
  116. }
  117. return $options;
  118. }
  119. function getStreamingConfig() {
  120. $streamingConfig["handshakeOnLoad"] = true; // TODO: make this configurable
  121. $streamingConfig["csrfToken"] = getCsrfToken();
  122. // configs in "$streamingConfig["cometdConfig"]" are loaded into CometD in JS and need to match their format
  123. $streamingConfig["cometdConfig"]["logLevel"] = "info";
  124. $streamingConfig["cometdConfig"]["appendMessageTypeToURL"] = false;
  125. $streamingConfig["cometdConfig"]["url"] =
  126. "http" . ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ? "s" : "") . "://" .
  127. $_SERVER['HTTP_HOST'] .
  128. str_replace('\\', '/', dirname(htmlspecialchars($_SERVER['PHP_SELF']))) .
  129. (strlen(dirname(htmlspecialchars($_SERVER['PHP_SELF']))) == 1 ? "" : "/") .
  130. "cometdProxy.php";
  131. return json_encode($streamingConfig);
  132. }
  133. function isEnabled() {
  134. return $this->enabled;
  135. }
  136. function isAjax() {
  137. return $this->isAjax;
  138. }
  139. function getAjaxResponse() {
  140. $ajaxResponse['messages'] = $this->getMessages();
  141. $ajaxResponse['pushTopicOptions'] = $this->getPushTopicOptions();
  142. return json_encode($ajaxResponse);
  143. }
  144. }
  145. class PushTopic {
  146. public $Id, $Name, $ApiVersion, $Query;
  147. function __construct($id, $name, $apiVersion, $query) {
  148. $this->Id = $id;
  149. $this->Name = $name;
  150. $this->ApiVersion = $apiVersion;
  151. $this->Query = $query;
  152. }
  153. static function template() {
  154. return new PushTopic(null, null, WorkbenchContext::get()->getApiVersion(), null);
  155. }
  156. static function fromJson($jsonStr) {
  157. $json = json_decode($jsonStr);
  158. return new PushTopic($json->Id, $json->Name, $json->ApiVersion, $json->Query);
  159. }
  160. function toJson($includeId = true) {
  161. $clone = $this;
  162. if (!$includeId) {
  163. unset($clone->Id);
  164. }
  165. return json_encode($clone);
  166. }
  167. }
  168. ?>