3 * xajaxResponse.inc.php :: xajax XML response class
6 * copyright (c) 2005 by Jared White & J. Max Wilson
7 * http://www.xajaxproject.org
9 * xajax is an open source PHP class library for easily creating powerful
10 * PHP-driven, web-based Ajax Applications. Using xajax, you can asynchronously
11 * call PHP functions and update the content of your your webpage without
14 * xajax is released under the terms of the LGPL license
15 * http://www.gnu.org/copyleft/lesser.html#SEC3
17 * This library is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public
19 * License as published by the Free Software Foundation; either
20 * version 2.1 of the License, or (at your option) any later version.
22 * This library is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * Lesser General Public License for more details.
27 * You should have received a copy of the GNU Lesser General Public
28 * License along with this library; if not, write to the Free Software
29 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 * @copyright Copyright (c) 2005-2006 by Jared White & J. Max Wilson
34 * @license http://www.gnu.org/copyleft/lesser.html#SEC3 LGPL License
38 ----------------------------------------------------------------------------
39 | Online documentation for this class is available on the xajax wiki at: |
40 | http://wiki.xajaxproject.org/Documentation:xajaxResponse.inc.php |
41 ----------------------------------------------------------------------------
45 * The xajaxResponse class is used to create responses to be sent back to your
46 * Web page. A response contains one or more command messages for updating
48 * Currently xajax supports 21 kinds of command messages, including some common
51 * <li>Assign - sets the specified attribute of an element in your page</li>
52 * <li>Append - appends data to the end of the specified attribute of an
53 * element in your page</li>
54 * <li>Prepend - prepends data to the beginning of the specified attribute of
55 * an element in your page</li>
56 * <li>Replace - searches for and replaces data in the specified attribute of
57 * an element in your page</li>
58 * <li>Script - runs the supplied JavaScript code</li>
59 * <li>Alert - shows an alert box with the supplied message text</li>
62 * <i>Note:</i> elements are identified by their HTML id, so if you don't see
63 * your browser HTML display changing from the request, make sure you're using
64 * the right id names in your response.
74 * @var string internal XML storage
78 * @var string the encoding type to use
82 * @var boolean if special characters in the XML should be converted to
90 * The constructor's main job is to set the character encoding for the
93 * <i>Note:</i> to change the character encoding for all of the
94 * responses, set the XAJAX_DEFAULT_ENCODING constant before you
97 * @param string contains the character encoding string to use
98 * @param boolean lets you set if you want special characters in the output
99 * converted to HTML entities
102 function xajaxResponse($sEncoding=XAJAX_DEFAULT_CHAR_ENCODING
, $bOutputEntities=false
)
104 $this->setCharEncoding($sEncoding);
105 $this->bOutputEntities
= $bOutputEntities;
109 * Sets the character encoding for the response based on $sEncoding, which
110 * is a string containing the character encoding to use. You don't need to
111 * use this method normally, since the character encoding for the response
112 * gets set automatically based on the XAJAX_DEFAULT_CHAR_ENCODING
117 function setCharEncoding($sEncoding)
119 $this->sEncoding
= $sEncoding;
123 * Tells the response object to convert special characters to HTML entities
124 * automatically (only works if the mb_string extension is available).
126 function outputEntitiesOn()
128 $this->bOutputEntities
= true
;
132 * Tells the response object to output special characters intact. (default
135 function outputEntitiesOff()
137 $this->bOutputEntities
= false
;
141 * Adds a confirm commands command message to the XML response.
143 * <i>Usage:</i> <kbd>$objResponse->addConfirmCommands(1, "Do you want to preview the new data?");</kbd>
145 * @param integer the number of commands to skip if the user presses
146 * Cancel in the browsers's confirm dialog
147 * @param string the message to show in the browser's confirm dialog
149 function addConfirmCommands($iCmdNumber, $sMessage)
151 $this->xml
.= $this->_cmdXML(array("n"=>"cc","t"=>$iCmdNumber),$sMessage);
155 * Adds an assign command message to the XML response.
157 * <i>Usage:</i> <kbd>$objResponse->addAssign("contentDiv", "innerHTML", "Some Text");</kbd>
159 * @param string contains the id of an HTML element
160 * @param string the part of the element you wish to modify ("innerHTML",
162 * @param string the data you want to set the attribute to
164 function addAssign($sTarget,$sAttribute,$sData)
166 $this->xml
.= $this->_cmdXML(array("n"=>"as","t"=>$sTarget,"p"=>$sAttribute),$sData);
170 * Adds an append command message to the XML response.
172 * <i>Usage:</i> <kbd>$objResponse->addAppend("contentDiv", "innerHTML", "Some New Text");</kbd>
174 * @param string contains the id of an HTML element
175 * @param string the part of the element you wish to modify ("innerHTML",
177 * @param string the data you want to append to the end of the attribute
179 function addAppend($sTarget,$sAttribute,$sData)
181 $this->xml
.= $this->_cmdXML(array("n"=>"ap","t"=>$sTarget,"p"=>$sAttribute),$sData);
185 * Adds an prepend command message to the XML response.
187 * <i>Usage:</i> <kbd>$objResponse->addPrepend("contentDiv", "innerHTML", "Some Starting Text");</kbd>
189 * @param string contains the id of an HTML element
190 * @param string the part of the element you wish to modify ("innerHTML",
192 * @param string the data you want to prepend to the beginning of the
195 function addPrepend($sTarget,$sAttribute,$sData)
197 $this->xml
.= $this->_cmdXML(array("n"=>"pp","t"=>$sTarget,"p"=>$sAttribute),$sData);
201 * Adds a replace command message to the XML response.
203 * <i>Usage:</i> <kbd>$objResponse->addReplace("contentDiv", "innerHTML", "text", "<b>text</b>");</kbd>
205 * @param string contains the id of an HTML element
206 * @param string the part of the element you wish to modify ("innerHTML",
208 * @param string the string to search for
209 * @param string the string to replace the search string when found in the
212 function addReplace($sTarget,$sAttribute,$sSearch,$sData)
214 $sDta = "<s><![CDATA[$sSearch]]></s><r><![CDATA[$sData]]></r>";
215 $this->xml
.= $this->_cmdXML(array("n"=>"rp","t"=>$sTarget,"p"=>$sAttribute),$sDta);
219 * Adds a clear command message to the XML response.
221 * <i>Usage:</i> <kbd>$objResponse->addClear("contentDiv", "innerHTML");</kbd>
223 * @param string contains the id of an HTML element
224 * @param string the part of the element you wish to clear ("innerHTML",
227 function addClear($sTarget,$sAttribute)
229 $this->addAssign($sTarget,$sAttribute,'');
233 * Adds an alert command message to the XML response.
235 * <i>Usage:</i> <kbd>$objResponse->addAlert("This is important information");</kbd>
237 * @param string the text to be displayed in the Javascript alert box
239 function addAlert($sMsg)
241 $this->xml
.= $this->_cmdXML(array("n"=>"al"),$sMsg);
245 * Uses the addScript() method to add a Javascript redirect to another URL.
247 * <i>Usage:</i> <kbd>$objResponse->addRedirect("http://www.xajaxproject.org");</kbd>
249 * @param string the URL to redirect the client browser to
251 function addRedirect($sURL)
253 //we need to parse the query part so that the values are rawurlencode()'ed
254 //can't just use parse_url() cos we could be dealing with a relative URL which
255 // parse_url() can't deal with.
256 $queryStart = strpos($sURL, '?', strrpos($sURL, '/'));
257 if ($queryStart !== FALSE
)
260 $queryEnd = strpos($sURL, '#', $queryStart);
261 if ($queryEnd === FALSE
)
262 $queryEnd = strlen($sURL);
263 $queryPart = substr($sURL, $queryStart, $queryEnd-$queryStart);
264 parse_str($queryPart, $queryParts);
266 foreach($queryParts as $key => $value)
268 $newQueryPart .= rawurlencode($key).'='.rawurlencode($value).ini_get('arg_separator.output');
270 $sURL = str_replace($queryPart, $newQueryPart, $sURL);
272 $this->addScript('window.location = "'.$sURL.'";');
276 * Adds a Javascript command message to the XML response.
278 * <i>Usage:</i> <kbd>$objResponse->addScript("var x = prompt('get some text');");</kbd>
280 * @param string contains Javascript code to be executed
282 function addScript($sJS)
284 $this->xml
.= $this->_cmdXML(array("n"=>"js"),$sJS);
288 * Adds a Javascript function call command message to the XML response.
290 * <i>Usage:</i> <kbd>$objResponse->addScriptCall("myJSFunction", "arg 1", "arg 2", 12345);</kbd>
292 * @param string $sFunc the name of a Javascript function
293 * @param mixed $args,... optional arguments to pass to the Javascript function
295 function addScriptCall() {
296 $arguments = func_get_args();
297 $sFunc = array_shift($arguments);
298 $sData = $this->_buildObjXml($arguments);
299 $this->xml
.= $this->_cmdXML(array("n"=>"jc","t"=>$sFunc),$sData);
303 * Adds a remove element command message to the XML response.
305 * <i>Usage:</i> <kbd>$objResponse->addRemove("Div2");</kbd>
307 * @param string contains the id of an HTML element to be removed
309 function addRemove($sTarget)
311 $this->xml
.= $this->_cmdXML(array("n"=>"rm","t"=>$sTarget),'');
315 * Adds a create element command message to the XML response.
317 * <i>Usage:</i> <kbd>$objResponse->addCreate("parentDiv", "h3", "myid");</kbd>
319 * @param string contains the id of an HTML element to to which the new
320 * element will be appended.
321 * @param string the tag to be added
322 * @param string the id to be assigned to the new element
323 * @param string deprecated, use the addCreateInput() method instead
325 function addCreate($sParent, $sTag, $sId, $sType="")
329 trigger_error("The \$sType parameter of addCreate has been deprecated. Use the addCreateInput() method instead.", E_USER_WARNING
);
332 $this->xml
.= $this->_cmdXML(array("n"=>"ce","t"=>$sParent,"p"=>$sId),$sTag);
336 * Adds a insert element command message to the XML response.
338 * <i>Usage:</i> <kbd>$objResponse->addInsert("childDiv", "h3", "myid");</kbd>
340 * @param string contains the id of the child before which the new element
342 * @param string the tag to be added
343 * @param string the id to be assigned to the new element
345 function addInsert($sBefore, $sTag, $sId)
347 $this->xml
.= $this->_cmdXML(array("n"=>"ie","t"=>$sBefore,"p"=>$sId),$sTag);
351 * Adds a insert element command message to the XML response.
353 * <i>Usage:</i> <kbd>$objResponse->addInsertAfter("childDiv", "h3", "myid");</kbd>
355 * @param string contains the id of the child after which the new element
357 * @param string the tag to be added
358 * @param string the id to be assigned to the new element
360 function addInsertAfter($sAfter, $sTag, $sId)
362 $this->xml
.= $this->_cmdXML(array("n"=>"ia","t"=>$sAfter,"p"=>$sId),$sTag);
366 * Adds a create input command message to the XML response.
368 * <i>Usage:</i> <kbd>$objResponse->addCreateInput("form1", "text", "username", "input1");</kbd>
370 * @param string contains the id of an HTML element to which the new input
372 * @param string the type of input to be created (text, radio, checkbox,
374 * @param string the name to be assigned to the new input and the variable
375 * name when it is submitted
376 * @param string the id to be assigned to the new input
378 function addCreateInput($sParent, $sType, $sName, $sId)
380 $this->xml
.= $this->_cmdXML(array("n"=>"ci","t"=>$sParent,"p"=>$sId,"c"=>$sType),$sName);
384 * Adds an insert input command message to the XML response.
386 * <i>Usage:</i> <kbd>$objResponse->addInsertInput("input5", "text", "username", "input1");</kbd>
388 * @param string contains the id of the child before which the new element
390 * @param string the type of input to be created (text, radio, checkbox,
392 * @param string the name to be assigned to the new input and the variable
393 * name when it is submitted
394 * @param string the id to be assigned to the new input
396 function addInsertInput($sBefore, $sType, $sName, $sId)
398 $this->xml
.= $this->_cmdXML(array("n"=>"ii","t"=>$sBefore,"p"=>$sId,"c"=>$sType),$sName);
402 * Adds an insert input command message to the XML response.
404 * <i>Usage:</i> <kbd>$objResponse->addInsertInputAfter("input7", "text", "email", "input2");</kbd>
406 * @param string contains the id of the child after which the new element
408 * @param string the type of input to be created (text, radio, checkbox,
410 * @param string the name to be assigned to the new input and the variable
411 * name when it is submitted
412 * @param string the id to be assigned to the new input
414 function addInsertInputAfter($sAfter, $sType, $sName, $sId)
416 $this->xml
.= $this->_cmdXML(array("n"=>"iia","t"=>$sAfter,"p"=>$sId,"c"=>$sType),$sName);
420 * Adds an event command message to the XML response.
422 * <i>Usage:</i> <kbd>$objResponse->addEvent("contentDiv", "onclick", "alert(\'Hello World\');");</kbd>
424 * @param string contains the id of an HTML element
425 * @param string the event you wish to set ("onclick", "onmouseover", etc.)
426 * @param string the Javascript string you want the event to invoke
428 function addEvent($sTarget,$sEvent,$sScript)
430 $this->xml
.= $this->_cmdXML(array("n"=>"ev","t"=>$sTarget,"p"=>$sEvent),$sScript);
434 * Adds a handler command message to the XML response.
436 * <i>Usage:</i> <kbd>$objResponse->addHandler("contentDiv", "onclick", "content_click");</kbd>
438 * @param string contains the id of an HTML element
439 * @param string the event you wish to set ("onclick", "onmouseover", etc.)
440 * @param string the name of a Javascript function that will handle the
441 * event. Multiple handlers can be added for the same event
443 function addHandler($sTarget,$sEvent,$sHandler)
445 $this->xml
.= $this->_cmdXML(array("n"=>"ah","t"=>$sTarget,"p"=>$sEvent),$sHandler);
449 * Adds a remove handler command message to the XML response.
451 * <i>Usage:</i> <kbd>$objResponse->addRemoveHandler("contentDiv", "onclick", "content_click");</kbd>
453 * @param string contains the id of an HTML element
454 * @param string the event you wish to remove ("onclick", "onmouseover",
456 * @param string the name of a Javascript handler function that you want to
459 function addRemoveHandler($sTarget,$sEvent,$sHandler)
461 $this->xml
.= $this->_cmdXML(array("n"=>"rh","t"=>$sTarget,"p"=>$sEvent),$sHandler);
465 * Adds an include script command message to the XML response.
467 * <i>Usage:</i> <kbd>$objResponse->addIncludeScript("functions.js");</kbd>
469 * @param string URL of the Javascript file to include
471 function addIncludeScript($sFileName)
473 $this->xml
.= $this->_cmdXML(array("n"=>"in"),$sFileName);
477 * Returns the XML to be returned from your function to the xajax processor
478 * on your page. Since xajax 0.2, you can also return an xajaxResponse
479 * object from your function directly, and xajax will automatically request
480 * the XML using this method call.
482 * <i>Usage:</i> <kbd>return $objResponse->getXML();</kbd>
484 * @return string response XML data
488 $sXML = "<?xml version=\"1.0\"";
489 if ($this->sEncoding
&& strlen(trim($this->sEncoding
)) > 0)
490 $sXML .= " encoding=\"".$this->sEncoding
."\"";
491 $sXML .= " ?"."><xjx>" . $this->xml
. "</xjx>";
497 * Adds the commands of the provided response XML output to this response
501 * <code>$r1 = $objResponse1->getXML();
502 * $objResponse2->loadXML($r1);
503 * return $objResponse2->getXML();</code>
505 * @param string the response XML (returned from a getXML() method) to add
506 * to the end of this response object
508 function loadXML($mXML)
510 if (is_a($mXML, "xajaxResponse")) {
511 $mXML = $mXML->getXML();
514 $iStartPos = strpos($mXML, "<xjx>") +
5;
515 $sNewXML = substr($mXML, $iStartPos);
516 $iEndPos = strpos($sNewXML, "</xjx>");
517 $sNewXML = substr($sNewXML, 0, $iEndPos);
518 $this->xml
.= $sNewXML;
522 * Generates XML from command data
525 * @param array associative array of attributes
527 * @return string XML command
529 function _cmdXML($aAttributes, $sData)
531 if ($this->bOutputEntities
) {
532 if (function_exists('mb_convert_encoding')) {
533 $sData = call_user_func_array('mb_convert_encoding', array(&$sData, 'HTML-ENTITIES', $this->sEncoding
));
536 trigger_error("The xajax XML response output could not be converted to HTML entities because the mb_convert_encoding function is not available", E_USER_NOTICE
);
540 foreach($aAttributes as $sAttribute => $sValue)
541 $xml .= " $sAttribute=\"$sValue\"";
542 if ($sData !== null
&& !stristr($sData,'<![CDATA['))
543 $xml .= "><![CDATA[$sData]]></cmd>";
544 else if ($sData !== null
)
545 $xml .= ">$sData</cmd>";
553 * Recursively serializes a data structure in XML so it can be sent to
554 * the client. It could be thought of as the opposite of
555 * {@link xajax::_parseObjXml()}.
558 * @param mixed data structure to serialize to XML
559 * @return string serialized XML
561 function _buildObjXml($var) {
562 if (gettype($var) == "object") $var = get_object_vars($var);
563 if (!is_array($var)) {
564 return "<![CDATA[$var]]>";
568 foreach ($var as $key => $value) {
570 $data .= "<k>" . htmlspecialchars($key) . "</k>";
571 $data .= "<v>" . $this->_buildObjXml($value) . "</v>";
574 $data .= "</xjxobj>";
579 }// end class xajaxResponse