‪TYPO3CMS  ‪main
AbstractTemplateModuleController.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;
26 use TYPO3\CMS\Backend\Utility\BackendUtility;
30 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
37 use TYPO3\CMS\Core\Imaging\IconSize;
43 
50 {
56 
58  {
59  $this->iconFactory = ‪$iconFactory;
60  }
61 
63  {
64  $this->uriBuilder = ‪$uriBuilder;
65  }
66 
68  {
69  $this->connectionPool = ‪$connectionPool;
70  }
71 
73  {
74  $this->dataHandler = ‪$dataHandler;
75  }
76 
78  {
79  $this->siteFinder = ‪$siteFinder;
80  }
81 
85  protected function ‪createExtensionTemplateAction(ServerRequestInterface $request, string $redirectTarget): ResponseInterface
86  {
87  $pageUid = (int)($request->getQueryParams()['id'] ?? 0);
88  if ($pageUid === 0) {
89  throw new \RuntimeException('No proper page uid given', 1661333864);
90  }
91  $recordData['sys_template']['NEW'] = [
92  'pid' => $pageUid,
93  'title' => '+ext',
94  ];
95  $this->dataHandler->start($recordData, []);
96  $this->dataHandler->process_datamap();
97  return new ‪RedirectResponse($this->uriBuilder->buildUriFromRoute($redirectTarget, ['id' => $pageUid]));
98  }
99 
103  protected function ‪createNewWebsiteTemplateAction(ServerRequestInterface $request, string $redirectTarget): ResponseInterface
104  {
105  $languageService = $this->‪getLanguageService();
106  $pageUid = (int)($request->getQueryParams()['id'] ?? 0);
107  if ($pageUid === 0) {
108  throw new \RuntimeException('No proper page uid given', 1661333863);
109  }
110  $recordData['sys_template']['NEW'] = [
111  'pid' => $pageUid,
112  'title' => $languageService->sL('LLL:EXT:tstemplate/Resources/Private/Language/locallang.xlf:noRecordFound.createRootTypoScriptRecord.title.placeholder'),
113  'sorting' => 0,
114  'root' => 1,
115  'clear' => 3,
116  'config' => "\n"
117  . "# Default PAGE object:\n"
118  . "page = PAGE\n"
119  . "page.10 = TEXT\n"
120  . "page.10.value = HELLO WORLD!\n",
121  ];
122  $this->dataHandler->start($recordData, []);
123  $this->dataHandler->process_datamap();
124  return new ‪RedirectResponse($this->uriBuilder->buildUriFromRoute($redirectTarget, ['id' => $pageUid]));
125  }
126 
127  protected function ‪addPreviewButtonToDocHeader(‪ModuleTemplate $view, int $pageId, int $dokType): void
128  {
129  $languageService = $this->‪getLanguageService();
130  $buttonBar = $view->‪getDocHeaderComponent()->getButtonBar();
131 
132  // Don't add preview button for sysfolders and spacers by default, and look up TS config options
133  $excludedDokTypes = [
136  ];
137  $pagesTsConfig = BackendUtility::getPagesTSconfig($pageId);
138  if (isset($pagesTsConfig['TCEMAIN.']['preview.']['disableButtonForDokType'])) {
139  $excludedDokTypes = ‪GeneralUtility::intExplode(
140  ',',
141  $pagesTsConfig['TCEMAIN.']['preview.']['disableButtonForDokType'],
142  true
143  );
144  }
145 
146  if ($pageId && !in_array($dokType, $excludedDokTypes, true)) {
147  $previewDataAttributes = ‪PreviewUriBuilder::create($pageId)
148  ->withRootLine(BackendUtility::BEgetRootLine($pageId))
149  ->buildDispatcherDataAttributes();
150  $viewButton = $buttonBar->makeLinkButton()
151  ->setHref('#')
152  ->setDataAttributes($previewDataAttributes ?? [])
153  ->setDisabled(!$previewDataAttributes)
154  ->setTitle($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.showPage'))
155  ->setIcon($this->iconFactory->getIcon('actions-view-page', IconSize::SMALL))
156  ->setShowLabelText(true);
157  $buttonBar->addButton($viewButton, ‪ButtonBar::BUTTON_POSITION_LEFT, 99);
158  }
159  }
160 
164  protected function ‪getClosestAncestorPageWithTemplateRecord(int $pageId): array
165  {
166  $rootLine = BackendUtility::BEgetRootLine($pageId);
167  foreach ($rootLine as $rootlineNode) {
168  if ($this->‪getFirstTemplateRecordOnPage((int)$rootlineNode['uid'])) {
169  return $rootlineNode;
170  }
171  }
172  return [];
173  }
174 
175  protected function ‪getScopedRootline(‪SiteInterface $site, array $fullRootLine): array
176  {
177  if (!$site instanceof ‪Site) {
178  return $fullRootLine;
179  }
180  if (!$site->isTypoScriptRoot()) {
181  return $fullRootLine;
182  }
183  $rootLineUntilSite = [];
184  foreach ($fullRootLine as $index => $rootlinePage) {
185  $rootlinePageId = (int)($rootlinePage['uid'] ?? 0);
186  $rootLineUntilSite[$index] = $rootlinePage;
187  if ($rootlinePageId === $site->‪getRootPageId()) {
188  break;
189  }
190  }
191  return $rootLineUntilSite;
192  }
193 
197  protected function ‪getAllTemplateRecordsOnPage(int $pageId): array
198  {
199  if (!$pageId) {
200  return [];
201  }
202 
203  $templateRecords = [];
204 
205  try {
206  $site = $this->siteFinder->getSiteByRootPageId($pageId);
207  if ($site->isTypoScriptRoot()) {
208  $typoScript = $site->getTypoScript();
209  $templateRecords[] = [
210  'type' => 'site',
211  'pid' => $pageId,
212  'constants' => $typoScript?->constants ?? '',
213  'config' => $typoScript?->setup ?? '',
214  'root' => 1,
215  'clear' => 1,
216  'sorting' => -1,
217  'uid' => -1,
218  'site' => $site,
219  'title' => $site->getConfiguration()['websiteTitle'] ?? '',
220  ];
221  }
222  } catch (‪SiteNotFoundException) {
223  // ignore
224  }
225 
226  $result = $this->‪getTemplateQueryBuilder($pageId)->executeQuery();
227  while ($row = $result->fetchAssociative()) {
228  $templateRecords[] = [...$row, 'type' => 'sys_template'];
229  }
230  return $templateRecords;
231  }
232 
242  protected function ‪getFirstTemplateRecordOnPage(int $pageId, int $templateUid = 0): array|false
243  {
244  if (empty($pageId)) {
245  return false;
246  }
247  $queryBuilder = $this->‪getTemplateQueryBuilder($pageId)->setMaxResults(1);
248  if ($templateUid) {
249  $queryBuilder->andWhere(
250  $queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($templateUid, ‪Connection::PARAM_INT))
251  );
252  }
253  return $queryBuilder->executeQuery()->fetchAssociative();
254  }
255 
259  protected function ‪getTemplateQueryBuilder(int $pid): QueryBuilder
260  {
261  $queryBuilder = $this->connectionPool->getQueryBuilderForTable('sys_template');
262  $queryBuilder->getRestrictions()
263  ->removeAll()
264  ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
265  return $queryBuilder->select('*')
266  ->from('sys_template')
267  ->where(
268  $queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter($pid, ‪Connection::PARAM_INT))
269  )
270  ->orderBy(‪$GLOBALS['TCA']['sys_template']['ctrl']['sortby']);
271  }
272 
274  {
275  return ‪$GLOBALS['LANG'];
276  }
277 
279  {
280  return ‪$GLOBALS['BE_USER'];
281  }
282 }
‪TYPO3\CMS\Core\DataHandling\DataHandler
Definition: DataHandler.php:94
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\$siteFinder
‪SiteFinder $siteFinder
Definition: AbstractTemplateModuleController.php:54
‪TYPO3\CMS\Core\Site\Entity\SiteInterface
Definition: SiteInterface.php:26
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\$dataHandler
‪DataHandler $dataHandler
Definition: AbstractTemplateModuleController.php:55
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:52
‪TYPO3\CMS\Backend\Template\Components\ButtonBar\BUTTON_POSITION_LEFT
‪const BUTTON_POSITION_LEFT
Definition: ButtonBar.php:37
‪TYPO3\CMS\Backend\Template\Components\ButtonBar
Definition: ButtonBar.php:33
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\createExtensionTemplateAction
‪createExtensionTemplateAction(ServerRequestInterface $request, string $redirectTarget)
Definition: AbstractTemplateModuleController.php:85
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\getClosestAncestorPageWithTemplateRecord
‪getClosestAncestorPageWithTemplateRecord(int $pageId)
Definition: AbstractTemplateModuleController.php:164
‪TYPO3\CMS\Core\Exception\SiteNotFoundException
Definition: SiteNotFoundException.php:25
‪TYPO3\CMS\Tstemplate\Controller
Definition: AbstractTemplateModuleController.php:18
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\getAllTemplateRecordsOnPage
‪getAllTemplateRecordsOnPage(int $pageId)
Definition: AbstractTemplateModuleController.php:197
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:46
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:42
‪TYPO3\CMS\Core\Site\Entity\SiteInterface\getRootPageId
‪getRootPageId()
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\injectConnectionPool
‪injectConnectionPool(ConnectionPool $connectionPool)
Definition: AbstractTemplateModuleController.php:67
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController
Definition: AbstractTemplateModuleController.php:50
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:44
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\injectDataHandler
‪injectDataHandler(DataHandler $dataHandler)
Definition: AbstractTemplateModuleController.php:72
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\getFirstTemplateRecordOnPage
‪array< string, mixed > false getFirstTemplateRecordOnPage(int $pageId, int $templateUid=0)
Definition: AbstractTemplateModuleController.php:242
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\getBackendUser
‪getBackendUser()
Definition: AbstractTemplateModuleController.php:278
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\create
‪static static create(int $pageId)
Definition: PreviewUriBuilder.php:65
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\$uriBuilder
‪UriBuilder $uriBuilder
Definition: AbstractTemplateModuleController.php:52
‪TYPO3\CMS\Core\Domain\Repository\PageRepository\DOKTYPE_SPACER
‪const DOKTYPE_SPACER
Definition: PageRepository.php:103
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\getScopedRootline
‪getScopedRootline(SiteInterface $site, array $fullRootLine)
Definition: AbstractTemplateModuleController.php:175
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Core\Domain\Repository\PageRepository\DOKTYPE_SYSFOLDER
‪const DOKTYPE_SYSFOLDER
Definition: PageRepository.php:104
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\getTemplateQueryBuilder
‪getTemplateQueryBuilder(int $pid)
Definition: AbstractTemplateModuleController.php:259
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\$iconFactory
‪IconFactory $iconFactory
Definition: AbstractTemplateModuleController.php:51
‪TYPO3\CMS\Core\Http\RedirectResponse
Definition: RedirectResponse.php:30
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:41
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\injectSiteFinder
‪injectSiteFinder(SiteFinder $siteFinder)
Definition: AbstractTemplateModuleController.php:77
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\getLanguageService
‪getLanguageService()
Definition: AbstractTemplateModuleController.php:273
‪TYPO3\CMS\Backend\Template\ModuleTemplate\getDocHeaderComponent
‪getDocHeaderComponent()
Definition: ModuleTemplate.php:181
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction
Definition: DeletedRestriction.php:28
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder
Definition: PreviewUriBuilder.php:44
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\createNewWebsiteTemplateAction
‪createNewWebsiteTemplateAction(ServerRequestInterface $request, string $redirectTarget)
Definition: AbstractTemplateModuleController.php:103
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\injectUriBuilder
‪injectUriBuilder(UriBuilder $uriBuilder)
Definition: AbstractTemplateModuleController.php:62
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Core\Domain\Repository\PageRepository
Definition: PageRepository.php:69
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\addPreviewButtonToDocHeader
‪addPreviewButtonToDocHeader(ModuleTemplate $view, int $pageId, int $dokType)
Definition: AbstractTemplateModuleController.php:127
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\$connectionPool
‪ConnectionPool $connectionPool
Definition: AbstractTemplateModuleController.php:53
‪TYPO3\CMS\Core\Utility\GeneralUtility\intExplode
‪static list< int > intExplode(string $delimiter, string $string, bool $removeEmptyValues=false)
Definition: GeneralUtility.php:756
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\injectIconFactory
‪injectIconFactory(IconFactory $iconFactory)
Definition: AbstractTemplateModuleController.php:57