class.t3lib_ajax.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2004-2008 Kasper Skaarhoj (kasperYYYY@typo3.com)
00006 *  All rights reserved
00007 *
00008 *  This script is part of the TYPO3 project. The TYPO3 project is
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 *
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *  A copy is found in the textfile GPL.txt and important notices to the license
00017 *  from the author is found in LICENSE.txt distributed with these scripts.
00018 *
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00027 /**
00028  * Contains the class "t3lib_ajax" containing functions for doing XMLHTTP requests to the TYPO3 backend and as well for generating replys. This technology is also known as ajax.
00029  * Call ALL methods without making an object!
00030  *
00031  * IMPORTANT NOTICE: The API the class provides is still NOT STABLE and SUBJECT TO CHANGE!
00032  * It is planned to integrate an external AJAX library, so the API will most likely change again.
00033  *
00034  * @author  Sebastian Kurfuerst <sebastian@garbage-group.de>
00035  */
00036 
00037 /**
00038  * TYPO3 XMLHTTP class (new in TYPO3 4.0.0)
00039  * This class contains two main parts:
00040  * (1) generation of JavaScript code which creates an XMLHTTP object in a cross-browser manner
00041  * (2) generation of XML data as a reply
00042  *
00043  * @author  Sebastian Kurfuerst <sebastian@garbage-group.de>
00044  * @package TYPO3
00045  * @subpackage t3lib
00046  */
00047 class t3lib_ajax {
00048     /**
00049  * Gets the JavaScript code needed to handle an XMLHTTP request in the frontend.
00050  * All JS functions have to call ajax_doRequest(url) to make a request to the server.
00051  * USE:
00052  * See examples of using this function in template.php -> getContextMenuCode and alt_clickmenu.php -> printContent
00053  *
00054  * @param   string      JS function handling the XML data from the server. That function gets the returned XML data as parameter.
00055  * @param   string      JS fallback function which is called with the URL of the request in case ajax is not available.
00056  * @param   boolean     If set to 1, the returned XML data is outputted as text in an alert window - useful for debugging, PHP errors are shown there, ...
00057  * @return  string      JavaScript code needed to make and handle an XMLHTTP request
00058  */
00059     function getJScode($handlerFunction, $fallback='', $debug=0)    {
00060             // Init the XMLHTTP request object
00061         $code = '
00062         function ajax_initObject()  {
00063             var A;
00064             try {
00065                 A=new ActiveXObject("Msxml2.XMLHTTP");
00066             } catch (e) {
00067                 try {
00068                     A=new ActiveXObject("Microsoft.XMLHTTP");
00069                 } catch (oc)    {
00070                     A=null;
00071                 }
00072             }
00073             if(!A && typeof XMLHttpRequest != "undefined")  {
00074                 A = new XMLHttpRequest();
00075             }
00076             return A;
00077         }';
00078             // in case AJAX is not available, fallback function
00079         if($fallback)   {
00080             $fallback .= '(url)';
00081         } else {
00082             $fallback = 'return';
00083         }
00084         $code .= '
00085         function ajax_doRequest(url)    {
00086             var x;
00087 
00088             x = ajax_initObject();
00089             if(!x)  {
00090                 '.$fallback.';
00091             }
00092             x.open("GET", url, true);
00093 
00094             x.onreadystatechange = function()   {
00095                 if (x.readyState != 4)  {
00096                     return;
00097                 }
00098                 '.($debug?'alert(x.responseText)':'').'
00099                 var xmldoc = x.responseXML;
00100                 var t3ajax = xmldoc.getElementsByTagName("t3ajax")[0];
00101                 '.$handlerFunction.'(t3ajax);
00102             }
00103             x.send("");
00104 
00105             delete x;
00106         }';
00107 
00108         return $code;
00109     }
00110 
00111     /**
00112      * Function outputting XML data for TYPO3 ajax. The function directly outputs headers and content to the browser.
00113      *
00114      * @param   string      $innerXML   XML data which will be sent to the browser
00115      * @return  void
00116      */
00117     function outputXMLreply($innerXML)  {
00118             // AJAX needs some XML data
00119         header('Content-Type: text/xml');
00120         $xml = '<?xml version="1.0"?>
00121 <t3ajax>'.$innerXML.'</t3ajax>';
00122         echo $xml;
00123     }
00124 
00125 }
00126 
00127 
00128 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_ajax.php'])  {
00129     include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_ajax.php']);
00130 }
00131 ?>

Generated on Sat Jan 3 04:23:26 2009 for TYPO3 API by  doxygen 1.4.7