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