1<?php
2
3/*
4 * This file is part of the TYPO3 CMS project.
5 *
6 * It is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License, either version 2
8 * of the License, or any later version.
9 *
10 * For the full copyright and license information, please read the
11 * LICENSE.txt file that was distributed with this source code.
12 *
13 * The TYPO3 project - inspiring people to share!
14 */
15
16namespace TYPO3\CMS\Backend\Controller;
17
18use Psr\Http\Message\ResponseInterface;
19use Psr\Http\Message\ServerRequestInterface;
20use TYPO3\CMS\Backend\Utility\BackendUtility;
21use TYPO3\CMS\Core\Http\JsonResponse;
22use TYPO3\CMS\Core\Imaging\Icon;
23use TYPO3\CMS\Core\Imaging\IconFactory;
24use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26/**
27 * Class ContextHelpAjaxController
28 * @internal This class is a specific Backend controller implementation and is not considered part of the Public TYPO3 API.
29 */
30class ContextHelpAjaxController
31{
32    /**
33     * The main dispatcher function. Collect data and prepare HTML output.
34     *
35     * @param ServerRequestInterface $request
36     * @return ResponseInterface
37     * @throws \RuntimeException
38     */
39    public function getHelpAction(ServerRequestInterface $request): ResponseInterface
40    {
41        $params = $request->getParsedBody()['params'] ?? $request->getQueryParams()['params'];
42        if (($params['action'] ?? '') !== 'getContextHelp') {
43            throw new \RuntimeException('Action must be set to "getContextHelp"', 1518787887);
44        }
45        $result = $this->getContextHelp($params['table'], $params['field']);
46        return new JsonResponse([
47            'title' => $result['title'],
48            'content' => $result['description'],
49            'link' => $result['moreInfo'],
50        ]);
51    }
52
53    /**
54     * Fetch the context help for the given table/field parameters
55     *
56     * @param string $table Table identifier
57     * @param string $field Field identifier
58     * @return array complete Help information
59     */
60    protected function getContextHelp($table, $field)
61    {
62        $helpTextArray = BackendUtility::helpTextArray($table, $field);
63        $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
64        $moreIcon = $helpTextArray['moreInfo'] ? $iconFactory->getIcon('actions-view-go-forward', Icon::SIZE_SMALL)->render() : '';
65        return [
66            'title' => $helpTextArray['title'],
67            'description' => '<p class="help-short' . ($moreIcon ? ' help-has-link' : '') . '">' . $helpTextArray['description'] . $moreIcon . '</p>',
68            'id' => $table . '.' . $field,
69            'moreInfo' => $helpTextArray['moreInfo'],
70        ];
71    }
72}
73