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