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

http://prosporous.googlecode.com/ · PHP · 228 lines · 120 code · 37 blank · 71 comment · 41 complexity · e4338033c47fd6471a8442fdc724685a MD5 · raw file

  1. <?php
  2. /*
  3. File: xajaxEventPlugin.inc.php
  4. Contains the xajaxEventPlugin class
  5. Title: xajaxEventPlugin class
  6. Please see <copyright.inc.php> for a detailed description, copyright
  7. and license information.
  8. */
  9. /*
  10. @package xajax
  11. @version $Id: xajaxEventPlugin.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_EVENT
  17. Specifies that the item being registered via the <xajax->register> function
  18. is an event.
  19. Constant: XAJAX_EVENT_HANDLER
  20. Specifies that the item being registered via the <xajax->register> function
  21. is an event handler.
  22. */
  23. if (!defined ('XAJAX_EVENT')) define ('XAJAX_EVENT', 'xajax event');
  24. if (!defined ('XAJAX_EVENT_HANDLER')) define ('XAJAX_EVENT_HANDLER', 'xajax event handler');
  25. //SkipAIO
  26. require dirname(__FILE__) . '/support/xajaxEvent.inc.php';
  27. //EndSkipAIO
  28. /*
  29. Class: xajaxEventPlugin
  30. Plugin that adds server side event handling capabilities to xajax. Events can
  31. be registered, then event handlers attached.
  32. */
  33. class xajaxEventPlugin extends xajaxRequestPlugin
  34. {
  35. /*
  36. Array: aEvents
  37. */
  38. var $aEvents;
  39. /*
  40. String: sXajaxPrefix
  41. */
  42. var $sXajaxPrefix;
  43. /*
  44. String: sEventPrefix
  45. */
  46. var $sEventPrefix;
  47. /*
  48. String: sDefer
  49. */
  50. var $sDefer;
  51. var $bDeferScriptGeneration;
  52. /*
  53. String: sRequestedEvent
  54. */
  55. var $sRequestedEvent;
  56. /*
  57. Function: xajaxEventPlugin
  58. */
  59. function xajaxEventPlugin()
  60. {
  61. $this->aEvents = array();
  62. $this->sXajaxPrefix = 'xajax_';
  63. $this->sEventPrefix = 'event_';
  64. $this->sDefer = '';
  65. $this->bDeferScriptGeneration = false;
  66. $this->sRequestedEvent = NULL;
  67. if (isset($_GET['xjxevt'])) $this->sRequestedEvent = $_GET['xjxevt'];
  68. if (isset($_POST['xjxevt'])) $this->sRequestedEvent = $_POST['xjxevt'];
  69. }
  70. /*
  71. Function: configure
  72. */
  73. function configure($sName, $mValue)
  74. {
  75. if ('wrapperPrefix' == $sName) {
  76. $this->sXajaxPrefix = $mValue;
  77. } else if ('eventPrefix' == $sName) {
  78. $this->sEventPrefix = $mValue;
  79. } else if ('scriptDefferal' == $sName) {
  80. if (true === $mValue) $this->sDefer = 'defer ';
  81. else $this->sDefer = '';
  82. } else if ('deferScriptGeneration' == $sName) {
  83. if (true === $mValue || false === $mValue)
  84. $this->bDeferScriptGeneration = $mValue;
  85. else if ('deferred' === $mValue)
  86. $this->bDeferScriptGeneration = $mValue;
  87. }
  88. }
  89. /*
  90. Function: register
  91. $sType - (string): type of item being registered
  92. $sEvent - (string): the name of the event
  93. $ufHandler - (function name or reference): a reference to the user function to call
  94. $aConfiguration - (array): an array containing configuration options
  95. */
  96. function register($aArgs)
  97. {
  98. if (1 < count($aArgs))
  99. {
  100. $sType = $aArgs[0];
  101. if (XAJAX_EVENT == $sType)
  102. {
  103. $sEvent = $aArgs[1];
  104. if (false === isset($this->aEvents[$sEvent]))
  105. {
  106. $xe =& new xajaxEvent($sEvent);
  107. if (2 < count($aArgs))
  108. if (is_array($aArgs[2]))
  109. foreach ($aArgs[2] as $sKey => $sValue)
  110. $xe->configure($sKey, $sValue);
  111. $this->aEvents[$sEvent] =& $xe;
  112. return $xe->generateRequest($this->sXajaxPrefix, $this->sEventPrefix);
  113. }
  114. }
  115. if (XAJAX_EVENT_HANDLER == $sType)
  116. {
  117. $sEvent = $aArgs[1];
  118. if (isset($this->aEvents[$sEvent]))
  119. {
  120. if (isset($aArgs[2]))
  121. {
  122. $xuf =& $aArgs[2];
  123. if (false === is_a($xuf, 'xajaxUserFunction'))
  124. $xuf =& new xajaxUserFunction($xuf);
  125. $objEvent =& $this->aEvents[$sEvent];
  126. $objEvent->addHandler($xuf);
  127. return true;
  128. }
  129. }
  130. }
  131. }
  132. return false;
  133. }
  134. /*
  135. Function: generateClientScript
  136. */
  137. function generateClientScript()
  138. {
  139. if (false === $this->bDeferScriptGeneration || 'deferred' === $this->bDeferScriptGeneration)
  140. {
  141. if (0 < count($this->aEvents))
  142. {
  143. echo "\n<script type='text/javascript' ";
  144. echo $this->sDefer;
  145. echo "charset='UTF-8'>\n";
  146. echo "/* <![CDATA[ */\n";
  147. foreach (array_keys($this->aEvents) as $sKey)
  148. $this->aEvents[$sKey]->generateClientScript($this->sXajaxPrefix, $this->sEventPrefix);
  149. echo "/* ]]> */\n";
  150. echo "</script>\n";
  151. }
  152. }
  153. }
  154. /*
  155. Function: canProcessRequest
  156. */
  157. function canProcessRequest()
  158. {
  159. if (NULL == $this->sRequestedEvent)
  160. return false;
  161. return true;
  162. }
  163. /*
  164. Function: processRequest
  165. */
  166. function processRequest()
  167. {
  168. if (NULL == $this->sRequestedEvent)
  169. return false;
  170. $objArgumentManager =& xajaxArgumentManager::getInstance();
  171. $aArgs = $objArgumentManager->process();
  172. foreach (array_keys($this->aEvents) as $sKey)
  173. {
  174. $objEvent =& $this->aEvents[$sKey];
  175. if ($objEvent->getName() == $this->sRequestedEvent)
  176. {
  177. $objEvent->fire($aArgs);
  178. return true;
  179. }
  180. }
  181. return 'Invalid event request received; no event was registered with this name.';
  182. }
  183. }
  184. $objPluginManager =& xajaxPluginManager::getInstance();
  185. $objPluginManager->registerPlugin(new xajaxEventPlugin(), 103);