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