‪TYPO3CMS  ‪main
InfoModifyController.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
25 use TYPO3\CMS\Backend\Utility\BackendUtility;
27 
33 #[AsController]
35 {
36  public function ‪__construct(
37  protected readonly ‪ModuleTemplateFactory $moduleTemplateFactory,
38  ) {}
39 
40  public function ‪handleRequest(ServerRequestInterface $request): ResponseInterface
41  {
42  $queryParams = $request->getQueryParams();
43  $parsedBody = $request->getParsedBody();
44 
45  $pageUid = (int)($queryParams['id'] ?? 0);
46  if ($pageUid === 0) {
47  // Redirect to template record overview if on page 0.
48  return new ‪RedirectResponse($this->uriBuilder->buildUriFromRoute('web_typoscript_recordsoverview'));
49  }
50 
51  if (($parsedBody['action'] ?? '') === 'createExtensionTemplate') {
52  return $this->‪createExtensionTemplateAction($request, 'web_typoscript_infomodify');
53  }
54  if (($parsedBody['action'] ?? '') === 'createNewWebsiteTemplate') {
55  return $this->‪createNewWebsiteTemplateAction($request, 'web_typoscript_infomodify');
56  }
57 
58  $allTemplatesOnPage = $this->‪getAllTemplateRecordsOnPage($pageUid);
59  if (empty($allTemplatesOnPage)) {
60  return $this->‪noTemplateAction($request);
61  }
62 
63  return $this->‪mainAction($request, $pageUid, $allTemplatesOnPage);
64  }
65 
66  private function ‪noTemplateAction(ServerRequestInterface $request): ResponseInterface
67  {
68  $languageService = $this->‪getLanguageService();
69  $currentModule = $request->getAttribute('module');
70  $currentModuleIdentifier = $currentModule->getIdentifier();
71  $pageUid = (int)($request->getQueryParams()['id'] ?? 0);
72  if ($pageUid === 0) {
73  throw new \RuntimeException('No proper page uid given', 1661769346);
74  }
75  $pageRecord = BackendUtility::readPageAccess($pageUid, '1=1') ?: [];
76  if (empty($pageRecord)) {
77  // Redirect to records overview if page could not be determined.
78  // Edge case if page has been removed meanwhile.
79  BackendUtility::setUpdateSignal('updatePageTree');
80  return new ‪RedirectResponse($this->uriBuilder->buildUriFromRoute('web_typoscript_recordsoverview'));
81  }
82 
83  $view = $this->moduleTemplateFactory->create($request);
84  $view->setTitle($languageService->sL($currentModule->getTitle()), $pageRecord['title']);
85  $view->getDocHeaderComponent()->setMetaInformation($pageRecord);
86  $this->‪addPreviewButtonToDocHeader($view, $pageUid, (int)$pageRecord['doktype']);
87  $view->makeDocHeaderModuleMenu(['id' => $pageUid]);
88  $view->assignMultiple([
89  'pageUid' => $pageUid,
90  'moduleIdentifier' => $currentModuleIdentifier,
91  'previousPage' => $this->‪getClosestAncestorPageWithTemplateRecord($pageUid),
92  ]);
93  return $view->renderResponse('InfoModifyNoTemplate');
94  }
95 
96  private function ‪mainAction(ServerRequestInterface $request, int $pageUid, array $allTemplatesOnPage): ResponseInterface
97  {
98  $queryParams = $request->getQueryParams();
99  $parsedBody = $request->getParsedBody();
100 
101  $backendUser = $this->‪getBackendUser();
102  $languageService = $this->‪getLanguageService();
103 
104  $pageRecord = BackendUtility::readPageAccess($pageUid, '1=1') ?: [];
105  if (empty($pageRecord)) {
106  // Redirect to records overview if page could not be determined.
107  // Edge case if page has been removed meanwhile.
108  BackendUtility::setUpdateSignal('updatePageTree');
109  return new ‪RedirectResponse($this->uriBuilder->buildUriFromRoute('web_typoscript_recordsoverview'));
110  }
111 
112  $currentModule = $request->getAttribute('module');
113  $currentModuleIdentifier = $currentModule->getIdentifier();
114  $moduleData = $request->getAttribute('moduleData');
115 
116  $selectedTemplateFromModuleData = (array)$moduleData->get('selectedTemplatePerPage');
117  $selectedTemplateUid = (int)($parsedBody['selectedTemplate'] ?? $queryParams['selectedTemplate'] ?? $selectedTemplateFromModuleData[$pageUid] ?? 0);
118  if (!in_array($selectedTemplateUid, array_column($allTemplatesOnPage, 'uid'))) {
119  $selectedTemplateUid = (int)($allTemplatesOnPage[0]['uid'] ?? 0);
120  }
121  if (($moduleData->get('selectedTemplatePerPage')[$pageUid] ?? 0) !== $selectedTemplateUid) {
122  $selectedTemplateFromModuleData[$pageUid] = $selectedTemplateUid;
123  $moduleData->set('selectedTemplatePerPage', $selectedTemplateFromModuleData);
124  $backendUser->pushModuleData($currentModuleIdentifier, $moduleData->toArray());
125  }
126  $currentTemplateRecord = [];
127  foreach ($allTemplatesOnPage as $templateRow) {
128  if ((int)$templateRow['uid'] === $selectedTemplateUid) {
129  $currentTemplateRecord = $templateRow;
130  }
131  }
132 
133  $view = $this->moduleTemplateFactory->create($request);
134  $view->setTitle($languageService->sL($currentModule->getTitle()), $pageRecord['title']);
135  $view->getDocHeaderComponent()->setMetaInformation($pageRecord);
136  $this->‪addPreviewButtonToDocHeader($view, $pageUid, (int)$pageRecord['doktype']);
137  $this->‪addShortcutButtonToDocHeader($view, $currentModuleIdentifier, $pageRecord, $pageUid);
138  $view->makeDocHeaderModuleMenu(['id' => $pageUid]);
139  $view->assignMultiple([
140  'pageUid' => $pageUid,
141  'previousPage' => $this->‪getClosestAncestorPageWithTemplateRecord($pageUid),
142  'templateRecord' => $currentTemplateRecord,
143  'allTemplatesOnPage' => $allTemplatesOnPage,
144  'numberOfConstantsLines' => trim((string)($currentTemplateRecord['constants'] ?? '')) ? count(explode(LF, (string)$currentTemplateRecord['constants'])) : 0,
145  'numberOfSetupLines' => trim((string)($currentTemplateRecord['config'] ?? '')) ? count(explode(LF, (string)$currentTemplateRecord['config'])) : 0,
146  ]);
147  return $view->renderResponse('InfoModifyMain');
148  }
149 
150  private function ‪addShortcutButtonToDocHeader(‪ModuleTemplate $view, string $moduleIdentifier, array $pageInfo, int $pageUid): void
151  {
152  $languageService = $this->‪getLanguageService();
153  $buttonBar = $view->‪getDocHeaderComponent()->getButtonBar();
154  $shortcutTitle = sprintf(
155  '%s: %s [%d]',
156  $languageService->sL('LLL:EXT:tstemplate/Resources/Private/Language/locallang_info.xlf:submodule.title'),
157  BackendUtility::getRecordTitle('pages', $pageInfo),
158  $pageUid
159  );
160  $shortcutButton = $buttonBar->makeShortcutButton()
161  ->setRouteIdentifier($moduleIdentifier)
162  ->setDisplayName($shortcutTitle)
163  ->setArguments(['id' => $pageUid]);
164  $buttonBar->addButton($shortcutButton);
165  }
166 }
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\createExtensionTemplateAction
‪createExtensionTemplateAction(ServerRequestInterface $request, string $redirectTarget)
Definition: AbstractTemplateModuleController.php:75
‪TYPO3\CMS\Tstemplate\Controller\InfoModifyController\addShortcutButtonToDocHeader
‪addShortcutButtonToDocHeader(ModuleTemplate $view, string $moduleIdentifier, array $pageInfo, int $pageUid)
Definition: InfoModifyController.php:150
‪TYPO3\CMS\Backend\Template\ModuleTemplateFactory
Definition: ModuleTemplateFactory.php:33
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\getClosestAncestorPageWithTemplateRecord
‪getClosestAncestorPageWithTemplateRecord(int $pageId)
Definition: AbstractTemplateModuleController.php:154
‪TYPO3\CMS\Tstemplate\Controller
Definition: AbstractTemplateModuleController.php:18
‪TYPO3\CMS\Tstemplate\Controller\InfoModifyController\noTemplateAction
‪noTemplateAction(ServerRequestInterface $request)
Definition: InfoModifyController.php:66
‪TYPO3\CMS\Tstemplate\Controller\InfoModifyController\handleRequest
‪handleRequest(ServerRequestInterface $request)
Definition: InfoModifyController.php:40
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\getAllTemplateRecordsOnPage
‪getAllTemplateRecordsOnPage(int $pageId)
Definition: AbstractTemplateModuleController.php:168
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:46
‪TYPO3\CMS\Tstemplate\Controller\InfoModifyController
Definition: InfoModifyController.php:35
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController
Definition: AbstractTemplateModuleController.php:46
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\getBackendUser
‪getBackendUser()
Definition: AbstractTemplateModuleController.php:226
‪TYPO3\CMS\Core\Http\RedirectResponse
Definition: RedirectResponse.php:30
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\getLanguageService
‪getLanguageService()
Definition: AbstractTemplateModuleController.php:221
‪TYPO3\CMS\Backend\Template\ModuleTemplate\getDocHeaderComponent
‪getDocHeaderComponent()
Definition: ModuleTemplate.php:181
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\createNewWebsiteTemplateAction
‪createNewWebsiteTemplateAction(ServerRequestInterface $request, string $redirectTarget)
Definition: AbstractTemplateModuleController.php:93
‪TYPO3\CMS\Backend\Attribute\AsController
Definition: AsController.php:25
‪TYPO3\CMS\Tstemplate\Controller\InfoModifyController\mainAction
‪mainAction(ServerRequestInterface $request, int $pageUid, array $allTemplatesOnPage)
Definition: InfoModifyController.php:96
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\addPreviewButtonToDocHeader
‪addPreviewButtonToDocHeader(ModuleTemplate $view, int $pageId, int $dokType)
Definition: AbstractTemplateModuleController.php:117
‪TYPO3\CMS\Tstemplate\Controller\InfoModifyController\__construct
‪__construct(protected readonly ModuleTemplateFactory $moduleTemplateFactory,)
Definition: InfoModifyController.php:36