‪TYPO3CMS  11.5
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;
38 
44 {
50  protected ‪$moduleTemplate;
51 
54 
56  {
57  $this->iconFactory = ‪$iconFactory;
58  $this->moduleTemplateFactory = ‪$moduleTemplateFactory;
59  }
60 
67  public function ‪mainAction(ServerRequestInterface $request): ResponseInterface
68  {
69  $this->moduleTemplate = $this->moduleTemplateFactory->create($request);
70  $backendUser = $this->‪getBackendUser();
71  $parentPageUid = (int)($request->getQueryParams()['id'] ?? 0);
72 
73  // Show only if there is a valid page and if this page may be viewed by the user
74  $pageInformation = BackendUtility::readPageAccess($parentPageUid, $backendUser->getPagePermsClause(‪Permission::PAGE_SHOW));
75  if (!is_array($pageInformation)) {
76  // User has no permission on parent page, should not happen, just render an empty page
77  $this->moduleTemplate->setContent('');
78  return new ‪HtmlResponse($this->moduleTemplate->renderContent());
79  }
80 
81  // Doc header handling
82  $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($pageInformation);
83  $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
84  $cshButton = $buttonBar->makeHelpButton()
85  ->setModuleName('pages_sort')
86  ->setFieldName('pages_sort');
87  $previewDataAttributes = ‪PreviewUriBuilder::create($parentPageUid)
88  ->withRootLine(BackendUtility::BEgetRootLine($parentPageUid))
89  ->buildDispatcherDataAttributes();
90  $viewButton = $buttonBar->makeLinkButton()
91  ->setDataAttributes($previewDataAttributes ?? [])
92  ->setTitle($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.showPage'))
93  ->setIcon($this->iconFactory->getIcon('actions-view-page', ‪Icon::SIZE_SMALL))
94  ->setHref('#');
95  $buttonBar->addButton($cshButton)->addButton($viewButton);
96 
97  // Main view setup
98  $view = GeneralUtility::makeInstance(StandaloneView::class);
99  $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
100  'EXT:backend/Resources/Private/Templates/Page/SortSubPages.html'
101  ));
102 
103  $isInWorkspace = $backendUser->workspace !== 0;
104  $view->assign('isInWorkspace', $isInWorkspace);
105  $view->assign('maxTitleLength', $backendUser->uc['titleLen'] ?? 20);
106  $view->assign('parentPageUid', $parentPageUid);
107  $view->assign('dateFormat', ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy']);
108  $view->assign('timeFormat', ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm']);
109 
110  if (!$isInWorkspace) {
111  // Apply new sorting if given
112  $newSortBy = $request->getQueryParams()['newSortBy'] ?? null;
113  if ($newSortBy && in_array($newSortBy, ['title', 'subtitle', 'nav_title', 'crdate', 'tstamp'], true)) {
114  $this->‪sortSubPagesByField($parentPageUid, (string)$newSortBy);
115  } elseif ($newSortBy && $newSortBy === 'reverseCurrentSorting') {
116  $this->‪reverseSortingOfPages($parentPageUid);
117  }
118 
119  // Get sub pages, loop through them and add page/user specific permission details
120  $pageRecords = $this->‪getSubPagesOfPage($parentPageUid);
121  $hasInvisiblePage = false;
122  $subPages = [];
123  foreach ($pageRecords as $page) {
124  $pageWithPermissions = [];
125  $pageWithPermissions['record'] = $page;
126  $calculatedPermissions = new Permission($backendUser->calcPerms($page));
127  $pageWithPermissions['canEdit'] = $backendUser->isAdmin() || $calculatedPermissions->editPagePermissionIsGranted();
128  $canSeePage = $backendUser->isAdmin() || $calculatedPermissions->showPagePermissionIsGranted();
129  if ($canSeePage) {
130  $subPages[] = $pageWithPermissions;
131  } else {
132  $hasInvisiblePage = true;
133  }
134  }
135  $view->assign('subPages', $subPages);
136  $view->assign('hasInvisiblePage', $hasInvisiblePage);
137  }
138 
139  $this->moduleTemplate->setContent($view->render());
140  return new HtmlResponse($this->moduleTemplate->renderContent());
141  }
142 
150  protected function ‪sortSubPagesByField(int $parentPageUid, string $newSortBy)
151  {
152  if (!in_array($newSortBy, ['title', 'subtitle', 'nav_title', 'crdate', 'tstamp'], true)) {
153  throw new \RuntimeException(
154  'New sort by must be one of "title", "subtitle", "nav_title", "crdate" or tstamp',
155  1498924810
156  );
157  }
158  $subPages = $this->‪getSubPagesOfPage($parentPageUid, $newSortBy);
159  if (!empty($subPages)) {
160  $subPages = array_reverse($subPages);
161  $this->‪persistNewSubPageOrder($parentPageUid, $subPages);
162  }
163  }
164 
170  protected function ‪reverseSortingOfPages(int $parentPageUid)
171  {
172  $subPages = $this->‪getSubPagesOfPage($parentPageUid);
173  if (!empty($subPages)) {
174  $this->‪persistNewSubPageOrder($parentPageUid, $subPages);
175  }
176  }
177 
184  protected function ‪persistNewSubPageOrder(int $parentPageUid, array $subPages)
185  {
186  $commandArray = [];
187  foreach ($subPages as $subPage) {
188  $commandArray['pages'][$subPage['uid']]['move'] = $parentPageUid;
189  }
190  $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
191  $dataHandler->start([], $commandArray);
192  $dataHandler->process_cmdmap();
193  BackendUtility::setUpdateSignal('updatePageTree');
194  }
195 
204  protected function ‪getSubPagesOfPage(int $parentPageUid, string $orderBy = 'sorting'): array
205  {
206  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
207  $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
208  return $queryBuilder->select('*')
209  ->from('pages')
210  ->where(
211  $queryBuilder->expr()->eq('sys_language_uid', 0),
212  $queryBuilder->expr()->eq(
213  'pid',
214  $queryBuilder->createNamedParameter($parentPageUid, ‪Connection::PARAM_INT)
215  )
216  )
217  ->orderBy($orderBy)
218  ->executeQuery()
219  ->fetchAllAssociative();
220  }
221 
227  protected function ‪getLanguageService()
228  {
229  return ‪$GLOBALS['LANG'];
230  }
231 
237  protected function ‪getBackendUser()
238  {
239  return ‪$GLOBALS['BE_USER'];
240  }
241 }
‪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\Backend\Controller\Page\SortSubPagesController\getBackendUser
‪BackendUserAuthentication getBackendUser()
Definition: SortSubPagesController.php:236
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:49
‪TYPO3\CMS\Backend\Controller\Page\SortSubPagesController\getLanguageService
‪LanguageService getLanguageService()
Definition: SortSubPagesController.php:226
‪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\SortSubPagesController\__construct
‪__construct(IconFactory $iconFactory, ModuleTemplateFactory $moduleTemplateFactory)
Definition: SortSubPagesController.php:54
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\Backend\Controller\Page\SortSubPagesController\$moduleTemplate
‪ModuleTemplate $moduleTemplate
Definition: SortSubPagesController.php:49
‪TYPO3\CMS\Backend\Controller\Page\SortSubPagesController\$moduleTemplateFactory
‪ModuleTemplateFactory $moduleTemplateFactory
Definition: SortSubPagesController.php:52
‪TYPO3\CMS\Backend\Controller\Page
Definition: LocalizationController.php:18
‪TYPO3\CMS\Backend\Controller\Page\SortSubPagesController\persistNewSubPageOrder
‪persistNewSubPageOrder(int $parentPageUid, array $subPages)
Definition: SortSubPagesController.php:183
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:46
‪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:149
‪TYPO3\CMS\Backend\Controller\Page\SortSubPagesController\reverseSortingOfPages
‪reverseSortingOfPages(int $parentPageUid)
Definition: SortSubPagesController.php:169
‪TYPO3\CMS\Backend\Controller\Page\SortSubPagesController\getSubPagesOfPage
‪array getSubPagesOfPage(int $parentPageUid, string $orderBy='sorting')
Definition: SortSubPagesController.php:203
‪TYPO3\CMS\Backend\Controller\Page\SortSubPagesController
Definition: SortSubPagesController.php:44
‪TYPO3\CMS\Backend\Controller\Page\SortSubPagesController\$iconFactory
‪IconFactory $iconFactory
Definition: SortSubPagesController.php:51
‪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\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\SortSubPagesController\mainAction
‪ResponseInterface mainAction(ServerRequestInterface $request)
Definition: SortSubPagesController.php:66
‪TYPO3\CMS\Core\Http\HtmlResponse
Definition: HtmlResponse.php:26