1 | <?php |
---|
2 | /* |
---|
3 | File: xajaxCall.inc.php |
---|
4 | |
---|
5 | Contains the xajaxCall class |
---|
6 | |
---|
7 | Title: xajaxCall 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: xajaxCall.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: xajaxCall |
---|
22 | |
---|
23 | Create a piece of javascript code that will invoke the <xajax.call> |
---|
24 | function. |
---|
25 | |
---|
26 | This class is deprecated and will be removed in future versions; please use |
---|
27 | <xajaxRequest> instead. |
---|
28 | */ |
---|
29 | class xajaxCall { |
---|
30 | |
---|
31 | /**#@+ |
---|
32 | * @access protected |
---|
33 | */ |
---|
34 | |
---|
35 | /* |
---|
36 | String: sFunction |
---|
37 | |
---|
38 | Required: The name of the xajax enabled function to call |
---|
39 | */ |
---|
40 | var $sFunction; |
---|
41 | |
---|
42 | /* |
---|
43 | String: sReturnValue |
---|
44 | |
---|
45 | Required: The value to return once the <xajax.call> has |
---|
46 | returned. (for asynchronous calls, this is immediate) |
---|
47 | */ |
---|
48 | var $sReturnValue; |
---|
49 | |
---|
50 | /* |
---|
51 | Array: aParameters |
---|
52 | |
---|
53 | The associative array that will be used to store the parameters for this |
---|
54 | call. |
---|
55 | - key: The textual representation of the parameter. |
---|
56 | - value: A boolean value indicating whether or not to use quotes around |
---|
57 | this parameter. |
---|
58 | */ |
---|
59 | var $aParameters; |
---|
60 | |
---|
61 | /* |
---|
62 | String: sMode |
---|
63 | |
---|
64 | The mode to use for the call |
---|
65 | - 'synchronous' |
---|
66 | - 'asynchronous' |
---|
67 | */ |
---|
68 | var $sMode; |
---|
69 | |
---|
70 | /* |
---|
71 | String: sRequestType |
---|
72 | |
---|
73 | The request type that will be used for the call |
---|
74 | - 'GET' |
---|
75 | - 'POST' |
---|
76 | */ |
---|
77 | var $sRequestType; |
---|
78 | |
---|
79 | /* |
---|
80 | String: sResponseProcessor |
---|
81 | |
---|
82 | The name of the javascript function that will be invoked |
---|
83 | to handle the response. |
---|
84 | */ |
---|
85 | var $sResponseProcessor; |
---|
86 | |
---|
87 | /* |
---|
88 | String: sRequestURI |
---|
89 | |
---|
90 | The URI for where this request will be sent. |
---|
91 | */ |
---|
92 | var $sRequestURI; |
---|
93 | |
---|
94 | /* |
---|
95 | String: sContentType |
---|
96 | |
---|
97 | The content type to use for the request. |
---|
98 | */ |
---|
99 | var $sContentType; |
---|
100 | |
---|
101 | /* |
---|
102 | Constructor: xajaxCall |
---|
103 | |
---|
104 | Initializes the xajaxCall object. |
---|
105 | |
---|
106 | sFunction - (string): The name of the xajax enabled function |
---|
107 | that will be invoked when this javascript code is executed |
---|
108 | on the browser. This function name should match a PHP |
---|
109 | function from your script. |
---|
110 | */ |
---|
111 | function xajaxCall($sFunction = '') { |
---|
112 | $this->sFunction = $sFunction; |
---|
113 | $this->aParameters = array(); |
---|
114 | $this->sMode = ''; |
---|
115 | $this->sRequestType = ''; |
---|
116 | $this->sResponseProcessor = ''; |
---|
117 | $this->sRequestURI = ''; |
---|
118 | $this->sContentType = ''; |
---|
119 | } |
---|
120 | |
---|
121 | /* |
---|
122 | Function: setFunction |
---|
123 | |
---|
124 | Override the function name set in the constructor. |
---|
125 | |
---|
126 | Returns: |
---|
127 | |
---|
128 | object - The <xajaxCall> object. |
---|
129 | */ |
---|
130 | function setFunction($sFunction) { |
---|
131 | $this->sFunction = $sFunction; |
---|
132 | return $this; |
---|
133 | } |
---|
134 | |
---|
135 | /* |
---|
136 | Function: clearParameters |
---|
137 | |
---|
138 | Clear the list of parameters being accumulated for this |
---|
139 | call. |
---|
140 | |
---|
141 | Returns: |
---|
142 | |
---|
143 | object - The <xajaxCall> object. |
---|
144 | */ |
---|
145 | function clearParameters() { |
---|
146 | $this->aParameters = array(); |
---|
147 | } |
---|
148 | |
---|
149 | /* |
---|
150 | Function: addParameter |
---|
151 | |
---|
152 | Adds a parameter to the list that will be specified for the |
---|
153 | request generated by this <xajaxCall> object. |
---|
154 | |
---|
155 | sParameter - (string): The parameter value or name. |
---|
156 | bUseQuotes - (boolean): Whether or not to put quotes around this value. |
---|
157 | |
---|
158 | If you specify the name of a javascript variable, or provide a javascript |
---|
159 | function call as a parameter, do not use quotes around the value. |
---|
160 | |
---|
161 | Returns: |
---|
162 | |
---|
163 | object - The <xajaxCall> object. |
---|
164 | */ |
---|
165 | function addParameter($sParameter, $bUseQuotes = true) { |
---|
166 | $this->aParameters[] = array($sParameter, $bUseQuotes); |
---|
167 | return $this; |
---|
168 | } |
---|
169 | |
---|
170 | /* |
---|
171 | Function: addFormValuesParameter |
---|
172 | |
---|
173 | Add a parameter value that is the result of calling <xajax.getFormValues> |
---|
174 | for the specified form. |
---|
175 | |
---|
176 | sFormID - (string): The id of the form for which you wish to return |
---|
177 | the input values. |
---|
178 | |
---|
179 | Returns: |
---|
180 | |
---|
181 | object - The <xajaxCall> object. |
---|
182 | */ |
---|
183 | function addFormValuesParameter($sFormID) { |
---|
184 | $this->aParameters[] = array('xajax.getFormValues("'.$sFormID.'")'); |
---|
185 | return $this; |
---|
186 | } |
---|
187 | |
---|
188 | /* |
---|
189 | Function: setMode |
---|
190 | |
---|
191 | Sets the mode that will be specified for this <xajax.call> |
---|
192 | |
---|
193 | - 'synchronous' |
---|
194 | - 'asynchronous' |
---|
195 | |
---|
196 | Returns: |
---|
197 | |
---|
198 | object - The <xajaxCall> object. |
---|
199 | */ |
---|
200 | function setMode($sMode) { |
---|
201 | $this->sMode = $sMode; |
---|
202 | return $this; |
---|
203 | } |
---|
204 | |
---|
205 | /* |
---|
206 | Function: setRequestType |
---|
207 | |
---|
208 | Sets the request type which will be specified for the |
---|
209 | generated <xajax.call>. |
---|
210 | |
---|
211 | - 'GET' |
---|
212 | - 'POST' |
---|
213 | |
---|
214 | Returns: |
---|
215 | |
---|
216 | object - The <xajaxCall> object. |
---|
217 | */ |
---|
218 | function setRequestType($sRequestType) { |
---|
219 | $this->sRequestType = $sRequestType; |
---|
220 | return $this; |
---|
221 | } |
---|
222 | |
---|
223 | /* |
---|
224 | Function: setResponseProcessor |
---|
225 | |
---|
226 | Sets the name of the javascript function that will be used |
---|
227 | to process this response. This is an advanced function, use |
---|
228 | with caution. |
---|
229 | |
---|
230 | Returns: |
---|
231 | |
---|
232 | object - The <xajaxCall> object. |
---|
233 | */ |
---|
234 | function setResponseProcessor($sResponseProcessor) { |
---|
235 | $this->sResponseProcessor = $sResponseProcessor; |
---|
236 | return $this; |
---|
237 | } |
---|
238 | |
---|
239 | /* |
---|
240 | Function: setRequestURI |
---|
241 | |
---|
242 | Override the default URI with the specified one. |
---|
243 | |
---|
244 | sRequestURI - (string): The URI that the generated request will be sent |
---|
245 | to. |
---|
246 | |
---|
247 | Returns: |
---|
248 | |
---|
249 | object - The <xajaxCall> object. |
---|
250 | */ |
---|
251 | function setRequestURI($sRequestURI) { |
---|
252 | $this->sRequestURI = $sRequestURI; |
---|
253 | return $this; |
---|
254 | } |
---|
255 | |
---|
256 | /* |
---|
257 | Function: setContentType |
---|
258 | |
---|
259 | Sets the content type that will be used by the generated request. |
---|
260 | |
---|
261 | Returns: |
---|
262 | |
---|
263 | object - The <xajaxCall> object. |
---|
264 | */ |
---|
265 | function setContentType($sContentType) { |
---|
266 | $this->sContentType = $sContentType; |
---|
267 | } |
---|
268 | |
---|
269 | /* |
---|
270 | Function: setReturnValue |
---|
271 | |
---|
272 | Sets the value that will be returned after the generated call. |
---|
273 | Set to an empty string if no return value is desired. |
---|
274 | |
---|
275 | Returns: |
---|
276 | |
---|
277 | object - The <xajaxCall> object. |
---|
278 | */ |
---|
279 | function setReturnValue($sReturnValue) { |
---|
280 | $this->sReturnValue = $sReturnValue; |
---|
281 | } |
---|
282 | |
---|
283 | /* |
---|
284 | Function: generate |
---|
285 | |
---|
286 | Construct a <xajax.call> statement in javascript that can be used |
---|
287 | to make a xajax request with the parameters and settings previously |
---|
288 | configured for this <xajaxCall> object. |
---|
289 | |
---|
290 | The output from this function can be used as an event handler in your |
---|
291 | javascript code. |
---|
292 | |
---|
293 | Returns: |
---|
294 | |
---|
295 | string - The javascript statement that will invoked the <xajax.call> |
---|
296 | function on the browser, causing a xajax request to be sent to |
---|
297 | the server. |
---|
298 | */ |
---|
299 | function generate() { |
---|
300 | $output = 'xajax.call("'; |
---|
301 | $output .= $this->sFunction; |
---|
302 | $output .= '", {'; |
---|
303 | $separator = ''; |
---|
304 | if (0 < count($this->aParameters)) { |
---|
305 | $output .= 'parameters: ['; |
---|
306 | foreach ($this->aParameters as $aParameter) { |
---|
307 | $output .= $separator; |
---|
308 | $bUseQuotes = $aParameter[1]; |
---|
309 | if ($bUseQuotes) |
---|
310 | $output .= '"'; |
---|
311 | $output .= $aParameter[0]; |
---|
312 | if ($bUseQuotes) |
---|
313 | $output .= '"'; |
---|
314 | $separator = ','; |
---|
315 | } |
---|
316 | $output .= ']'; |
---|
317 | } |
---|
318 | if (0 < strlen($this->sMode)) { |
---|
319 | $output .= $separator; |
---|
320 | $output .= 'mode:"'; |
---|
321 | $output .= $this->sMode; |
---|
322 | $output .= '"'; |
---|
323 | $separator = ','; |
---|
324 | } |
---|
325 | if (0 < strlen($this->sRequestType)) { |
---|
326 | $output .= $separator; |
---|
327 | $output .= 'requestType:"'; |
---|
328 | $output .= $this->sRequestType; |
---|
329 | $output .= '"'; |
---|
330 | $separator = ','; |
---|
331 | } |
---|
332 | if (0 < strlen($this->sResponseProcessor)) { |
---|
333 | $output .= $separator; |
---|
334 | $output .= 'responseProcessor:'; |
---|
335 | $output .= $this->sResponseProcessor; |
---|
336 | $separator = ','; |
---|
337 | } |
---|
338 | if (0 < strlen($this->sRequestURI)) { |
---|
339 | $output .= $separator; |
---|
340 | $output .= 'requestURI:"'; |
---|
341 | $output .= $this->sRequestURI; |
---|
342 | $output .= '"'; |
---|
343 | $separator = ','; |
---|
344 | } |
---|
345 | if (0 < strlen($this->sContentType)) { |
---|
346 | $output .= $separator; |
---|
347 | $output .= 'contentType:"'; |
---|
348 | $output .= $this->sContentType; |
---|
349 | $output .= '"'; |
---|
350 | $separator = ','; |
---|
351 | } |
---|
352 | $output .= '}); '; |
---|
353 | if (0 < strlen($this->sReturnValue)) { |
---|
354 | $output .= 'return '; |
---|
355 | $output .= $this->sReturnValue; |
---|
356 | } else { |
---|
357 | $output .= 'return false;'; |
---|
358 | } |
---|
359 | |
---|
360 | return $output; |
---|
361 | } |
---|
362 | } |
---|