[TASK] Create own response instance in controller actions
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / Page / NewMultiplePagesController.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Backend\Controller\Page;
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;
20 use TYPO3\CMS\Backend\Template\ModuleTemplate;
21 use TYPO3\CMS\Backend\Utility\BackendUtility;
22 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
23 use TYPO3\CMS\Core\Database\ConnectionPool;
24 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
25 use TYPO3\CMS\Core\DataHandling\DataHandler;
26 use TYPO3\CMS\Core\Http\HtmlResponse;
27 use TYPO3\CMS\Core\Imaging\Icon;
28 use TYPO3\CMS\Core\Imaging\IconFactory;
29 use TYPO3\CMS\Core\Localization\LanguageService;
30 use TYPO3\CMS\Core\Type\Bitmask\Permission;
31 use TYPO3\CMS\Core\Utility\GeneralUtility;
32 use TYPO3\CMS\Core\Utility\MathUtility;
33 use TYPO3\CMS\Fluid\View\StandaloneView;
34
35 /**
36 * "Create multiple pages" controller
37 */
38 class NewMultiplePagesController
39 {
40 /**
41 * ModuleTemplate object
42 *
43 * @var ModuleTemplate
44 */
45 protected $moduleTemplate;
46
47 /**
48 * Constructor Method
49 *
50 * @var $moduleTemplate ModuleTemplate
51 */
52 public function __construct(ModuleTemplate $moduleTemplate = null)
53 {
54 $this->moduleTemplate = $moduleTemplate ?? GeneralUtility::makeInstance(ModuleTemplate::class);
55 }
56
57 /**
58 * Main function Handling input variables and rendering main view
59 *
60 * @param $request ServerRequestInterface
61 * @return ResponseInterface Response
62 */
63 public function mainAction(ServerRequestInterface $request): ResponseInterface
64 {
65 $backendUser = $this->getBackendUser();
66 $pageUid = (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 $pageRecord = BackendUtility::readPageAccess($pageUid, $backendUser->getPagePermsClause(Permission::PAGE_SHOW));
70 if (!is_array($pageRecord)) {
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($pageRecord);
79 $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
80 $cshButton = $buttonBar->makeHelpButton()
81 ->setModuleName('pages_new')
82 ->setFieldName('pages_new');
83 $viewButton = $buttonBar->makeLinkButton()
84 ->setOnClick(BackendUtility::viewOnClick($pageUid, '', BackendUtility::BEgetRootLine($pageUid)))
85 ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/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_new')
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/NewPages.html'
97 ));
98
99 $calculatedPermissions = $backendUser->calcPerms($pageRecord);
100 $canCreateNew = $backendUser->isAdmin() || $calculatedPermissions & Permission::PAGE_NEW;
101
102 $view->assign('canCreateNew', $canCreateNew);
103 $view->assign('maxTitleLength', $backendUser->uc['titleLen'] ?? 20);
104 $view->assign('pageUid', $pageUid);
105
106 if ($canCreateNew) {
107 $newPagesData = (array)$request->getParsedBody()['pages'];
108 if (!empty($newPagesData)) {
109 $hasNewPagesData = true;
110 $afterExisting = isset($request->getParsedBody()['createInListEnd']);
111 $hidePages = isset($request->getParsedBody()['hidePages']);
112 $hidePagesInMenu = isset($request->getParsedBody()['hidePagesInMenus']);
113 $pagesCreated = $this->createPages($newPagesData, $pageUid, $afterExisting, $hidePages, $hidePagesInMenu);
114 $view->assign('pagesCreated', $pagesCreated);
115 $subPages = $this->getSubPagesOfPage($pageUid);
116 $visiblePages = [];
117 foreach ($subPages as $page) {
118 $calculatedPermissions = $backendUser->calcPerms($page);
119 if ($backendUser->isAdmin() || $calculatedPermissions & Permission::PAGE_SHOW) {
120 $visiblePages[] = $page;
121 }
122 }
123 $view->assign('visiblePages', $visiblePages);
124 } else {
125 $hasNewPagesData = false;
126 $view->assign('pageTypes', $this->getTypeSelectData($pageUid));
127 }
128 $view->assign('hasNewPagesData', $hasNewPagesData);
129 }
130
131 $this->moduleTemplate->setContent($view->render());
132 return new HtmlResponse($this->moduleTemplate->renderContent());
133 }
134
135 /**
136 * Persist new pages in DB
137 *
138 * @param array $newPagesData Data array with title and page type
139 * @param int $pageUid Uid of page new pages should be added in
140 * @param bool $afterExisting True if new pages should be created after existing pages
141 * @param bool $hidePages True if new pages should be set to hidden
142 * @param bool $hidePagesInMenu True if new pages should be set to hidden in menu
143 * @return bool TRUE if at least on pages has been added
144 */
145 protected function createPages(array $newPagesData, int $pageUid, bool $afterExisting, bool $hidePages, bool $hidePagesInMenu): bool
146 {
147 $pagesCreated = false;
148
149 // Set first pid to "-1 * uid of last existing sub page" if pages should be created at end
150 $firstPid = $pageUid;
151 if ($afterExisting) {
152 $subPages = $this->getSubPagesOfPage($pageUid);
153 $lastPage = end($subPages);
154 if (isset($lastPage['uid']) && MathUtility::canBeInterpretedAsInteger($lastPage['uid'])) {
155 $firstPid = -(int)($lastPage['uid']);
156 }
157 }
158
159 $commandArray = [];
160 $firstRecord = true;
161 $previousIdentifier = '';
162 foreach ($newPagesData as $identifier => $data) {
163 if (!trim($data['title'])) {
164 continue;
165 }
166 $commandArray['pages'][$identifier]['hidden'] = (int)$hidePages;
167 $commandArray['pages'][$identifier]['nav_hide'] = (int)$hidePagesInMenu;
168 $commandArray['pages'][$identifier]['title'] = $data['title'];
169 $commandArray['pages'][$identifier]['doktype'] = $data['doktype'];
170 if ($firstRecord) {
171 $firstRecord = false;
172 $commandArray['pages'][$identifier]['pid'] = $firstPid;
173 } else {
174 $commandArray['pages'][$identifier]['pid'] = '-' . $previousIdentifier;
175 }
176 $previousIdentifier = $identifier;
177 }
178
179 if (!empty($commandArray)) {
180 $pagesCreated = true;
181 $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
182 // Set default TCA values specific for the user
183 $backendUser = $this->getBackendUser();
184 $tcaDefaultOverride = $backendUser->getTSConfigProp('TCAdefaults');
185 if (is_array($tcaDefaultOverride)) {
186 $dataHandler->setDefaultsFromUserTS($tcaDefaultOverride);
187 }
188 $dataHandler->start($commandArray, []);
189 $dataHandler->process_datamap();
190 BackendUtility::setUpdateSignal('updatePageTree');
191 }
192
193 return $pagesCreated;
194 }
195
196 /**
197 * Page selector type data
198 *
199 * @param int $pageUid Page Uid
200 * @return array
201 */
202 protected function getTypeSelectData(int $pageUid)
203 {
204 $tsConfig = BackendUtility::getPagesTSconfig($pageUid);
205 $pagesTsConfig = $tsConfig['TCEFORM.']['pages.'] ?? [];
206
207 // Find all available doktypes for the current user
208 $types = $GLOBALS['PAGES_TYPES'];
209 unset($types['default']);
210 $types = array_keys($types);
211 $types[] = 1; // default
212 $types[] = 3; // link
213 $types[] = 4; // shortcut
214 $types[] = 7; // mount point
215 $types[] = 199; // spacer
216
217 if (!$this->getBackendUser()->isAdmin() && isset($this->getBackendUser()->groupData['pagetypes_select'])) {
218 $types = GeneralUtility::trimExplode(',', $this->getBackendUser()->groupData['pagetypes_select'], true);
219 }
220 $removeItems = isset($pagesTsConfig['doktype.']['removeItems']) ? GeneralUtility::trimExplode(',', $pagesTsConfig['doktype.']['removeItems'], true) : [];
221 $allowedDoktypes = array_diff($types, $removeItems);
222
223 // All doktypes in the TCA
224 $availableDoktypes = $GLOBALS['TCA']['pages']['columns']['doktype']['config']['items'];
225
226 // Sort by group and allowedDoktypes
227 $groupedData = [];
228 $groupLabel = '';
229 foreach ($availableDoktypes as $doktypeData) {
230 // If it is a group, save the group label for the children underneath
231 if ($doktypeData[1] === '--div--') {
232 $groupLabel = $doktypeData[0];
233 } else {
234 if (in_array($doktypeData[1], $allowedDoktypes)) {
235 $groupedData[$groupLabel][] = $doktypeData;
236 }
237 }
238 }
239
240 return $groupedData;
241 }
242
243 /**
244 * Get a list of sub pages with some all fields from given page.
245 * Fetch all data fields for full page icon display
246 *
247 * @param int $pageUid Get sub pages from this pages
248 * @return array
249 */
250 protected function getSubPagesOfPage(int $pageUid): array
251 {
252 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
253 $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
254 return $queryBuilder->select('*')
255 ->from('pages')
256 ->where(
257 $queryBuilder->expr()->eq(
258 'pid',
259 $queryBuilder->createNamedParameter($pageUid, \PDO::PARAM_INT)
260 )
261 )
262 ->orderBy('sorting')
263 ->execute()
264 ->fetchAll();
265 }
266
267 /**
268 * Returns LanguageService
269 *
270 * @return LanguageService
271 */
272 protected function getLanguageService()
273 {
274 return $GLOBALS['LANG'];
275 }
276
277 /**
278 * Returns current BE user
279 *
280 * @return BackendUserAuthentication
281 */
282 protected function getBackendUser()
283 {
284 return $GLOBALS['BE_USER'];
285 }
286 }