[!!!][FEATURE] Introduce PSR-7-based Routing for Backend AJAX Requests
[Packages/TYPO3.CMS.git] / typo3 / sysext / context_help / Classes / Controller / ContextHelpAjaxController.php
1 <?php
2 namespace TYPO3\CMS\ContextHelp\Controller;
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
17 use Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Core\Imaging\Icon;
20 use TYPO3\CMS\Core\Imaging\IconFactory;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Backend\Utility\BackendUtility;
23
24 /**
25 * Class ContextHelpAjaxController
26 */
27 class ContextHelpAjaxController {
28
29 /**
30 * The main dispatcher function. Collect data and prepare HTML output.
31 *
32 * @param ServerRequestInterface $request
33 * @param ResponseInterface $response
34 * @return ResponseInterface
35 */
36 public function getHelpAction(ServerRequestInterface $request, ResponseInterface $response) {
37 $params = isset($request->getParsedBody()['params']) ? $request->getParsedBody()['params'] : $request->getQueryParams()['params'];
38 if ($params['action'] === 'getContextHelp') {
39 $result = $this->getContextHelp($params['table'], $params['field']);
40 $response->getBody()->write(json_encode([
41 'title' => $result['title'],
42 'content' => $result['description'],
43 'link' => $result['moreInfo']
44 ]));
45 }
46 return $response;
47 }
48
49 /**
50 * Fetch the context help for the given table/field parameters
51 *
52 * @param string $table Table identifier
53 * @param string $field Field identifier
54 * @return array complete Help information
55 */
56 protected function getContextHelp($table, $field) {
57 $helpTextArray = BackendUtility::helpTextArray($table, $field);
58 /** @var IconFactory $iconFactory */
59 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
60 $moreIcon = $helpTextArray['moreInfo'] ? $iconFactory->getIcon('actions-view-go-forward', Icon::SIZE_SMALL)->render() : '';
61 return array(
62 'title' => $helpTextArray['title'],
63 'description' => '<p class="t3-help-short' . ($moreIcon ? ' tipIsLinked' : '') . '">' . $helpTextArray['description'] . $moreIcon . '</p>',
64 'id' => $table . '.' . $field,
65 'moreInfo' => $helpTextArray['moreInfo']
66 );
67 }
68
69 }