[TASK] Create own response instance in controller actions
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / ContentElement / MoveElementController.php
1 <?php
2 namespace TYPO3\CMS\Backend\Controller\ContentElement;
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\Template\ModuleTemplate;
20 use TYPO3\CMS\Backend\Tree\View\ContentMovingPagePositionMap;
21 use TYPO3\CMS\Backend\Tree\View\PageMovingPagePositionMap;
22 use TYPO3\CMS\Backend\Utility\BackendUtility;
23 use TYPO3\CMS\Core\Http\HtmlResponse;
24 use TYPO3\CMS\Core\Imaging\Icon;
25 use TYPO3\CMS\Core\Type\Bitmask\Permission;
26 use TYPO3\CMS\Core\Utility\GeneralUtility;
27 use TYPO3\CMS\Fluid\View\StandaloneView;
28
29 /**
30 * Script Class for rendering the move-element wizard display
31 */
32 class MoveElementController
33 {
34 /**
35 * @var int
36 */
37 public $sys_language = 0;
38
39 /**
40 * @var int
41 */
42 public $page_id;
43
44 /**
45 * @var string
46 */
47 public $table;
48
49 /**
50 * @var string
51 */
52 public $R_URI;
53
54 /**
55 * @var int
56 */
57 public $input_moveUid;
58
59 /**
60 * @var int
61 */
62 public $moveUid;
63
64 /**
65 * @var int
66 */
67 public $makeCopy;
68
69 /**
70 * Pages-select clause
71 *
72 * @var string
73 */
74 public $perms_clause;
75
76 /**
77 * Content for module accumulated here.
78 *
79 * @var string
80 */
81 public $content;
82
83 /**
84 * ModuleTemplate object
85 *
86 * @var ModuleTemplate
87 */
88 protected $moduleTemplate;
89
90 /**
91 * Constructor
92 */
93 public function __construct()
94 {
95 $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
96 $this->getLanguageService()->includeLLFile('EXT:lang/Resources/Private/Language/locallang_misc.xlf');
97 $GLOBALS['SOBE'] = $this;
98 $this->init();
99 }
100
101 /**
102 * Constructor, initializing internal variables.
103 */
104 public function init()
105 {
106 // Setting internal vars:
107 $this->sys_language = (int)GeneralUtility::_GP('sys_language');
108 $this->page_id = (int)GeneralUtility::_GP('uid');
109 $this->table = GeneralUtility::_GP('table');
110 $this->R_URI = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
111 $this->input_moveUid = GeneralUtility::_GP('moveUid');
112 $this->moveUid = $this->input_moveUid ? $this->input_moveUid : $this->page_id;
113 $this->makeCopy = GeneralUtility::_GP('makeCopy');
114 // Select-pages where clause for read-access:
115 $this->perms_clause = $this->getBackendUser()->getPagePermsClause(Permission::PAGE_SHOW);
116 }
117
118 /**
119 * Injects the request object for the current request or subrequest
120 * As this controller goes only through the main() method, it is rather simple for now
121 *
122 * @param ServerRequestInterface $request the current request
123 * @return ResponseInterface the response with the content
124 */
125 public function mainAction(ServerRequestInterface $request): ResponseInterface
126 {
127 $this->main();
128 $this->moduleTemplate->setContent($this->content);
129 return new HtmlResponse($this->moduleTemplate->renderContent());
130 }
131
132 /**
133 * Creating the module output.
134 */
135 public function main()
136 {
137 $lang = $this->getLanguageService();
138 if ($this->page_id) {
139 $assigns = [];
140 $backendUser = $this->getBackendUser();
141 $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/Tooltip');
142 // Get record for element:
143 $elRow = BackendUtility::getRecordWSOL($this->table, $this->moveUid);
144 // Headerline: Icon, record title:
145 $assigns['table'] = $this->table;
146 $assigns['elRow'] = $elRow;
147 $assigns['recordTooltip'] = BackendUtility::getRecordToolTip($elRow, $this->table);
148 $assigns['recordTitle'] = BackendUtility::getRecordTitle($this->table, $elRow, true);
149 // Make-copy checkbox (clicking this will reload the page with the GET var makeCopy set differently):
150 $onClick = 'window.location.href=' . GeneralUtility::quoteJSvalue(GeneralUtility::linkThisScript(['makeCopy' => !$this->makeCopy])) . ';';
151 $assigns['makeCopyChecked'] = $this->makeCopy ? ' checked="checked"' : '';
152 $assigns['makeCopyOnClick'] = $onClick;
153 // IF the table is "pages":
154 if ((string)$this->table === 'pages') {
155 // Get page record (if accessible):
156 $pageInfo = BackendUtility::readPageAccess($this->page_id, $this->perms_clause);
157 if (is_array($pageInfo) && $backendUser->isInWebMount($pageInfo['pid'], $this->perms_clause)) {
158 // Initialize the position map:
159 $posMap = GeneralUtility::makeInstance(PageMovingPagePositionMap::class);
160 $posMap->moveOrCopy = $this->makeCopy ? 'copy' : 'move';
161 // Print a "go-up" link IF there is a real parent page (and if the user has read-access to that page).
162 if ($pageInfo['pid']) {
163 $pidPageInfo = BackendUtility::readPageAccess($pageInfo['pid'], $this->perms_clause);
164 if (is_array($pidPageInfo)) {
165 if ($backendUser->isInWebMount($pidPageInfo['pid'], $this->perms_clause)) {
166 $assigns['pages']['goUpUrl'] = GeneralUtility::linkThisScript([
167 'uid' => (int)$pageInfo['pid'],
168 'moveUid' => $this->moveUid
169 ]);
170 } else {
171 $assigns['pages']['pidPageInfo'] = $pidPageInfo;
172 }
173 $assigns['pages']['pidRecordTitle'] = BackendUtility::getRecordTitle('pages', $pidPageInfo, true);
174 }
175 }
176 // Create the position tree:
177 $assigns['pages']['positionTree'] = $posMap->positionTree($this->page_id, $pageInfo, $this->perms_clause, $this->R_URI);
178 }
179 }
180 // IF the table is "tt_content":
181 if ((string)$this->table === 'tt_content') {
182 // First, get the record:
183 $tt_content_rec = BackendUtility::getRecord('tt_content', $this->moveUid);
184 // ?
185 if (!$this->input_moveUid) {
186 $this->page_id = $tt_content_rec['pid'];
187 }
188 // Checking if the parent page is readable:
189 $pageInfo = BackendUtility::readPageAccess($this->page_id, $this->perms_clause);
190 if (is_array($pageInfo) && $backendUser->isInWebMount($pageInfo['pid'], $this->perms_clause)) {
191 // Initialize the position map:
192 $posMap = GeneralUtility::makeInstance(ContentMovingPagePositionMap::class);
193 $posMap->moveOrCopy = $this->makeCopy ? 'copy' : 'move';
194 $posMap->cur_sys_language = $this->sys_language;
195 // Headerline for the parent page: Icon, record title:
196 $assigns['ttContent']['pageInfo'] = $pageInfo;
197 $assigns['ttContent']['recordTooltip'] = BackendUtility::getRecordToolTip($pageInfo, 'pages');
198 $assigns['ttContent']['recordTitle'] = BackendUtility::getRecordTitle('pages', $pageInfo, true);
199 // Load SHARED page-TSconfig settings and retrieve column list from there, if applicable:
200 // SHARED page-TSconfig settings.
201 // $modTSconfig_SHARED = BackendUtility::getModTSconfig($this->pageId, 'mod.SHARED');
202 $colPosArray = GeneralUtility::callUserFunction(\TYPO3\CMS\Backend\View\BackendLayoutView::class . '->getColPosListItemsParsed', $this->page_id, $this);
203 $colPosIds = [];
204 foreach ($colPosArray as $colPos) {
205 $colPosIds[] = $colPos[1];
206 }
207 // Removing duplicates, if any
208 $colPosList = implode(',', array_unique($colPosIds));
209 // Adding parent page-header and the content element columns from position-map:
210 $assigns['ttContent']['contentElementColumns'] = $posMap->printContentElementColumns($this->page_id, $this->moveUid, $colPosList, 1, $this->R_URI);
211 // Print a "go-up" link IF there is a real parent page (and if the user has read-access to that page).
212 if ($pageInfo['pid']) {
213 $pidPageInfo = BackendUtility::readPageAccess($pageInfo['pid'], $this->perms_clause);
214 if (is_array($pidPageInfo)) {
215 if ($backendUser->isInWebMount($pidPageInfo['pid'], $this->perms_clause)) {
216 $assigns['ttContent']['goUpUrl'] = GeneralUtility::linkThisScript([
217 'uid' => (int)$pageInfo['pid'],
218 'moveUid' => $this->moveUid
219 ]);
220 } else {
221 $assigns['ttContent']['pidPageInfo'] = $pidPageInfo;
222 }
223 $assigns['ttContent']['pidRecordTitle'] = BackendUtility::getRecordTitle('pages', $pidPageInfo, true);
224 }
225 }
226 // Create the position tree (for pages):
227 $assigns['ttContent']['positionTree'] = $posMap->positionTree($this->page_id, $pageInfo, $this->perms_clause, $this->R_URI);
228 }
229 }
230 // Rendering of the output via fluid
231 $view = GeneralUtility::makeInstance(StandaloneView::class);
232 $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates')]);
233 $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials')]);
234 $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
235 'EXT:backend/Resources/Private/Templates/ContentElement/MoveElement.html'
236 ));
237 $view->assignMultiple($assigns);
238 $this->content .= $view->render();
239 }
240 // Setting up the buttons and markers for docheader
241 $this->getButtons();
242 // Build the <body> for the module
243 $this->moduleTemplate->setTitle($lang->getLL('movingElement'));
244 }
245
246 /**
247 * Create the panel of buttons for submitting the form or otherwise perform operations.
248 */
249 protected function getButtons()
250 {
251 $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
252 if ($this->page_id) {
253 if ((string)$this->table === 'pages') {
254 $cshButton = $buttonBar->makeHelpButton()
255 ->setModuleName('xMOD_csh_corebe')
256 ->setFieldName('move_el_pages');
257 $buttonBar->addButton($cshButton);
258 } elseif ((string)$this->table === 'tt_content') {
259 $cshButton = $buttonBar->makeHelpButton()
260 ->setModuleName('xMOD_csh_corebe')
261 ->setFieldName('move_el_cs');
262 $buttonBar->addButton($cshButton);
263 }
264
265 if ($this->R_URI) {
266 $backButton = $buttonBar->makeLinkButton()
267 ->setHref($this->R_URI)
268 ->setTitle($this->getLanguageService()->getLL('goBack'))
269 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon(
270 'actions-view-go-back',
271 Icon::SIZE_SMALL
272 ));
273 $buttonBar->addButton($backButton);
274 }
275 }
276 }
277
278 /**
279 * Returns LanguageService
280 *
281 * @return \TYPO3\CMS\Core\Localization\LanguageService
282 */
283 protected function getLanguageService()
284 {
285 return $GLOBALS['LANG'];
286 }
287
288 /**
289 * Returns the current BE user.
290 *
291 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
292 */
293 protected function getBackendUser()
294 {
295 return $GLOBALS['BE_USER'];
296 }
297 }