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