1c7f00d4f80e41295d121575c68e82798f082db4
[Packages/TYPO3.CMS.git] / typo3 / classes / class.typo3ajax.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2008 Benjamin Mack <mack@xnos.org>
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 * class to hold all the information about an AJAX call and send
29 * the right headers for the request type
30 *
31 * @author Benjamin Mack <mack@xnos.org>
32 * @package TYPO3
33 * @subpackage core
34 */
35 class TYPO3AJAX {
36 private $id = null;
37 private $errorMessage = null;
38 private $isError = false;
39 private $content = array();
40 private $contentFormat = 'plain'; // could be 'plain' (default), 'xml', 'json', 'jsonbody' or 'jsonhead'
41 private $charset = 'utf-8';
42
43 /**
44 * sets the charset and the ID for the AJAX call
45 *
46 * @param string the AJAX id
47 * @return void
48 */
49 public function init($id) {
50 global $TYPO3_CONF_VARS;
51
52 if (isset($TYPO3_CONF_VARS['BE']['forceCharset'])) {
53 $this->charset = $TYPO3_CONF_VARS['BE']['forceCharset'];
54 }
55
56 $this->id = $id;
57 }
58
59
60 /**
61 * returns the ID for the AJAX call
62 *
63 * @return string the AJAX id
64 */
65 public function getID() {
66 return $this->id;
67 }
68
69
70 /**
71 * adds new content
72 *
73 * @param string the new content key where the content should be added in the content array
74 * @param string the new content to add
75 * @return string the old content
76 */
77 public function addContent($key, $content) {
78 $oldcontent = '';
79 if (array_key_exists($key, $this->content)) {
80 $oldcontent = $this->content[$key];
81 }
82 if (!$content) {
83 unset($this->content[$key]);
84 } else {
85 $this->content[$key] = $content;
86 }
87 return $oldcontent;
88 }
89
90
91 /**
92 * returns the content for the ajax call
93 *
94 * @return mixed the content for a specific key or the whole content
95 */
96 public function getContent($key = '') {
97 return ($key && array_key_exists($key, $this->content) ? $this->content[$key] : $this->content);
98 }
99
100
101 /**
102 * sets the content format for the ajax call
103 *
104 * @param string can be one of the following keywords 'plain', '
105 * @return void
106 */
107 public function setContentFormat($format) {
108 if (t3lib_div::inArray(array('plain', 'xml', 'json'), $format)) {
109 $this->contentFormat = $format;
110 }
111 }
112
113
114 /**
115 * sets an error message and the error flag
116 *
117 * @param string the error message
118 * @return void
119 */
120 public function setError($errorMsg = '') {
121 $this->errorMessage = $errorMsg;
122 $this->isError = true;
123 }
124
125
126 /**
127 * checks whether an error occured during the execution or not
128 *
129 * @return boolean whether this AJAX call
130 */
131 public function isError() {
132 return $this->isError;
133 }
134
135
136 /**
137 * renders the AJAX call based on the $contentFormat variable and exits the request
138 *
139 * @return void
140 */
141 public function render() {
142 if ($this->isError) {
143 $this->renderAsError();
144 exit;
145 }
146 switch ($this->contentFormat) {
147 case 'jsonhead':
148 case 'jsonbody':
149 case 'json':
150 $this->renderAsJSON();
151 break;
152 case 'xml':
153 $this->renderAsXML();
154 break;
155 default:
156 $this->renderAsPlain();
157 }
158 exit;
159 }
160
161
162 /**
163 * renders the AJAX call in XML error style to handle with JS
164 * the "responseXML" of the transport object will be filled with the error message then
165 *
166 * @return void
167 */
168 private function renderAsError() {
169 header('Content-type: text/xml; charset='.$this->charset);
170 header('X-JSON: false');
171 die('<t3err>'.htmlspecialchars($this->errorMessage).'</t3err>');
172 }
173
174
175 /**
176 * renders the AJAX call with text/html headers
177 * the content will be available in the "responseText" value of the transport object
178 *
179 * @return void
180 */
181 private function renderAsPlain() {
182 header('Content-type: text/html; charset='.$this->charset);
183 header('X-JSON: true');
184 echo implode('', $this->content);
185 }
186
187
188 /**
189 * renders the AJAX call with text/xml headers
190 * the content will be available in the "responseXML" value of the transport object
191 *
192 * @return void
193 */
194 private function renderAsXML() {
195 header('Content-type: text/xml; charset='.$this->charset);
196 header('X-JSON: true');
197 echo implode('', $this->content);
198 }
199
200
201 /**
202 * renders the AJAX call with JSON evaluated headers
203 * note that you need to have requestHeaders: {Accept: 'application/json'},
204 * in your AJAX options of your AJAX request object in JS
205 *
206 * the content will be available
207 * - in the second parameter of the onSuccess / onComplete callback (except when contentFormat = 'jsonbody')
208 * - and in the xhr.responseText as a string (except when contentFormat = 'jsonhead')
209 * you can evaluate this in JS with xhr.responseText.evalJSON();
210 *
211 * @return void
212 */
213 private function renderAsJSON() {
214 $content = t3lib_div::array2json($this->content);
215
216 header('Content-type: application/json; charset='.$this->charset);
217 header('X-JSON: '.($this->contentFormat != 'jsonbody' ? $content : true));
218
219 // bring content in xhr.responseText except when in "json head only" mode
220 if ($this->contentFormat != 'jsonhead') {
221 echo $content;
222 }
223 }
224 }
225
226
227 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/classes/class.typo3ajax.php']) {
228 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/classes/class.typo3ajax.php']);
229 }
230
231 ?>