PageLayoutController.php 49.6 KB
Newer Older
1
<?php
2
declare(strict_types = 1);
3
4
namespace TYPO3\CMS\Backend\Controller;

5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
9
10
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
11
 *
12
13
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
16
 * The TYPO3 project - inspiring people to share!
 */
17

18
19
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
20
use TYPO3\CMS\Backend\Module\ModuleLoader;
21
use TYPO3\CMS\Backend\Routing\UriBuilder;
22
23
use TYPO3\CMS\Backend\Template\Components\ButtonBar;
use TYPO3\CMS\Backend\Template\ModuleTemplate;
Nicole Cordes's avatar
Nicole Cordes committed
24
use TYPO3\CMS\Backend\Utility\BackendUtility;
25
26
use TYPO3\CMS\Backend\View\BackendLayoutView;
use TYPO3\CMS\Backend\View\PageLayoutView;
27
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
28
use TYPO3\CMS\Core\Database\ConnectionPool;
29
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
30
31
use TYPO3\CMS\Core\Database\Query\Restriction\BackendWorkspaceRestriction;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
32
use TYPO3\CMS\Core\Database\Query\Restriction\WorkspaceRestriction;
33
use TYPO3\CMS\Core\Domain\Repository\PageRepository;
34
use TYPO3\CMS\Core\Http\HtmlResponse;
35
36
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
37
use TYPO3\CMS\Core\Localization\LanguageService;
38
use TYPO3\CMS\Core\Page\PageRenderer;
39
use TYPO3\CMS\Core\Site\Entity\SiteInterface;
40
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
41
use TYPO3\CMS\Core\Type\Bitmask\Permission;
Nicole Cordes's avatar
Nicole Cordes committed
42
43
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
44
use TYPO3\CMS\Core\Versioning\VersionState;
45
use TYPO3\CMS\Fluid\View\StandaloneView;
46
use TYPO3\CMS\Fluid\ViewHelpers\Be\InfoboxViewHelper;
Nicole Cordes's avatar
Nicole Cordes committed
47

48
49
50
/**
 * Script Class for Web > Layout module
 */
51
52
53
54
55
56
class PageLayoutController
{
    /**
     * Page Id for which to make the listing
     *
     * @var int
57
     * @internal
58
59
60
61
62
63
64
65
     */
    public $id;

    /**
     * Pointer - for browsing list of records.
     *
     * @var int
     */
66
    protected $pointer;
67
68
69
70
71
72

    /**
     * Thumbnails or not
     *
     * @var string
     */
73
    protected $imagemode;
74
75
76
77
78
79

    /**
     * Search-fields
     *
     * @var string
     */
80
    protected $search_field;
81
82
83
84
85
86

    /**
     * Search-levels
     *
     * @var int
     */
87
    protected $search_levels;
88
89
90
91
92
93

    /**
     * Show-limit
     *
     * @var int
     */
94
    protected $showLimit;
95
96
97
98
99
100

    /**
     * Return URL
     *
     * @var string
     */
101
    protected $returnUrl;
102
103
104
105
106
107

    /**
     * PopView id - for opening a window with the page
     *
     * @var bool
     */
108
    protected $popView;
109
110
111
112
113
114

    /**
     * Page select perms clause
     *
     * @var string
     */
115
    protected $perms_clause;
116
117
118
119
120
121

    /**
     * Module TSconfig
     *
     * @var array
     */
122
    protected $modTSconfig = [];
123
124
125
126
127
128

    /**
     * Module shared TSconfig
     *
     * @var array
     */
129
    protected $modSharedTSconfig = [];
130
131
132
133
134

    /**
     * Current ids page record
     *
     * @var array
135
     * @internal
136
137
138
139
140
141
142
143
     */
    public $pageinfo;

    /**
     * "Pseudo" Description -table name
     *
     * @var string
     */
144
    protected $descrTable;
145
146
147
148
149
150

    /**
     * List of column-integers to edit. Is set from TSconfig, default is "1,0,2,3"
     *
     * @var string
     */
151
    protected $colPosList;
152
153
154
155
156
157

    /**
     * Flag: If content can be edited or not.
     *
     * @var bool
     */
158
    protected $EDIT_CONTENT;
159
160
161
162
163
164

    /**
     * Users permissions integer for this page.
     *
     * @var int
     */
165
    protected $CALC_PERMS;
166
167
168
169
170
171

    /**
     * Currently selected language for editing content elements
     *
     * @var int
     */
172
    protected $current_sys_language;
173
174
175
176
177
178

    /**
     * Module configuration
     *
     * @var array
     */
179
    protected $MCONF = [];
180
181
182
183
184
185

    /**
     * Menu configuration
     *
     * @var array
     */
186
    protected $MOD_MENU = [];
187
188
189
190
191

    /**
     * Module settings (session variable)
     *
     * @var array
192
     * @internal
193
     */
194
    public $MOD_SETTINGS = [];
195
196
197
198
199
200

    /**
     * Module output accumulation
     *
     * @var string
     */
201
    protected $content;
202
203
204
205
206
207
208

    /**
     * List of column-integers accessible to the current BE user.
     * Is set from TSconfig, default is $colPosList
     *
     * @var string
     */
209
    protected $activeColPosList;
210
211
212
213
214
215

    /**
     * @var string
     */
    protected $editSelect;

216
217
218
219
220
    /**
     * Caches the available languages in a colPos
     *
     * @var array
     */
221
    protected $languagesInColumnCache = [];
222

223
224
225
226
227
228
229
230
231
232
233
234
    /**
     * @var IconFactory
     */
    protected $iconFactory;

    /**
     * The name of the module
     *
     * @var string
     */
    protected $moduleName = 'web_layout';

235
236
237
238
239
240
241
242
243
244
    /**
     * @var ModuleTemplate
     */
    protected $moduleTemplate;

    /**
     * @var ButtonBar
     */
    protected $buttonBar;

245
246
247
248
249
    /**
     * @var string
     */
    protected $searchContent;

250
251
252
253
254
    /**
     * @var SiteLanguage[]
     */
    protected $availableLanguages;

255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
    /**
     * Injects the request object for the current request or subrequest
     * As this controller goes only through the main() method, it is rather simple for now
     *
     * @param ServerRequestInterface $request the current request
     * @return ResponseInterface the response with the content
     */
    public function mainAction(ServerRequestInterface $request): ResponseInterface
    {
        $GLOBALS['SOBE'] = $this;
        $this->init($request);
        $this->main($request);
        return new HtmlResponse($this->moduleTemplate->renderContent());
    }

270
271
    /**
     * Initializing the module
272
     * @param ServerRequestInterface $request
273
     */
274
    protected function init(ServerRequestInterface $request): void
275
    {
276
277
278
279
        // Set the GPvars from outside
        $parsedBody = $request->getParsedBody();
        $queryParams = $request->getQueryParams();

280
        $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
281
        $this->iconFactory = $this->moduleTemplate->getIconFactory();
282
        $this->buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
283
284
285
        $this->getLanguageService()->includeLLFile('EXT:backend/Resources/Private/Language/locallang_layout.xlf');
        // Setting module configuration / page select clause
        $this->MCONF['name'] = $this->moduleName;
286
        $this->perms_clause = $this->getBackendUser()->getPagePermsClause(Permission::PAGE_SHOW);
287
        // Get session data
288
        $sessionData = $this->getBackendUser()->getSessionData(__CLASS__);
289
        $this->search_field = !empty($sessionData['search_field']) ? $sessionData['search_field'] : '';
290
291
292
293
294
295
296
297
298
299
300

        $this->id = (int)($parsedBody['id'] ?? $queryParams['id'] ?? 0);
        $this->pointer = $parsedBody['pointer'] ?? $queryParams['pointer'] ?? null;
        $this->imagemode = $parsedBody['imagemode'] ?? $queryParams['imagemode'] ?? null;
        $this->popView = $parsedBody['popView'] ?? $queryParams['popView'] ?? null;
        $this->search_field = $parsedBody['search_field'] ?? $queryParams['search_field'] ?? null;
        $this->search_levels = $parsedBody['search_levels'] ?? $queryParams['search_levels'] ?? null;
        $this->showLimit = $parsedBody['showLimit'] ?? $queryParams['showLimit'] ?? null;
        $returnUrl = $parsedBody['returnUrl'] ?? $queryParams['returnUrl'] ?? null;
        $this->returnUrl = GeneralUtility::sanitizeLocalUrl($returnUrl);

301
302
        $sessionData['search_field'] = $this->search_field;
        // Store session data
303
        $this->getBackendUser()->setAndSaveSessionData(__CLASS__, $sessionData);
304
305
306
        // Load page info array:
        $this->pageinfo = BackendUtility::readPageAccess($this->id, $this->perms_clause);
        // Initialize menu
307
        $this->menuConfig($request);
308
309
310
311
312
313
314
315
        // Setting sys language from session var:
        $this->current_sys_language = (int)$this->MOD_SETTINGS['language'];
        // CSH / Descriptions:
        $this->descrTable = '_MOD_' . $this->moduleName;
    }

    /**
     * Initialize menu array
316
     * @param ServerRequestInterface $request
317
     */
318
    protected function menuConfig(ServerRequestInterface $request): void
319
    {
320
321
322
323
        // Set the GPvars from outside
        $parsedBody = $request->getParsedBody();
        $queryParams = $request->getQueryParams();

324
325
        /** @var SiteInterface $currentSite */
        $currentSite = $request->getAttribute('site');
326
        $this->availableLanguages = $currentSite->getAvailableLanguages($this->getBackendUser(), false, $this->id);
327

328
329
        $lang = $this->getLanguageService();
        // MENU-ITEMS:
330
        $this->MOD_MENU = [
331
            'tt_content_showHidden' => '',
332
            'function' => [
333
334
                1 => $lang->getLL('m_function_1'),
                2 => $lang->getLL('m_function_2')
335
336
            ],
            'language' => [
337
                0 => $lang->getLL('m_default')
338
339
            ]
        ];
340
        // initialize page/be_user TSconfig settings
341
342
343
        $pageTsConfig = BackendUtility::getPagesTSconfig($this->id);
        $this->modSharedTSconfig['properties'] = $pageTsConfig['mod.']['SHARED.'] ?? [];
        $this->modTSconfig['properties'] = $pageTsConfig['mod.']['web_layout.'] ?? [];
344

345
346
        // First, select all localized page records on the current page.
        // Each represents a possibility for a language on the page. Add these to language selector.
347
        if ($this->id) {
348
349
350
351
352
353
354
355
            // Compile language data for pid != 0 only. The language drop-down is not shown on pid 0
            // since pid 0 can't be localized.
            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
            $queryBuilder->getRestrictions()->removeAll()
                ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
                ->add(GeneralUtility::makeInstance(BackendWorkspaceRestriction::class));
            $statement = $queryBuilder->select('uid', $GLOBALS['TCA']['pages']['ctrl']['languageField'])
                ->from('pages')
356
                ->where(
357
                    $queryBuilder->expr()->eq(
358
                        $GLOBALS['TCA']['pages']['ctrl']['transOrigPointerField'],
359
                        $queryBuilder->createNamedParameter($this->id, \PDO::PARAM_INT)
360
                    )
361
362
363
                )->execute();
            while ($pageTranslation = $statement->fetch()) {
                $languageId = $pageTranslation[$GLOBALS['TCA']['pages']['ctrl']['languageField']];
364
365
                if (isset($this->availableLanguages[$languageId])) {
                    $this->MOD_MENU['language'][$languageId] = $this->availableLanguages[$languageId]->getTitle();
366
                }
367
            }
368
            // Override the label
369
370
            if (isset($this->availableLanguages[0])) {
                $this->MOD_MENU['language'][0] = $this->availableLanguages[0]->getTitle();
371
372
            }
        }
373
        // Initialize the available actions
374
        $actions = $this->initActions();
375
        // Clean up settings
376
        $this->MOD_SETTINGS = BackendUtility::getModuleData($this->MOD_MENU, $parsedBody['SET'] ?? $queryParams['SET'] ?? [], $this->moduleName);
377
        // For all elements to be shown in draft workspaces & to also show hidden elements by default if user hasn't disabled the option
378
379
380
        if ($this->getBackendUser()->workspace != 0
            || !isset($this->MOD_SETTINGS['tt_content_showHidden'])
            || $this->MOD_SETTINGS['tt_content_showHidden'] !== '0'
Pawel Cieslik's avatar
Pawel Cieslik committed
381
        ) {
382
383
            $this->MOD_SETTINGS['tt_content_showHidden'] = 1;
        }
384
385
        // Make action menu from available actions
        $this->makeActionMenu($actions);
386
387
388
    }

    /**
389
     * Initializes the available actions this module provides
390
     *
391
     * @return array the available actions
392
     */
393
    protected function initActions(): array
394
    {
395
        $actions = [
396
            1 => $this->getLanguageService()->getLL('m_function_1')
397
        ];
398
399
400
        // Find if there are ANY languages at all (and if not, do not show the language option from function menu).
        if (count($this->availableLanguages) > 1) {
            $actions[2] = $this->getLanguageService()->getLL('m_function_2');
401
        }
402
        $this->makeLanguageMenu();
403
404
405
406
407
408
409
        // Page / user TSconfig blinding of menu-items
        $blindActions = $this->modTSconfig['properties']['menu.']['functions.'] ?? [];
        foreach ($blindActions as $key => $value) {
            if (!$value && array_key_exists($key, $actions)) {
                unset($actions[$key]);
            }
        }
410
411
412
413
414
415
416
417
418
419

        return $actions;
    }

    /**
     * This creates the dropdown menu with the different actions this module is able to provide.
     * For now they are Columns, Quick Edit and Languages.
     *
     * @param array $actions array with the available actions
     */
420
    protected function makeActionMenu(array $actions): void
421
    {
422
423
424
425
        $actionMenu = $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
        $actionMenu->setIdentifier('actionMenu');
        $actionMenu->setLabel('');

426
        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
427

428
        $defaultKey = null;
429
        $foundDefaultKey = false;
430
        foreach ($actions as $key => $action) {
431
432
433
            $menuItem = $actionMenu
                ->makeMenuItem()
                ->setTitle($action)
434
                ->setHref((string)$uriBuilder->buildUriFromRoute($this->moduleName) . '&id=' . $this->id . '&SET[function]=' . $key);
435

436
            if (!$foundDefaultKey) {
437
                $defaultKey = $key;
438
                $foundDefaultKey = true;
439
            }
440
441
            if ((int)$this->MOD_SETTINGS['function'] === $key) {
                $menuItem->setActive(true);
442
                $defaultKey = null;
443
444
            }
            $actionMenu->addMenuItem($menuItem);
445
        }
446
447
448
        if (isset($defaultKey)) {
            $this->MOD_SETTINGS['function'] = $defaultKey;
        }
449
        $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->addMenu($actionMenu);
450
451
452
453
454
455
456
    }

    /**
     * Generate the flashmessages for current pid
     *
     * @return string HTML content with flashmessages
     */
457
    protected function getHeaderFlashMessagesForCurrentPid(): string
458
459
460
    {
        $content = '';
        $lang = $this->getLanguageService();
461

462
463
464
        $view = GeneralUtility::makeInstance(StandaloneView::class);
        $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates/InfoBox.html'));

465
466
467
468
469
470
471
472
473
        // If page is a folder
        if ($this->pageinfo['doktype'] == PageRepository::DOKTYPE_SYSFOLDER) {
            $moduleLoader = GeneralUtility::makeInstance(ModuleLoader::class);
            $moduleLoader->load($GLOBALS['TBE_MODULES']);
            $modules = $moduleLoader->modules;
            if (is_array($modules['web']['sub']['list'])) {
                $title = $lang->getLL('goToListModule');
                $message = '<p>' . $lang->getLL('goToListModuleMessage') . '</p>';
                $message .= '<a class="btn btn-info" href="javascript:top.goToModule(\'web_list\',1);">' . $lang->getLL('goToListModule') . '</a>';
474
                $view->assignMultiple([
475
476
477
                    'title' => $title,
                    'message' => $message,
                    'state' => InfoboxViewHelper::STATE_INFO
478
                ]);
479
480
                $content .= $view->render();
            }
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
        } elseif ($this->pageinfo['doktype'] === PageRepository::DOKTYPE_SHORTCUT) {
            $shortcutMode = (int)$this->pageinfo['shortcut_mode'];
            $pageRepository = GeneralUtility::makeInstance(PageRepository::class);
            $targetPage = [];

            if ($this->pageinfo['shortcut'] || $shortcutMode) {
                switch ($shortcutMode) {
                    case PageRepository::SHORTCUT_MODE_NONE:
                        $targetPage = $pageRepository->getPage($this->pageinfo['shortcut']);
                        break;
                    case PageRepository::SHORTCUT_MODE_FIRST_SUBPAGE:
                        $targetPage = reset($pageRepository->getMenu($this->pageinfo['shortcut'] ?: $this->pageinfo['uid']));
                        break;
                    case PageRepository::SHORTCUT_MODE_PARENT_PAGE:
                        $targetPage = $pageRepository->getPage($this->pageinfo['pid']);
                        break;
                }

                $message = '';
                if ($shortcutMode === PageRepository::SHORTCUT_MODE_RANDOM_SUBPAGE) {
                    $message .= sprintf($lang->getLL('pageIsRandomInternalLinkMessage'));
                } else {
                    $linkToPid = $this->local_linkThisScript(['id' => $targetPage['uid']]);
                    $path = BackendUtility::getRecordPath($targetPage['uid'], $this->getBackendUser()->getPagePermsClause(Permission::PAGE_SHOW), 1000);
505
                    $linkedPath = '<a href="' . htmlspecialchars($linkToPid) . '">' . htmlspecialchars($path) . '</a>';
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
                    $message .= sprintf($lang->getLL('pageIsInternalLinkMessage'), $linkedPath);
                }

                $message .= ' (' . htmlspecialchars($lang->sL(BackendUtility::getLabelFromItemlist('pages', 'shortcut_mode', $shortcutMode))) . ')';

                $view->assignMultiple([
                    'title' => $this->pageinfo['title'],
                    'message' => $message,
                    'state' => InfoboxViewHelper::STATE_INFO
                ]);
                $content .= $view->render();
            } else {
                if (empty($targetPage) && $shortcutMode !== PageRepository::SHORTCUT_MODE_RANDOM_SUBPAGE) {
                    $view->assignMultiple([
                        'title' => $this->pageinfo['title'],
                        'message' => $lang->getLL('pageIsMisconfiguredInternalLinkMessage'),
                        'state' => InfoboxViewHelper::STATE_ERROR
                    ]);
                    $content .= $view->render();
                }
            }
        } elseif ($this->pageinfo['doktype'] === PageRepository::DOKTYPE_LINK) {
            if (empty($this->pageinfo['url'])) {
                $view->assignMultiple([
                    'title' => $this->pageinfo['title'],
                    'message' => $lang->getLL('pageIsMisconfiguredExternalLinkMessage'),
                    'state' => InfoboxViewHelper::STATE_ERROR
                ]);
                $content .= $view->render();
            } else {
                $externalUrl = htmlspecialchars(GeneralUtility::makeInstance(PageRepository::class)->getExtURL($this->pageinfo));
                if ($externalUrl !== false) {
538
                    $externalUrlHtml = '<a href="' . $externalUrl . '" target="_blank" rel="noopener noreferrer">' . $externalUrl . '</a>';
539
540
541
542
543
544
545
546
                    $view->assignMultiple([
                        'title' => $this->pageinfo['title'],
                        'message' => sprintf($lang->getLL('pageIsExternalLinkMessage'), $externalUrlHtml),
                        'state' => InfoboxViewHelper::STATE_INFO
                    ]);
                    $content .= $view->render();
                }
            }
547
548
549
550
        }
        // If content from different pid is displayed
        if ($this->pageinfo['content_from_pid']) {
            $contentPage = BackendUtility::getRecord('pages', (int)$this->pageinfo['content_from_pid']);
551
            $linkToPid = $this->local_linkThisScript(['id' => $this->pageinfo['content_from_pid']]);
552
            $title = BackendUtility::getRecordTitle('pages', $contentPage);
553
            $link = '<a href="' . htmlspecialchars($linkToPid) . '">' . htmlspecialchars($title) . ' (PID ' . (int)$this->pageinfo['content_from_pid'] . ')</a>';
554
            $message = sprintf($lang->getLL('content_from_pid_title'), $link);
555
            $view->assignMultiple([
556
557
558
                'title' => $title,
                'message' => $message,
                'state' => InfoboxViewHelper::STATE_INFO
559
            ]);
560
            $content .= $view->render();
561
562
563
564
565
566
567
568
569
570
571
        } else {
            $links = $this->getPageLinksWhereContentIsAlsoShownOn($this->pageinfo['uid']);
            if (!empty($links)) {
                $message = sprintf($lang->getLL('content_on_pid_title'), $links);
                $view->assignMultiple([
                    'title' => '',
                    'message' => $message,
                    'state' => InfoboxViewHelper::STATE_INFO
                ]);
                $content .= $view->render();
            }
572
573
574
575
        }
        return $content;
    }

576
577
578
579
580
581
    /**
     * Get all pages with links where the content of a page $pageId is also shown on
     *
     * @param int $pageId
     * @return string
     */
582
    protected function getPageLinksWhereContentIsAlsoShownOn($pageId): string
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
    {
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
        $queryBuilder->getRestrictions()->removeAll();
        $queryBuilder->getRestrictions()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
        $queryBuilder
            ->select('*')
            ->from('pages')
            ->where($queryBuilder->expr()->eq('content_from_pid', $queryBuilder->createNamedParameter($pageId, \PDO::PARAM_INT)));

        $links = [];
        $rows = $queryBuilder->execute()->fetchAll();
        if (!empty($rows)) {
            foreach ($rows as $row) {
                $linkToPid = $this->local_linkThisScript(['id' => $row['uid']]);
                $title = BackendUtility::getRecordTitle('pages', $row);
                $link = '<a href="' . htmlspecialchars($linkToPid) . '">' . htmlspecialchars($title) . ' (PID ' . (int)$row['uid'] . ')</a>';
                $links[] = $link;
            }
        }
        return implode(', ', $links);
    }

605
606
607
    /**
     * @return string $title
     */
608
    protected function getLocalizedPageTitle(): string
609
610
    {
        if ($this->current_sys_language > 0) {
611
            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
612
                ->getQueryBuilderForTable('pages');
613
614
615
616
            $queryBuilder->getRestrictions()
                ->removeAll()
                ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
                ->add(GeneralUtility::makeInstance(BackendWorkspaceRestriction::class));
617
            $localizedPage = $queryBuilder
618
                ->select('*')
619
                ->from('pages')
620
                ->where(
621
                    $queryBuilder->expr()->eq(
622
                        $GLOBALS['TCA']['pages']['ctrl']['transOrigPointerField'],
623
624
                        $queryBuilder->createNamedParameter($this->id, \PDO::PARAM_INT)
                    ),
625
                    $queryBuilder->expr()->eq(
626
                        $GLOBALS['TCA']['pages']['ctrl']['languageField'],
627
628
                        $queryBuilder->createNamedParameter($this->current_sys_language, \PDO::PARAM_INT)
                    )
629
630
631
632
                )
                ->setMaxResults(1)
                ->execute()
                ->fetch();
633
634
            BackendUtility::workspaceOL('pages', $localizedPage);
            return $localizedPage['title'];
635
        }
636
        return $this->pageinfo['title'];
637
638
639
640
641
    }

    /**
     * Main function.
     * Creates some general objects and calls other functions for the main rendering of module content.
642
643
     *
     * @param ServerRequestInterface $request
644
     */
645
    protected function main(ServerRequestInterface $request): void
646
    {
647
        $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Recordlist/ClearCache');
648
649
650
        $lang = $this->getLanguageService();
        // Access check...
        // The page will show only if there is a valid page and if this page may be viewed by the user
651
        $access = is_array($this->pageinfo);
652
653
        // Content
        $content = '';
654
655
656
        if ($this->id && $access) {
            // Initialize permission settings:
            $this->CALC_PERMS = $this->getBackendUser()->calcPerms($this->pageinfo);
657
            $this->EDIT_CONTENT = $this->isContentEditable();
658

659
660
            $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($this->pageinfo);

661
            $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
662

663
664
665
            $this->moduleTemplate->addJavaScriptCode('mainJsFunctions', '
                if (top.fsMod) {
                    top.fsMod.recentIds["web"] = ' . (int)$this->id . ';
666
                    top.fsMod.navFrameHighlightedID["web"] = top.fsMod.currentBank + "_" + ' . (int)$this->id . ';
667
668
669
                }
                ' . ($this->popView ? BackendUtility::viewOnClick($this->id, '', BackendUtility::BEgetRootLine($this->id)) : '') . '
                function deleteRecord(table,id,url) {   //
670
                    window.location.href = ' . GeneralUtility::quoteJSvalue((string)$uriBuilder->buildUriFromRoute('tce_db') . '&cmd[')
671
                                            . ' + table + "][" + id + "][delete]=1&redirect=" + encodeURIComponent(url);
672
673
674
675
                    return false;
                }
            ');

676
            // Find backend layout / columns
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
            $backendLayout = GeneralUtility::callUserFunction(BackendLayoutView::class . '->getSelectedBackendLayout', $this->id, $this);
            if (!empty($backendLayout['__colPosList'])) {
                $this->colPosList = implode(',', $backendLayout['__colPosList']);
            }
            // Removing duplicates, if any
            $this->colPosList = array_unique(GeneralUtility::intExplode(',', $this->colPosList));
            // Accessible columns
            if (isset($this->modSharedTSconfig['properties']['colPos_list']) && trim($this->modSharedTSconfig['properties']['colPos_list']) !== '') {
                $this->activeColPosList = array_unique(GeneralUtility::intExplode(',', trim($this->modSharedTSconfig['properties']['colPos_list'])));
                // Match with the list which is present in the colPosList for the current page
                if (!empty($this->colPosList) && !empty($this->activeColPosList)) {
                    $this->activeColPosList = array_unique(array_intersect(
                        $this->activeColPosList,
                        $this->colPosList
                    ));
                }
            } else {
                $this->activeColPosList = $this->colPosList;
            }
            $this->activeColPosList = implode(',', $this->activeColPosList);
            $this->colPosList = implode(',', $this->colPosList);

699
700
            $content .= $this->getHeaderFlashMessagesForCurrentPid();

701
            // Render the primary module content:
702
            if ($this->MOD_SETTINGS['function'] == 1 || $this->MOD_SETTINGS['function'] == 2) {
703
                $content .= '<form action="' . htmlspecialchars((string)$uriBuilder->buildUriFromRoute($this->moduleName, ['id' => $this->id, 'imagemode' =>  $this->imagemode])) . '" id="PageLayoutController" method="post">';
704
                // Page title
705
                $content .= '<h1 class="t3js-title-inlineedit">' . htmlspecialchars($this->getLocalizedPageTitle()) . '</h1>';
706
                // All other listings
707
                $content .= $this->renderContent();
708
            }
709
710
            $content .= '</form>';
            $content .= $this->searchContent;
711
            // Setting up the buttons for the docheader
712
713
            $this->makeButtons($request);

714
715
716
717
718
719
720
            // Create LanguageMenu
            $this->makeLanguageMenu();
        } else {
            $this->moduleTemplate->addJavaScriptCode(
                'mainJsFunctions',
                'if (top.fsMod) top.fsMod.recentIds["web"] = ' . (int)$this->id . ';'
            );
721
            $content .= '<h1>' . htmlspecialchars($GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename']) . '</h1>';
722
723
            $view = GeneralUtility::makeInstance(StandaloneView::class);
            $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates/InfoBox.html'));
724
            $view->assignMultiple([
725
726
                'title' => $lang->getLL('clickAPage_header'),
                'message' => $lang->getLL('clickAPage_content'),
727
                'state' => InfoboxViewHelper::STATE_INFO
728
            ]);
729
            $content .= $view->render();
730
        }
731
732
        // Set content
        $this->moduleTemplate->setContent($content);
733
734
735
    }

    /**
736
     * Rendering content
737
738
739
     *
     * @return string
     */
740
    protected function renderContent(): string
741
    {
742
        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
743

744
        $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/ContextMenu');
745
746
747
748
749
        $dbList = GeneralUtility::makeInstance(PageLayoutView::class);
        $dbList->thumbs = $this->imagemode;
        $dbList->no_noWrap = 1;
        $dbList->descrTable = $this->descrTable;
        $this->pointer = MathUtility::forceIntegerInRange($this->pointer, 0, 100000);
750
        $dbList->script = (string)$uriBuilder->buildUriFromRoute($this->moduleName);
751
752
753
754
        $dbList->showIcon = 0;
        $dbList->setLMargin = 0;
        $dbList->doEdit = $this->EDIT_CONTENT;
        $dbList->ext_CALC_PERMS = $this->CALC_PERMS;
755
        $dbList->agePrefixes = $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.minutesHoursDaysYears');
756
757
        $dbList->id = $this->id;
        $dbList->nextThree = MathUtility::forceIntegerInRange($this->modTSconfig['properties']['editFieldsAtATime'], 0, 10);
758
759
        $dbList->option_newWizard = empty($this->modTSconfig['properties']['disableNewContentElementWizard']);
        $dbList->defLangBinding = !empty($this->modTSconfig['properties']['defLangBinding']);
760
761
762
763
764
765
766
        if (!$dbList->nextThree) {
            $dbList->nextThree = 1;
        }
        // Create menu for selecting a table to jump to (this is, if more than just pages/tt_content elements are found on the page!)
        // also fills $dbList->activeTables
        $dbList->getTableMenu($this->id);
        // Initialize other variables:
767
768
        $tableOutput = [];
        $tableJSOutput = [];
769
770
771
772
        $CMcounter = 0;
        // Traverse the list of table names which has records on this page (that array is populated
        // by the $dblist object during the function getTableMenu()):
        foreach ($dbList->activeTables as $table => $value) {
773
            $h_func = '';
774
775
776
777
778
779
780
781
            $h_func_b = '';
            if (!isset($dbList->externalTables[$table])) {
                // Boolean: Display up/down arrows and edit icons for tt_content records
                $dbList->tt_contentConfig['showCommands'] = 1;
                // Boolean: Display info-marks or not
                $dbList->tt_contentConfig['showInfo'] = 1;
                // Setting up the tt_content columns to show:
                if (is_array($GLOBALS['TCA']['tt_content']['columns']['colPos']['config']['items'])) {
782
                    $colList = [];
783
784
785
786
787
788
                    $tcaItems = GeneralUtility::callUserFunction(BackendLayoutView::class . '->getColPosListItemsParsed', $this->id, $this);
                    foreach ($tcaItems as $temp) {
                        $colList[] = $temp[1];
                    }
                } else {
                    // ... should be impossible that colPos has no array. But this is the fallback should it make any sense:
789
                    $colList = ['1', '0', '2', '3'];
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
                }
                if ($this->colPosList !== '') {
                    $colList = array_intersect(GeneralUtility::intExplode(',', $this->colPosList), $colList);
                }
                // The order of the rows: Default is left(1), Normal(0), right(2), margin(3)
                $dbList->tt_contentConfig['cols'] = implode(',', $colList);
                $dbList->tt_contentConfig['activeCols'] = $this->activeColPosList;
                $dbList->tt_contentConfig['showHidden'] = $this->MOD_SETTINGS['tt_content_showHidden'];
                $dbList->tt_contentConfig['sys_language_uid'] = (int)$this->current_sys_language;
                // If the function menu is set to "Language":
                if ($this->MOD_SETTINGS['function'] == 2) {
                    $dbList->tt_contentConfig['languageMode'] = 1;
                    $dbList->tt_contentConfig['languageCols'] = $this->MOD_MENU['language'];
                    $dbList->tt_contentConfig['languageColsPointer'] = $this->current_sys_language;
                }
805
806
807
808
809
810
811
812
813
814
815
                // Toggle hidden ContentElements
                $numberOfHiddenElements = $this->getNumberOfHiddenElements($dbList->tt_contentConfig);
                if ($numberOfHiddenElements > 0) {
                    $h_func_b = '
                        <div class="checkbox">
                            <label for="checkTt_content_showHidden">
                                <input type="checkbox" id="checkTt_content_showHidden" class="checkbox" name="SET[tt_content_showHidden]" value="1" ' . ($this->MOD_SETTINGS['tt_content_showHidden'] ? 'checked="checked"' : '') . ' />
                                ' . htmlspecialchars($this->getLanguageService()->getLL('hiddenCE')) . ' (<span class="t3js-hidden-counter">' . $numberOfHiddenElements . '</span>)
                            </label>
                        </div>';
                }
816
817
818
819
820
821
822
823
824
825
826
827
828
            } else {
                if (isset($this->MOD_SETTINGS) && isset($this->MOD_MENU)) {
                    $h_func = BackendUtility::getFuncMenu($this->id, 'SET[' . $table . ']', $this->MOD_SETTINGS[$table], $this->MOD_MENU[$table], '', '');
                }
            }
            // Start the dblist object:
            $dbList->itemsLimitSingleTable = 1000;
            $dbList->start($this->id, $table, $this->pointer, $this->search_field, $this->search_levels, $this->showLimit);
            $dbList->counter = $CMcounter;
            $dbList->ext_function = $this->MOD_SETTINGS['function'];
            // Generate the list of elements here:
            $dbList->generateList();
            // Adding the list content to the tableOutput variable:
829
            $tableOutput[$table] = $h_func . $dbList->HTMLcode . $h_func_b;
830
831
832
833
834
835
836
837
838
839
840
841
842
            // ... and any accumulated JavaScript goes the same way!
            $tableJSOutput[$table] = $dbList->JScode;
            // Increase global counter:
            $CMcounter += $dbList->counter;
            // Reset variables after operation:
            $dbList->HTMLcode = '';
            $dbList->JScode = '';
        }
        // END: traverse tables
        // For Context Sensitive Menus:
        // Init the content
        $content = '';
        // Additional header content
843
844
845
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/db_layout.php']['drawHeaderHook'] ?? [] as $hook) {
            $params = [];
            $content .= GeneralUtility::callUserFunction($hook, $params, $this);
846
847
848
        }
        // Add the content for each table we have rendered (traversing $tableOutput variable)
        foreach ($tableOutput as $table => $output) {
849
            $content .= $output;
850
851
        }
        // Making search form:
852
        if (!$this->modTSconfig['properties']['disableSearchBox'] && ($dbList->counter > 0 || $this->currentPageHasSubPages())) {
853
            $this->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/ToggleSearchToolbox');
854
855
            $toggleSearchFormButton = $this->buttonBar->makeLinkButton()
                ->setClasses('t3js-toggle-search-toolbox')
856
                ->setTitle($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.title.searchIcon'))
857
858
859
                ->setIcon($this->iconFactory->getIcon('actions-search', Icon::SIZE_SMALL))
                ->setHref('#');
            $this->buttonBar->addButton($toggleSearchFormButton, ButtonBar::BUTTON_POSITION_LEFT, 4);
860
            $this->searchContent = $dbList->getSearchBox();
861
862
        }
        // Additional footer content
863
864
865
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/db_layout.php']['drawFooterHook'] ?? [] as $hook) {
            $params = [];
            $content .= GeneralUtility::callUserFunction($hook, $params, $this);
866
867
868
869
        }
        return $content;
    }

870
871
872
    /**
     * @return ModuleTemplate
     */
873
    protected function getModuleTemplate(): ModuleTemplate
874
875
876
877
    {
        return $this->moduleTemplate;
    }

878
879
880
881
882
883
    /***************************
     *
     * Sub-content functions, rendering specific parts of the module content.
     *
     ***************************/
    /**
884
     * This creates the buttons for the modules
885
     * @param ServerRequestInterface $request
886
     */
887
    protected function makeButtons(ServerRequestInterface $request): void
888
    {
889
890
891
892
        if ($this->MOD_SETTINGS['function'] == 1 || $this->MOD_SETTINGS['function'] == 2) {
            // Add CSH (Context Sensitive Help) icon to tool bar
            $contextSensitiveHelpButton = $this->buttonBar->makeHelpButton()
                ->setModuleName($this->descrTable)
893
                ->setFieldName('columns_' . $this->MOD_SETTINGS['function']);
894
895
            $this->buttonBar->addButton($contextSensitiveHelpButton);
        }
896
897
898
        $lang = $this->getLanguageService();
        // View page
        if (!VersionState::cast($this->pageinfo['t3ver_state'])->equals(VersionState::DELETE_PLACEHOLDER)) {
899
            $viewButton = $this->buttonBar->makeLinkButton()
900
                ->setOnClick(BackendUtility::viewOnClick($this->pageinfo['uid'], '', BackendUtility::BEgetRootLine($this->pageinfo['uid'])))
901
                ->setTitle($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.showPage'))
902
                ->setIcon($this->iconFactory->getIcon('actions-view-page', Icon::SIZE_SMALL))
903
904
905
                ->setHref('#');

            $this->buttonBar->addButton($viewButton, ButtonBar::BUTTON_POSITION_LEFT, 3);
906
907
        }
        // Shortcut
908
909
910
911
        $shortcutButton = $this->buttonBar->makeShortcutButton()
            ->setModuleName($this->moduleName)
            ->setGetVariables([
                'id',
912
                'route',
913
914
915
916
917
918
919
920
921
922
                'edit_record',
                'pointer',
                'new_unique_uid',
                'search_field',
                'search_levels',
                'showLimit'
            ])
            ->setSetVariables(array_keys($this->MOD_MENU));
        $this->buttonBar->addButton($shortcutButton);

923
        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
924
        // Cache
925
        if (empty($this->modTSconfig['properties']['disableAdvanced'])) {
926
            $clearCacheButton = $this->buttonBar->makeLinkButton()
927
928
929
                ->setHref('#')
                ->setDataAttributes(['id' => $this->pageinfo['uid']])
                ->setClasses('t3js-clear-page-cache')
930
                ->setTitle($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.clear_cache'))
931
                ->setIcon($this->iconFactory->getIcon('actions-system-cache-clear', Icon::SIZE_SMALL));
932
            $this->buttonBar->addButton($clearCacheButton, ButtonBar::BUTTON_POSITION_RIGHT, 1);
933
        }
934
        if (empty($this->modTSconfig['properties']['disableIconToolbar'])) {
935
            // Edit page properties and page language overlay icons
936
937
938
            if ($this->isPageEditable() && $this->getBackendUser()->checkLanguageAccess(0)) {
                /** @var \TYPO3\CMS\Core\Http\NormalizedParams */
                $normalizedParams = $request->getAttribute('normalizedParams');
939
                // Edit localized pages only when one specific language is selected
940
                if ($this->MOD_SETTINGS['function'] == 1 && $this->current_sys_language > 0) {
941
942
                    $localizationParentField = $GLOBALS['TCA']['pages']['ctrl']['transOrigPointerField'];
                    $languageField = $GLOBALS['TCA']['pages']['ctrl']['languageField'];
943
                    $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
944
                        ->getQueryBuilderForTable('pages');
945
946
947
948
949
950
                    $queryBuilder->getRestrictions()
                        ->removeAll()
                        ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
                        ->add(GeneralUtility::makeInstance(BackendWorkspaceRestriction::class));
                    $overlayRecord = $queryBuilder
                        ->select('uid')
951
                        ->from('pages')
952
                        ->where(
953
                            $queryBuilder->expr()->eq(
954
                                $localizationParentField,
955
956
957
                                $queryBuilder->createNamedParameter($this->id, \PDO::PARAM_INT)
                            ),
                            $queryBuilder->expr()->eq(
958
                                $languageField,
959
960
                                $queryBuilder->createNamedParameter($this->current_sys_language, \PDO::PARAM_INT)
                            )
961
962
963
964
                        )
                        ->setMaxResults(1)
                        ->execute()
                        ->fetch();
965
966
967
                    // Edit button
                    $urlParameters = [
                        'edit' => [
968
                            'pages' => [
969
970
971
                                $overlayRecord['uid'] => 'edit'
                            ]
                        ],
972
                        'returnUrl' => $normalizedParams->getRequestUri(),
973
                    ];
974
975

                    $url = (string)$uriBuilder->buildUriFromRoute('record_edit', $urlParameters);
976
                    $editLanguageButton = $this->buttonBar->makeLinkButton()
977
                        ->setHref($url)
978
                        ->setTitle($lang->getLL('editPageLanguageOverlayProperties'))
979
980
                        ->setIcon($this->iconFactory->getIcon('mimetypes-x-content-page-language-overlay', Icon::SIZE_SMALL));
                    $this->buttonBar->addButton($editLanguageButton, ButtonBar::BUTTON_POSITION_LEFT, 3);
981
                }
982
983
984
985
986
987
                $urlParameters = [
                    'edit' => [
                        'pages' => [
                            $this->id => 'edit'
                        ]
                    ],
988
                    'returnUrl' => $normalizedParams->getRequestUri(),
989
                ];
990
                $url = (string)$uriBuilder->buildUriFromRoute('record_edit', $urlParameters);
991
                $editPageButton = $this->buttonBar->makeLinkButton()
992
                    ->setHref($url)
993
                    ->setTitle($lang->getLL('editPageProperties'))
994
995
                    ->setIcon($this->iconFactory->getIcon('actions-page-open', Icon::SIZE_SMALL));
                $this->buttonBar->addButton($editPageButton, ButtonBar::BUTTON_POSITION_LEFT, 3);
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
            }
        }
    }

    /*******************************
     *
     * Other functions
     *
     ******************************/
    /**
     * Returns the number of hidden elements (including those hidden by start/end times)
     * on the current page (for the current sys_language)
     *
1009
     * @param array $contentConfig
1010
1011
     * @return int
     */
1012
    protected function getNumberOfHiddenElements(array $contentConfig = []): int
1013
    {