PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/installation/includes/xajax/xajaxResponse.inc.php

https://github.com/adothompson/ucsc-identity-joomla
PHP | 301 lines | 119 code | 28 blank | 154 comment | 7 complexity | 16385121960ff28953697b6717fbe49d MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. ///////////////////////////////////////////////////////////////////////////////
  3. // xajaxResponse.inc.php::xajax XML response class
  4. //
  5. // xajax version 0.2
  6. // Copyright (C) 2005 - 2006 by Jared White & J. Max Wilson
  7. // http://www.xajaxproject.org
  8. //
  9. // xajax is an open source PHP class library for easily creating powerful
  10. // PHP-driven, web-based Ajax Applications. Using xajax, you can asynchronously
  11. // call PHP functions and update the content of your your webpage without
  12. // reloading the page.
  13. //
  14. // xajax is released under the terms of the LGPL license
  15. // http://www.gnu.org/copyleft/lesser.html#SEC3
  16. //
  17. // This library is free software; you can redistribute it and/or
  18. // modify it under the terms of the GNU Lesser General Public
  19. // License as published by the Free Software Foundation; either
  20. // version 2.1 of the License, or (at your option) any later version.
  21. //
  22. // This library is distributed in the hope that it will be useful,
  23. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  25. // Lesser General Public License for more details.
  26. //
  27. // You should have received a copy of the GNU Lesser General Public
  28. // License along with this library; if not, write to the Free Software
  29. // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  30. ///////////////////////////////////////////////////////////////////////////////
  31. /*
  32. ----------------------------------------------------------------------------
  33. | Online documentation for this class is available on the xajax wiki at: |
  34. | http://wiki.xajaxproject.org/Documentation:xajaxResponse.inc.php |
  35. ----------------------------------------------------------------------------
  36. */
  37. // The xajaxResponse class is used to created responses to be sent back to your
  38. // webpage. A response contains one or more command messages for updating your page.
  39. // Currently xajax supports 17 kinds of command messages, including some common ones such as:
  40. // * Assign - sets the specified attribute of an element in your page
  41. // * Append - appends data to the end of the specified attribute of an element in your page
  42. // * Prepend - prepends data to the beginning of the specified attribute of an element in your page
  43. // * Replace - searches for and replaces data in the specified attribute of an element in your page
  44. // * Script - runs the supplied JavaScript code
  45. // * Alert - shows an alert box with the supplied message text
  46. //
  47. // *Note: elements are identified by their HTML id, so if you don't see your browser HTML display changing from the request, make sure you're using the right id names in your response.
  48. class xajaxResponse
  49. {
  50. var $xml;
  51. var $sEncoding;
  52. // Constructor. Its main job is to set the character encoding for the response.
  53. // $sEncoding is a string containing the character encoding string to use.
  54. // * Note: to change the character encoding for all of the responses, set the
  55. // XAJAX_DEFAULT_ENCODING constant near the beginning of the xajax.inc.php file
  56. function xajaxResponse($sEncoding=XAJAX_DEFAULT_CHAR_ENCODING)
  57. {
  58. $this->setCharEncoding($sEncoding);
  59. }
  60. // setCharEncoding() sets the character encoding for the response based on
  61. // $sEncoding, which is a string containing the character encoding to use. You
  62. // don't need to use this method normally, since the character encoding for the
  63. // response gets set automatically based on the XAJAX_DEFAULT_CHAR_ENCODING
  64. // constant.
  65. function setCharEncoding($sEncoding)
  66. {
  67. $this->sEncoding = $sEncoding;
  68. }
  69. // addAssign() adds an assign command message to the XML response
  70. // $sTarget is a string containing the id of an HTML element
  71. // $sAttribute is the part of the element you wish to modify ("innerHTML", "value", etc.)
  72. // $sData is the data you want to set the attribute to
  73. // usage: $objResponse->addAssign("contentDiv", "innerHTML", "Some Text");
  74. function addAssign($sTarget,$sAttribute,$sData)
  75. {
  76. $this->xml .= $this->_cmdXML(array("n"=>"as","t"=>$sTarget,"p"=>$sAttribute),$sData);
  77. }
  78. // addAppend() adds an append command message to the XML response
  79. // $sTarget is a string containing the id of an HTML element
  80. // $sAttribute is the part of the element you wish to modify ("innerHTML", "value", etc.)
  81. // $sData is the data you want to append to the end of the attribute
  82. // usage: $objResponse->addAppend("contentDiv", "innerHTML", "Some New Text");
  83. function addAppend($sTarget,$sAttribute,$sData)
  84. {
  85. $this->xml .= $this->_cmdXML(array("n"=>"ap","t"=>$sTarget,"p"=>$sAttribute),$sData);
  86. }
  87. // addPrepend() adds an prepend command message to the XML response
  88. // $sTarget is a string containing the id of an HTML element
  89. // $sAttribute is the part of the element you wish to modify ("innerHTML", "value", etc.)
  90. // $sData is the data you want to prepend to the beginning of the attribute
  91. // usage: $objResponse->addPrepend("contentDiv", "innerHTML", "Some Starting Text");
  92. function addPrepend($sTarget,$sAttribute,$sData)
  93. {
  94. $this->xml .= $this->_cmdXML(array("n"=>"pp","t"=>$sTarget,"p"=>$sAttribute),$sData);
  95. }
  96. // addReplace() adds an replace command message to the XML response
  97. // $sTarget is a string containing the id of an HTML element
  98. // $sAttribute is the part of the element you wish to modify ("innerHTML", "value", etc.)
  99. // $sSearch is a string to search for
  100. // $sData is a string to replace the search string when found in the attribute
  101. // usage: $objResponse->addReplace("contentDiv", "innerHTML", "text", "<b>text</b>");
  102. function addReplace($sTarget,$sAttribute,$sSearch,$sData)
  103. {
  104. $sDta = "<s><![CDATA[$sSearch]]></s><r><![CDATA[$sData]]></r>";
  105. $this->xml .= $this->_cmdXML(array("n"=>"rp","t"=>$sTarget,"p"=>$sAttribute),$sDta);
  106. }
  107. // addClear() adds an clear command message to the XML response
  108. // $sTarget is a string containing the id of an HTML element
  109. // $sAttribute is the part of the element you wish to clear ("innerHTML", "value", etc.)
  110. // usage: $objResponse->addClear("contentDiv", "innerHTML");
  111. function addClear($sTarget,$sAttribute)
  112. {
  113. $this->addAssign($sTarget,$sAttribute,'');
  114. }
  115. // addAlert() adds an alert command message to the XML response
  116. // $sMsg is the text to be displayed in the Javascript alert box
  117. // usage: $objResponse->addAlert("This is important information");
  118. function addAlert($sMsg)
  119. {
  120. $this->xml .= $this->_cmdXML(array("n"=>"al"),$sMsg);
  121. }
  122. // addRedirect() uses the addScript() method to add a Javascript redirect to
  123. // another URL
  124. // $sURL is the URL to redirect the client browser to
  125. // usage: $objResponse->addRedirect("http://www.xajaxproject.org");
  126. function addRedirect($sURL)
  127. {
  128. $this->addScript('window.location = "'.rawurlencode($sURL).'";');
  129. }
  130. // addScript() adds a Javascript command message to the XML response
  131. // $sJS is a string containing Javascript code to be executed
  132. // usage: $objResponse->addScript("var x = prompt('get some text');");
  133. function addScript($sJS)
  134. {
  135. $this->xml .= $this->_cmdXML(array("n"=>"js"),$sJS);
  136. }
  137. // addRemove() adds a remove element command message to the XML response
  138. // $sTarget is a string containing the id of an HTML element to be removed
  139. // from your page
  140. // usage: $objResponse->addRemove("Div2");
  141. function addRemove($sTarget)
  142. {
  143. $this->xml .= $this->_cmdXML(array("n"=>"rm","t"=>$sTarget),'');
  144. }
  145. // addCreate() adds a create element command message to the XML response
  146. // $sParent is a string containing the id of an HTML element to which the new
  147. // element will be appended.
  148. // $sTag is the tag to be added
  149. // $sId is the id to be assigned to the new element
  150. // $sType has been deprecated, use the addCreateInput() method instead
  151. // usage: $objResponse->addCreate("parentDiv", "h3", "myid");
  152. function addCreate($sParent, $sTag, $sId, $sType="")
  153. {
  154. if ($sType)
  155. {
  156. trigger_error("The \$sType parameter of addCreate has been deprecated. Use the addCreateInput() method instead.", E_USER_WARNING);
  157. return;
  158. }
  159. $this->xml .= $this->_cmdXML(array("n"=>"ce","t"=>$sParent,"p"=>$sId),$sTag);
  160. }
  161. // addInsert() adds an insert element command message to the XML response
  162. // $sBefore is a string containing the id of the child before which the new element
  163. // will be inserted
  164. // $sTag is the tag to be added
  165. // $sId is the id to be assigned to the new element
  166. // usage: $objResponse->addInsert("childDiv", "h3", "myid");
  167. function addInsert($sBefore, $sTag, $sId)
  168. {
  169. $this->xml .= $this->_cmdXML(array("n"=>"ie","t"=>$sBefore,"p"=>$sId),$sTag);
  170. }
  171. // addCreateInput() adds a create input command message to the XML response
  172. // $sParent is a string containing the id of an HTML element to which the new
  173. // input will be appended
  174. // $sType is the type of input to be created (text, radio, checkbox, etc.)
  175. // $sName is the name to be assigned to the new input and the variable name when it is submitted
  176. // $sId is the id to be assigned to the new input
  177. // usage: $objResponse->addCreateInput("form1", "text", "username", "input1");
  178. function addCreateInput($sParent, $sType, $sName, $sId)
  179. {
  180. $this->xml .= $this->_cmdXML(array("n"=>"ci","t"=>$sParent,"p"=>$sId,"c"=>$sType),$sName);
  181. }
  182. // addInsertInput() adds an insert input command message to the XML response
  183. // $sBefore is a string containing the id of the child before which the new element
  184. // will be inserted
  185. // $sType is the type of input to be created (text, radio, checkbox, etc.)
  186. // $sName is the name to be assigned to the new input and the variable name when it is submitted
  187. // $sId is the id to be assigned to the new input
  188. // usage: $objResponse->addInsertInput("input5", "text", "username", "input1");
  189. function addInsertInput($sBefore, $sType, $sName, $sId)
  190. {
  191. $this->xml .= $this->_cmdXML(array("n"=>"ii","t"=>$sBefore,"p"=>$sId,"c"=>$sType),$sName);
  192. }
  193. // addEvent() adds an event command message to the XML response
  194. // $sTarget is a string containing the id of an HTML element
  195. // $sEvent is the event you wish to set ("click", "mouseover", etc.)
  196. // $sScript is the Javascript string you want to the event to invoke
  197. // usage: $objResponse->addEvent("contentDiv", "click", "alert(\'Hello World\');");
  198. function addEvent($sTarget,$sEvent,$sScript)
  199. {
  200. $this->xml .= $this->_cmdXML(array("n"=>"ev","t"=>$sTarget,"p"=>$sEvent),$sScript);
  201. }
  202. // addHandler() adds a handler command message to the XML response
  203. // $sTarget is a string containing the id of an HTML element
  204. // $sEvent is the event you wish to set ("click", "mouseover", etc.)
  205. // $sHandler is a string containing the name of a Javascript function
  206. // that will handle the event. Multiple handlers can be added for the same event
  207. // usage: $objResponse->addHandler("contentDiv", "click", "content_click");
  208. function addHandler($sTarget,$sEvent,$sHandler)
  209. {
  210. $this->xml .= $this->_cmdXML(array("n"=>"ah","t"=>$sTarget,"p"=>$sEvent),$sHandler);
  211. }
  212. // addRemoveHandler() adds a remove handler command message to the XML response
  213. // $sTarget is a string containing the id of an HTML element
  214. // $sEvent is the event you wish to remove ("click", "mouseover", etc.)
  215. // $sHandler is a string containing the name of a Javascript handler function
  216. // that you want to remove
  217. // usage: $objResponse->addRemoveHandler("contentDiv", "click", "content_click");
  218. function addRemoveHandler($sTarget,$sEvent,$sHandler)
  219. {
  220. $this->xml .= $this->_cmdXML(array("n"=>"rh","t"=>$sTarget,"p"=>$sEvent),$sHandler);
  221. }
  222. // addIncludeScript() adds an include script command message to the XML response
  223. // $sFileName is a URL of the Javascript file to include
  224. // usage: $objResponse->addIncludeScript("functions.js");
  225. function addIncludeScript($sFileName)
  226. {
  227. $this->xml .= $this->_cmdXML(array("n"=>"in"),$sFileName);
  228. }
  229. // getXML() returns the XML to be returned from your function to the xajax
  230. // processor on your page. Since xajax 0.2, you can also return an xajaxResponse
  231. // object from your function directly, and xajax will automatically request the
  232. // XML using this method call.
  233. // usage: return $objResponse->getXML();
  234. function getXML()
  235. {
  236. $sXML = "<?xml version=\"1.0\"";
  237. if ($this->sEncoding && strlen(trim($this->sEncoding)) > 0)
  238. $sXML .= " encoding=\"".$this->sEncoding."\"";
  239. $sXML .= " ?"."><xjx>" . $this->xml . "</xjx>";
  240. return $sXML;
  241. }
  242. // loadXML() adds the commands of the provided response XML output to this
  243. // response object
  244. // $sXML is the response XML (returned from a getXML() method) to add to the
  245. // end of this response object
  246. // usage: $r1 = $objResponse1->getXML();
  247. // $objResponse2->loadXML($r1);
  248. // return $objResponse2->getXML();
  249. function loadXML($sXML)
  250. {
  251. $sNewXML = "";
  252. $iStartPos = strpos($sXML, "<xjx>") + 5;
  253. $sNewXML = substr($sXML, $iStartPos);
  254. $iEndPos = strpos($sNewXML, "</xjx>");
  255. $sNewXML = substr($sNewXML, 0, $iEndPos);
  256. $this->xml .= $sNewXML;
  257. }
  258. // private method, used internally
  259. function _cmdXML($aAttributes, $sData)
  260. {
  261. $xml = "<cmd";
  262. foreach($aAttributes as $sAttribute => $sValue)
  263. $xml .= " $sAttribute=\"$sValue\"";
  264. if ($sData && !stristr($sData,'<![CDATA['))
  265. $xml .= "><![CDATA[$sData]]></cmd>";
  266. else if ($sData)
  267. $xml .= ">$sData</cmd>";
  268. else
  269. $xml .= "></cmd>";
  270. return $xml;
  271. }
  272. }// end class xajaxResponse
  273. ?>