‪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;
29 
35 #[AsController]
37 {
38  public function ‪__construct(
39  private readonly ‪ModuleTemplateFactory $moduleTemplateFactory,
40  ) {}
41 
42  public function ‪handleRequest(ServerRequestInterface $request): ResponseInterface
43  {
44  $backendUser = $this->‪getBackendUser();
45  $currentModule = $request->getAttribute('module');
46  $currentModuleIdentifier = $currentModule->getIdentifier();
47  $pageUid = (int)($request->getQueryParams()['id'] ?? 0);
48  $pageRecord = BackendUtility::readPageAccess($pageUid, '1=1') ?: [];
49  if ($pageUid > 0 && empty($pageRecord)) {
50  // Redirect to records overview of page 0 if page could not be determined.
51  // Edge case if page has been removed meanwhile.
52  BackendUtility::setUpdateSignal('updatePageTree');
53  return new ‪RedirectResponse($this->uriBuilder->buildUriFromRoute('web_typoscript_recordsoverview'));
54  }
55 
56  $moduleData = $request->getAttribute('moduleData');
57  if ($moduleData->cleanUp([])) {
58  $backendUser->pushModuleData($currentModuleIdentifier, $moduleData->toArray());
59  }
60 
61  $queryBuilder = $this->connectionPool->getQueryBuilderForTable('sys_template');
62  $queryBuilder->getRestrictions()->removeAll()
63  ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
64  $result = $queryBuilder
65  ->select('uid', 'pid', 'title', 'root', 'hidden', 'starttime', 'endtime')
66  ->from('sys_template')
67  ->orderBy('sys_template.pid')
68  ->addOrderBy('sys_template.sorting')
69  ->executeQuery();
70  $pagesWithTemplates = [];
71  while (‪$record = $result->fetchAssociative()) {
72  $additionalFieldsForRootline = ['sorting', 'shortcut'];
73  $rootline = array_reverse(BackendUtility::BEgetRootLine(‪$record['pid'], '', true, $additionalFieldsForRootline));
74  if ($rootline !== []) {
75  $pagesWithTemplates = $this->‪setInPageArray($pagesWithTemplates, $rootline, ‪$record);
76  }
77  }
78 
79  $view = $this->moduleTemplateFactory->create($request);
80  $view->setTitle($this->‪getLanguageService()->sL($currentModule->getTitle()), '');
81  $view->getDocHeaderComponent()->setMetaInformation($pageRecord);
82  $this->‪addShortcutButtonToDocHeader($view, $currentModuleIdentifier, $pageRecord, $pageUid);
83  if ($pageUid !== 0) {
84  $view->makeDocHeaderModuleMenu(['id' => $pageUid]);
85  }
86  $view->assign('pageTree', $pagesWithTemplates);
87  return $view->renderResponse('TemplateRecordsOverview');
88  }
89 
94  private function ‪setInPageArray(array $pages, array $rootline, array $row): array
95  {
96  if (!$rootline[0]['uid']) {
97  // Skip 'root'
98  array_shift($rootline);
99  }
100  $currentRootlineElement = current($rootline);
101  if (empty($pages[$currentRootlineElement['uid']])) {
102  // Page not in tree yet. Add it.
103  $pages[$currentRootlineElement['uid']] = $currentRootlineElement;
104  }
105  array_shift($rootline);
106  if (empty($rootline)) {
107  // Last rootline element: Add template row
108  $pages[$currentRootlineElement['uid']]['_templates'][] = $row;
109  } else {
110  // Recurse into sub array
111  $pages[$currentRootlineElement['uid']]['_nodes'] ??= [];
112  $pages[$currentRootlineElement['uid']]['_nodes'] = $this->‪setInPageArray($pages[$currentRootlineElement['uid']]['_nodes'], $rootline, $row);
113  }
114  // Tree node sorting by pages sorting field
115  uasort($pages, static fn($a, $b) => $a['sorting'] - $b['sorting']);
116  return $pages;
117  }
118 
119  private function ‪addShortcutButtonToDocHeader(‪ModuleTemplate $view, string $moduleIdentifier, array $pageInfo, int $pageUid): void
120  {
121  $languageService = $this->‪getLanguageService();
122  $buttonBar = $view->‪getDocHeaderComponent()->getButtonBar();
123  $shortcutTitle = sprintf(
124  '%s: %s [%d]',
125  $languageService->sL('LLL:EXT:tstemplate/Resources/Private/Language/locallang_records_overview.xlf:typoscriptRecords.title'),
126  BackendUtility::getRecordTitle('pages', $pageInfo),
127  $pageUid
128  );
129  $shortcutButton = $buttonBar->makeShortcutButton()
130  ->setRouteIdentifier($moduleIdentifier)
131  ->setDisplayName($shortcutTitle)
132  ->setArguments(['id' => $pageUid]);
133  $buttonBar->addButton($shortcutButton);
134  }
135 }
‪TYPO3\CMS\Tstemplate\Controller\TemplateRecordsOverviewController\handleRequest
‪handleRequest(ServerRequestInterface $request)
Definition: TemplateRecordsOverviewController.php:42
‪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\Tstemplate\Controller\TemplateRecordsOverviewController
Definition: TemplateRecordsOverviewController.php:37
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController
Definition: AbstractTemplateModuleController.php:46
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\getBackendUser
‪getBackendUser()
Definition: AbstractTemplateModuleController.php:226
‪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:38
‪TYPO3\CMS\Tstemplate\Controller\TemplateRecordsOverviewController\addShortcutButtonToDocHeader
‪addShortcutButtonToDocHeader(ModuleTemplate $view, string $moduleIdentifier, array $pageInfo, int $pageUid)
Definition: TemplateRecordsOverviewController.php:119
‪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\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:94