1 | <?php |
---|
2 | /* |
---|
3 | File: xajaxRequest.inc.php |
---|
4 | |
---|
5 | Contains the xajaxRequest class |
---|
6 | |
---|
7 | Title: xajaxRequest 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: xajaxRequest.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_FORM_VALUES |
---|
22 | Specifies that the parameter will consist of an array of form values. |
---|
23 | |
---|
24 | Constant: XAJAX_INPUT_VALUE |
---|
25 | Specifies that the parameter will contain the value of an input control. |
---|
26 | |
---|
27 | Constant: XAJAX_CHECKED_VALUE |
---|
28 | Specifies that the parameter will consist of a boolean value of a checkbox. |
---|
29 | |
---|
30 | Constant: XAJAX_ELEMENT_INNERHTML |
---|
31 | Specifies that the parameter value will be the innerHTML value of the element. |
---|
32 | |
---|
33 | Constant: XAJAX_QUOTED_VALUE |
---|
34 | Specifies that the parameter will be a quoted value (string). |
---|
35 | |
---|
36 | Constant: XAJAX_JS_VALUE |
---|
37 | Specifies that the parameter will be a non-quoted value (evaluated by the |
---|
38 | browsers javascript engine at run time. |
---|
39 | */ |
---|
40 | if (!defined ('XAJAX_FORM_VALUES')) define ('XAJAX_FORM_VALUES', 'get form values'); |
---|
41 | if (!defined ('XAJAX_INPUT_VALUE')) define ('XAJAX_INPUT_VALUE', 'get input value'); |
---|
42 | if (!defined ('XAJAX_CHECKED_VALUE')) define ('XAJAX_CHECKED_VALUE', 'get checked value'); |
---|
43 | if (!defined ('XAJAX_ELEMENT_INNERHTML')) define ('XAJAX_ELEMENT_INNERHTML', 'get element innerHTML'); |
---|
44 | if (!defined ('XAJAX_QUOTED_VALUE')) define ('XAJAX_QUOTED_VALUE', 'quoted value'); |
---|
45 | if (!defined ('XAJAX_JS_VALUE')) define ('XAJAX_JS_VALUE', 'unquoted value'); |
---|
46 | |
---|
47 | /* |
---|
48 | Class: xajaxRequest |
---|
49 | |
---|
50 | Used to store and generate the client script necessary to invoke |
---|
51 | a xajax request from the browser to the server script. |
---|
52 | |
---|
53 | This object is typically generated by the <xajax->register> method |
---|
54 | and can be used to quickly generate the javascript that is used |
---|
55 | to initiate a xajax request to the registered function, object, event |
---|
56 | or other xajax call. |
---|
57 | */ |
---|
58 | class xajaxRequest |
---|
59 | { |
---|
60 | /* |
---|
61 | String: sName |
---|
62 | |
---|
63 | The name of the function. |
---|
64 | */ |
---|
65 | var $sName; |
---|
66 | |
---|
67 | /* |
---|
68 | String: sQuoteCharacter |
---|
69 | |
---|
70 | A string containing either a single or a double quote character |
---|
71 | that will be used during the generation of the javascript for |
---|
72 | this function. This can be set prior to calling <xajaxRequest->printScript> |
---|
73 | */ |
---|
74 | var $sQuoteCharacter; |
---|
75 | |
---|
76 | /* |
---|
77 | Array: aParameters |
---|
78 | |
---|
79 | An array of parameters that will be used to populate the argument list |
---|
80 | for this function when the javascript is output in <xajaxRequest->printScript> |
---|
81 | */ |
---|
82 | var $aParameters; |
---|
83 | |
---|
84 | /* |
---|
85 | Function: xajaxRequest |
---|
86 | |
---|
87 | Construct and initialize this request. |
---|
88 | |
---|
89 | sName - (string): The name of this request. |
---|
90 | */ |
---|
91 | function xajaxRequest($sName) |
---|
92 | { |
---|
93 | $this->aParameters = array(); |
---|
94 | $this->sQuoteCharacter = '"'; |
---|
95 | $this->sName = $sName; |
---|
96 | } |
---|
97 | |
---|
98 | /* |
---|
99 | Function: useSingleQuote |
---|
100 | |
---|
101 | Call this to instruct the request to use single quotes when generating |
---|
102 | the javascript. |
---|
103 | */ |
---|
104 | function useSingleQuote() |
---|
105 | { |
---|
106 | $this->sQuoteCharacter = "'"; |
---|
107 | } |
---|
108 | |
---|
109 | /* |
---|
110 | Function: useDoubleQuote |
---|
111 | |
---|
112 | Call this to instruct the request to use double quotes while generating |
---|
113 | the javascript. |
---|
114 | */ |
---|
115 | function useDoubleQuote() |
---|
116 | { |
---|
117 | $this->sQuoteCharacter = '"'; |
---|
118 | } |
---|
119 | |
---|
120 | /* |
---|
121 | Function: clearParameters |
---|
122 | |
---|
123 | Clears the parameter list associated with this request. |
---|
124 | */ |
---|
125 | function clearParameters() |
---|
126 | { |
---|
127 | $this->aParameters = array(); |
---|
128 | } |
---|
129 | |
---|
130 | /* |
---|
131 | Function: addParameter |
---|
132 | |
---|
133 | Adds a parameter value to the parameter list for this request. |
---|
134 | |
---|
135 | sType - (string): The type of the value to be used. |
---|
136 | sValue - (string: The value to be used. |
---|
137 | |
---|
138 | See <xajaxRequest->setParameter> for details. |
---|
139 | */ |
---|
140 | function addParameter() |
---|
141 | { |
---|
142 | $aArgs = func_get_args(); |
---|
143 | |
---|
144 | if (1 < count($aArgs)) |
---|
145 | $this->setParameter( |
---|
146 | count($this->aParameters), |
---|
147 | $aArgs[0], |
---|
148 | $aArgs[1]); |
---|
149 | } |
---|
150 | |
---|
151 | /* |
---|
152 | Function: setParameter |
---|
153 | |
---|
154 | Sets a specific parameter value. |
---|
155 | |
---|
156 | nParameter - (number): The index of the parameter to set |
---|
157 | sType - (string): The type of value |
---|
158 | sValue - (string): The value as it relates to the specified type |
---|
159 | |
---|
160 | Types should be one of the following <XAJAX_FORM_VALUES>, <XAJAX_QUOTED_VALUE>, |
---|
161 | <XAJAX_JS_VALUE>, <XAJAX_INPUT_VALUE>, <XAJAX_CHECKED_VALUE>. |
---|
162 | The value should be as follows: |
---|
163 | <XAJAX_FORM_VALUES> - Use the ID of the form you want to process. |
---|
164 | <XAJAX_QUOTED_VALUE> - The string data to be passed. |
---|
165 | <XAJAX_JS_VALUE> - A string containing valid javascript (either a javascript |
---|
166 | variable name that will be in scope at the time of the call or a |
---|
167 | javascript function call whose return value will become the parameter. |
---|
168 | |
---|
169 | TODO: finish documenting the options. |
---|
170 | */ |
---|
171 | function setParameter() |
---|
172 | { |
---|
173 | $aArgs = func_get_args(); |
---|
174 | |
---|
175 | if (2 < count($aArgs)) |
---|
176 | { |
---|
177 | $nParameter = $aArgs[0]; |
---|
178 | $sType = $aArgs[1]; |
---|
179 | |
---|
180 | if (XAJAX_FORM_VALUES == $sType) |
---|
181 | { |
---|
182 | $sFormID = $aArgs[2]; |
---|
183 | $this->aParameters[$nParameter] = |
---|
184 | "xajax.getFormValues(" |
---|
185 | . $this->sQuoteCharacter |
---|
186 | . $sFormID |
---|
187 | . $this->sQuoteCharacter |
---|
188 | . ")"; |
---|
189 | } |
---|
190 | else if (XAJAX_INPUT_VALUE == $sType) |
---|
191 | { |
---|
192 | $sInputID = $aArgs[2]; |
---|
193 | $this->aParameters[$nParameter] = |
---|
194 | "xajax.$(" |
---|
195 | . $this->sQuoteCharacter |
---|
196 | . $sInputID |
---|
197 | . $this->sQuoteCharacter |
---|
198 | . ").value"; |
---|
199 | } |
---|
200 | else if (XAJAX_CHECKED_VALUE == $sType) |
---|
201 | { |
---|
202 | $sCheckedID = $aArgs[2]; |
---|
203 | $this->aParameters[$nParameter] = |
---|
204 | "xajax.$(" |
---|
205 | . $this->sQuoteCharacter |
---|
206 | . $sCheckedID |
---|
207 | . $this->sQuoteCharacter |
---|
208 | . ").checked"; |
---|
209 | } |
---|
210 | else if (XAJAX_ELEMENT_INNERHTML == $sType) |
---|
211 | { |
---|
212 | $sElementID = $aArgs[2]; |
---|
213 | $this->aParameters[$nParameter] = |
---|
214 | "xajax.$(" |
---|
215 | . $this->sQuoteCharacter |
---|
216 | . $sElementID |
---|
217 | . $this->sQuoteCharacter |
---|
218 | . ").innerHTML"; |
---|
219 | } |
---|
220 | else if (XAJAX_QUOTED_VALUE == $sType) |
---|
221 | { |
---|
222 | $sValue = $aArgs[2]; |
---|
223 | $this->aParameters[$nParameter] = |
---|
224 | $this->sQuoteCharacter |
---|
225 | . $sValue |
---|
226 | . $this->sQuoteCharacter; |
---|
227 | } |
---|
228 | else if (XAJAX_JS_VALUE == $sType) |
---|
229 | { |
---|
230 | $sValue = $aArgs[2]; |
---|
231 | $this->aParameters[$nParameter] = $sValue; |
---|
232 | } |
---|
233 | } |
---|
234 | } |
---|
235 | |
---|
236 | /* |
---|
237 | Function: getScript |
---|
238 | |
---|
239 | Returns a string representation of the script output (javascript) from |
---|
240 | this request object. See also: <xajaxRequest::printScript> |
---|
241 | */ |
---|
242 | function getScript() |
---|
243 | { |
---|
244 | ob_start(); |
---|
245 | $this->printScript(); |
---|
246 | return ob_get_clean(); |
---|
247 | } |
---|
248 | |
---|
249 | /* |
---|
250 | Function: printScript |
---|
251 | |
---|
252 | Generates a block of javascript code that can be used to invoke |
---|
253 | the specified xajax request. |
---|
254 | */ |
---|
255 | function printScript() |
---|
256 | { |
---|
257 | $sOutput = $this->sName; |
---|
258 | $sOutput .= "("; |
---|
259 | |
---|
260 | $sSeparator = ""; |
---|
261 | |
---|
262 | foreach ($this->aParameters as $sParameter) |
---|
263 | { |
---|
264 | $sOutput .= $sSeparator; |
---|
265 | $sOutput .= $sParameter; |
---|
266 | $sSeparator = ", "; |
---|
267 | } |
---|
268 | |
---|
269 | $sOutput .= ")"; |
---|
270 | |
---|
271 | print $sOutput; |
---|
272 | } |
---|
273 | } |
---|
274 | |
---|
275 | /* |
---|
276 | Class: xajaxCustomRequest |
---|
277 | |
---|
278 | This class extends the <xajaxRequest> class such that simple javascript |
---|
279 | can be put in place of a xajax request to the server. The primary purpose |
---|
280 | of this class is to provide simple scripting services to the <xajaxControl> |
---|
281 | based objects, like <clsInput>, <clsTable> and <clsButton>. |
---|
282 | */ |
---|
283 | class xajaxCustomRequest extends xajaxRequest |
---|
284 | { |
---|
285 | /* |
---|
286 | Array: aVariables; |
---|
287 | */ |
---|
288 | var $aVariables; |
---|
289 | |
---|
290 | /* |
---|
291 | String: sScript; |
---|
292 | */ |
---|
293 | var $sScript; |
---|
294 | |
---|
295 | /* |
---|
296 | Function: xajaxCustomRequest |
---|
297 | |
---|
298 | Constructs and initializes an instance of the object. |
---|
299 | |
---|
300 | sScript - (string): The javascript (template) that will be printed |
---|
301 | upon request. |
---|
302 | aVariables - (associative array, optional): An array of variable name, |
---|
303 | value pairs that will be passed to <xajaxCustomRequest->setVariable> |
---|
304 | */ |
---|
305 | function xajaxCustomRequest($sScript) |
---|
306 | { |
---|
307 | $this->aVariables = array(); |
---|
308 | $this->sScript = $sScript; |
---|
309 | } |
---|
310 | |
---|
311 | /* |
---|
312 | Function: clearVariables |
---|
313 | |
---|
314 | Clears the array of variables that will be used to modify the script before |
---|
315 | it is printed and sent to the client. |
---|
316 | */ |
---|
317 | function clearVariables() |
---|
318 | { |
---|
319 | $this->aVariables = array(); |
---|
320 | } |
---|
321 | |
---|
322 | /* |
---|
323 | Function: setVariable |
---|
324 | |
---|
325 | Sets a value that will be used to modify the script before it is sent to |
---|
326 | the browser. The <xajaxCustomRequest> object will perform a string |
---|
327 | replace operation on each of the values set with this function. |
---|
328 | */ |
---|
329 | function setVariable($sName, $sValue) |
---|
330 | { |
---|
331 | $this->aVariables[$sName] = $sValue; |
---|
332 | } |
---|
333 | |
---|
334 | /* |
---|
335 | Function: printScript |
---|
336 | */ |
---|
337 | function printScript() |
---|
338 | { |
---|
339 | $sScript = $this->sScript; |
---|
340 | foreach ($this->aVariables as $sKey => $sValue) |
---|
341 | $sScript = str_replace($sKey, $sValue, $sScript); |
---|
342 | echo $sScript; |
---|
343 | } |
---|
344 | } |
---|