‪TYPO3CMS  ‪main
TemplateRecordsOverviewController.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;
30 
36 #[AsController]
38 {
39  public function ‪__construct(
40  private readonly ‪ModuleTemplateFactory $moduleTemplateFactory,
41  ) {}
42 
43  public function ‪handleRequest(ServerRequestInterface $request): ResponseInterface
44  {
45  $backendUser = $this->‪getBackendUser();
46  $currentModule = $request->getAttribute('module');
47  $currentModuleIdentifier = $currentModule->getIdentifier();
48  $pageUid = (int)($request->getQueryParams()['id'] ?? 0);
49  $pageRecord = BackendUtility::readPageAccess($pageUid, '1=1') ?: [];
50  if ($pageUid > 0 && empty($pageRecord)) {
51  // Redirect to records overview of page 0 if page could not be determined.
52  // Edge case if page has been removed meanwhile.
53  BackendUtility::setUpdateSignal('updatePageTree');
54  return new ‪RedirectResponse($this->uriBuilder->buildUriFromRoute('web_typoscript_recordsoverview'));
55  }
56 
57  $moduleData = $request->getAttribute('moduleData');
58  if ($moduleData->cleanUp([])) {
59  $backendUser->pushModuleData($currentModuleIdentifier, $moduleData->toArray());
60  }
61 
62  $pagesWithTemplates = [];
63 
64  $sites = $this->siteFinder->getAllSites();
65  foreach ($sites as ‪$identifier => $site) {
66  if (!$site instanceof ‪Site) {
67  continue;
68  }
69  if (!$site->isTypoScriptRoot()) {
70  continue;
71  }
72  $rootPageId = $site->getRootPageId();
73  $additionalFieldsForRootline = ['sorting', 'shortcut'];
74  $rootline = array_reverse(BackendUtility::BEgetRootLine($rootPageId, '', true, $additionalFieldsForRootline));
75  if ($rootline !== []) {
76  $pagesWithTemplates = $this->‪setInPageArray($pagesWithTemplates, $rootline, [
77  'type' => 'site',
78  'root' => 1,
79  'clear' => 1,
80  'pid' => $rootPageId,
81  'sorting' => -1,
82  'uid' => -1,
83  'title' => $site->getConfiguration()['websiteTitle'] ?? '',
84  'site' => $site,
85  ]);
86  }
87  }
88 
89  $queryBuilder = $this->connectionPool->getQueryBuilderForTable('sys_template');
90  $queryBuilder->getRestrictions()->removeAll()
91  ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
92  $result = $queryBuilder
93  ->select('uid', 'pid', 'title', 'root', 'hidden', 'starttime', 'endtime')
94  ->from('sys_template')
95  ->orderBy('sys_template.pid')
96  ->addOrderBy('sys_template.sorting')
97  ->executeQuery();
98  while (‪$record = $result->fetchAssociative()) {
99  $additionalFieldsForRootline = ['sorting', 'shortcut'];
100  $rootline = array_reverse(BackendUtility::BEgetRootLine(‪$record['pid'], '', true, $additionalFieldsForRootline));
101  if ($rootline !== []) {
102  $pagesWithTemplates = $this->‪setInPageArray($pagesWithTemplates, $rootline, [...‪$record, 'type' => 'sys_template']);
103  }
104  }
105 
106  $view = $this->moduleTemplateFactory->create($request);
107  $view->setTitle($this->‪getLanguageService()->sL($currentModule->getTitle()), '');
108  $view->getDocHeaderComponent()->setMetaInformation($pageRecord);
109  $this->‪addShortcutButtonToDocHeader($view, $currentModuleIdentifier, $pageRecord, $pageUid);
110  if ($pageUid !== 0) {
111  $view->makeDocHeaderModuleMenu(['id' => $pageUid]);
112  }
113  $view->assign('pageTree', $pagesWithTemplates);
114  return $view->renderResponse('TemplateRecordsOverview');
115  }
116 
121  private function ‪setInPageArray(array $pages, array $rootline, array $row): array
122  {
123  if (!$rootline[0]['uid']) {
124  // Skip 'root'
125  array_shift($rootline);
126  }
127  $currentRootlineElement = current($rootline);
128  if (empty($pages[$currentRootlineElement['uid']])) {
129  // Page not in tree yet. Add it.
130  $pages[$currentRootlineElement['uid']] = $currentRootlineElement;
131  }
132  array_shift($rootline);
133  if (empty($rootline)) {
134  // Last rootline element: Add template row
135  $pages[$currentRootlineElement['uid']]['_templates'][] = $row;
136  } else {
137  // Recurse into sub array
138  $pages[$currentRootlineElement['uid']]['_nodes'] ??= [];
139  $pages[$currentRootlineElement['uid']]['_nodes'] = $this->‪setInPageArray($pages[$currentRootlineElement['uid']]['_nodes'], $rootline, $row);
140  }
141  // Tree node sorting by pages sorting field
142  uasort($pages, static fn($a, $b) => $a['sorting'] - $b['sorting']);
143  return $pages;
144  }
145 
146  private function ‪addShortcutButtonToDocHeader(‪ModuleTemplate $view, string $moduleIdentifier, array $pageInfo, int $pageUid): void
147  {
148  $languageService = $this->‪getLanguageService();
149  $buttonBar = $view->‪getDocHeaderComponent()->getButtonBar();
150  $shortcutTitle = sprintf(
151  '%s: %s [%d]',
152  $languageService->sL('LLL:EXT:tstemplate/Resources/Private/Language/locallang_records_overview.xlf:typoscriptRecords.title'),
153  BackendUtility::getRecordTitle('pages', $pageInfo),
154  $pageUid
155  );
156  $shortcutButton = $buttonBar->makeShortcutButton()
157  ->setRouteIdentifier($moduleIdentifier)
158  ->setDisplayName($shortcutTitle)
159  ->setArguments(['id' => $pageUid]);
160  $buttonBar->addButton($shortcutButton);
161  }
162 }
‪TYPO3\CMS\Tstemplate\Controller\TemplateRecordsOverviewController\handleRequest
‪handleRequest(ServerRequestInterface $request)
Definition: TemplateRecordsOverviewController.php:43
‪TYPO3\CMS\Backend\Template\ModuleTemplateFactory
Definition: ModuleTemplateFactory.php:33
‪TYPO3\CMS\Tstemplate\Controller
Definition: AbstractTemplateModuleController.php:18
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:46
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:42
‪TYPO3\CMS\Tstemplate\Controller\TemplateRecordsOverviewController
Definition: TemplateRecordsOverviewController.php:38
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController
Definition: AbstractTemplateModuleController.php:50
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\getBackendUser
‪getBackendUser()
Definition: AbstractTemplateModuleController.php:278
‪TYPO3\CMS\Webhooks\Message\$record
‪identifier readonly int readonly array $record
Definition: PageModificationMessage.php:36
‪TYPO3\CMS\Tstemplate\Controller\TemplateRecordsOverviewController\__construct
‪__construct(private readonly ModuleTemplateFactory $moduleTemplateFactory,)
Definition: TemplateRecordsOverviewController.php:39
‪TYPO3\CMS\Tstemplate\Controller\TemplateRecordsOverviewController\addShortcutButtonToDocHeader
‪addShortcutButtonToDocHeader(ModuleTemplate $view, string $moduleIdentifier, array $pageInfo, int $pageUid)
Definition: TemplateRecordsOverviewController.php:146
‪TYPO3\CMS\Core\Http\RedirectResponse
Definition: RedirectResponse.php:30
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\getLanguageService
‪getLanguageService()
Definition: AbstractTemplateModuleController.php:273
‪TYPO3\CMS\Backend\Template\ModuleTemplate\getDocHeaderComponent
‪getDocHeaderComponent()
Definition: ModuleTemplate.php:181
‪TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction
Definition: DeletedRestriction.php:28
‪TYPO3\CMS\Backend\Attribute\AsController
Definition: AsController.php:25
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Tstemplate\Controller\TemplateRecordsOverviewController\setInPageArray
‪setInPageArray(array $pages, array $rootline, array $row)
Definition: TemplateRecordsOverviewController.php:121
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37