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