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

/common/xajax/xajax_core/plugin_layer/support/xajaxUserFunction.inc.php

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