‪TYPO3CMS  9.5
SortSubPagesController.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;
33 
39 {
45  protected ‪$moduleTemplate;
46 
53  {
54  $this->moduleTemplate = ‪$moduleTemplate ?? GeneralUtility::makeInstance(ModuleTemplate::class);
55  }
56 
63  public function ‪mainAction(ServerRequestInterface $request): ResponseInterface
64  {
65  $backendUser = $this->‪getBackendUser();
66  $parentPageUid = (int)$request->getQueryParams()['id'];
67 
68  // Show only if there is a valid page and if this page may be viewed by the user
69  $pageInformation = ‪BackendUtility::readPageAccess($parentPageUid, $backendUser->getPagePermsClause(‪Permission::PAGE_SHOW));
70  if (!is_array($pageInformation)) {
71  // User has no permission on parent page, should not happen, just render an empty page
72  $this->moduleTemplate->setContent('');
73  return new ‪HtmlResponse($this->moduleTemplate->renderContent());
74  }
75 
76  // Doc header handling
77  $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
78  $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($pageInformation);
79  $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
80  $cshButton = $buttonBar->makeHelpButton()
81  ->setModuleName('pages_sort')
82  ->setFieldName('pages_sort');
83  $viewButton = $buttonBar->makeLinkButton()
84  ->setOnClick(‪BackendUtility::viewOnClick($parentPageUid, '', ‪BackendUtility::BEgetRootLine($parentPageUid)))
85  ->setTitle($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.showPage'))
86  ->setIcon($iconFactory->getIcon('actions-view-page', ‪Icon::SIZE_SMALL))
87  ->setHref('#');
88  $shortcutButton = $buttonBar->makeShortcutButton()
89  ->setModuleName('pages_sort')
90  ->setGetVariables(['id']);
91  $buttonBar->addButton($cshButton)->addButton($viewButton)->addButton($shortcutButton);
92 
93  // Main view setup
94  $view = GeneralUtility::makeInstance(StandaloneView::class);
95  $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
96  'EXT:backend/Resources/Private/Templates/Page/SortSubPages.html'
97  ));
98 
99  $isInWorkspace = $backendUser->workspace !== 0;
100  $view->assign('isInWorkspace', $isInWorkspace);
101  $view->assign('maxTitleLength', $backendUser->uc['titleLen'] ?? 20);
102  $view->assign('parentPageUid', $parentPageUid);
103  $view->assign('dateFormat', ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy']);
104  $view->assign('timeFormat', ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm']);
105 
106  if (!$isInWorkspace) {
107  // Apply new sorting if given
108  $newSortBy = $request->getQueryParams()['newSortBy'] ?? null;
109  if ($newSortBy && in_array($newSortBy, ['title', 'subtitle', 'nav_title', 'crdate', 'tstamp'], true)) {
110  $this->‪sortSubPagesByField($parentPageUid, (string)$newSortBy);
111  } elseif ($newSortBy && $newSortBy === 'reverseCurrentSorting') {
112  $this->‪reverseSortingOfPages($parentPageUid);
113  }
114 
115  // Get sub pages, loop through them and add page/user specific permission details
116  $pageRecords = $this->‪getSubPagesOfPage($parentPageUid);
117  $hasInvisiblePage = false;
118  $subPages = [];
119  foreach ($pageRecords as $page) {
120  $pageWithPermissions = [];
121  $pageWithPermissions['record'] = $page;
122  $calculatedPermissions = $backendUser->calcPerms($page);
123  $pageWithPermissions['canEdit'] = $backendUser->isAdmin() || $calculatedPermissions & ‪Permission::PAGE_EDIT;
124  $canSeePage = $backendUser->isAdmin() || $calculatedPermissions & ‪Permission::PAGE_SHOW;
125  if ($canSeePage) {
126  $subPages[] = $pageWithPermissions;
127  } else {
128  $hasInvisiblePage = true;
129  }
130  }
131  $view->assign('subPages', $subPages);
132  $view->assign('hasInvisiblePage', $hasInvisiblePage);
133  }
134 
135  $this->moduleTemplate->setContent($view->render());
136  return new ‪HtmlResponse($this->moduleTemplate->renderContent());
137  }
138 
146  protected function ‪sortSubPagesByField(int $parentPageUid, string $newSortBy)
147  {
148  if (!in_array($newSortBy, ['title', 'subtitle', 'nav_title', 'crdate', 'tstamp'], true)) {
149  throw new \RuntimeException(
150  'New sort by must be one of "title", "subtitle", "nav_title", "crdate" or tstamp',
151  1498924810
152  );
153  }
154  $subPages = $this->‪getSubPagesOfPage($parentPageUid, $newSortBy);
155  if (!empty($subPages)) {
156  $subPages = array_reverse($subPages);
157  $this->‪persistNewSubPageOrder($parentPageUid, $subPages);
158  }
159  }
160 
166  protected function ‪reverseSortingOfPages(int $parentPageUid)
167  {
168  $subPages = $this->‪getSubPagesOfPage($parentPageUid);
169  if (!empty($subPages)) {
170  $this->‪persistNewSubPageOrder($parentPageUid, $subPages);
171  }
172  }
173 
180  protected function ‪persistNewSubPageOrder(int $parentPageUid, array $subPages)
181  {
182  $commandArray = [];
183  foreach ($subPages as $subPage) {
184  $commandArray['pages'][$subPage['uid']]['move'] = $parentPageUid;
185  }
186  $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
187  $dataHandler->start([], $commandArray);
188  $dataHandler->process_cmdmap();
189  ‪BackendUtility::setUpdateSignal('updatePageTree');
190  }
191 
200  protected function ‪getSubPagesOfPage(int $parentPageUid, string $orderBy = 'sorting'): array
201  {
202  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
203  $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
204  return $queryBuilder->select('*')
205  ->from('pages')
206  ->where(
207  $queryBuilder->expr()->eq('sys_language_uid', 0),
208  $queryBuilder->expr()->eq(
209  'pid',
210  $queryBuilder->createNamedParameter($parentPageUid, \PDO::PARAM_INT)
211  )
212  )
213  ->orderBy($orderBy)
214  ->execute()
215  ->fetchAll();
216  }
217 
223  protected function ‪getLanguageService()
224  {
225  return ‪$GLOBALS['LANG'];
226  }
227 
233  protected function ‪getBackendUser()
234  {
235  return ‪$GLOBALS['BE_USER'];
236  }
237 }
‪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\SortSubPagesController\getBackendUser
‪BackendUserAuthentication getBackendUser()
Definition: SortSubPagesController.php:232
‪TYPO3\CMS\Backend\Controller\Page\SortSubPagesController\getLanguageService
‪LanguageService getLanguageService()
Definition: SortSubPagesController.php:222
‪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\Core\Imaging\IconFactory
Definition: IconFactory.php:31
‪TYPO3\CMS\Backend\Controller\Page\SortSubPagesController\$moduleTemplate
‪ModuleTemplate $moduleTemplate
Definition: SortSubPagesController.php:44
‪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\Controller\Page\SortSubPagesController\persistNewSubPageOrder
‪persistNewSubPageOrder(int $parentPageUid, array $subPages)
Definition: SortSubPagesController.php:179
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:40
‪TYPO3\CMS\Core\Type\Bitmask\Permission
Definition: Permission.php:23
‪TYPO3\CMS\Backend\Controller\Page\SortSubPagesController\sortSubPagesByField
‪sortSubPagesByField(int $parentPageUid, string $newSortBy)
Definition: SortSubPagesController.php:145
‪TYPO3\CMS\Backend\Controller\Page\SortSubPagesController\reverseSortingOfPages
‪reverseSortingOfPages(int $parentPageUid)
Definition: SortSubPagesController.php:165
‪TYPO3\CMS\Backend\Controller\Page\SortSubPagesController\getSubPagesOfPage
‪array getSubPagesOfPage(int $parentPageUid, string $orderBy='sorting')
Definition: SortSubPagesController.php:199
‪TYPO3\CMS\Backend\Controller\Page\SortSubPagesController
Definition: SortSubPagesController.php:39
‪TYPO3\CMS\Backend\Controller\Page\SortSubPagesController\__construct
‪__construct(ModuleTemplate $moduleTemplate=null)
Definition: SortSubPagesController.php:51
‪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
‪$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\Type\Bitmask\Permission\PAGE_EDIT
‪const PAGE_EDIT
Definition: Permission.php:37
‪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\Backend\Controller\Page\SortSubPagesController\mainAction
‪ResponseInterface mainAction(ServerRequestInterface $request)
Definition: SortSubPagesController.php:62
‪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