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