‪TYPO3CMS  10.4
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;
35 
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  $parentPageUid = (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  $pageInformation = ‪BackendUtility::readPageAccess($parentPageUid, $backendUser->getPagePermsClause(‪Permission::PAGE_SHOW));
72  if (!is_array($pageInformation)) {
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($pageInformation);
81  $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
82  $cshButton = $buttonBar->makeHelpButton()
83  ->setModuleName('pages_sort')
84  ->setFieldName('pages_sort');
85  $viewButton = $buttonBar->makeLinkButton()
86  ->setOnClick(‪BackendUtility::viewOnClick($parentPageUid, '', ‪BackendUtility::BEgetRootLine($parentPageUid)))
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_sort')
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/SortSubPages.html'
99  ));
100 
101  $isInWorkspace = $backendUser->workspace !== 0;
102  $view->assign('isInWorkspace', $isInWorkspace);
103  $view->assign('maxTitleLength', $backendUser->uc['titleLen'] ?? 20);
104  $view->assign('parentPageUid', $parentPageUid);
105  $view->assign('dateFormat', ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy']);
106  $view->assign('timeFormat', ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm']);
107 
108  if (!$isInWorkspace) {
109  // Apply new sorting if given
110  $newSortBy = $request->getQueryParams()['newSortBy'] ?? null;
111  if ($newSortBy && in_array($newSortBy, ['title', 'subtitle', 'nav_title', 'crdate', 'tstamp'], true)) {
112  $this->‪sortSubPagesByField($parentPageUid, (string)$newSortBy);
113  } elseif ($newSortBy && $newSortBy === 'reverseCurrentSorting') {
114  $this->‪reverseSortingOfPages($parentPageUid);
115  }
116 
117  // Get sub pages, loop through them and add page/user specific permission details
118  $pageRecords = $this->‪getSubPagesOfPage($parentPageUid);
119  $hasInvisiblePage = false;
120  $subPages = [];
121  foreach ($pageRecords as $page) {
122  $pageWithPermissions = [];
123  $pageWithPermissions['record'] = $page;
124  $calculatedPermissions = $backendUser->calcPerms($page);
125  $pageWithPermissions['canEdit'] = $backendUser->isAdmin() || $calculatedPermissions & ‪Permission::PAGE_EDIT;
126  $canSeePage = $backendUser->isAdmin() || $calculatedPermissions & ‪Permission::PAGE_SHOW;
127  if ($canSeePage) {
128  $subPages[] = $pageWithPermissions;
129  } else {
130  $hasInvisiblePage = true;
131  }
132  }
133  $view->assign('subPages', $subPages);
134  $view->assign('hasInvisiblePage', $hasInvisiblePage);
135  }
136 
137  $this->moduleTemplate->setContent($view->render());
138  return new ‪HtmlResponse($this->moduleTemplate->renderContent());
139  }
140 
148  protected function ‪sortSubPagesByField(int $parentPageUid, string $newSortBy)
149  {
150  if (!in_array($newSortBy, ['title', 'subtitle', 'nav_title', 'crdate', 'tstamp'], true)) {
151  throw new \RuntimeException(
152  'New sort by must be one of "title", "subtitle", "nav_title", "crdate" or tstamp',
153  1498924810
154  );
155  }
156  $subPages = $this->‪getSubPagesOfPage($parentPageUid, $newSortBy);
157  if (!empty($subPages)) {
158  $subPages = array_reverse($subPages);
159  $this->‪persistNewSubPageOrder($parentPageUid, $subPages);
160  }
161  }
162 
168  protected function ‪reverseSortingOfPages(int $parentPageUid)
169  {
170  $subPages = $this->‪getSubPagesOfPage($parentPageUid);
171  if (!empty($subPages)) {
172  $this->‪persistNewSubPageOrder($parentPageUid, $subPages);
173  }
174  }
175 
182  protected function ‪persistNewSubPageOrder(int $parentPageUid, array $subPages)
183  {
184  $commandArray = [];
185  foreach ($subPages as $subPage) {
186  $commandArray['pages'][$subPage['uid']]['move'] = $parentPageUid;
187  }
188  $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
189  $dataHandler->start([], $commandArray);
190  $dataHandler->process_cmdmap();
191  ‪BackendUtility::setUpdateSignal('updatePageTree');
192  }
193 
202  protected function ‪getSubPagesOfPage(int $parentPageUid, string $orderBy = 'sorting'): array
203  {
204  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
205  $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
206  return $queryBuilder->select('*')
207  ->from('pages')
208  ->where(
209  $queryBuilder->expr()->eq('sys_language_uid', 0),
210  $queryBuilder->expr()->eq(
211  'pid',
212  $queryBuilder->createNamedParameter($parentPageUid, \PDO::PARAM_INT)
213  )
214  )
215  ->orderBy($orderBy)
216  ->execute()
217  ->fetchAll();
218  }
219 
225  protected function ‪getLanguageService()
226  {
227  return ‪$GLOBALS['LANG'];
228  }
229 
235  protected function ‪getBackendUser()
236  {
237  return ‪$GLOBALS['BE_USER'];
238  }
239 }
‪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\SortSubPagesController\getBackendUser
‪BackendUserAuthentication getBackendUser()
Definition: SortSubPagesController.php:234
‪TYPO3\CMS\Backend\Controller\Page\SortSubPagesController\getLanguageService
‪LanguageService getLanguageService()
Definition: SortSubPagesController.php:224
‪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\Core\Imaging\IconFactory
Definition: IconFactory.php:33
‪TYPO3\CMS\Backend\Controller\Page\SortSubPagesController\$moduleTemplate
‪ModuleTemplate $moduleTemplate
Definition: SortSubPagesController.php:46
‪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\Controller\Page\SortSubPagesController\persistNewSubPageOrder
‪persistNewSubPageOrder(int $parentPageUid, array $subPages)
Definition: SortSubPagesController.php:181
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:43
‪TYPO3\CMS\Core\Type\Bitmask\Permission
Definition: Permission.php:24
‪TYPO3\CMS\Backend\Controller\Page\SortSubPagesController\sortSubPagesByField
‪sortSubPagesByField(int $parentPageUid, string $newSortBy)
Definition: SortSubPagesController.php:147
‪TYPO3\CMS\Backend\Controller\Page\SortSubPagesController\reverseSortingOfPages
‪reverseSortingOfPages(int $parentPageUid)
Definition: SortSubPagesController.php:167
‪TYPO3\CMS\Backend\Controller\Page\SortSubPagesController\getSubPagesOfPage
‪array getSubPagesOfPage(int $parentPageUid, string $orderBy='sorting')
Definition: SortSubPagesController.php:201
‪TYPO3\CMS\Backend\Controller\Page\SortSubPagesController
Definition: SortSubPagesController.php:41
‪TYPO3\CMS\Backend\Controller\Page\SortSubPagesController\__construct
‪__construct(ModuleTemplate $moduleTemplate=null)
Definition: SortSubPagesController.php:53
‪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\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\Type\Bitmask\Permission\PAGE_EDIT
‪const PAGE_EDIT
Definition: Permission.php:38
‪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\Backend\Controller\Page\SortSubPagesController\mainAction
‪ResponseInterface mainAction(ServerRequestInterface $request)
Definition: SortSubPagesController.php:64
‪TYPO3\CMS\Core\Http\HtmlResponse
Definition: HtmlResponse.php:26