‪TYPO3CMS  11.5
NewMultiplePagesController.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;
39 
46 {
52  protected ‪$moduleTemplate;
53 
56 
58  {
59  $this->iconFactory = ‪$iconFactory;
60  $this->moduleTemplateFactory = ‪$moduleTemplateFactory;
61  }
62 
69  public function ‪mainAction(ServerRequestInterface $request): ResponseInterface
70  {
71  $this->moduleTemplate = $this->moduleTemplateFactory->create($request);
72  $backendUser = $this->‪getBackendUser();
73  $pageUid = (int)$request->getQueryParams()['id'];
74 
75  // Show only if there is a valid page and if this page may be viewed by the user
76  $pageRecord = BackendUtility::readPageAccess($pageUid, $backendUser->getPagePermsClause(‪Permission::PAGE_SHOW));
77  if (!is_array($pageRecord)) {
78  // User has no permission on parent page, should not happen, just render an empty page
79  $this->moduleTemplate->setContent('');
80  return new ‪HtmlResponse($this->moduleTemplate->renderContent());
81  }
82 
83  // Doc header handling
84  $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($pageRecord);
85  $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
86  $cshButton = $buttonBar->makeHelpButton()
87  ->setModuleName('pages_new')
88  ->setFieldName('pages_new');
89  $previewDataAttributes = ‪PreviewUriBuilder::create($pageUid)
90  ->withRootLine(BackendUtility::BEgetRootLine($pageUid))
91  ->buildDispatcherDataAttributes();
92  $viewButton = $buttonBar->makeLinkButton()
93  ->setDataAttributes($previewDataAttributes ?? [])
94  ->setTitle($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.showPage'))
95  ->setIcon($this->iconFactory->getIcon('actions-view-page', ‪Icon::SIZE_SMALL))
96  ->setHref('#');
97  $buttonBar->addButton($cshButton)->addButton($viewButton);
98 
99  // Main view setup
100  $view = GeneralUtility::makeInstance(StandaloneView::class);
101  $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
102  'EXT:backend/Resources/Private/Templates/Page/NewPages.html'
103  ));
104 
105  $calculatedPermissions = new ‪Permission($backendUser->calcPerms($pageRecord));
106  $canCreateNew = $backendUser->isAdmin() || $calculatedPermissions->createPagePermissionIsGranted();
107 
108  $view->assign('canCreateNew', $canCreateNew);
109  $view->assign('maxTitleLength', $backendUser->uc['titleLen'] ?? 20);
110  $view->assign('pageUid', $pageUid);
111 
112  if ($canCreateNew) {
113  $newPagesData = (array)($request->getParsedBody()['pages'] ?? []);
114  if (!empty($newPagesData)) {
115  $hasNewPagesData = true;
116  $afterExisting = isset($request->getParsedBody()['createInListEnd']);
117  $hidePages = isset($request->getParsedBody()['hidePages']);
118  $hidePagesInMenu = isset($request->getParsedBody()['hidePagesInMenus']);
119  $pagesCreated = $this->‪createPages($newPagesData, $pageUid, $afterExisting, $hidePages, $hidePagesInMenu);
120  $view->assign('pagesCreated', $pagesCreated);
121  $subPages = $this->‪getSubPagesOfPage($pageUid);
122  $visiblePages = [];
123  foreach ($subPages as $page) {
124  $calculatedPermissions = new ‪Permission($backendUser->calcPerms($page));
125  if ($backendUser->isAdmin() || $calculatedPermissions->showPagePermissionIsGranted()) {
126  $visiblePages[] = $page;
127  }
128  }
129  $view->assign('visiblePages', $visiblePages);
130  } else {
131  $hasNewPagesData = false;
132  $view->assign('pageTypes', $this->‪getTypeSelectData($pageUid));
133  }
134  $view->assign('hasNewPagesData', $hasNewPagesData);
135  }
136 
137  $this->moduleTemplate->setContent($view->render());
138  return new HtmlResponse($this->moduleTemplate->renderContent());
139  }
140 
151  protected function ‪createPages(array $newPagesData, int $pageUid, bool $afterExisting, bool $hidePages, bool $hidePagesInMenu): bool
152  {
153  $pagesCreated = false;
154 
155  // Set first pid to "-1 * uid of last existing sub page" if pages should be created at end
156  $firstPid = $pageUid;
157  if ($afterExisting) {
158  $subPages = $this->‪getSubPagesOfPage($pageUid);
159  $lastPage = end($subPages);
160  if (isset($lastPage['uid']) && ‪MathUtility::canBeInterpretedAsInteger($lastPage['uid'])) {
161  $firstPid = -(int)$lastPage['uid'];
162  }
163  }
164 
165  $commandArray = [];
166  $firstRecord = true;
167  $previousIdentifier = '';
168  foreach ($newPagesData as $identifier => $data) {
169  if (!trim($data['title'])) {
170  continue;
171  }
172  $commandArray['pages'][$identifier]['hidden'] = (int)$hidePages;
173  $commandArray['pages'][$identifier]['nav_hide'] = (int)$hidePagesInMenu;
174  $commandArray['pages'][$identifier]['title'] = $data['title'];
175  $commandArray['pages'][$identifier]['doktype'] = $data['doktype'];
176  if ($firstRecord) {
177  $firstRecord = false;
178  $commandArray['pages'][$identifier]['pid'] = $firstPid;
179  } else {
180  $commandArray['pages'][$identifier]['pid'] = '-' . $previousIdentifier;
181  }
182  $previousIdentifier = $identifier;
183  }
184 
185  if (!empty($commandArray)) {
186  $pagesCreated = true;
187  $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
188  $dataHandler->start($commandArray, []);
189  $dataHandler->process_datamap();
190  BackendUtility::setUpdateSignal('updatePageTree');
191  }
192 
193  return $pagesCreated;
194  }
195 
202  protected function ‪getTypeSelectData(int $pageUid)
203  {
204  $tsConfig = BackendUtility::getPagesTSconfig($pageUid);
205  $pagesTsConfig = $tsConfig['TCEFORM.']['pages.'] ?? [];
206 
207  // Find all available doktypes for the current user
208  $types = ‪$GLOBALS['PAGES_TYPES'];
209  unset($types['default']);
210  $types = array_keys($types);
211  $types[] = 1; // default
212  $types[] = 3; // link
213  $types[] = 4; // shortcut
214  $types[] = 7; // mount point
215  $types[] = 199; // spacer
216 
217  if (!$this->‪getBackendUser()->isAdmin() && isset($this->‪getBackendUser()->groupData['pagetypes_select'])) {
218  $types = ‪GeneralUtility::trimExplode(',', $this->‪getBackendUser()->groupData['pagetypes_select'], true);
219  }
220  $removeItems = isset($pagesTsConfig['doktype.']['removeItems']) ? ‪GeneralUtility::trimExplode(',', $pagesTsConfig['doktype.']['removeItems'], true) : [];
221  $allowedDoktypes = array_diff($types, $removeItems);
222 
223  // All doktypes in the TCA
224  $availableDoktypes = ‪$GLOBALS['TCA']['pages']['columns']['doktype']['config']['items'];
225 
226  // Sort by group and allowedDoktypes
227  $groupedData = [];
228  $groupLabel = '';
229  foreach ($availableDoktypes as $doktypeData) {
230  // If it is a group, save the group label for the children underneath
231  if ($doktypeData[1] === '--div--') {
232  $groupLabel = $doktypeData[0];
233  } else {
234  if (in_array($doktypeData[1], $allowedDoktypes)) {
235  $groupedData[$groupLabel][] = $doktypeData;
236  }
237  }
238  }
239 
240  return $groupedData;
241  }
242 
250  protected function ‪getSubPagesOfPage(int $pageUid): array
251  {
252  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
253  $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
254  return $queryBuilder->select('*')
255  ->from('pages')
256  ->where(
257  $queryBuilder->expr()->eq(
258  'pid',
259  $queryBuilder->createNamedParameter($pageUid, ‪Connection::PARAM_INT)
260  ),
261  $queryBuilder->expr()->eq(
262  ‪$GLOBALS['TCA']['pages']['ctrl']['languageField'],
263  $queryBuilder->createNamedParameter(0, ‪Connection::PARAM_INT)
264  )
265  )
266  ->orderBy('sorting')
267  ->executeQuery()
268  ->fetchAllAssociative();
269  }
270 
276  protected function ‪getLanguageService()
277  {
278  return ‪$GLOBALS['LANG'];
279  }
280 
286  protected function ‪getBackendUser()
287  {
288  return ‪$GLOBALS['BE_USER'];
289  }
290 }
‪TYPO3\CMS\Core\DataHandling\DataHandler
Definition: DataHandler.php:86
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_SMALL
‪const SIZE_SMALL
Definition: Icon.php:30
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:999
‪TYPO3\CMS\Backend\Controller\Page\NewMultiplePagesController\getLanguageService
‪LanguageService getLanguageService()
Definition: NewMultiplePagesController.php:275
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:49
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:74
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\create
‪static static create(int $pageId, string $alternativeUri=null)
Definition: PreviewUriBuilder.php:75
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:26
‪TYPO3\CMS\Backend\Template\ModuleTemplateFactory
Definition: ModuleTemplateFactory.php:29
‪TYPO3\CMS\Backend\Controller\Page\NewMultiplePagesController\$moduleTemplate
‪ModuleTemplate $moduleTemplate
Definition: NewMultiplePagesController.php:51
‪TYPO3\CMS\Backend\Controller\Page\NewMultiplePagesController
Definition: NewMultiplePagesController.php:46
‪TYPO3\CMS\Backend\Controller\Page\NewMultiplePagesController\createPages
‪bool createPages(array $newPagesData, int $pageUid, bool $afterExisting, bool $hidePages, bool $hidePagesInMenu)
Definition: NewMultiplePagesController.php:150
‪TYPO3\CMS\Backend\Controller\Page\NewMultiplePagesController\getSubPagesOfPage
‪array getSubPagesOfPage(int $pageUid)
Definition: NewMultiplePagesController.php:249
‪TYPO3\CMS\Backend\Controller\Page\NewMultiplePagesController\$moduleTemplateFactory
‪ModuleTemplateFactory $moduleTemplateFactory
Definition: NewMultiplePagesController.php:54
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\Backend\Controller\Page
Definition: LocalizationController.php:18
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:46
‪TYPO3\CMS\Core\Type\Bitmask\Permission
Definition: Permission.php:26
‪TYPO3\CMS\Backend\Controller\Page\NewMultiplePagesController\getTypeSelectData
‪array getTypeSelectData(int $pageUid)
Definition: NewMultiplePagesController.php:201
‪TYPO3\CMS\Backend\Controller\Page\NewMultiplePagesController\mainAction
‪ResponseInterface mainAction(ServerRequestInterface $request)
Definition: NewMultiplePagesController.php:68
‪TYPO3\CMS\Backend\Controller\Page\NewMultiplePagesController\getBackendUser
‪BackendUserAuthentication getBackendUser()
Definition: NewMultiplePagesController.php:285
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Core\Type\Bitmask\Permission\PAGE_SHOW
‪const PAGE_SHOW
Definition: Permission.php:35
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:38
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:31
‪$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:32
‪TYPO3\CMS\Backend\Controller\Page\NewMultiplePagesController\$iconFactory
‪IconFactory $iconFactory
Definition: NewMultiplePagesController.php:53
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Backend\Controller\Page\NewMultiplePagesController\__construct
‪__construct(IconFactory $iconFactory, ModuleTemplateFactory $moduleTemplateFactory)
Definition: NewMultiplePagesController.php:56
‪TYPO3\CMS\Core\Http\HtmlResponse
Definition: HtmlResponse.php:26