PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/source/Plug-in/xajax/xajax_core/xajaxRequest.inc.php

http://prosporous.googlecode.com/
PHP | 342 lines | 257 code | 7 blank | 78 comment | 6 complexity | fc5f826fe191597ad2e37dc8ca204d20 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*
  3. File: xajaxRequest.inc.php
  4. Contains the xajaxRequest class
  5. Title: xajaxRequest class
  6. Please see <copyright.inc.php> for a detailed description, copyright
  7. and license information.
  8. */
  9. /*
  10. @package xajax
  11. @version $Id: xajaxRequest.inc.php 362 2007-05-29 15:32:24Z calltoconstruct $
  12. @copyright Copyright (c) 2005-2006 by Jared White & J. Max Wilson
  13. @license http://www.xajaxproject.org/bsd_license.txt BSD License
  14. */
  15. /*
  16. Constant: XAJAX_FORM_VALUES
  17. Specifies that the parameter will consist of an array of form values.
  18. Constant: XAJAX_INPUT_VALUE
  19. Specifies that the parameter will contain the value of an input control.
  20. Constant: XAJAX_CHECKED_VALUE
  21. Specifies that the parameter will consist of a boolean value of a checkbox.
  22. Constant: XAJAX_ELEMENT_INNERHTML
  23. Specifies that the parameter value will be the innerHTML value of the element.
  24. Constant: XAJAX_QUOTED_VALUE
  25. Specifies that the parameter will be a quoted value (string).
  26. Constant: XAJAX_JS_VALUE
  27. Specifies that the parameter will be a non-quoted value (evaluated by the
  28. browsers javascript engine at run time.
  29. */
  30. if (!defined ('XAJAX_FORM_VALUES')) define ('XAJAX_FORM_VALUES', 'get form values');
  31. if (!defined ('XAJAX_INPUT_VALUE')) define ('XAJAX_INPUT_VALUE', 'get input value');
  32. if (!defined ('XAJAX_CHECKED_VALUE')) define ('XAJAX_CHECKED_VALUE', 'get checked value');
  33. if (!defined ('XAJAX_ELEMENT_INNERHTML')) define ('XAJAX_ELEMENT_INNERHTML', 'get element innerHTML');
  34. if (!defined ('XAJAX_QUOTED_VALUE')) define ('XAJAX_QUOTED_VALUE', 'quoted value');
  35. if (!defined ('XAJAX_JS_VALUE')) define ('XAJAX_JS_VALUE', 'unquoted value');
  36. /*
  37. Class: xajaxRequest
  38. Used to store and generate the client script necessary to invoke
  39. a xajax request from the browser to the server script.
  40. This object is typically generated by the <xajax->register> method
  41. and can be used to quickly generate the javascript that is used
  42. to initiate a xajax request to the registered function, object, event
  43. or other xajax call.
  44. */
  45. class xajaxRequest
  46. {
  47. /*
  48. String: sName
  49. The name of the function.
  50. */
  51. var $sName;
  52. /*
  53. String: sQuoteCharacter
  54. A string containing either a single or a double quote character
  55. that will be used during the generation of the javascript for
  56. this function. This can be set prior to calling <xajaxRequest->printScript>
  57. */
  58. var $sQuoteCharacter;
  59. /*
  60. Array: aParameters
  61. An array of parameters that will be used to populate the argument list
  62. for this function when the javascript is output in <xajaxRequest->printScript>
  63. */
  64. var $aParameters;
  65. /*
  66. Function: xajaxRequest
  67. Construct and initialize this request.
  68. sName - (string): The name of this request.
  69. */
  70. function xajaxRequest($sName)
  71. {
  72. $this->aParameters = array();
  73. $this->sQuoteCharacter = '"';
  74. $this->sName = $sName;
  75. }
  76. /*
  77. Function: useSingleQuote
  78. Call this to instruct the request to use single quotes when generating
  79. the javascript.
  80. */
  81. function useSingleQuote()
  82. {
  83. $this->sQuoteCharacter = "'";
  84. }
  85. /*
  86. Function: useDoubleQuote
  87. Call this to instruct the request to use double quotes while generating
  88. the javascript.
  89. */
  90. function useDoubleQuote()
  91. {
  92. $this->sQuoteCharacter = '"';
  93. }
  94. /*
  95. Function: clearParameters
  96. Clears the parameter list associated with this request.
  97. */
  98. function clearParameters()
  99. {
  100. $this->aParameters = array();
  101. }
  102. /*
  103. Function: addParameter
  104. Adds a parameter value to the parameter list for this request.
  105. sType - (string): The type of the value to be used.
  106. sValue - (string: The value to be used.
  107. See <xajaxRequest->setParameter> for details.
  108. */
  109. function addParameter()
  110. {
  111. $aArgs = func_get_args();
  112. if (1 < count($aArgs))
  113. $this->setParameter(
  114. count($this->aParameters),
  115. $aArgs[0],
  116. $aArgs[1]);
  117. }
  118. /*
  119. Function: setParameter
  120. Sets a specific parameter value.
  121. nParameter - (number): The index of the parameter to set
  122. sType - (string): The type of value
  123. sValue - (string): The value as it relates to the specified type
  124. Types should be one of the following <XAJAX_FORM_VALUES>, <XAJAX_QUOTED_VALUE>,
  125. <XAJAX_JS_VALUE>, <XAJAX_INPUT_VALUE>, <XAJAX_CHECKED_VALUE>.
  126. The value should be as follows:
  127. <XAJAX_FORM_VALUES> - Use the ID of the form you want to process.
  128. <XAJAX_QUOTED_VALUE> - The string data to be passed.
  129. <XAJAX_JS_VALUE> - A string containing valid javascript (either a javascript
  130. variable name that will be in scope at the time of the call or a
  131. javascript function call whose return value will become the parameter.
  132. TODO: finish documenting the options.
  133. */
  134. function setParameter()
  135. {
  136. $aArgs = func_get_args();
  137. if (2 < count($aArgs))
  138. {
  139. $nParameter = $aArgs[0];
  140. $sType = $aArgs[1];
  141. if (XAJAX_FORM_VALUES == $sType)
  142. {
  143. $sFormID = $aArgs[2];
  144. $this->aParameters[$nParameter] =
  145. "xajax.getFormValues("
  146. . $this->sQuoteCharacter
  147. . $sFormID
  148. . $this->sQuoteCharacter
  149. . ")";
  150. }
  151. else if (XAJAX_INPUT_VALUE == $sType)
  152. {
  153. $sInputID = $aArgs[2];
  154. $this->aParameters[$nParameter] =
  155. "xajax.$("
  156. . $this->sQuoteCharacter
  157. . $sInputID
  158. . $this->sQuoteCharacter
  159. . ").value";
  160. }
  161. else if (XAJAX_CHECKED_VALUE == $sType)
  162. {
  163. $sCheckedID = $aArgs[2];
  164. $this->aParameters[$nParameter] =
  165. "xajax.$("
  166. . $this->sQuoteCharacter
  167. . $sCheckedID
  168. . $this->sQuoteCharacter
  169. . ").checked";
  170. }
  171. else if (XAJAX_ELEMENT_INNERHTML == $sType)
  172. {
  173. $sElementID = $aArgs[2];
  174. $this->aParameters[$nParameter] =
  175. "xajax.$("
  176. . $this->sQuoteCharacter
  177. . $sElementID
  178. . $this->sQuoteCharacter
  179. . ").innerHTML";
  180. }
  181. else if (XAJAX_QUOTED_VALUE == $sType)
  182. {
  183. $sValue = $aArgs[2];
  184. $this->aParameters[$nParameter] =
  185. $this->sQuoteCharacter
  186. . $sValue
  187. . $this->sQuoteCharacter;
  188. }
  189. else if (XAJAX_JS_VALUE == $sType)
  190. {
  191. $sValue = $aArgs[2];
  192. $this->aParameters[$nParameter] = $sValue;
  193. }
  194. }
  195. }
  196. /*
  197. Function: getScript
  198. Returns a string representation of the script output (javascript) from
  199. this request object. See also: <xajaxRequest::printScript>
  200. */
  201. function getScript()
  202. {
  203. ob_start();
  204. $this->printScript();
  205. return ob_get_clean();
  206. }
  207. /*
  208. Function: printScript
  209. Generates a block of javascript code that can be used to invoke
  210. the specified xajax request.
  211. */
  212. function printScript()
  213. {
  214. echo $this->sName;
  215. echo '(';
  216. $sSeparator = '';
  217. foreach ($this->aParameters as $sParameter)
  218. {
  219. echo $sSeparator;
  220. echo $sParameter;
  221. $sSeparator = ', ';
  222. }
  223. echo ')';
  224. }
  225. }
  226. /*
  227. Class: xajaxCustomRequest
  228. This class extends the <xajaxRequest> class such that simple javascript
  229. can be put in place of a xajax request to the server. The primary purpose
  230. of this class is to provide simple scripting services to the <xajaxControl>
  231. based objects, like <clsInput>, <clsTable> and <clsButton>.
  232. */
  233. class xajaxCustomRequest extends xajaxRequest
  234. {
  235. /*
  236. Array: aVariables;
  237. */
  238. var $aVariables;
  239. /*
  240. String: sScript;
  241. */
  242. var $sScript;
  243. /*
  244. Function: xajaxCustomRequest
  245. Constructs and initializes an instance of the object.
  246. sScript - (string): The javascript (template) that will be printed
  247. upon request.
  248. aVariables - (associative array, optional): An array of variable name,
  249. value pairs that will be passed to <xajaxCustomRequest->setVariable>
  250. */
  251. function xajaxCustomRequest($sScript)
  252. {
  253. $this->aVariables = array();
  254. $this->sScript = $sScript;
  255. }
  256. /*
  257. Function: clearVariables
  258. Clears the array of variables that will be used to modify the script before
  259. it is printed and sent to the client.
  260. */
  261. function clearVariables()
  262. {
  263. $this->aVariables = array();
  264. }
  265. /*
  266. Function: setVariable
  267. Sets a value that will be used to modify the script before it is sent to
  268. the browser. The <xajaxCustomRequest> object will perform a string
  269. replace operation on each of the values set with this function.
  270. */
  271. function setVariable($sName, $sValue)
  272. {
  273. $this->aVariables[$sName] = $sValue;
  274. }
  275. /*
  276. Function: printScript
  277. */
  278. function printScript()
  279. {
  280. $sScript = $this->sScript;
  281. foreach ($this->aVariables as $sKey => $sValue)
  282. $sScript = str_replace($sKey, $sValue, $sScript);
  283. echo $sScript;
  284. }
  285. }