/source/Plug-in/xajax/xajax_core/plugin_layer/xajaxFunctionPlugin.inc.php

http://prosporous.googlecode.com/ · PHP · 230 lines · 95 code · 31 blank · 104 comment · 32 complexity · 33674853742b8a77a5e4eec7323c83fc MD5 · raw file

  1. <?php
  2. /*
  3. File: xajaxFunctionPlugin.inc.php
  4. Contains the xajaxFunctionPlugin class
  5. Title: xajaxFunctionPlugin class
  6. Please see <copyright.inc.php> for a detailed description, copyright
  7. and license information.
  8. */
  9. /*
  10. @package xajax
  11. @version $Id: xajaxFunctionPlugin.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_FUNCTION
  17. Specifies that the item being registered via the <xajax->register> function
  18. is a php function available at global scope, or a specific function from
  19. an instance of an object.
  20. */
  21. if (!defined ('XAJAX_FUNCTION')) define ('XAJAX_FUNCTION', 'function');
  22. // require_once is necessary here as the xajaxEvent class will include this also
  23. //SkipAIO
  24. require_once dirname(__FILE__) . '/support/xajaxUserFunction.inc.php';
  25. //EndSkipAIO
  26. /*
  27. Class: xajaxFunctionPlugin
  28. */
  29. class xajaxFunctionPlugin extends xajaxRequestPlugin
  30. {
  31. /*
  32. Array: aFunctions
  33. An array of <xajaxUserFunction> object that are registered and
  34. available via a <xajax.request> call.
  35. */
  36. var $aFunctions;
  37. /*
  38. String: sXajaxPrefix
  39. A configuration setting that is stored locally and used during
  40. the client script generation phase.
  41. */
  42. var $sXajaxPrefix;
  43. /*
  44. String: sDefer
  45. Configuration option that can be used to request that the
  46. javascript file is loaded after the page has been fully loaded.
  47. */
  48. var $sDefer;
  49. var $bDeferScriptGeneration;
  50. /*
  51. String: sRequestedFunction
  52. This string is used to temporarily hold the name of the function
  53. that is being requested (during the request processing phase).
  54. Since canProcessRequest loads this value from the get or post
  55. data, it is unnecessary to load it again.
  56. */
  57. var $sRequestedFunction;
  58. /*
  59. Function: xajaxFunctionPlugin
  60. Constructs and initializes the <xajaxFunctionPlugin>. The GET and POST
  61. data is searched for xajax function call parameters. This will later
  62. be used to determine if the request is for a registered function in
  63. <xajaxFunctionPlugin->canProcessRequest>
  64. */
  65. function xajaxFunctionPlugin()
  66. {
  67. $this->aFunctions = array();
  68. $this->sXajaxPrefix = 'xajax_';
  69. $this->sDefer = '';
  70. $this->bDeferScriptGeneration = false;
  71. $this->sRequestedFunction = NULL;
  72. if (isset($_GET['xjxfun'])) $this->sRequestedFunction = $_GET['xjxfun'];
  73. if (isset($_POST['xjxfun'])) $this->sRequestedFunction = $_POST['xjxfun'];
  74. }
  75. /*
  76. Function: configure
  77. Sets/stores configuration options used by this plugin.
  78. */
  79. function configure($sName, $mValue)
  80. {
  81. if ('wrapperPrefix' == $sName) {
  82. $this->sXajaxPrefix = $mValue;
  83. } else if ('scriptDefferal' == $sName) {
  84. if (true === $mValue) $this->sDefer = 'defer ';
  85. else $this->sDefer = '';
  86. } else if ('deferScriptGeneration' == $sName) {
  87. if (true === $mValue || false === $mValue)
  88. $this->bDeferScriptGeneration = $mValue;
  89. else if ('deferred' === $mValue)
  90. $this->bDeferScriptGeneration = $mValue;
  91. }
  92. }
  93. /*
  94. Function: register
  95. Provides a mechanism for functions to be registered and made available to
  96. the page via the javascript <xajax.request> call.
  97. */
  98. function register($aArgs)
  99. {
  100. if (1 < count($aArgs))
  101. {
  102. $sType = $aArgs[0];
  103. if (XAJAX_FUNCTION == $sType)
  104. {
  105. $xuf =& $aArgs[1];
  106. if (false === is_a($xuf, 'xajaxUserFunction'))
  107. $xuf =& new xajaxUserFunction($xuf);
  108. if (2 < count($aArgs))
  109. if (is_array($aArgs[2]))
  110. foreach ($aArgs[2] as $sName => $sValue)
  111. $xuf->configure($sName, $sValue);
  112. $this->aFunctions[] =& $xuf;
  113. return $xuf->generateRequest($this->sXajaxPrefix);
  114. }
  115. }
  116. return false;
  117. }
  118. /*
  119. Function: generateClientScript
  120. Called by the <xajaxPluginManager> during the client script generation
  121. phase. This is used to generate a block of javascript code that will
  122. contain function declarations that can be used on the browser through
  123. javascript to initiate xajax requests.
  124. */
  125. function generateClientScript()
  126. {
  127. if (false === $this->bDeferScriptGeneration || 'deferred' === $this->bDeferScriptGeneration)
  128. {
  129. if (0 < count($this->aFunctions))
  130. {
  131. echo "\n<script type='text/javascript' " . $this->sDefer . "charset='UTF-8'>\n";
  132. echo "/* <![CDATA[ */\n";
  133. foreach (array_keys($this->aFunctions) as $sKey)
  134. $this->aFunctions[$sKey]->generateClientScript($this->sXajaxPrefix);
  135. echo "/* ]]> */\n";
  136. echo "</script>\n";
  137. }
  138. }
  139. }
  140. /*
  141. Function: canProcessRequest
  142. Determines whether or not the current request can be processed
  143. by this plugin.
  144. Returns:
  145. boolean - True if the current request can be handled by this plugin;
  146. false otherwise.
  147. */
  148. function canProcessRequest()
  149. {
  150. if (NULL == $this->sRequestedFunction)
  151. return false;
  152. return true;
  153. }
  154. /*
  155. Function: processRequest
  156. Called by the <xajaxPluginManager> when a request needs to be
  157. processed.
  158. Returns:
  159. mixed - True when the request has been processed successfully.
  160. An error message when an error has occurred.
  161. */
  162. function processRequest()
  163. {
  164. if (NULL == $this->sRequestedFunction)
  165. return false;
  166. $objArgumentManager =& xajaxArgumentManager::getInstance();
  167. $aArgs = $objArgumentManager->process();
  168. foreach (array_keys($this->aFunctions) as $sKey)
  169. {
  170. $xuf =& $this->aFunctions[$sKey];
  171. if ($xuf->getName() == $this->sRequestedFunction)
  172. {
  173. $xuf->call($aArgs);
  174. return true;
  175. }
  176. }
  177. return 'Invalid function request received; no request processor found with this name.';
  178. }
  179. }
  180. $objPluginManager =& xajaxPluginManager::getInstance();
  181. $objPluginManager->registerPlugin(new xajaxFunctionPlugin(), 100);