2005-11-09 Sebastian Kurfuerst <sebastian@garbage-group.de>
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_ajax.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2004-2005 Kasper Skaarhoj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * 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.
29 * Call ALL methods without making an object!
30 *
31 * @author Sebastian Kurfuerst <sebastian@garbage-group.de>
32 */
33
34 /**
35 * TYPO3 XMLHTTP class (new in TYPO3 4.0.0)
36 * This class contains two main parts:
37 * (1) generation of JavaScript code which creates an XMLHTTP object in a cross-browser manner
38 * (2) generation of XML data as a reply
39 *
40 * @author Sebastian Kurfuerst <sebastian@garbage-group.de>
41 * @package TYPO3
42 * @subpackage t3lib
43 */
44 class t3lib_ajax {
45 /**
46 * gets javascript code needed to handle an XMLHTTP request in the frontend.
47 * all JS functions have to call ajax_doRequest(url) to make a request to the server.
48 * USE:
49 * see examples of using this function in template.php -> getContextMenuCode and alt_clickmenu.php -> printContent
50 *
51 * @param string $handlerFunction JS function handling the XML data from the server. That function gets the returned XML data as parameter.
52 * @param string $fallback JS fallback function which is called with the URL of the request in case ajax is not available.
53 * @param boolean $debug If set to 1, the returned XML data is outputted as text in an alert window - useful for debugging, PHP errors are shown there, ...
54 * @return string JavaScript code needed to make and handle an XMLHTTP request
55 */
56 function getJScode($handlerFunction, $fallback = '', $debug=0) {
57 // init xmlhttp request object
58 $code = '
59 function ajax_initObject() {
60 var A;
61 try {
62 A=new ActiveXObject("Msxml2.XMLHTTP");
63 } catch (e) {
64 try {
65 A=new ActiveXObject("Microsoft.XMLHTTP");
66 } catch (oc) {
67 A=null;
68 }
69 }
70 if(!A && typeof XMLHttpRequest != "undefined") {
71 A = new XMLHttpRequest();
72 }
73 return A;
74 }';
75 // in case ajax is not available, fallback function
76 if($fallback) {
77 $fallback .= '(url)';
78 } else {
79 $fallback = 'return';
80 }
81 $code .= '
82 function ajax_doRequest(url) {
83 var x;
84
85 x = ajax_initObject();
86 if(!x) {
87 '.$fallback.';
88 }
89 x.open("GET", url, true);
90
91 x.onreadystatechange = function() {
92 if (x.readyState != 4) {
93 return;
94 }
95 '.($debug?'alert(x.responseText)':'').'
96 var xmldoc = x.responseXML;
97 var t3ajax = xmldoc.getElementsByTagName("t3ajax")[0];
98 '.$handlerFunction.'(t3ajax);
99 }
100 x.send("");
101
102 delete x;
103 }';
104
105 return $code;
106 }
107
108 /**
109 * Function outputting XML data for TYPO3 ajax. The function directly outputs headers and content to the browser.
110 *
111 * @param string $innerXML XML data which will be sent to the browser
112 * @return void
113 */
114 function outputXMLreply($innerXML) {
115 // ajax needs xml data
116 header('Content-Type: text/xml');
117 $xml = '<?xml version="1.0"?>
118 <t3ajax>'.$innerXML.'</t3ajax>';
119 echo $xml;
120 }
121
122 }
123
124
125 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_ajax.php']) {
126 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_ajax.php']);
127 }
128 ?>