PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/ccds_library/xajax/xajax_core/plugin_layer/support/xajaxUserFunction.inc.php

http://ccds.googlecode.com/
PHP | 232 lines | 81 code | 18 blank | 133 comment | 13 complexity | f842848200b9fd937b0afdb6e4d2cd12 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. /*
  3. File: xajaxUserFunction.inc.php
  4. Contains the xajaxUserFunction class
  5. Title: xajaxUserFunction class
  6. Please see <copyright.inc.php> for a detailed description, copyright
  7. and license information.
  8. */
  9. /*
  10. @package xajax
  11. @version $Id: xajaxUserFunction.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. Class: xajaxUserFunction
  17. Construct instances of this class to define functions that will be registered
  18. with the <xajax> request processor. This class defines the parameters that
  19. are needed for the definition of a xajax enabled function. While you can
  20. still specify functions by name during registration, it is advised that you
  21. convert to using this class when you wish to register external functions or
  22. to specify call options as well.
  23. */
  24. class xajaxUserFunction
  25. {
  26. /*
  27. String: sAlias
  28. An alias to use for this function. This is useful when you want
  29. to call the same xajax enabled function with a different set of
  30. call options from what was already registered.
  31. */
  32. var $sAlias;
  33. /*
  34. Object: uf
  35. A string or array which defines the function to be registered.
  36. */
  37. var $uf;
  38. /*
  39. String: sInclude
  40. The path and file name of the include file that contains the function.
  41. */
  42. var $sInclude;
  43. /*
  44. Array: aConfiguration
  45. An associative array containing call options that will be sent to the
  46. browser curing client script generation.
  47. */
  48. var $aConfiguration;
  49. /*
  50. Function: xajaxUserFunction
  51. Constructs and initializes the <xajaxUserFunction> object.
  52. $uf - (mixed): A function specification in one of the following formats:
  53. - a three element array:
  54. (string) Alternate function name: when a method of a class has the same
  55. name as another function in the system, you can provide an alias to
  56. help avoid collisions.
  57. (object or class name) Class: the name of the class or an instance of
  58. the object which contains the function to be called.
  59. (string) Method: the name of the method that will be called.
  60. - a two element array:
  61. (object or class name) Class: the name of the class or an instance of
  62. the object which contains the function to be called.
  63. (string) Method: the name of the method that will be called.
  64. - a string:
  65. the name of the function that is available at global scope (not in a
  66. class.
  67. $sInclude - (string, optional): The path and file name of the include file
  68. that contains the class or function to be called.
  69. $aConfiguration - (array, optional): An associative array of call options
  70. that will be used when sending the request from the client.
  71. Examples:
  72. $myFunction = array('alias', 'myClass', 'myMethod');
  73. $myFunction = array('alias', &$myObject, 'myMethod');
  74. $myFunction = array('myClass', 'myMethod');
  75. $myFunction = array(&$myObject, 'myMethod');
  76. $myFunction = 'myFunction';
  77. $myUserFunction = new xajaxUserFunction($myFunction, 'myFile.inc.php', array(
  78. 'method' => 'get',
  79. 'mode' => 'synchronous'
  80. ));
  81. $xajax->register(XAJAX_FUNCTION, $myUserFunction);
  82. */
  83. function xajaxUserFunction($uf, $sInclude=NULL, $aConfiguration=array())
  84. {
  85. $this->sAlias = '';
  86. $this->uf =& $uf;
  87. $this->sInclude = $sInclude;
  88. $this->aConfiguration = array();
  89. foreach ($aConfiguration as $sKey => $sValue)
  90. $this->configure($sKey, $sValue);
  91. if (is_array($this->uf) && 2 < count($this->uf))
  92. {
  93. $this->sAlias = $this->uf[0];
  94. $this->uf = array_slice($this->uf, 1);
  95. }
  96. //SkipDebug
  97. if (is_array($this->uf) && 2 != count($this->uf))
  98. trigger_error(
  99. 'Invalid function declaration for xajaxUserFunction.',
  100. E_USER_ERROR
  101. );
  102. //EndSkipDebug
  103. }
  104. /*
  105. Function: getName
  106. Get the name of the function being referenced.
  107. Returns:
  108. string - the name of the function contained within this object.
  109. */
  110. function getName()
  111. {
  112. // Do not use sAlias here!
  113. if (is_array($this->uf))
  114. return $this->uf[1];
  115. return $this->uf;
  116. }
  117. /*
  118. Function: configure
  119. Call this to set call options for this instance.
  120. */
  121. function configure($sName, $sValue)
  122. {
  123. if ('alias' == $sName)
  124. $this->sAlias = $sValue;
  125. else
  126. $this->aConfiguration[$sName] = $sValue;
  127. }
  128. /*
  129. Function: generateRequest
  130. Constructs and returns a <xajaxRequest> object which is capable
  131. of generating the javascript call to invoke this xajax enabled
  132. function.
  133. */
  134. function generateRequest($sXajaxPrefix)
  135. {
  136. $sAlias = $this->getName();
  137. if (0 < strlen($this->sAlias))
  138. $sAlias = $this->sAlias;
  139. return new xajaxRequest("{$sXajaxPrefix}{$sAlias}");
  140. }
  141. /*
  142. Function: generateClientScript
  143. Called by the <xajaxPlugin> that is referencing this function
  144. reference during the client script generation phase. This function
  145. will generate the javascript function stub that is sent to the
  146. browser on initial page load.
  147. */
  148. function generateClientScript($sXajaxPrefix)
  149. {
  150. $sFunction = $this->getName();
  151. $sAlias = $sFunction;
  152. if (0 < strlen($this->sAlias))
  153. $sAlias = $this->sAlias;
  154. echo "{$sXajaxPrefix}{$sAlias} = function() { ";
  155. echo "return xajax.request( ";
  156. echo "{ xjxfun: '{$sFunction}' }, ";
  157. echo "{ parameters: arguments";
  158. $sSeparator = ", ";
  159. foreach ($this->aConfiguration as $sKey => $sValue)
  160. echo "{$sSeparator}{$sKey}: {$sValue}";
  161. echo " } ); ";
  162. echo "};\n";
  163. }
  164. /*
  165. Function: call
  166. Called by the <xajaxPlugin> that references this function during the
  167. request processing phase. This function will call the specified
  168. function, including an external file if needed and passing along
  169. the specified arguments.
  170. */
  171. function call($aArgs=array())
  172. {
  173. $objResponseManager =& xajaxResponseManager::getInstance();
  174. if (NULL != $this->sInclude)
  175. {
  176. ob_start();
  177. require_once $this->sInclude;
  178. $sOutput = ob_get_clean();
  179. //SkipDebug
  180. if (0 < strlen($sOutput))
  181. {
  182. $sOutput = 'From include file: ' . $this->sInclude . ' => ' . $sOutput;
  183. $objResponseManager->debug($sOutput);
  184. }
  185. //EndSkipDebug
  186. }
  187. $mFunction = $this->uf;
  188. $objResponseManager->append(call_user_func_array($mFunction, $aArgs));
  189. }
  190. }
  191. ?>