56e70443fada98ffef8709b8f11e0f597f75b8c9
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / NewRecordController.php
1 <?php
2 namespace TYPO3\CMS\Backend\Controller;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Backend\Module\AbstractModule;
20 use TYPO3\CMS\Backend\Template\Components\ButtonBar;
21 use TYPO3\CMS\Backend\Tree\View\NewRecordPageTreeView;
22 use TYPO3\CMS\Backend\Tree\View\PagePositionMap;
23 use TYPO3\CMS\Backend\Utility\BackendUtility;
24 use TYPO3\CMS\Core\Database\ConnectionPool;
25 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
26 use TYPO3\CMS\Core\Imaging\Icon;
27 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
28 use TYPO3\CMS\Core\Utility\GeneralUtility;
29 use TYPO3\CMS\Core\Utility\HttpUtility;
30 use TYPO3\CMS\Core\Utility\PathUtility;
31 use TYPO3\CMS\Frontend\Page\PageRepository;
32
33 /**
34 * Script class for 'db_new'
35 */
36 class NewRecordController extends AbstractModule
37 {
38 /**
39 * @var array
40 */
41 public $pageinfo;
42
43 /**
44 * @var array
45 */
46 public $pidInfo;
47
48 /**
49 * @var array
50 */
51 protected $newRecordSortList;
52
53 /**
54 * @var int
55 */
56 public $newPagesInto;
57
58 /**
59 * @var int
60 */
61 public $newContentInto;
62
63 /**
64 * @var int
65 */
66 public $newPagesAfter;
67
68 /**
69 * Determines, whether "Select Position" for new page should be shown
70 *
71 * @var bool
72 */
73 protected $newPagesSelectPosition = true;
74
75 /**
76 * @var array
77 */
78 public $web_list_modTSconfig;
79
80 /**
81 * @var array
82 */
83 public $allowedNewTables;
84
85 /**
86 * @var array
87 */
88 public $deniedNewTables;
89
90 /**
91 * @var array
92 */
93 public $web_list_modTSconfig_pid;
94
95 /**
96 * @var array
97 */
98 public $allowedNewTables_pid;
99
100 /**
101 * @var array
102 */
103 public $deniedNewTables_pid;
104
105 /**
106 * @var string
107 */
108 public $code;
109
110 /**
111 * @var string
112 */
113 public $R_URI;
114
115 /**
116 * @var int
117 */
118 public $id;
119
120 /**
121 * @var string
122 */
123 public $returnUrl;
124
125 /**
126 * pagesOnly flag.
127 *
128 * @var int
129 */
130 public $pagesOnly;
131
132 /**
133 * @var string
134 */
135 public $perms_clause;
136
137 /**
138 * Accumulated HTML output
139 *
140 * @var string
141 */
142 public $content;
143
144 /**
145 * @var array
146 */
147 public $tRows;
148
149 /**
150 * Constructor
151 */
152 public function __construct()
153 {
154 parent::__construct();
155 $GLOBALS['SOBE'] = $this;
156 $this->getLanguageService()->includeLLFile('EXT:lang/Resources/Private/Language/locallang_misc.xlf');
157 $this->init();
158 }
159
160 /**
161 * Constructor function for the class
162 */
163 protected function init()
164 {
165 $beUser = $this->getBackendUserAuthentication();
166 // Page-selection permission clause (reading)
167 $this->perms_clause = $beUser->getPagePermsClause(1);
168 // This will hide records from display - it has nothing to do with user rights!!
169 if ($pidList = $beUser->getTSConfigVal('options.hideRecords.pages')) {
170 if (!empty($pidList)) {
171 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
172 ->getQueryBuilderForTable('pages');
173 $this->perms_clause .= ' AND ' . $queryBuilder->expr()->notIn(
174 'pages.uid',
175 GeneralUtility::intExplode(',', $pidList)
176 );
177 }
178 }
179 // Setting GPvars:
180 // The page id to operate from
181 $this->id = (int)GeneralUtility::_GP('id');
182 $this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
183 $this->pagesOnly = GeneralUtility::_GP('pagesOnly');
184 // Setting up the context sensitive menu:
185 $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/ContextMenu');
186 $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/Tooltip');
187 // Creating content
188 $this->content = '';
189 $this->content .= '<h1>'
190 . $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:db_new.php.pagetitle')
191 . '</h1>';
192 // Id a positive id is supplied, ask for the page record with permission information contained:
193 if ($this->id > 0) {
194 $this->pageinfo = BackendUtility::readPageAccess($this->id, $this->perms_clause);
195 }
196 // If a page-record was returned, the user had read-access to the page.
197 if ($this->pageinfo['uid']) {
198 // Get record of parent page
199 $this->pidInfo = BackendUtility::getRecord('pages', $this->pageinfo['pid']);
200 // Checking the permissions for the user with regard to the parent page: Can he create new pages, new
201 // content record, new page after?
202 if ($beUser->doesUserHaveAccess($this->pageinfo, 8)) {
203 $this->newPagesInto = 1;
204 }
205 if ($beUser->doesUserHaveAccess($this->pageinfo, 16)) {
206 $this->newContentInto = 1;
207 }
208 if (($beUser->isAdmin() || is_array($this->pidInfo)) && $beUser->doesUserHaveAccess($this->pidInfo, 8)) {
209 $this->newPagesAfter = 1;
210 }
211 } elseif ($beUser->isAdmin()) {
212 // Admins can do it all
213 $this->newPagesInto = 1;
214 $this->newContentInto = 1;
215 $this->newPagesAfter = 0;
216 } else {
217 // People with no permission can do nothing
218 $this->newPagesInto = 0;
219 $this->newContentInto = 0;
220 $this->newPagesAfter = 0;
221 }
222 }
223
224 /**
225 * Injects the request object for the current request or subrequest
226 * As this controller goes only through the main() method, it is rather simple for now
227 *
228 * @param ServerRequestInterface $request the current request
229 * @param ResponseInterface $response
230 * @return ResponseInterface the response with the content
231 */
232 public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
233 {
234 $this->main();
235
236 $response->getBody()->write($this->moduleTemplate->renderContent());
237 return $response;
238 }
239
240 /**
241 * Main processing, creating the list of new record tables to select from
242 */
243 public function main()
244 {
245 // If there was a page - or if the user is admin (admins has access to the root) we proceed:
246 if (!empty($this->pageinfo['uid']) || $this->getBackendUserAuthentication()->isAdmin()) {
247 if (empty($this->pageinfo)) {
248 // Explicitly pass an empty array to the docHeader
249 $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation([]);
250 } else {
251 $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($this->pageinfo);
252 }
253 // Acquiring TSconfig for this module/current page:
254 $this->web_list_modTSconfig = BackendUtility::getModTSconfig($this->pageinfo['uid'], 'mod.web_list');
255 $this->allowedNewTables = GeneralUtility::trimExplode(
256 ',',
257 $this->web_list_modTSconfig['properties']['allowedNewTables'],
258 true
259 );
260 $this->deniedNewTables = GeneralUtility::trimExplode(
261 ',',
262 $this->web_list_modTSconfig['properties']['deniedNewTables'],
263 true
264 );
265 // Acquiring TSconfig for this module/parent page:
266 $this->web_list_modTSconfig_pid = BackendUtility::getModTSconfig($this->pageinfo['pid'], 'mod.web_list');
267 $this->allowedNewTables_pid = GeneralUtility::trimExplode(
268 ',',
269 $this->web_list_modTSconfig_pid['properties']['allowedNewTables'],
270 true
271 );
272 $this->deniedNewTables_pid = GeneralUtility::trimExplode(
273 ',',
274 $this->web_list_modTSconfig_pid['properties']['deniedNewTables'],
275 true
276 );
277 // More init:
278 if (!$this->showNewRecLink('pages')) {
279 $this->newPagesInto = 0;
280 }
281 if (!$this->showNewRecLink('pages', $this->allowedNewTables_pid, $this->deniedNewTables_pid)) {
282 $this->newPagesAfter = 0;
283 }
284 // Set header-HTML and return_url
285 if (is_array($this->pageinfo) && $this->pageinfo['uid']) {
286 $title = strip_tags($this->pageinfo[$GLOBALS['TCA']['pages']['ctrl']['label']]);
287 } else {
288 $title = $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'];
289 }
290 $this->moduleTemplate->setTitle($title);
291 // GENERATE the HTML-output depending on mode (pagesOnly is the page wizard)
292 // Regular new element:
293 if (!$this->pagesOnly) {
294 $this->regularNew();
295 } elseif ($this->showNewRecLink('pages')) {
296 // Pages only wizard
297 $this->pagesOnly();
298 }
299 // Add all the content to an output section
300 $this->content .= '<div>' . $this->code . '</div>';
301 // Setting up the buttons and markers for docheader
302 $this->getButtons();
303 // Build the <body> for the module
304 $this->moduleTemplate->setContent($this->content);
305 }
306 }
307
308 /**
309 * Create the panel of buttons for submitting the form or otherwise perform operations.
310 */
311 protected function getButtons()
312 {
313 $lang = $this->getLanguageService();
314 $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
315 // Regular new element:
316 if (!$this->pagesOnly) {
317 // New page
318 if ($this->showNewRecLink('pages')) {
319 $newPageButton = $buttonBar->makeLinkButton()
320 ->setHref(GeneralUtility::linkThisScript(['pagesOnly' => '1']))
321 ->setTitle($lang->sL('LLL:EXT:backend/Resources/Private/Language/locallang_layout.xlf:newPage'))
322 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-page-new', Icon::SIZE_SMALL));
323 $buttonBar->addButton($newPageButton, ButtonBar::BUTTON_POSITION_LEFT, 20);
324 }
325 // CSH
326 $cshButton = $buttonBar->makeHelpButton()->setModuleName('xMOD_csh_corebe')->setFieldName('new_regular');
327 $buttonBar->addButton($cshButton);
328 } elseif ($this->showNewRecLink('pages')) {
329 // Pages only wizard
330 // CSH
331 $buttons['csh'] = BackendUtility::cshItem('xMOD_csh_corebe', 'new_pages');
332 $cshButton = $buttonBar->makeHelpButton()->setModuleName('xMOD_csh_corebe')->setFieldName('new_pages');
333 $buttonBar->addButton($cshButton);
334 }
335 // Back
336 if ($this->returnUrl) {
337 $returnButton = $buttonBar->makeLinkButton()
338 ->setHref($this->returnUrl)
339 ->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.goBack'))
340 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-view-go-back', Icon::SIZE_SMALL));
341 $buttonBar->addButton($returnButton, ButtonBar::BUTTON_POSITION_LEFT, 10);
342 }
343
344 if (is_array($this->pageinfo) && $this->pageinfo['uid']) {
345 // View
346 $pagesTSconfig = BackendUtility::getPagesTSconfig($this->pageinfo['uid']);
347 if (isset($pagesTSconfig['TCEMAIN.']['preview.']['disableButtonForDokType'])) {
348 $excludeDokTypes = GeneralUtility::intExplode(
349 ',',
350 $pagesTSconfig['TCEMAIN.']['preview.']['disableButtonForDokType'],
351 true
352 );
353 } else {
354 // exclude sysfolders and recycler by default
355 $excludeDokTypes = [
356 PageRepository::DOKTYPE_RECYCLER,
357 PageRepository::DOKTYPE_SYSFOLDER,
358 PageRepository::DOKTYPE_SPACER
359 ];
360 }
361 if (!in_array((int)$this->pageinfo['doktype'], $excludeDokTypes, true)) {
362 $viewButton = $buttonBar->makeLinkButton()
363 ->setHref('#')
364 ->setOnClick(BackendUtility::viewOnClick(
365 $this->pageinfo['uid'],
366 '',
367 BackendUtility::BEgetRootLine($this->pageinfo['uid'])
368 ))
369 ->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.showPage'))
370 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon(
371 'actions-view-page',
372 Icon::SIZE_SMALL
373 ));
374 $buttonBar->addButton($viewButton, ButtonBar::BUTTON_POSITION_LEFT, 30);
375 }
376 }
377 }
378
379 /**
380 * Creates the position map for pages wizard
381 */
382 public function pagesOnly()
383 {
384 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
385 ->getQueryBuilderForTable('sys_language');
386 $queryBuilder->getRestrictions()
387 ->removeAll()
388 ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
389 $numberOfPages = $queryBuilder
390 ->count('*')
391 ->from('pages')
392 ->execute()
393 ->fetchColumn(0);
394
395 if ($numberOfPages > 0) {
396 $this->code .= '
397 <h3>' . htmlspecialchars($this->getLanguageService()->getLL('selectPosition')) . ':</h3>
398 ';
399 $positionMap = GeneralUtility::makeInstance(PagePositionMap::class, NewRecordPageTreeView::class);
400 /** @var $positionMap \TYPO3\CMS\Backend\Tree\View\PagePositionMap */
401 $this->code .= $positionMap->positionTree(
402 $this->id,
403 $this->pageinfo,
404 $this->perms_clause,
405 $this->returnUrl
406 );
407 } else {
408 // No pages yet, no need to prompt for position, redirect to page creation.
409 $urlParameters = [
410 'edit' => [
411 'pages' => [
412 0 => 'new'
413 ]
414 ],
415 'returnNewPageId' => 1,
416 'returnUrl' => BackendUtility::getModuleUrl('db_new', ['id' => $this->id, 'pagesOnly' => '1'])
417 ];
418 $url = BackendUtility::getModuleUrl('record_edit', $urlParameters);
419 @ob_end_clean();
420 HttpUtility::redirect($url);
421 }
422 }
423
424 /**
425 * Create a regular new element (pages and records)
426 */
427 public function regularNew()
428 {
429 $lang = $this->getLanguageService();
430 // Initialize array for accumulating table rows:
431 $this->tRows = [];
432 // Get TSconfig for current page
433 $pageTS = BackendUtility::getPagesTSconfig($this->id);
434 // Finish initializing new pages options with TSconfig
435 // Each new page option may be hidden by TSconfig
436 // Enabled option for the position of a new page
437 $this->newPagesSelectPosition = !empty(
438 $pageTS['mod.']['wizards.']['newRecord.']['pages.']['show.']['pageSelectPosition']
439 );
440 // Pseudo-boolean (0/1) for backward compatibility
441 $displayNewPagesIntoLink = $this->newPagesInto && !empty($pageTS['mod.']['wizards.']['newRecord.']['pages.']['show.']['pageInside']) ? 1 : 0;
442 $displayNewPagesAfterLink = $this->newPagesAfter && !empty($pageTS['mod.']['wizards.']['newRecord.']['pages.']['show.']['pageAfter']) ? 1 : 0;
443 // Slight spacer from header:
444 $this->code .= '';
445 // New Page
446 $table = 'pages';
447 $v = $GLOBALS['TCA'][$table];
448 $pageIcon = $this->moduleTemplate->getIconFactory()->getIconForRecord(
449 $table,
450 [],
451 Icon::SIZE_SMALL
452 )->render();
453 $newPageIcon = $this->moduleTemplate->getIconFactory()->getIcon('actions-page-new', Icon::SIZE_SMALL)->render();
454 $rowContent = '';
455 // New pages INSIDE this pages
456 $newPageLinks = [];
457 if ($displayNewPagesIntoLink && $this->isTableAllowedForThisPage($this->pageinfo, 'pages') && $this->getBackendUserAuthentication()->check('tables_modify', 'pages') && $this->getBackendUserAuthentication()->workspaceCreateNewRecord(($this->pageinfo['_ORIG_uid'] ?: $this->id), 'pages')) {
458 // Create link to new page inside:
459 $newPageLinks[] = $this->linkWrap($this->moduleTemplate->getIconFactory()->getIconForRecord($table, [], Icon::SIZE_SMALL)->render() . htmlspecialchars($lang->sL($v['ctrl']['title'])) . ' (' . htmlspecialchars($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:db_new.php.inside')) . ')', $table, $this->id);
460 }
461 // New pages AFTER this pages
462 if ($displayNewPagesAfterLink && $this->isTableAllowedForThisPage($this->pidInfo, 'pages') && $this->getBackendUserAuthentication()->check('tables_modify', 'pages') && $this->getBackendUserAuthentication()->workspaceCreateNewRecord($this->pidInfo['uid'], 'pages')) {
463 $newPageLinks[] = $this->linkWrap($pageIcon . htmlspecialchars($lang->sL($v['ctrl']['title'])) . ' (' . htmlspecialchars($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:db_new.php.after')) . ')', 'pages', -$this->id);
464 }
465 // New pages at selection position
466 if ($this->newPagesSelectPosition && $this->showNewRecLink('pages')) {
467 // Link to page-wizard:
468 $newPageLinks[] = '<a href="' . htmlspecialchars(GeneralUtility::linkThisScript(['pagesOnly' => 1])) . '">' . $pageIcon . htmlspecialchars($lang->getLL('pageSelectPosition')) . '</a>';
469 }
470 // Assemble all new page links
471 $numPageLinks = count($newPageLinks);
472 for ($i = 0; $i < $numPageLinks; $i++) {
473 $rowContent .= '<li>' . $newPageLinks[$i] . '</li>';
474 }
475 if ($this->showNewRecLink('pages')) {
476 $rowContent = '<ul class="list-tree"><li>' . $newPageIcon . '<strong>' .
477 $lang->getLL('createNewPage') . '</strong><ul>' . $rowContent . '</ul></li>';
478 } else {
479 $rowContent = '<ul class="list-tree"><li><ul>' . $rowContent . '</li></ul>';
480 }
481 // Compile table row
482 $startRows = [$rowContent];
483 $iconFile = [];
484 // New tables (but not pages) INSIDE this pages
485 $isAdmin = $this->getBackendUserAuthentication()->isAdmin();
486 $newContentIcon = $this->moduleTemplate->getIconFactory()->getIcon('actions-document-new', Icon::SIZE_SMALL)->render();
487 if ($this->newContentInto) {
488 if (is_array($GLOBALS['TCA'])) {
489 $groupName = '';
490 foreach ($GLOBALS['TCA'] as $table => $v) {
491 $rootLevelConfiguration = isset($v['ctrl']['rootLevel']) ? (int)$v['ctrl']['rootLevel'] : 0;
492 if ($table !== 'pages'
493 && $this->showNewRecLink($table)
494 && $this->isTableAllowedForThisPage($this->pageinfo, $table)
495 && $this->getBackendUserAuthentication()->check('tables_modify', $table)
496 && ($rootLevelConfiguration === -1 || ($this->id xor $rootLevelConfiguration))
497 && $this->getBackendUserAuthentication()->workspaceCreateNewRecord(($this->pageinfo['_ORIG_uid'] ? $this->pageinfo['_ORIG_uid'] : $this->id), $table)
498 ) {
499 $newRecordIcon = $this->moduleTemplate->getIconFactory()->getIconForRecord($table, [], Icon::SIZE_SMALL)->render();
500 $rowContent = '';
501 $thisTitle = '';
502 // Create new link for record:
503 $newLink = $this->linkWrap($newRecordIcon . htmlspecialchars($lang->sL($v['ctrl']['title'])), $table, $this->id);
504 // If the table is 'tt_content', create link to wizard
505 if ($table === 'tt_content') {
506 $groupName = $lang->getLL('createNewContent');
507 $rowContent = $newContentIcon . '<strong>' . $lang->getLL('createNewContent') . '</strong><ul>';
508 // If mod.newContentElementWizard.override is set, use that extension's wizard instead:
509 $tsConfig = BackendUtility::getModTSconfig($this->id, 'mod');
510 $moduleName = isset($tsConfig['properties']['newContentElementWizard.']['override'])
511 ? $tsConfig['properties']['newContentElementWizard.']['override']
512 : 'new_content_element';
513 $url = BackendUtility::getModuleUrl($moduleName, ['id' => $this->id, 'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI')]);
514 $rowContent .= '<li>' . $newLink . ' ' . BackendUtility::wrapInHelp($table, '') . '</li><li><a href="' . htmlspecialchars($url) . '">' . $newContentIcon . htmlspecialchars($lang->getLL('clickForWizard')) . '</a></li></ul>';
515 } else {
516 // Get the title
517 if ($v['ctrl']['readOnly'] || $v['ctrl']['hideTable'] || $v['ctrl']['is_static']) {
518 continue;
519 }
520 if ($v['ctrl']['adminOnly'] && !$isAdmin) {
521 continue;
522 }
523 $nameParts = explode('_', $table);
524 $thisTitle = '';
525 $_EXTKEY = '';
526 if ($nameParts[0] === 'tx' || $nameParts[0] === 'tt') {
527 // Try to extract extension name
528 if (substr($v['ctrl']['title'], 0, 8) === 'LLL:EXT:') {
529 $_EXTKEY = substr($v['ctrl']['title'], 8);
530 $_EXTKEY = substr($_EXTKEY, 0, strpos($_EXTKEY, '/'));
531 if ($_EXTKEY !== '') {
532 // First try to get localisation of extension title
533 $temp = explode(':', substr($v['ctrl']['title'], 9 + strlen($_EXTKEY)));
534 $langFile = $temp[0];
535 $thisTitle = $lang->sL('LLL:EXT:' . $_EXTKEY . '/' . $langFile . ':extension.title');
536 // If no localisation available, read title from ext_emconf.php
537 $extPath = ExtensionManagementUtility::extPath($_EXTKEY);
538 $extEmConfFile = $extPath . 'ext_emconf.php';
539 if (!$thisTitle && is_file($extEmConfFile)) {
540 $EM_CONF = [];
541 include $extEmConfFile;
542 $thisTitle = $EM_CONF[$_EXTKEY]['title'];
543 }
544 $iconFile[$_EXTKEY] = '<img src="' . PathUtility::getAbsoluteWebPath(ExtensionManagementUtility::getExtensionIcon($extPath, true)) . '" ' . 'width="16" height="16" ' . 'alt="' . $thisTitle . '" />';
545 }
546 }
547 if (empty($thisTitle)) {
548 $_EXTKEY = $nameParts[1];
549 $thisTitle = $nameParts[1];
550 $iconFile[$_EXTKEY] = '';
551 }
552 } else {
553 if ($table === 'pages_language_overlay' && !$this->checkIfLanguagesExist()) {
554 continue;
555 }
556 $_EXTKEY = 'system';
557 $thisTitle = $lang->getLL('system_records');
558 $iconFile['system'] = $this->moduleTemplate->getIconFactory()->getIcon('apps-pagetree-root', Icon::SIZE_SMALL)->render();
559 }
560
561 if ($groupName === '' || $groupName !== $_EXTKEY) {
562 $groupName = empty($v['ctrl']['groupName']) ? $_EXTKEY : $v['ctrl']['groupName'];
563 }
564 $rowContent .= $newLink;
565 }
566 // Compile table row:
567 if ($table === 'tt_content') {
568 $startRows[] = '<li>' . $rowContent . '</li>';
569 } else {
570 $this->tRows[$groupName]['title'] = $thisTitle;
571 $this->tRows[$groupName]['html'][] = $rowContent;
572 $this->tRows[$groupName]['table'][] = $table;
573 }
574 }
575 }
576 }
577 }
578 // User sort
579 if (isset($pageTS['mod.']['wizards.']['newRecord.']['order'])) {
580 $this->newRecordSortList = GeneralUtility::trimExplode(',', $pageTS['mod.']['wizards.']['newRecord.']['order'], true);
581 }
582 uksort($this->tRows, [$this, 'sortNewRecordsByConfig']);
583 // Compile table row:
584 $finalRows = [];
585 $finalRows[] = implode('', $startRows);
586 foreach ($this->tRows as $key => $value) {
587 $row = '<li>' . $iconFile[$key] . ' <strong>' . $value['title'] . '</strong><ul>';
588 foreach ($value['html'] as $recordKey => $record) {
589 $row .= '<li>' . $record . ' ' . BackendUtility::wrapInHelp($value['table'][$recordKey], '') . '</li>';
590 }
591 $row .= '</ul></li>';
592 $finalRows[] = $row;
593 }
594
595 $finalRows[] = '</ul>';
596 // Make table:
597 $this->code .= implode('', $finalRows);
598 }
599
600 /**
601 * User array sort function used by regularNew
602 *
603 * @param string $a First array element for compare
604 * @param string $b First array element for compare
605 * @return int -1 for lower, 0 for equal, 1 for greater
606 */
607 public function sortNewRecordsByConfig($a, $b)
608 {
609 if (!empty($this->newRecordSortList)) {
610 if (in_array($a, $this->newRecordSortList) && in_array($b, $this->newRecordSortList)) {
611 // Both are in the list, return relative to position in array
612 $sub = array_search($a, $this->newRecordSortList) - array_search($b, $this->newRecordSortList);
613 $ret = ($sub < 0 ? -1 : $sub == 0) ? 0 : 1;
614 } elseif (in_array($a, $this->newRecordSortList)) {
615 // First element is in array, put to top
616 $ret = -1;
617 } elseif (in_array($b, $this->newRecordSortList)) {
618 // Second element is in array, put first to bottom
619 $ret = 1;
620 } else {
621 // No element is in array, return alphabetic order
622 $ret = strnatcasecmp($this->tRows[$a]['title'], $this->tRows[$b]['title']);
623 }
624 return $ret;
625 } else {
626 // Return alphabetic order
627 return strnatcasecmp($this->tRows[$a]['title'], $this->tRows[$b]['title']);
628 }
629 }
630
631 /**
632 * Links the string $code to a create-new form for a record in $table created on page $pid
633 *
634 * @param string $linkText Link text
635 * @param string $table Table name (in which to create new record)
636 * @param int $pid PID value for the "&edit['.$table.']['.$pid.']=new" command (positive/negative)
637 * @param bool $addContentTable If $addContentTable is set, then a new tt_content record is created together with pages
638 * @return string The link.
639 */
640 public function linkWrap($linkText, $table, $pid, $addContentTable = false)
641 {
642 $urlParameters = [
643 'edit' => [
644 $table => [
645 $pid => 'new'
646 ]
647 ],
648 'returnUrl' => $this->returnUrl
649 ];
650 if ($table === 'pages' && $addContentTable) {
651 $urlParameters['tt_content']['prev'] = 'new';
652 $urlParameters['returnNewPageId'] = 1;
653 } elseif ($table === 'pages_language_overlay') {
654 $urlParameters['overrideVals']['pages_language_overlay']['doktype'] = (int)$this->pageinfo['doktype'];
655 }
656 $url = BackendUtility::getModuleUrl('record_edit', $urlParameters);
657 return '<a href="' . htmlspecialchars($url) . '">' . $linkText . '</a>';
658 }
659
660 /**
661 * Returns TRUE if the tablename $checkTable is allowed to be created on the page with record $pid_row
662 *
663 * @param array $pid_row Record for parent page.
664 * @param string $checkTable Table name to check
665 * @return bool Returns TRUE if the tablename $checkTable is allowed to be created on the page with record $pid_row
666 */
667 public function isTableAllowedForThisPage($pid_row, $checkTable)
668 {
669 if (!is_array($pid_row)) {
670 return $this->getBackendUserAuthentication()->isAdmin();
671 }
672 // be_users and be_groups may not be created anywhere but in the root.
673 if ($checkTable === 'be_users' || $checkTable === 'be_groups') {
674 return false;
675 }
676 // Checking doktype:
677 $doktype = (int)$pid_row['doktype'];
678 if (!($allowedTableList = $GLOBALS['PAGES_TYPES'][$doktype]['allowedTables'])) {
679 $allowedTableList = $GLOBALS['PAGES_TYPES']['default']['allowedTables'];
680 }
681 // If all tables or the table is listed as an allowed type, return TRUE
682 if (strstr($allowedTableList, '*') || GeneralUtility::inList($allowedTableList, $checkTable)) {
683 return true;
684 }
685
686 return false;
687 }
688
689 /**
690 * Returns TRUE if:
691 * - $allowedNewTables and $deniedNewTables are empty
692 * - the table is not found in $deniedNewTables and $allowedNewTables is not set or the $table tablename is found in
693 * $allowedNewTables
694 *
695 * If $table tablename is found in $allowedNewTables and $deniedNewTables, $deniedNewTables
696 * has priority over $allowedNewTables.
697 *
698 * @param string $table Table name to test if in allowedTables
699 * @param array $allowedNewTables Array of new tables that are allowed.
700 * @param array $deniedNewTables Array of new tables that are not allowed.
701 *
702 * @return bool Returns TRUE if a link for creating new records should be displayed for $table
703 */
704 public function showNewRecLink($table, array $allowedNewTables = [], array $deniedNewTables = [])
705 {
706 if (!$this->getBackendUserAuthentication()->check('tables_modify', $table)) {
707 return false;
708 }
709
710 $allowedNewTables = $allowedNewTables ?: $this->allowedNewTables;
711 $deniedNewTables = $deniedNewTables ?: $this->deniedNewTables;
712 // No deny/allow tables are set:
713 if (empty($allowedNewTables) && empty($deniedNewTables)) {
714 return true;
715 }
716
717 return !in_array($table, $deniedNewTables) && (empty($allowedNewTables) || in_array($table, $allowedNewTables));
718 }
719
720 /**
721 * Checks if sys_language records are present
722 *
723 * @return bool
724 */
725 protected function checkIfLanguagesExist()
726 {
727 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
728 ->getQueryBuilderForTable('sys_language');
729 $queryBuilder->getRestrictions()->removeAll();
730
731 $count = $queryBuilder
732 ->count('uid')
733 ->from('sys_language')
734 ->execute()
735 ->fetchColumn(0);
736 return (bool)$count;
737 }
738
739 /**
740 * Return language service instance
741 *
742 * @return \TYPO3\CMS\Core\Localization\LanguageService
743 */
744 protected function getLanguageService()
745 {
746 return $GLOBALS['LANG'];
747 }
748
749 /**
750 * Returns the global BackendUserAuthentication object.
751 *
752 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
753 */
754 protected function getBackendUserAuthentication()
755 {
756 return $GLOBALS['BE_USER'];
757 }
758 }