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