2 /***************************************************************
5 * (c) 2008 Benjamin Mack <mack@xnos.org>
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.
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.
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.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
28 * class to hold all the information about an AJAX call and send
29 * the right headers for the request type
31 * @author Benjamin Mack <mack@xnos.org>
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';
44 * sets the charset and the ID for the AJAX call
46 * @param string the AJAX id
49 public function init($id) {
50 global $TYPO3_CONF_VARS;
52 if (isset($TYPO3_CONF_VARS['BE']['forceCharset'])) {
53 $this->charset
= $TYPO3_CONF_VARS['BE']['forceCharset'];
61 * returns the ID for the AJAX call
63 * @return string the AJAX id
65 public function getID() {
71 * overwrites the existing content with the first parameter
73 * @param array the new content
74 * @return mixed the old content as array; if the new content was not an array, false is returned
76 public function setContent($content) {
78 if (is_array($content)) {
79 $oldcontent = $this->content
;
80 $this->content
= $content;
89 * @param string the new content key where the content should be added in the content array
90 * @param string the new content to add
91 * @return mixed the old content; if the old content didn't exist before, false is returned
93 public function addContent($key, $content) {
95 if (array_key_exists($key, $this->content
)) {
96 $oldcontent = $this->content
[$key];
98 if (!isset($content) ||
!strlen($content)) {
99 unset($this->content
[$key]);
100 } elseif (!isset($key) ||
!strlen($key)) {
101 $this->content
[] = $content;
103 $this->content
[$key] = $content;
110 * returns the content for the ajax call
112 * @return mixed the content for a specific key or the whole content
114 public function getContent($key = '') {
115 return ($key && array_key_exists($key, $this->content
) ?
$this->content
[$key] : $this->content
);
120 * sets the content format for the ajax call
122 * @param string can be one of the following keywords 'plain', '
125 public function setContentFormat($format) {
126 if (t3lib_div
::inArray(array('plain', 'xml', 'json', 'jsonhead', 'jsonbody'), $format)) {
127 $this->contentFormat
= $format;
133 * sets an error message and the error flag
135 * @param string the error message
138 public function setError($errorMsg = '') {
139 $this->errorMessage
= $errorMsg;
140 $this->isError
= true;
145 * checks whether an error occured during the execution or not
147 * @return boolean whether this AJAX call
149 public function isError() {
150 return $this->isError
;
155 * renders the AJAX call based on the $contentFormat variable and exits the request
159 public function render() {
160 if ($this->isError
) {
161 $this->renderAsError();
164 switch ($this->contentFormat
) {
168 $this->renderAsJSON();
171 $this->renderAsXML();
174 $this->renderAsPlain();
181 * renders the AJAX call in XML error style to handle with JS
182 * the "responseXML" of the transport object will be filled with the error message then
186 private function renderAsError() {
187 header('Content-type: text/xml; charset='.$this->charset
);
188 header('X-JSON: false');
189 die('<t3err>'.htmlspecialchars($this->errorMessage
).'</t3err>');
194 * renders the AJAX call with text/html headers
195 * the content will be available in the "responseText" value of the transport object
199 private function renderAsPlain() {
200 header('Content-type: text/html; charset='.$this->charset
);
201 header('X-JSON: true');
202 echo implode('', $this->content
);
207 * renders the AJAX call with text/xml headers
208 * the content will be available in the "responseXML" value of the transport object
212 private function renderAsXML() {
213 header('Content-type: text/xml; charset='.$this->charset
);
214 header('X-JSON: true');
215 echo implode('', $this->content
);
220 * renders the AJAX call with JSON evaluated headers
221 * note that you need to have requestHeaders: {Accept: 'application/json'},
222 * in your AJAX options of your AJAX request object in JS
224 * the content will be available
225 * - in the second parameter of the onSuccess / onComplete callback (except when contentFormat = 'jsonbody')
226 * - and in the xhr.responseText as a string (except when contentFormat = 'jsonhead')
227 * you can evaluate this in JS with xhr.responseText.evalJSON();
231 private function renderAsJSON() {
232 $content = t3lib_div
::array2json($this->content
);
234 header('Content-type: application/json; charset='.$this->charset
);
235 header('X-JSON: '.($this->contentFormat
!= 'jsonbody' ?
$content : true));
237 // bring content in xhr.responseText except when in "json head only" mode
238 if ($this->contentFormat
!= 'jsonhead') {
245 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE
]['XCLASS']['typo3/classes/class.typo3ajax.php']) {
246 include_once($TYPO3_CONF_VARS[TYPO3_MODE
]['XCLASS']['typo3/classes/class.typo3ajax.php']);