-
Notifications
You must be signed in to change notification settings - Fork 638
/
PreviewModule.php
196 lines (180 loc) · 6.31 KB
/
PreviewModule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
declare(strict_types=1);
namespace TYPO3\CMS\Adminpanel\Modules;
/*
* This file is part of the TYPO3 CMS project.
*
* 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.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Adminpanel\Repositories\FrontendGroupsRepository;
use TYPO3\CMS\Core\Type\Bitmask\Permission;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
/**
* Admin Panel Preview Module
*/
class PreviewModule extends AbstractModule
{
/**
* Force the preview panel to be opened
*
* @var bool
*/
protected $forceOpen = false;
/**
* @inheritdoc
*/
public function getAdditionalJavaScriptCode(): string
{
return 'TSFEtypo3FormFieldSet("TSFE_ADMIN_PANEL[preview_simulateDate]", "datetime", "", 0, 0);';
}
/**
* Creates the content for the "preview" section ("module") of the Admin Panel
*
* @return string HTML content for the section. Consists of a string with table-rows with four columns.
* @see display()
* @throws \InvalidArgumentException
*/
public function getContent(): string
{
$view = GeneralUtility::makeInstance(StandaloneView::class);
$templateNameAndPath = $this->extResources . '/Templates/Modules/Preview.html';
$view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($templateNameAndPath));
$view->setPartialRootPaths([$this->extResources . '/Partials']);
$frontendGroupsRepository = GeneralUtility::makeInstance(FrontendGroupsRepository::class);
$view->assignMultiple(
[
'show' => [
'hiddenPages' => $this->getConfigurationOption('showHiddenPages'),
'hiddenRecords' => $this->getConfigurationOption('showHiddenRecords'),
'fluidDebug' => $this->getConfigurationOption('showFluidDebug'),
],
'simulateDate' => $this->getConfigurationOption('simulateDate'),
'frontendUserGroups' => [
'availableGroups' => $frontendGroupsRepository->getAvailableFrontendUserGroups(),
'selected' => $this->getConfigurationOption('simulateUserGroup'),
],
]
);
return $view->render();
}
/**
* @inheritdoc
*/
public function getIdentifier(): string
{
return 'preview';
}
/**
* @inheritdoc
*/
public function getLabel(): string
{
$locallangFileAndPath = 'LLL:' . $this->extResources . '/Language/locallang_preview.xlf:module.label';
return $this->getLanguageService()->sL($locallangFileAndPath);
}
public function initializeModule(): void
{
$this->initializeFrontendPreview();
if (GeneralUtility::_GP('ADMCMD_simUser')) {
$this->getBackendUser()->uc['TSFE_adminConfig']['preview_simulateUserGroup'] = (int)GeneralUtility::_GP(
'ADMCMD_simUser'
);
$this->forceOpen = true;
}
if (GeneralUtility::_GP('ADMCMD_simTime')) {
$this->getBackendUser()->uc['TSFE_adminConfig']['preview_simulateDate'] = (int)GeneralUtility::_GP(
'ADMCMD_simTime'
);
$this->forceOpen = true;
}
}
/**
* Force module to be shown if either time or users/groups are simulated
*
* @return bool
*/
public function isShown(): bool
{
if ($this->forceOpen) {
return true;
}
return parent::isShown();
}
/**
* Clear page cache if fluid debug output is enabled
*
* @param array $input
*/
public function onSubmit(array $input): void
{
if ($input['preview_showFluidDebug'] ?? false) {
$theStartId = (int)$this->getTypoScriptFrontendController()->id;
$this->getTypoScriptFrontendController()
->clearPageCacheContent_pidList(
$this->getBackendUser()->extGetTreeList(
$theStartId,
0,
0,
$this->getBackendUser()->getPagePermsClause(Permission::PAGE_SHOW)
) . $theStartId
);
}
}
/**
* @inheritdoc
*/
public function showFormSubmitButton(): bool
{
return true;
}
/**
* @return TypoScriptFrontendController
*/
protected function getTypoScriptFrontendController(): TypoScriptFrontendController
{
return $GLOBALS['TSFE'];
}
/**
* Initialize frontend preview functionality incl.
* simulation of users or time
*/
protected function initializeFrontendPreview()
{
$tsfe = $this->getTypoScriptFrontendController();
$tsfe->clear_preview();
$tsfe->fePreview = 1;
$tsfe->showHiddenPage = (bool)$this->getConfigurationOption('showHiddenPages');
$tsfe->showHiddenRecords = (bool)$this->getConfigurationOption('showHiddenRecords');
// Simulate date
$simTime = $this->getConfigurationOption('simulateDate');
if ($simTime) {
$GLOBALS['SIM_EXEC_TIME'] = $simTime;
$GLOBALS['SIM_ACCESS_TIME'] = $simTime - $simTime % 60;
}
// simulate user
$tsfe->simUserGroup = $this->getConfigurationOption('simulateUserGroup');
if ($tsfe->simUserGroup) {
if ($tsfe->fe_user->user) {
$tsfe->fe_user->user[$tsfe->fe_user->usergroup_column] = $tsfe->simUserGroup;
} else {
$tsfe->fe_user = GeneralUtility::makeInstance(FrontendUserAuthentication::class);
$tsfe->fe_user->user = [
$tsfe->fe_user->usergroup_column => $tsfe->simUserGroup,
];
}
}
if (!$tsfe->simUserGroup && !$simTime && !$tsfe->showHiddenPage && !$tsfe->showHiddenRecords) {
$tsfe->fePreview = 0;
}
}
}