PageRenderTime 23ms CodeModel.GetById 16ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/source/Plug-in/xajax/xajax_core/xajaxRequest.inc.php

http://prosporous.googlecode.com/
PHP | 342 lines | 257 code | 7 blank | 78 comment | 6 complexity | fc5f826fe191597ad2e37dc8ca204d20 MD5 | raw file
  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*/
 40if (!defined ('XAJAX_FORM_VALUES')) define ('XAJAX_FORM_VALUES', 'get form values');
 41if (!defined ('XAJAX_INPUT_VALUE')) define ('XAJAX_INPUT_VALUE', 'get input value');
 42if (!defined ('XAJAX_CHECKED_VALUE')) define ('XAJAX_CHECKED_VALUE', 'get checked value');
 43if (!defined ('XAJAX_ELEMENT_INNERHTML')) define ('XAJAX_ELEMENT_INNERHTML', 'get element innerHTML');
 44if (!defined ('XAJAX_QUOTED_VALUE')) define ('XAJAX_QUOTED_VALUE', 'quoted value');
 45if (!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*/
 58class 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		echo $this->sName;
258		echo '(';
259		
260		$sSeparator = '';
261		
262		foreach ($this->aParameters as $sParameter)
263		{
264			echo $sSeparator;
265			echo $sParameter;
266			$sSeparator = ', ';
267		}
268		
269		echo ')';
270	}
271}
272
273/*
274	Class: xajaxCustomRequest
275	
276	This class extends the <xajaxRequest> class such that simple javascript
277	can be put in place of a xajax request to the server.  The primary purpose
278	of this class is to provide simple scripting services to the <xajaxControl>
279	based objects, like <clsInput>, <clsTable> and <clsButton>.
280*/
281class xajaxCustomRequest extends xajaxRequest
282{
283	/*
284		Array: aVariables;
285	*/
286	var $aVariables;
287	
288	/*
289		String: sScript;
290	*/
291	var $sScript;
292	
293	/*
294		Function: xajaxCustomRequest
295		
296		Constructs and initializes an instance of the object.
297		
298		sScript - (string):  The javascript (template) that will be printed
299			upon request.
300		aVariables - (associative array, optional):  An array of variable name, 
301			value pairs that will be passed to <xajaxCustomRequest->setVariable>
302	*/
303	function xajaxCustomRequest($sScript)
304	{
305		$this->aVariables = array();
306		$this->sScript = $sScript;
307	}
308	
309	/*
310		Function: clearVariables
311		
312		Clears the array of variables that will be used to modify the script before
313		it is printed and sent to the client.
314	*/
315	function clearVariables()
316	{
317		$this->aVariables = array();
318	}
319	
320	/*
321		Function: setVariable
322		
323		Sets a value that will be used to modify the script before it is sent to
324		the browser.  The <xajaxCustomRequest> object will perform a string 
325		replace operation on each of the values set with this function.
326	*/
327	function setVariable($sName, $sValue)
328	{
329		$this->aVariables[$sName] = $sValue;
330	}
331	
332	/*
333		Function: printScript
334	*/
335	function printScript()
336	{
337		$sScript = $this->sScript;
338		foreach ($this->aVariables as $sKey => $sValue)
339			$sScript = str_replace($sKey, $sValue, $sScript);
340		echo $sScript;
341	}
342}