PageRenderTime 51ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/common/xajax/xajax_core/xajaxRequest.inc.php

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