TYPO3 CMS  TYPO3_8-7
AjaxRequestHandler.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
19 
26 {
30  protected $ajaxId = null;
31 
35  protected $errorMessage = null;
36 
40  protected $isError = false;
41 
45  protected $content = [];
46 
50  protected $contentFormat = 'plain';
51 
56  <script type="text/javascript">
57  /*<![CDATA[*/
58  response = |;
59  /*]]>*/
60  </script>
61  ';
62 
68  public function __construct($ajaxId)
69  {
70  $this->ajaxId = $ajaxId;
71  }
72 
78  public function getAjaxID()
79  {
80  return $this->ajaxId;
81  }
82 
89  public function setContent($content)
90  {
91  $oldcontent = false;
92  if (is_array($content)) {
93  $oldcontent = $this->content;
94  $this->content = $content;
95  }
96  return $oldcontent;
97  }
98 
106  public function addContent($key, $content)
107  {
108  $oldcontent = false;
109  if (array_key_exists($key, $this->content)) {
110  $oldcontent = $this->content[$key];
111  }
112  if (!isset($content) || empty($content)) {
113  unset($this->content[$key]);
114  } elseif (!isset($key) || empty($key)) {
115  $this->content[] = $content;
116  } else {
117  $this->content[$key] = $content;
118  }
119  return $oldcontent;
120  }
121 
127  public function getContent($key = '')
128  {
129  return $key && array_key_exists($key, $this->content) ? $this->content[$key] : $this->content;
130  }
131 
137  public function setContentFormat($format)
138  {
139  if (in_array($format, ['plain', 'xml', 'json', 'jsonhead', 'jsonbody', 'javascript'], true)) {
140  $this->contentFormat = $format;
141  }
142  }
143 
152  {
153  $this->javascriptCallbackWrap = $javascriptCallbackWrap;
154  }
155 
161  public function setError($errorMsg = '')
162  {
163  $this->errorMessage = $errorMsg;
164  $this->isError = true;
165  }
166 
172  public function isError()
173  {
174  return $this->isError;
175  }
176 
182  public function render()
183  {
184  if ($this->isError) {
185  return $this->renderAsError();
186  }
187  switch ($this->contentFormat) {
188  case 'jsonhead':
189  case 'jsonbody':
190  case 'json':
191  return $this->renderAsJSON();
192  break;
193  case 'javascript':
194  return $this->renderAsJavascript();
195  break;
196  case 'xml':
197  return $this->renderAsXML();
198  break;
199  default:
200  return $this->renderAsPlain();
201  }
202  }
203 
210  protected function renderAsError()
211  {
213  $response = GeneralUtility::makeInstance(Response::class);
214  $response = $response
215  ->withStatus(500, ' (AJAX)')
216  ->withHeader('Content-Type', 'text/xml; charset=utf-8')
217  ->withHeader('X-JSON', 'false');
218 
219  $response->getBody()->write('<t3err>' . htmlspecialchars($this->errorMessage) . '</t3err>');
220  return $response;
221  }
222 
230  protected function renderAsPlain()
231  {
233  $response = GeneralUtility::makeInstance(Response::class);
234  $response = $response
235  ->withHeader('Content-Type', 'text/html; charset=utf-8')
236  ->withHeader('X-JSON', 'true');
237 
238  $response->getBody()->write(implode('', $this->content));
239  return $response;
240  }
241 
249  protected function renderAsXML()
250  {
252  $response = GeneralUtility::makeInstance(Response::class);
253  $response = $response
254  ->withHeader('Content-Type', 'text/xml; charset=utf-8')
255  ->withHeader('X-JSON', 'true');
256 
257  $response->getBody()->write(implode('', $this->content));
258  return $response;
259  }
260 
274  protected function renderAsJSON()
275  {
277  $response = GeneralUtility::makeInstance(Response::class);
278  $response = $response->withHeader('Content-Type', 'application/json; charset=utf-8');
279 
280  $content = json_encode($this->content);
281  // Bring content in xhr.responseText except when in "json head only" mode
282  if ($this->contentFormat === 'jsonhead') {
283  $response = $response->withHeader('X-JSON', $content);
284  } else {
285  $response = $response->withHeader('X-JSON', 'true');
286  $response->getBody()->write($content);
287  }
288  return $response;
289  }
290 
298  protected function renderAsJavascript()
299  {
300  $response = GeneralUtility::makeInstance(Response::class);
301  $response = $response->withHeader('Content-Type', 'text/html; charset=utf-8');
302  $response->getBody()->write(str_replace('|', json_encode($this->content), $this->javascriptCallbackWrap));
303  return $response;
304  }
305 }
setJavascriptCallbackWrap($javascriptCallbackWrap)
static makeInstance($className,... $constructorArguments)