| Total Complexity | 109 |
| Total Lines | 736 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like NewRecordController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use NewRecordController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 49 | class NewRecordController |
||
| 50 | { |
||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $pageinfo = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | protected $pidInfo = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $newRecordSortList; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var int |
||
| 68 | */ |
||
| 69 | protected $newPagesInto; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var int |
||
| 73 | */ |
||
| 74 | protected $newContentInto; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var int |
||
| 78 | */ |
||
| 79 | protected $newPagesAfter; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Determines, whether "Select Position" for new page should be shown |
||
| 83 | * |
||
| 84 | * @var bool |
||
| 85 | */ |
||
| 86 | protected $newPagesSelectPosition = true; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | protected $web_list_modTSconfig; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | protected $allowedNewTables; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var array |
||
| 100 | */ |
||
| 101 | protected $deniedNewTables; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var array |
||
| 105 | */ |
||
| 106 | protected $web_list_modTSconfig_pid; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var array |
||
| 110 | */ |
||
| 111 | protected $allowedNewTables_pid; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var array |
||
| 115 | */ |
||
| 116 | protected $deniedNewTables_pid; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var string |
||
| 120 | */ |
||
| 121 | protected $code; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var int |
||
| 125 | * |
||
| 126 | * @see \TYPO3\CMS\Backend\Tree\View\NewRecordPageTreeView::expandNext() |
||
| 127 | * @internal |
||
| 128 | */ |
||
| 129 | public $id; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var string |
||
| 133 | */ |
||
| 134 | protected $returnUrl; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * pagesOnly flag. |
||
| 138 | * |
||
| 139 | * @var int |
||
| 140 | */ |
||
| 141 | protected $pagesOnly; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @var string |
||
| 145 | */ |
||
| 146 | protected $perms_clause; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Accumulated HTML output |
||
| 150 | * |
||
| 151 | * @var string |
||
| 152 | */ |
||
| 153 | protected $content; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @var array |
||
| 157 | */ |
||
| 158 | protected $tRows; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * ModuleTemplate object |
||
| 162 | * |
||
| 163 | * @var ModuleTemplate |
||
| 164 | */ |
||
| 165 | protected $moduleTemplate; |
||
| 166 | |||
| 167 | protected PageRenderer $pageRenderer; |
||
| 168 | protected IconFactory $iconFactory; |
||
| 169 | protected UriBuilder $uriBuilder; |
||
| 170 | protected ModuleTemplateFactory $moduleTemplateFactory; |
||
| 171 | |||
| 172 | public function __construct( |
||
| 173 | IconFactory $iconFactory, |
||
| 174 | PageRenderer $pageRenderer, |
||
| 175 | UriBuilder $uriBuilder, |
||
| 176 | ModuleTemplateFactory $moduleTemplateFactory |
||
| 177 | ) { |
||
| 178 | $this->iconFactory = $iconFactory; |
||
| 179 | $this->pageRenderer = $pageRenderer; |
||
| 180 | $this->uriBuilder = $uriBuilder; |
||
| 181 | $this->moduleTemplateFactory = $moduleTemplateFactory; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Injects the request object for the current request or subrequest |
||
| 186 | * As this controller goes only through the main() method, it is rather simple for now |
||
| 187 | * |
||
| 188 | * @param ServerRequestInterface $request the current request |
||
| 189 | * @return ResponseInterface the response with the content |
||
| 190 | */ |
||
| 191 | public function mainAction(ServerRequestInterface $request): ResponseInterface |
||
| 192 | { |
||
| 193 | $this->init($request); |
||
| 194 | $response = $this->renderContent($request); |
||
| 195 | |||
| 196 | if (empty($response)) { |
||
| 197 | $response = new HtmlResponse($this->moduleTemplate->renderContent()); |
||
| 198 | } |
||
| 199 | |||
| 200 | return $response; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Constructor function for the class |
||
| 205 | * |
||
| 206 | * @param ServerRequestInterface $request |
||
| 207 | */ |
||
| 208 | protected function init(ServerRequestInterface $request): void |
||
| 209 | { |
||
| 210 | $this->moduleTemplate = $this->moduleTemplateFactory->create($request); |
||
| 211 | $this->getLanguageService()->includeLLFile('EXT:core/Resources/Private/Language/locallang_misc.xlf'); |
||
| 212 | $beUser = $this->getBackendUserAuthentication(); |
||
| 213 | // Page-selection permission clause (reading) |
||
| 214 | $this->perms_clause = $beUser->getPagePermsClause(Permission::PAGE_SHOW); |
||
| 215 | // This will hide records from display - it has nothing to do with user rights!! |
||
| 216 | $pidList = $beUser->getTSConfig()['options.']['hideRecords.']['pages'] ?? ''; |
||
| 217 | if (!empty($pidList)) { |
||
| 218 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 219 | ->getQueryBuilderForTable('pages'); |
||
| 220 | $this->perms_clause .= ' AND ' . $queryBuilder->expr()->notIn( |
||
| 221 | 'pages.uid', |
||
| 222 | GeneralUtility::intExplode(',', $pidList) |
||
| 223 | ); |
||
| 224 | } |
||
| 225 | // Setting GPvars: |
||
| 226 | $parsedBody = $request->getParsedBody(); |
||
| 227 | $queryParams = $request->getQueryParams(); |
||
| 228 | // The page id to operate from |
||
| 229 | $this->id = (int)($parsedBody['id'] ?? $queryParams['id'] ?? 0); |
||
| 230 | $this->returnUrl = GeneralUtility::sanitizeLocalUrl($parsedBody['returnUrl'] ?? $queryParams['returnUrl'] ?? ''); |
||
| 231 | $this->pagesOnly = $parsedBody['pagesOnly'] ?? $queryParams['pagesOnly'] ?? null; |
||
| 232 | // Setting up the context sensitive menu: |
||
| 233 | $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/ContextMenu'); |
||
| 234 | $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/Tooltip'); |
||
| 235 | $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/PageActions'); |
||
| 236 | // Creating content |
||
| 237 | $this->content = ''; |
||
| 238 | $this->content .= '<h1>' |
||
| 239 | . $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:db_new.php.pagetitle') |
||
| 240 | . '</h1>'; |
||
| 241 | // Id a positive id is supplied, ask for the page record with permission information contained: |
||
| 242 | if ($this->id > 0) { |
||
| 243 | $this->pageinfo = BackendUtility::readPageAccess($this->id, $this->perms_clause) ?: []; |
||
| 244 | } |
||
| 245 | // If a page-record was returned, the user had read-access to the page. |
||
| 246 | if ($this->pageinfo['uid']) { |
||
| 247 | // Get record of parent page |
||
| 248 | $this->pidInfo = BackendUtility::getRecord('pages', $this->pageinfo['pid']) ?: []; |
||
| 249 | // Checking the permissions for the user with regard to the parent page: Can he create new pages, new |
||
| 250 | // content record, new page after? |
||
| 251 | if ($beUser->doesUserHaveAccess($this->pageinfo, 8)) { |
||
| 252 | $this->newPagesInto = 1; |
||
| 253 | } |
||
| 254 | if ($beUser->doesUserHaveAccess($this->pageinfo, 16)) { |
||
| 255 | $this->newContentInto = 1; |
||
| 256 | } |
||
| 257 | if (($beUser->isAdmin() || !empty($this->pidInfo)) && $beUser->doesUserHaveAccess($this->pidInfo, 8)) { |
||
| 258 | $this->newPagesAfter = 1; |
||
| 259 | } |
||
| 260 | } elseif ($beUser->isAdmin()) { |
||
| 261 | // Admins can do it all |
||
| 262 | $this->newPagesInto = 1; |
||
| 263 | $this->newContentInto = 1; |
||
| 264 | $this->newPagesAfter = 0; |
||
| 265 | } else { |
||
| 266 | // People with no permission can do nothing |
||
| 267 | $this->newPagesInto = 0; |
||
| 268 | $this->newContentInto = 0; |
||
| 269 | $this->newPagesAfter = 0; |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Main processing, creating the list of new record tables to select from |
||
| 275 | * |
||
| 276 | * @param ServerRequestInterface $request |
||
| 277 | * @return ResponseInterface|null |
||
| 278 | */ |
||
| 279 | protected function renderContent(ServerRequestInterface $request): ?ResponseInterface |
||
| 280 | { |
||
| 281 | // If there was a page - or if the user is admin (admins has access to the root) we proceed: |
||
| 282 | if (!empty($this->pageinfo['uid']) || $this->getBackendUserAuthentication()->isAdmin()) { |
||
| 283 | $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($this->pageinfo); |
||
| 284 | // Acquiring TSconfig for this module/current page: |
||
| 285 | $this->web_list_modTSconfig = BackendUtility::getPagesTSconfig($this->pageinfo['uid'])['mod.']['web_list.'] ?? []; |
||
| 286 | $this->allowedNewTables = GeneralUtility::trimExplode(',', $this->web_list_modTSconfig['allowedNewTables'] ?? '', true); |
||
| 287 | $this->deniedNewTables = GeneralUtility::trimExplode(',', $this->web_list_modTSconfig['deniedNewTables'] ?? '', true); |
||
| 288 | // Acquiring TSconfig for this module/parent page: |
||
| 289 | $this->web_list_modTSconfig_pid = BackendUtility::getPagesTSconfig($this->pageinfo['pid'])['mod.']['web_list.'] ?? []; |
||
| 290 | $this->allowedNewTables_pid = GeneralUtility::trimExplode(',', $this->web_list_modTSconfig_pid['allowedNewTables'] ?? '', true); |
||
| 291 | $this->deniedNewTables_pid = GeneralUtility::trimExplode(',', $this->web_list_modTSconfig_pid['deniedNewTables'] ?? '', true); |
||
| 292 | // More init: |
||
| 293 | if (!$this->isRecordCreationAllowedForTable('pages')) { |
||
| 294 | $this->newPagesInto = 0; |
||
| 295 | } |
||
| 296 | if (!$this->isRecordCreationAllowedForTable('pages', $this->allowedNewTables_pid, $this->deniedNewTables_pid)) { |
||
| 297 | $this->newPagesAfter = 0; |
||
| 298 | } |
||
| 299 | // Set header-HTML and return_url |
||
| 300 | if ($this->pageinfo['uid'] ?? false) { |
||
| 301 | $title = strip_tags($this->pageinfo[$GLOBALS['TCA']['pages']['ctrl']['label']]); |
||
| 302 | } else { |
||
| 303 | $title = $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename']; |
||
| 304 | } |
||
| 305 | $this->moduleTemplate->setTitle($title); |
||
| 306 | // GENERATE the HTML-output depending on mode (pagesOnly is the page wizard) |
||
| 307 | // Regular new element: |
||
| 308 | if (!$this->pagesOnly) { |
||
| 309 | $this->renderNewRecordControls($request); |
||
| 310 | } elseif ($this->isRecordCreationAllowedForTable('pages')) { |
||
| 311 | // Pages only wizard |
||
| 312 | $response = $this->renderPositionTree(); |
||
| 313 | |||
| 314 | if (!empty($response)) { |
||
| 315 | return $response; |
||
| 316 | } |
||
| 317 | } |
||
| 318 | // Add all the content to an output section |
||
| 319 | $this->content .= '<div>' . $this->code . '</div>'; |
||
| 320 | // Setting up the buttons and markers for docheader |
||
| 321 | $this->getButtons(); |
||
| 322 | // Build the <body> for the module |
||
| 323 | $this->moduleTemplate->setContent($this->content); |
||
| 324 | } |
||
| 325 | |||
| 326 | return null; |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Create the panel of buttons for submitting the form or otherwise perform operations. |
||
| 331 | */ |
||
| 332 | protected function getButtons(): void |
||
| 333 | { |
||
| 334 | $buttons = []; |
||
| 335 | $lang = $this->getLanguageService(); |
||
| 336 | $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar(); |
||
| 337 | // Regular new element: |
||
| 338 | if (!$this->pagesOnly) { |
||
| 339 | // New page |
||
| 340 | if ($this->isRecordCreationAllowedForTable('pages')) { |
||
| 341 | $newPageButton = $buttonBar->makeLinkButton() |
||
| 342 | ->setHref(GeneralUtility::linkThisScript(['pagesOnly' => '1'])) |
||
| 343 | ->setTitle($lang->sL('LLL:EXT:backend/Resources/Private/Language/locallang_layout.xlf:newPage')) |
||
| 344 | ->setIcon($this->iconFactory->getIcon('actions-page-new', Icon::SIZE_SMALL)); |
||
| 345 | $buttonBar->addButton($newPageButton, ButtonBar::BUTTON_POSITION_LEFT, 20); |
||
| 346 | } |
||
| 347 | // CSH |
||
| 348 | $cshButton = $buttonBar->makeHelpButton()->setModuleName('xMOD_csh_corebe')->setFieldName('new_regular'); |
||
| 349 | $buttonBar->addButton($cshButton); |
||
| 350 | } elseif ($this->isRecordCreationAllowedForTable('pages')) { |
||
| 351 | // Pages only wizard |
||
| 352 | // CSH |
||
| 353 | $buttons['csh'] = BackendUtility::cshItem('xMOD_csh_corebe', 'new_pages'); |
||
| 354 | $cshButton = $buttonBar->makeHelpButton()->setModuleName('xMOD_csh_corebe')->setFieldName('new_pages'); |
||
| 355 | $buttonBar->addButton($cshButton); |
||
| 356 | } |
||
| 357 | // Back |
||
| 358 | if ($this->returnUrl) { |
||
| 359 | $returnButton = $buttonBar->makeLinkButton() |
||
| 360 | ->setHref($this->returnUrl) |
||
| 361 | ->setTitle($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.goBack')) |
||
| 362 | ->setIcon($this->iconFactory->getIcon('actions-view-go-back', Icon::SIZE_SMALL)); |
||
| 363 | $buttonBar->addButton($returnButton, ButtonBar::BUTTON_POSITION_LEFT, 10); |
||
| 364 | } |
||
| 365 | |||
| 366 | if (is_array($this->pageinfo) && $this->pageinfo['uid']) { |
||
| 367 | // View |
||
| 368 | $pagesTSconfig = BackendUtility::getPagesTSconfig($this->pageinfo['uid']); |
||
| 369 | if (isset($pagesTSconfig['TCEMAIN.']['preview.']['disableButtonForDokType'])) { |
||
| 370 | $excludeDokTypes = GeneralUtility::intExplode( |
||
| 371 | ',', |
||
| 372 | $pagesTSconfig['TCEMAIN.']['preview.']['disableButtonForDokType'], |
||
| 373 | true |
||
| 374 | ); |
||
| 375 | } else { |
||
| 376 | // exclude sysfolders and recycler by default |
||
| 377 | $excludeDokTypes = [ |
||
| 378 | PageRepository::DOKTYPE_RECYCLER, |
||
| 379 | PageRepository::DOKTYPE_SYSFOLDER, |
||
| 380 | PageRepository::DOKTYPE_SPACER |
||
| 381 | ]; |
||
| 382 | } |
||
| 383 | if (!in_array((int)$this->pageinfo['doktype'], $excludeDokTypes, true)) { |
||
| 384 | $previewDataAttributes = PreviewUriBuilder::create((int)$this->pageinfo['uid']) |
||
| 385 | ->withRootLine(BackendUtility::BEgetRootLine($this->pageinfo['uid'])) |
||
| 386 | ->buildDispatcherDataAttributes(); |
||
| 387 | $viewButton = $buttonBar->makeLinkButton() |
||
| 388 | ->setHref('#') |
||
| 389 | ->setDataAttributes($previewDataAttributes ?? []) |
||
| 390 | ->setTitle($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.showPage')) |
||
| 391 | ->setIcon($this->iconFactory->getIcon( |
||
| 392 | 'actions-view-page', |
||
| 393 | Icon::SIZE_SMALL |
||
| 394 | )); |
||
| 395 | $buttonBar->addButton($viewButton, ButtonBar::BUTTON_POSITION_LEFT, 30); |
||
| 396 | } |
||
| 397 | } |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Renders the position map for pages wizard |
||
| 402 | * |
||
| 403 | * @return ResponseInterface|null |
||
| 404 | */ |
||
| 405 | protected function renderPositionTree(): ?ResponseInterface |
||
| 406 | { |
||
| 407 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 408 | ->getQueryBuilderForTable('pages'); |
||
| 409 | $queryBuilder->getRestrictions() |
||
| 410 | ->removeAll() |
||
| 411 | ->add(GeneralUtility::makeInstance(DeletedRestriction::class)); |
||
| 412 | $numberOfPages = $queryBuilder |
||
| 413 | ->count('*') |
||
| 414 | ->from('pages') |
||
| 415 | ->execute() |
||
| 416 | ->fetchColumn(0); |
||
| 417 | |||
| 418 | if ($numberOfPages > 0) { |
||
| 419 | $this->code .= '<h3>' . htmlspecialchars($this->getLanguageService()->getLL('selectPosition')) . ':</h3>'; |
||
| 420 | $positionMap = GeneralUtility::makeInstance(PagePositionMap::class, NewRecordPageTreeView::class); |
||
| 421 | $this->code .= $positionMap->positionTree( |
||
| 422 | $this->id, |
||
| 423 | $this->pageinfo, |
||
| 424 | $this->perms_clause, |
||
| 425 | $this->returnUrl |
||
| 426 | ); |
||
| 427 | } else { |
||
| 428 | // No pages yet, no need to prompt for position, redirect to page creation. |
||
| 429 | $urlParameters = [ |
||
| 430 | 'edit' => [ |
||
| 431 | 'pages' => [ |
||
| 432 | 0 => 'new' |
||
| 433 | ] |
||
| 434 | ], |
||
| 435 | 'returnNewPageId' => 1, |
||
| 436 | 'returnUrl' => (string)$this->uriBuilder->buildUriFromRoute('db_new', ['id' => $this->id, 'pagesOnly' => '1']) |
||
| 437 | ]; |
||
| 438 | $url = (string)$this->uriBuilder->buildUriFromRoute('record_edit', $urlParameters); |
||
| 439 | @ob_end_clean(); |
||
| 440 | |||
| 441 | return new RedirectResponse($url); |
||
| 442 | } |
||
| 443 | |||
| 444 | return null; |
||
| 445 | } |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Render controls for creating a regular new element (pages or records) |
||
| 449 | * |
||
| 450 | * @param ServerRequestInterface $request |
||
| 451 | */ |
||
| 452 | protected function renderNewRecordControls(ServerRequestInterface $request): void |
||
| 453 | { |
||
| 454 | $lang = $this->getLanguageService(); |
||
| 455 | // Initialize array for accumulating table rows: |
||
| 456 | $this->tRows = []; |
||
| 457 | // Get TSconfig for current page |
||
| 458 | $pageTS = BackendUtility::getPagesTSconfig($this->id); |
||
| 459 | // Finish initializing new pages options with TSconfig |
||
| 460 | // Each new page option may be hidden by TSconfig |
||
| 461 | // Enabled option for the position of a new page |
||
| 462 | $this->newPagesSelectPosition = !empty($pageTS['mod.']['wizards.']['newRecord.']['pages.']['show.']['pageSelectPosition']); |
||
| 463 | // Pseudo-boolean (0/1) for backward compatibility |
||
| 464 | $displayNewPagesIntoLink = $this->newPagesInto && !empty($pageTS['mod.']['wizards.']['newRecord.']['pages.']['show.']['pageInside']); |
||
| 465 | $displayNewPagesAfterLink = $this->newPagesAfter && !empty($pageTS['mod.']['wizards.']['newRecord.']['pages.']['show.']['pageAfter']); |
||
| 466 | // Slight spacer from header: |
||
| 467 | $this->code .= ''; |
||
| 468 | // New Page |
||
| 469 | $table = 'pages'; |
||
| 470 | $v = $GLOBALS['TCA'][$table]; |
||
| 471 | $pageIcon = $this->iconFactory->getIconForRecord( |
||
| 472 | $table, |
||
| 473 | [], |
||
| 474 | Icon::SIZE_SMALL |
||
| 475 | )->render(); |
||
| 476 | $newPageIcon = $this->iconFactory->getIcon('actions-page-new', Icon::SIZE_SMALL)->render(); |
||
| 477 | $rowContent = ''; |
||
| 478 | // New pages INSIDE this pages |
||
| 479 | $newPageLinks = []; |
||
| 480 | if ($displayNewPagesIntoLink |
||
| 481 | && $this->isTableAllowedOnPage('pages', $this->pageinfo) |
||
| 482 | && $this->getBackendUserAuthentication()->check('tables_modify', 'pages') |
||
| 483 | && $this->getBackendUserAuthentication()->workspaceCanCreateNewRecord('pages') |
||
| 484 | ) { |
||
| 485 | // Create link to new page inside: |
||
| 486 | $recordIcon = $this->iconFactory->getIconForRecord($table, [], Icon::SIZE_SMALL)->render(); |
||
| 487 | $newPageLinks[] = $this->renderLink( |
||
| 488 | $recordIcon . htmlspecialchars($lang->sL($v['ctrl']['title'])) . ' (' . htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:db_new.php.inside')) . ')', |
||
| 489 | $table, |
||
| 490 | $this->id |
||
| 491 | ); |
||
| 492 | } |
||
| 493 | // New pages AFTER this pages |
||
| 494 | if ($displayNewPagesAfterLink |
||
| 495 | && $this->isTableAllowedOnPage('pages', $this->pidInfo) |
||
| 496 | && $this->getBackendUserAuthentication()->check('tables_modify', 'pages') |
||
| 497 | && $this->getBackendUserAuthentication()->workspaceCanCreateNewRecord('pages') |
||
| 498 | ) { |
||
| 499 | $newPageLinks[] = $this->renderLink( |
||
| 500 | $pageIcon . htmlspecialchars($lang->sL($v['ctrl']['title'])) . ' (' . htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:db_new.php.after')) . ')', |
||
| 501 | 'pages', |
||
| 502 | -$this->id |
||
| 503 | ); |
||
| 504 | } |
||
| 505 | // New pages at selection position |
||
| 506 | if ($this->newPagesSelectPosition && $this->isRecordCreationAllowedForTable('pages')) { |
||
| 507 | // Link to page-wizard: |
||
| 508 | $newPageLinks[] = '<a href="' . htmlspecialchars(GeneralUtility::linkThisScript(['pagesOnly' => 1])) . '">' . $pageIcon . htmlspecialchars($lang->getLL('pageSelectPosition')) . '</a>'; |
||
| 509 | } |
||
| 510 | // Assemble all new page links |
||
| 511 | $numPageLinks = count($newPageLinks); |
||
| 512 | for ($i = 0; $i < $numPageLinks; $i++) { |
||
| 513 | $rowContent .= '<li>' . $newPageLinks[$i] . '</li>'; |
||
| 514 | } |
||
| 515 | if ($this->isRecordCreationAllowedForTable('pages')) { |
||
| 516 | $rowContent = '<ul class="list-tree"><li>' . $newPageIcon . '<strong>' . |
||
| 517 | $lang->getLL('createNewPage') . '</strong><ul>' . $rowContent . '</ul></li>'; |
||
| 518 | } else { |
||
| 519 | $rowContent = '<ul class="list-tree"><li><ul>' . $rowContent . '</li></ul>'; |
||
| 520 | } |
||
| 521 | // Compile table row |
||
| 522 | $startRows = [$rowContent]; |
||
| 523 | $iconFile = []; |
||
| 524 | // New tables (but not pages) INSIDE this pages |
||
| 525 | $isAdmin = $this->getBackendUserAuthentication()->isAdmin(); |
||
| 526 | $newContentIcon = $this->iconFactory->getIcon('actions-document-new', Icon::SIZE_SMALL)->render(); |
||
| 527 | if ($this->newContentInto) { |
||
| 528 | if (is_array($GLOBALS['TCA'])) { |
||
| 529 | $groupName = ''; |
||
| 530 | foreach ($GLOBALS['TCA'] as $table => $v) { |
||
| 531 | /** @var string $table */ |
||
| 532 | $rootLevelConfiguration = isset($v['ctrl']['rootLevel']) ? (int)$v['ctrl']['rootLevel'] : 0; |
||
| 533 | if ($table !== 'pages' |
||
| 534 | && $this->isRecordCreationAllowedForTable($table) |
||
| 535 | && $this->isTableAllowedOnPage($table, $this->pageinfo) |
||
| 536 | && $this->getBackendUserAuthentication()->check('tables_modify', $table) |
||
| 537 | && ($rootLevelConfiguration === -1 || ($this->id xor $rootLevelConfiguration)) |
||
| 538 | && $this->getBackendUserAuthentication()->workspaceCanCreateNewRecord($table) |
||
| 539 | ) { |
||
| 540 | $newRecordIcon = $this->iconFactory->getIconForRecord($table, [], Icon::SIZE_SMALL)->render(); |
||
| 541 | $rowContent = ''; |
||
| 542 | $thisTitle = ''; |
||
| 543 | // Create new link for record: |
||
| 544 | $newLink = $this->renderLink( |
||
| 545 | $newRecordIcon . htmlspecialchars($lang->sL($v['ctrl']['title'])), |
||
| 546 | $table, |
||
| 547 | $this->id |
||
| 548 | ); |
||
| 549 | // If the table is 'tt_content', add link to wizard |
||
| 550 | if ($table === 'tt_content') { |
||
| 551 | $groupName = $lang->getLL('createNewContent'); |
||
| 552 | $rowContent = $newContentIcon |
||
| 553 | . '<strong>' . $lang->getLL('createNewContent') . '</strong>' |
||
| 554 | . '<ul>'; |
||
| 555 | // If mod.newContentElementWizard.override is set, use that extension's wizard instead: |
||
| 556 | $moduleName = BackendUtility::getPagesTSconfig($this->id)['mod.']['newContentElementWizard.']['override'] |
||
| 557 | ?? 'new_content_element_wizard'; |
||
| 558 | /** @var \TYPO3\CMS\Core\Http\NormalizedParams */ |
||
| 559 | $normalizedParams = $request->getAttribute('normalizedParams'); |
||
| 560 | $url = (string)$this->uriBuilder->buildUriFromRoute($moduleName, ['id' => $this->id, 'returnUrl' => $normalizedParams->getRequestUri()]); |
||
| 561 | $title = htmlspecialchars($this->getLanguageService()->getLL('newContentElement')); |
||
| 562 | $rowContent .= '<li>' . $newLink . ' ' . BackendUtility::wrapInHelp($table, '') . '</li>' |
||
| 563 | . '<li>' |
||
| 564 | . '<a href="' . htmlspecialchars($url) . '" title="' . $title . '" data-title="' . $title . '" class="t3js-toggle-new-content-element-wizard disabled">' |
||
| 565 | . $newContentIcon . htmlspecialchars($lang->getLL('clickForWizard')) |
||
| 566 | . '</a>' |
||
| 567 | . '</li>' |
||
| 568 | . '</ul>'; |
||
| 569 | } else { |
||
| 570 | // Get the title |
||
| 571 | if ($v['ctrl']['readOnly'] || $v['ctrl']['hideTable'] || $v['ctrl']['is_static']) { |
||
| 572 | continue; |
||
| 573 | } |
||
| 574 | if ($v['ctrl']['adminOnly'] && !$isAdmin) { |
||
| 575 | continue; |
||
| 576 | } |
||
| 577 | $nameParts = explode('_', $table); |
||
| 578 | $thisTitle = ''; |
||
| 579 | $_EXTKEY = ''; |
||
| 580 | if ($nameParts[0] === 'tx' || $nameParts[0] === 'tt') { |
||
| 581 | // Try to extract extension name |
||
| 582 | $title = (string)($v['ctrl']['title'] ?? ''); |
||
| 583 | if (strpos($title, 'LLL:EXT:') === 0) { |
||
| 584 | $_EXTKEY = substr($title, 8); |
||
| 585 | $_EXTKEY = substr($_EXTKEY, 0, (int)strpos($_EXTKEY, '/')); |
||
| 586 | if ($_EXTKEY !== '') { |
||
| 587 | // First try to get localisation of extension title |
||
| 588 | $temp = explode(':', substr($title, 9 + strlen($_EXTKEY))); |
||
| 589 | $langFile = $temp[0]; |
||
| 590 | $thisTitle = $lang->sL('LLL:EXT:' . $_EXTKEY . '/' . $langFile . ':extension.title'); |
||
| 591 | // If no localisation available, read title from ext_emconf.php |
||
| 592 | $extPath = ExtensionManagementUtility::extPath($_EXTKEY); |
||
| 593 | $extEmConfFile = $extPath . 'ext_emconf.php'; |
||
| 594 | if (!$thisTitle && is_file($extEmConfFile)) { |
||
| 595 | $EM_CONF = []; |
||
| 596 | include $extEmConfFile; |
||
| 597 | $thisTitle = $EM_CONF[$_EXTKEY]['title']; |
||
| 598 | } |
||
| 599 | $iconFile[$_EXTKEY] = '<img src="' . PathUtility::getAbsoluteWebPath(ExtensionManagementUtility::getExtensionIcon($extPath, true)) . '" width="16" height="16" alt="' . $thisTitle . '" />'; |
||
| 600 | } |
||
| 601 | } |
||
| 602 | if (empty($thisTitle)) { |
||
| 603 | $_EXTKEY = $nameParts[1]; |
||
| 604 | $thisTitle = $nameParts[1]; |
||
| 605 | $iconFile[$_EXTKEY] = ''; |
||
| 606 | } |
||
| 607 | } else { |
||
| 608 | $_EXTKEY = 'system'; |
||
| 609 | $thisTitle = $lang->getLL('system_records'); |
||
| 610 | $iconFile['system'] = $this->iconFactory->getIcon('apps-pagetree-root', Icon::SIZE_SMALL)->render(); |
||
| 611 | } |
||
| 612 | |||
| 613 | if ($groupName === '' || $groupName !== $_EXTKEY) { |
||
| 614 | $groupName = empty($v['ctrl']['groupName']) ? $_EXTKEY : $v['ctrl']['groupName']; |
||
| 615 | } |
||
| 616 | $rowContent .= $newLink; |
||
| 617 | } |
||
| 618 | // Compile table row: |
||
| 619 | if ($table === 'tt_content') { |
||
| 620 | $startRows[] = '<li>' . $rowContent . '</li>'; |
||
| 621 | } else { |
||
| 622 | $this->tRows[$groupName]['title'] = $thisTitle; |
||
| 623 | $this->tRows[$groupName]['html'][] = $rowContent; |
||
| 624 | $this->tRows[$groupName]['table'][] = $table; |
||
| 625 | } |
||
| 626 | } |
||
| 627 | } |
||
| 628 | } |
||
| 629 | } |
||
| 630 | // User sort |
||
| 631 | if (isset($pageTS['mod.']['wizards.']['newRecord.']['order'])) { |
||
| 632 | $this->newRecordSortList = GeneralUtility::trimExplode(',', $pageTS['mod.']['wizards.']['newRecord.']['order'], true); |
||
| 633 | } |
||
| 634 | uksort($this->tRows, [$this, 'sortTableRows']); |
||
| 635 | // Compile table row: |
||
| 636 | $finalRows = []; |
||
| 637 | $finalRows[] = implode('', $startRows); |
||
| 638 | foreach ($this->tRows as $key => $value) { |
||
| 639 | $row = '<li>' . $iconFile[$key] . ' <strong>' . $value['title'] . '</strong><ul>'; |
||
| 640 | foreach ($value['html'] as $recordKey => $record) { |
||
| 641 | $row .= '<li>' . $record . ' ' . BackendUtility::wrapInHelp($value['table'][$recordKey], '') . '</li>'; |
||
| 642 | } |
||
| 643 | $row .= '</ul></li>'; |
||
| 644 | $finalRows[] = $row; |
||
| 645 | } |
||
| 646 | |||
| 647 | $finalRows[] = '</ul>'; |
||
| 648 | // Make table: |
||
| 649 | $this->code .= implode('', $finalRows); |
||
| 650 | } |
||
| 651 | |||
| 652 | /** |
||
| 653 | * User array sort function used by renderNewRecordControls |
||
| 654 | * |
||
| 655 | * @param string $a First array element for compare |
||
| 656 | * @param string $b First array element for compare |
||
| 657 | * @return int -1 for lower, 0 for equal, 1 for greater |
||
| 658 | */ |
||
| 659 | protected function sortTableRows(string $a, string $b): int |
||
| 680 | } |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Links the string $code to a create-new form for a record in $table created on page $pid |
||
| 684 | * |
||
| 685 | * @param string $linkText Link text |
||
| 686 | * @param string $table Table name (in which to create new record) |
||
| 687 | * @param int $pid PID value for the "&edit['.$table.']['.$pid.']=new" command (positive/negative) |
||
| 688 | * @param bool $addContentTable If $addContentTable is set, then a new tt_content record is created together with pages |
||
| 689 | * @return string The link. |
||
| 690 | */ |
||
| 691 | protected function renderLink(string $linkText, string $table, int $pid, bool $addContentTable = false): string |
||
| 692 | { |
||
| 693 | $urlParameters = [ |
||
| 694 | 'edit' => [ |
||
| 695 | $table => [ |
||
| 696 | $pid => 'new' |
||
| 697 | ] |
||
| 698 | ], |
||
| 699 | 'returnUrl' => $this->returnUrl |
||
| 700 | ]; |
||
| 701 | |||
| 702 | if ($table === 'pages' && $addContentTable) { |
||
| 703 | $urlParameters['tt_content']['prev'] = 'new'; |
||
| 704 | $urlParameters['returnNewPageId'] = 1; |
||
| 705 | } |
||
| 706 | |||
| 707 | return '<a href="' . htmlspecialchars((string)$this->uriBuilder->buildUriFromRoute('record_edit', $urlParameters)) . '">' . $linkText . '</a>'; |
||
| 708 | } |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Returns TRUE if the tablename $checkTable is allowed to be created on the page with record $pid_row |
||
| 712 | * |
||
| 713 | * @param string $table Table name to check |
||
| 714 | * @param array $page Potential parent page |
||
| 715 | * @return bool Returns TRUE if the tablename $table is allowed to be created on the $page |
||
| 716 | */ |
||
| 717 | protected function isTableAllowedOnPage(string $table, array $page): bool |
||
| 737 | } |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Returns whether the record link should be shown for a table |
||
| 741 | * |
||
| 742 | * Returns TRUE if: |
||
| 743 | * - $allowedNewTables and $deniedNewTables are empty |
||
| 744 | * - the table is not found in $deniedNewTables and $allowedNewTables is not set or the $table tablename is found in |
||
| 745 | * $allowedNewTables |
||
| 746 | * |
||
| 747 | * If $table tablename is found in $allowedNewTables and $deniedNewTables, |
||
| 748 | * $deniedNewTables has priority over $allowedNewTables. |
||
| 749 | * |
||
| 750 | * @param string $table Table name to test if in allowedTables |
||
| 751 | * @param array $allowedNewTables Array of new tables that are allowed. |
||
| 752 | * @param array $deniedNewTables Array of new tables that are not allowed. |
||
| 753 | * @return bool Returns TRUE if a link for creating new records should be displayed for $table |
||
| 754 | */ |
||
| 755 | protected function isRecordCreationAllowedForTable(string $table, array $allowedNewTables = [], array $deniedNewTables = []): bool |
||
| 756 | { |
||
| 757 | if (!$this->getBackendUserAuthentication()->check('tables_modify', $table)) { |
||
| 758 | return false; |
||
| 759 | } |
||
| 760 | |||
| 761 | $allowedNewTables = $allowedNewTables ?: $this->allowedNewTables; |
||
| 762 | $deniedNewTables = $deniedNewTables ?: $this->deniedNewTables; |
||
| 763 | // No deny/allow tables are set: |
||
| 764 | if (empty($allowedNewTables) && empty($deniedNewTables)) { |
||
| 765 | return true; |
||
| 766 | } |
||
| 767 | |||
| 768 | return !in_array($table, $deniedNewTables) && (empty($allowedNewTables) || in_array($table, $allowedNewTables)); |
||
| 769 | } |
||
| 770 | |||
| 771 | /** |
||
| 772 | * @return LanguageService |
||
| 773 | */ |
||
| 774 | protected function getLanguageService(): LanguageService |
||
| 775 | { |
||
| 776 | return $GLOBALS['LANG']; |
||
| 777 | } |
||
| 778 | |||
| 779 | /** |
||
| 780 | * @return BackendUserAuthentication |
||
| 781 | */ |
||
| 782 | protected function getBackendUserAuthentication(): BackendUserAuthentication |
||
| 785 | } |
||
| 786 | } |
||
| 787 |