‪TYPO3CMS  ‪main
SortSubPagesController.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;
32 use TYPO3\CMS\Core\Imaging\IconSize;
36 
41 #[AsController]
43 {
44  public function ‪__construct(
45  protected readonly ‪IconFactory $iconFactory,
46  protected readonly ‪ModuleTemplateFactory $moduleTemplateFactory,
47  ) {}
48 
52  public function ‪mainAction(ServerRequestInterface $request): ResponseInterface
53  {
54  $view = $this->moduleTemplateFactory->create($request);
55  $backendUser = $this->‪getBackendUser();
56  $parentPageUid = (int)($request->getQueryParams()['id'] ?? 0);
57 
58  // Show only if there is a valid page and if this page may be viewed by the user
59  $pageInformation = BackendUtility::readPageAccess($parentPageUid, $backendUser->getPagePermsClause(‪Permission::PAGE_SHOW));
60  if (!is_array($pageInformation)) {
61  // User has no permission on parent page, should not happen, just render an empty page
62  return $view->renderResponse('Dummy/Index');
63  }
64 
65  // Doc header handling
66  $view->getDocHeaderComponent()->setMetaInformation($pageInformation);
67  $buttonBar = $view->getDocHeaderComponent()->getButtonBar();
68  $previewDataAttributes = ‪PreviewUriBuilder::create($parentPageUid)
69  ->withRootLine(BackendUtility::BEgetRootLine($parentPageUid))
70  ->buildDispatcherDataAttributes();
71  $viewButton = $buttonBar->makeLinkButton()
72  ->setHref('#')
73  ->setDataAttributes($previewDataAttributes ?? [])
74  ->setDisabled(!$previewDataAttributes)
75  ->setTitle($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.showPage'))
76  ->setIcon($this->iconFactory->getIcon('actions-view-page', IconSize::SMALL))
77  ->setShowLabelText(true);
78  $buttonBar->addButton($viewButton);
79 
80  $isInWorkspace = $backendUser->workspace !== 0;
81  $view->assignMultiple([
82  'isInWorkspace' => $isInWorkspace,
83  'maxTitleLength' => $backendUser->uc['titleLen'] ?? 20,
84  'parentPageUid' => $parentPageUid,
85  'dateFormat' => ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'],
86  'timeFormat' => ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm'],
87  ]);
88 
89  if (!$isInWorkspace) {
90  // Apply new sorting if given
91  $newSortBy = $request->getQueryParams()['newSortBy'] ?? null;
92  if ($newSortBy && in_array($newSortBy, ['title', 'subtitle', 'nav_title', 'crdate', 'tstamp'], true)) {
93  $this->‪sortSubPagesByField($parentPageUid, (string)$newSortBy);
94  } elseif ($newSortBy && $newSortBy === 'reverseCurrentSorting') {
95  $this->‪reverseSortingOfPages($parentPageUid);
96  }
97 
98  // Get sub pages, loop through them and add page/user specific permission details
99  $pageRecords = $this->‪getSubPagesOfPage($parentPageUid);
100  $hasInvisiblePage = false;
101  $subPages = [];
102  foreach ($pageRecords as $page) {
103  $pageWithPermissions = [];
104  $pageWithPermissions['record'] = $page;
105  $calculatedPermissions = new ‪Permission($backendUser->calcPerms($page));
106  $pageWithPermissions['canEdit'] = $backendUser->isAdmin() || $calculatedPermissions->editPagePermissionIsGranted();
107  $canSeePage = $backendUser->isAdmin() || $calculatedPermissions->showPagePermissionIsGranted();
108  if ($canSeePage) {
109  $subPages[] = $pageWithPermissions;
110  } else {
111  $hasInvisiblePage = true;
112  }
113  }
114  $view->assign('subPages', $subPages);
115  $view->assign('hasInvisiblePage', $hasInvisiblePage);
116  }
117 
118  return $view->renderResponse('Page/SortSubPages');
119  }
120 
128  protected function ‪sortSubPagesByField(int $parentPageUid, string $newSortBy)
129  {
130  if (!in_array($newSortBy, ['title', 'subtitle', 'nav_title', 'crdate', 'tstamp'], true)) {
131  throw new \RuntimeException(
132  'New sort by must be one of "title", "subtitle", "nav_title", "crdate" or tstamp',
133  1498924810
134  );
135  }
136  $subPages = $this->‪getSubPagesOfPage($parentPageUid, $newSortBy);
137  if (!empty($subPages)) {
138  $subPages = array_reverse($subPages);
139  $this->‪persistNewSubPageOrder($parentPageUid, $subPages);
140  }
141  }
142 
148  protected function ‪reverseSortingOfPages(int $parentPageUid)
149  {
150  $subPages = $this->‪getSubPagesOfPage($parentPageUid);
151  if (!empty($subPages)) {
152  $this->‪persistNewSubPageOrder($parentPageUid, $subPages);
153  }
154  }
155 
162  protected function ‪persistNewSubPageOrder(int $parentPageUid, array $subPages)
163  {
164  $commandArray = [];
165  foreach ($subPages as $subPage) {
166  $commandArray['pages'][$subPage['uid']]['move'] = $parentPageUid;
167  }
168  $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
169  $dataHandler->start([], $commandArray);
170  $dataHandler->process_cmdmap();
171  BackendUtility::setUpdateSignal('updatePageTree');
172  }
173 
181  protected function ‪getSubPagesOfPage(int $parentPageUid, string $orderBy = 'sorting'): array
182  {
183  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
184  $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
185  return $queryBuilder->select('*')
186  ->from('pages')
187  ->where(
188  $queryBuilder->expr()->eq('sys_language_uid', 0),
189  $queryBuilder->expr()->eq(
190  'pid',
191  $queryBuilder->createNamedParameter($parentPageUid, ‪Connection::PARAM_INT)
192  )
193  )
194  ->orderBy($orderBy)
195  ->executeQuery()
196  ->fetchAllAssociative();
197  }
198 
200  {
201  return ‪$GLOBALS['LANG'];
202  }
203 
205  {
206  return ‪$GLOBALS['BE_USER'];
207  }
208 }
‪TYPO3\CMS\Core\DataHandling\DataHandler
Definition: DataHandler.php:104
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:52
‪TYPO3\CMS\Backend\Controller\Page\SortSubPagesController\mainAction
‪mainAction(ServerRequestInterface $request)
Definition: SortSubPagesController.php:52
‪TYPO3\CMS\Backend\Template\ModuleTemplateFactory
Definition: ModuleTemplateFactory.php:35
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:35
‪TYPO3\CMS\Backend\Controller\Page
Definition: LocalizationController.php:18
‪TYPO3\CMS\Backend\Controller\Page\SortSubPagesController\getLanguageService
‪getLanguageService()
Definition: SortSubPagesController.php:199
‪TYPO3\CMS\Backend\Controller\Page\SortSubPagesController\persistNewSubPageOrder
‪persistNewSubPageOrder(int $parentPageUid, array $subPages)
Definition: SortSubPagesController.php:162
‪TYPO3\CMS\Backend\Controller\Page\SortSubPagesController\__construct
‪__construct(protected readonly IconFactory $iconFactory, protected readonly ModuleTemplateFactory $moduleTemplateFactory,)
Definition: SortSubPagesController.php:44
‪TYPO3\CMS\Core\Type\Bitmask\Permission
Definition: Permission.php:26
‪TYPO3\CMS\Backend\Controller\Page\SortSubPagesController\sortSubPagesByField
‪sortSubPagesByField(int $parentPageUid, string $newSortBy)
Definition: SortSubPagesController.php:128
‪TYPO3\CMS\Backend\Controller\Page\SortSubPagesController\reverseSortingOfPages
‪reverseSortingOfPages(int $parentPageUid)
Definition: SortSubPagesController.php:148
‪TYPO3\CMS\Backend\Controller\Page\SortSubPagesController
Definition: SortSubPagesController.php:43
‪TYPO3\CMS\Backend\Routing\PreviewUriBuilder\create
‪static static create(int $pageId)
Definition: PreviewUriBuilder.php:65
‪TYPO3\CMS\Backend\Controller\Page\SortSubPagesController\getBackendUser
‪getBackendUser()
Definition: SortSubPagesController.php:204
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Backend\Controller\Page\SortSubPagesController\getSubPagesOfPage
‪getSubPagesOfPage(int $parentPageUid, string $orderBy='sorting')
Definition: SortSubPagesController.php:181
‪TYPO3\CMS\Core\Type\Bitmask\Permission\PAGE_SHOW
‪const PAGE_SHOW
Definition: Permission.php:35
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:41
‪$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\Backend\Attribute\AsController
Definition: AsController.php:25
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52