‪TYPO3CMS  10.4
CodeCompletionController.php
Go to the documentation of this file.
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 
17 
18 use Psr\Http\Message\ResponseInterface;
19 use Psr\Http\Message\ServerRequestInterface;
26 
32 {
40  public function ‪loadCompletions(ServerRequestInterface $request): ResponseInterface
41  {
42  // Check whether access is granted (only admin have access to sys_template records):
43  if (!‪$GLOBALS['BE_USER']->isAdmin()) {
44  return new ‪HtmlResponse($this->‪getLanguageService()->sL('LLL:EXT:t3editor/Resources/Private/Language/locallang.xlf:noPermission'), 500);
45  }
46  $pageId = (int)($request->getParsedBody()['pageId'] ?? $request->getQueryParams()['pageId']);
47  // Check whether there is a pageId given:
48  if (!$pageId) {
49  return new ‪HtmlResponse($this->‪getLanguageService()->sL('LLL:EXT:t3editor/Resources/Private/Language/locallang.xlf:pageIDInteger'), 500);
50  }
51  // Fetch the templates
52  return (new ‪JsonResponse())->setPayload($this->‪getMergedTemplates($pageId));
53  }
54 
62  protected function ‪getMergedTemplates($pageId)
63  {
64  $tsParser = GeneralUtility::makeInstance(ExtendedTemplateService::class);
65  // Gets the rootLine
66  $rootLine = GeneralUtility::makeInstance(RootlineUtility::class, $pageId)->get();
67  // This generates the constants/config + hierarchy info for the template.
68  $tsParser->runThroughTemplates($rootLine);
69  // ts-setup & ts-constants of the currently edited template should not be included
70  // therefor we have to delete the last template from the stack
71  array_pop($tsParser->config);
72  array_pop($tsParser->constants);
73  $tsParser->linkObjects = true;
74  $tsParser->ext_regLinenumbers = false;
75  $tsParser->generateConfig();
76  $result = $this->‪treeWalkCleanup($tsParser->setup);
77  return $result;
78  }
79 
87  private function ‪treeWalkCleanup(array $treeBranch)
88  {
89  $cleanedTreeBranch = [];
90  foreach ($treeBranch as $key => $value) {
91  //type definition or value-assignment
92  if (substr($key, -1) !== '.') {
93  if ($value != '') {
94  if (mb_strlen($value) > 20) {
95  $value = mb_substr($value, 0, 20);
96  }
97  if (!isset($cleanedTreeBranch[$key])) {
98  $cleanedTreeBranch[$key] = [];
99  }
100  $cleanedTreeBranch[$key]['v'] = $value;
101  }
102  } else {
103  // subtree (definition of properties)
104  $subBranch = $this->‪treeWalkCleanup($value);
105  if ($subBranch) {
106  if (substr($key, -1) === '.') {
107  $key = rtrim($key, '.');
108  }
109  if (!isset($cleanedTreeBranch[$key])) {
110  $cleanedTreeBranch[$key] = [];
111  }
112  $cleanedTreeBranch[$key]['c'] = $subBranch;
113  }
114  }
115  }
116  return $cleanedTreeBranch;
117  }
118 
123  {
124  return ‪$GLOBALS['LANG'];
125  }
126 }
‪TYPO3\CMS\Core\Utility\RootlineUtility
Definition: RootlineUtility.php:39
‪TYPO3\CMS\Core\TypoScript\ExtendedTemplateService
Definition: ExtendedTemplateService.php:43
‪TYPO3\CMS\T3editor\Controller\CodeCompletionController\getMergedTemplates
‪array getMergedTemplates($pageId)
Definition: CodeCompletionController.php:62
‪TYPO3\CMS\T3editor\Controller\CodeCompletionController\getLanguageService
‪LanguageService getLanguageService()
Definition: CodeCompletionController.php:122
‪TYPO3\CMS\T3editor\Controller\CodeCompletionController\loadCompletions
‪ResponseInterface loadCompletions(ServerRequestInterface $request)
Definition: CodeCompletionController.php:40
‪TYPO3\CMS\Core\Http\JsonResponse
Definition: JsonResponse.php:26
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\T3editor\Controller\CodeCompletionController
Definition: CodeCompletionController.php:32
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\T3editor\Controller\CodeCompletionController\treeWalkCleanup
‪array treeWalkCleanup(array $treeBranch)
Definition: CodeCompletionController.php:87
‪TYPO3\CMS\Core\Http\HtmlResponse
Definition: HtmlResponse.php:26
‪TYPO3\CMS\T3editor\Controller
Definition: CodeCompletionController.php:16