‪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;
36 use TYPO3\CMS\Core\Imaging\IconSize;
39 
46 {
51 
53  {
54  $this->iconFactory = ‪$iconFactory;
55  }
56 
58  {
59  $this->uriBuilder = ‪$uriBuilder;
60  }
61 
63  {
64  $this->connectionPool = ‪$connectionPool;
65  }
66 
68  {
69  $this->dataHandler = ‪$dataHandler;
70  }
71 
75  protected function ‪createExtensionTemplateAction(ServerRequestInterface $request, string $redirectTarget): ResponseInterface
76  {
77  $pageUid = (int)($request->getQueryParams()['id'] ?? 0);
78  if ($pageUid === 0) {
79  throw new \RuntimeException('No proper page uid given', 1661333864);
80  }
81  $recordData['sys_template']['NEW'] = [
82  'pid' => $pageUid,
83  'title' => '+ext',
84  ];
85  $this->dataHandler->start($recordData, []);
86  $this->dataHandler->process_datamap();
87  return new ‪RedirectResponse($this->uriBuilder->buildUriFromRoute($redirectTarget, ['id' => $pageUid]));
88  }
89 
93  protected function ‪createNewWebsiteTemplateAction(ServerRequestInterface $request, string $redirectTarget): ResponseInterface
94  {
95  $languageService = $this->‪getLanguageService();
96  $pageUid = (int)($request->getQueryParams()['id'] ?? 0);
97  if ($pageUid === 0) {
98  throw new \RuntimeException('No proper page uid given', 1661333863);
99  }
100  $recordData['sys_template']['NEW'] = [
101  'pid' => $pageUid,
102  'title' => $languageService->sL('LLL:EXT:tstemplate/Resources/Private/Language/locallang.xlf:noRecordFound.createRootTypoScriptRecord.title.placeholder'),
103  'sorting' => 0,
104  'root' => 1,
105  'clear' => 3,
106  'config' => "\n"
107  . "# Default PAGE object:\n"
108  . "page = PAGE\n"
109  . "page.10 = TEXT\n"
110  . "page.10.value = HELLO WORLD!\n",
111  ];
112  $this->dataHandler->start($recordData, []);
113  $this->dataHandler->process_datamap();
114  return new ‪RedirectResponse($this->uriBuilder->buildUriFromRoute($redirectTarget, ['id' => $pageUid]));
115  }
116 
117  protected function ‪addPreviewButtonToDocHeader(‪ModuleTemplate $view, int $pageId, int $dokType): void
118  {
119  $languageService = $this->‪getLanguageService();
120  $buttonBar = $view->‪getDocHeaderComponent()->getButtonBar();
121 
122  // Don't add preview button for sysfolders and spacers by default, and look up TS config options
123  $excludedDokTypes = [
126  ];
127  $pagesTsConfig = BackendUtility::getPagesTSconfig($pageId);
128  if (isset($pagesTsConfig['TCEMAIN.']['preview.']['disableButtonForDokType'])) {
129  $excludedDokTypes = ‪GeneralUtility::intExplode(
130  ',',
131  $pagesTsConfig['TCEMAIN.']['preview.']['disableButtonForDokType'],
132  true
133  );
134  }
135 
136  if ($pageId && !in_array($dokType, $excludedDokTypes, true)) {
137  $previewDataAttributes = ‪PreviewUriBuilder::create($pageId)
138  ->withRootLine(BackendUtility::BEgetRootLine($pageId))
139  ->buildDispatcherDataAttributes();
140  $viewButton = $buttonBar->makeLinkButton()
141  ->setHref('#')
142  ->setDataAttributes($previewDataAttributes ?? [])
143  ->setDisabled(!$previewDataAttributes)
144  ->setTitle($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.showPage'))
145  ->setIcon($this->iconFactory->getIcon('actions-view-page', IconSize::SMALL))
146  ->setShowLabelText(true);
147  $buttonBar->addButton($viewButton, ‪ButtonBar::BUTTON_POSITION_LEFT, 99);
148  }
149  }
150 
154  protected function ‪getClosestAncestorPageWithTemplateRecord(int $pageId): array
155  {
156  $rootLine = BackendUtility::BEgetRootLine($pageId);
157  foreach ($rootLine as $rootlineNode) {
158  if ($this->‪getFirstTemplateRecordOnPage((int)$rootlineNode['uid'])) {
159  return $rootlineNode;
160  }
161  }
162  return [];
163  }
164 
168  protected function ‪getAllTemplateRecordsOnPage(int $pageId): array
169  {
170  if (!$pageId) {
171  return [];
172  }
173  $result = $this->‪getTemplateQueryBuilder($pageId)->executeQuery();
174  $templateRows = [];
175  while ($row = $result->fetchAssociative()) {
176  $templateRows[] = $row;
177  }
178  return $templateRows;
179  }
180 
190  protected function ‪getFirstTemplateRecordOnPage(int $pageId, int $templateUid = 0): array|false
191  {
192  if (empty($pageId)) {
193  return false;
194  }
195  $queryBuilder = $this->‪getTemplateQueryBuilder($pageId)->setMaxResults(1);
196  if ($templateUid) {
197  $queryBuilder->andWhere(
198  $queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($templateUid, ‪Connection::PARAM_INT))
199  );
200  }
201  return $queryBuilder->executeQuery()->fetchAssociative();
202  }
203 
207  protected function ‪getTemplateQueryBuilder(int $pid): QueryBuilder
208  {
209  $queryBuilder = $this->connectionPool->getQueryBuilderForTable('sys_template');
210  $queryBuilder->getRestrictions()
211  ->removeAll()
212  ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
213  return $queryBuilder->select('*')
214  ->from('sys_template')
215  ->where(
216  $queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter($pid, ‪Connection::PARAM_INT))
217  )
218  ->orderBy(‪$GLOBALS['TCA']['sys_template']['ctrl']['sortby']);
219  }
220 
222  {
223  return ‪$GLOBALS['LANG'];
224  }
225 
227  {
228  return ‪$GLOBALS['BE_USER'];
229  }
230 }
‪TYPO3\CMS\Core\DataHandling\DataHandler
Definition: DataHandler.php:94
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\$dataHandler
‪DataHandler $dataHandler
Definition: AbstractTemplateModuleController.php:50
‪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:75
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\getClosestAncestorPageWithTemplateRecord
‪getClosestAncestorPageWithTemplateRecord(int $pageId)
Definition: AbstractTemplateModuleController.php:154
‪TYPO3\CMS\Tstemplate\Controller
Definition: AbstractTemplateModuleController.php:18
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪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\AbstractTemplateModuleController\injectConnectionPool
‪injectConnectionPool(ConnectionPool $connectionPool)
Definition: AbstractTemplateModuleController.php:62
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController
Definition: AbstractTemplateModuleController.php:46
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:44
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\injectDataHandler
‪injectDataHandler(DataHandler $dataHandler)
Definition: AbstractTemplateModuleController.php:67
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\getFirstTemplateRecordOnPage
‪array< string, mixed > false getFirstTemplateRecordOnPage(int $pageId, int $templateUid=0)
Definition: AbstractTemplateModuleController.php:190
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\getBackendUser
‪getBackendUser()
Definition: AbstractTemplateModuleController.php:226
‪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:48
‪TYPO3\CMS\Core\Domain\Repository\PageRepository\DOKTYPE_SPACER
‪const DOKTYPE_SPACER
Definition: PageRepository.php:103
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:61
‪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:207
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\$iconFactory
‪IconFactory $iconFactory
Definition: AbstractTemplateModuleController.php:47
‪TYPO3\CMS\Core\Http\RedirectResponse
Definition: RedirectResponse.php:30
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:41
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\getLanguageService
‪getLanguageService()
Definition: AbstractTemplateModuleController.php:221
‪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:93
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\injectUriBuilder
‪injectUriBuilder(UriBuilder $uriBuilder)
Definition: AbstractTemplateModuleController.php:57
‪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:117
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\$connectionPool
‪ConnectionPool $connectionPool
Definition: AbstractTemplateModuleController.php:49
‪TYPO3\CMS\Core\Utility\GeneralUtility\intExplode
‪static list< int > intExplode(string $delimiter, string $string, bool $removeEmptyValues=false)
Definition: GeneralUtility.php:751
‪TYPO3\CMS\Tstemplate\Controller\AbstractTemplateModuleController\injectIconFactory
‪injectIconFactory(IconFactory $iconFactory)
Definition: AbstractTemplateModuleController.php:52