[TASK] update the comments regarding ajax.php
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / PageTreeNavigationController.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\Template\DocumentTemplate;
20 use TYPO3\CMS\Backend\Utility\BackendUtility;
21 use TYPO3\CMS\Backend\View\PageTreeView;
22 use TYPO3\CMS\Core\Imaging\Icon;
23 use TYPO3\CMS\Core\Imaging\IconFactory;
24 use TYPO3\CMS\Core\Messaging\FlashMessage;
25 use TYPO3\CMS\Core\Page\PageRenderer;
26 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
27 use TYPO3\CMS\Core\Utility\GeneralUtility;
28 use TYPO3\CMS\Core\Utility\MathUtility;
29 use TYPO3\CMS\Workspaces\Service\WorkspaceService;
30
31 /**
32 * Main script class for the page tree navigation frame
33 * This is the class for rendering the "page tree" navigation frame without ExtJS, used prior to TYPO3 CMS 4.5.
34 * This functionality is deprecated since TYPO3 CMS 7, and will be removed with TYPO3 CMS 8
35 */
36 class PageTreeNavigationController {
37
38 /**
39 * @var string
40 */
41 public $content;
42
43 /**
44 * @var \TYPO3\CMS\Backend\View\PageTreeView
45 */
46 public $pagetree;
47
48 /**
49 * document template object
50 *
51 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
52 */
53 public $doc;
54
55 /**
56 * Temporary mount point (record), if any
57 *
58 * @var int
59 */
60 public $active_tempMountPoint = 0;
61
62 /**
63 * @var string
64 */
65 public $currentSubScript;
66
67 /**
68 * @var bool
69 */
70 public $cMR;
71
72 /**
73 * If not '' (blank) then it will clear (0) or set (>0) Temporary DB mount.
74 *
75 * @var string
76 */
77 public $setTempDBmount;
78
79 /**
80 * @var string
81 * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8
82 */
83 public $template;
84
85 /**
86 * @var IconFactory
87 */
88 protected $iconFactory;
89
90 /**
91 * Constructor
92 */
93 public function __construct() {
94 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
95 GeneralUtility::deprecationLog('PageTreeNavigationController is deprecated in favor of new pagetrees');
96 $GLOBALS['SOBE'] = $this;
97 $this->init();
98 }
99
100 /**
101 * Initialization of the class
102 *
103 * @return void
104 */
105 protected function init() {
106 // Setting GPvars:
107 $this->cMR = (bool)GeneralUtility::_GP('cMR');
108 $this->currentSubScript = GeneralUtility::_GP('currentSubScript');
109 $this->setTempDBmount = GeneralUtility::_GP('setTempDBmount');
110 // Create page tree object:
111 $beUser = $this->getBackendUser();
112 $this->pagetree = GeneralUtility::makeInstance(PageTreeView::class);
113 $this->pagetree->ext_IconMode = $beUser->getTSConfigVal('options.pageTree.disableIconLinkToContextmenu');
114 $this->pagetree->ext_showPageId = (bool)$beUser->getTSConfigVal('options.pageTree.showPageIdWithTitle');
115 $this->pagetree->ext_showNavTitle = (bool)$beUser->getTSConfigVal('options.pageTree.showNavTitle');
116 $this->pagetree->ext_separateNotinmenuPages = $beUser->getTSConfigVal('options.pageTree.separateNotinmenuPages');
117 $this->pagetree->ext_alphasortNotinmenuPages = $beUser->getTSConfigVal('options.pageTree.alphasortNotinmenuPages');
118 $this->pagetree->thisScript = 'alt_db_navframe.php';
119 $this->pagetree->addField('alias');
120 $this->pagetree->addField('shortcut');
121 $this->pagetree->addField('shortcut_mode');
122 $this->pagetree->addField('mount_pid');
123 $this->pagetree->addField('mount_pid_ol');
124 $this->pagetree->addField('url');
125 // Temporary DB mounts:
126 $this->initializeTemporaryDBmount();
127 }
128
129 /**
130 * Initialization for the visual parts of the class
131 * Use template rendering only if this is a non-AJAX call
132 *
133 * @return void
134 */
135 public function initPage() {
136 // Setting highlight mode:
137 $doHighlight = !$this->getBackendUser()->getTSConfigVal('options.pageTree.disableTitleHighlight');
138 // Create template object:
139 $this->doc = GeneralUtility::makeInstance(DocumentTemplate::class);
140 $this->doc->setModuleTemplate('EXT:backend/Resources/Private/Templates/alt_db_navframe.html');
141 $this->doc->showFlashMessages = FALSE;
142 // Get HTML-Template
143
144 // Adding javascript for drag & drop activation and highlighting
145 $dragDropCode = 'Tree.registerDragDropHandlers();';
146
147 // If highlighting is active, define the CSS class for the active item depending on the workspace
148 if ($doHighlight) {
149 $hlClass = $this->getBackendUser()->workspace === 0 ? 'active' : 'active active-ws wsver' . $this->getBackendUser()->workspace;
150 $dragDropCode .= '
151 Tree.highlightClass = "' . $hlClass . '";
152 Tree.highlightActiveItem("",top.fsMod.navFrameHighlightedID["web"]);';
153 }
154 // Adding javascript code for drag&drop and the pagetree as well as the click menu code
155 $this->doc->getDragDropCode('pages', $dragDropCode);
156 $this->doc->getContextMenuCode();
157 /** @var $pageRenderer PageRenderer */
158 $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
159 $pageRenderer->loadExtJS();
160 $this->doc->JScode .= $this->doc->wrapScriptTags(($this->currentSubScript ? 'top.currentSubScript=unescape("' . rawurlencode($this->currentSubScript) . '");' : '') . '
161 // Function, loading the list frame from navigation tree:
162 function jumpTo(id, linkObj, highlightID, bank) { //
163 var theUrl = top.currentSubScript ;
164 if (theUrl.indexOf("?") != -1) {
165 theUrl += "&id=" + id
166 } else {
167 theUrl += "?id=" + id
168 }
169 top.fsMod.currentBank = bank;
170 top.TYPO3.Backend.ContentContainer.setUrl(theUrl);
171
172 ' . ($doHighlight ? 'Tree.highlightActiveItem("web", highlightID + "_" + bank);' : '') . '
173 if (linkObj) { linkObj.blur(); }
174 return false;
175 }
176 ' . ($this->cMR ? 'jumpTo(top.fsMod.recentIds[\'web\'],\'\');' : '') . '
177
178 ');
179 $this->doc->bodyTagId = 'typo3-pagetree';
180 }
181
182 /**
183 * Main function, rendering the browsable page tree
184 *
185 * @return void
186 */
187 public function main() {
188 // Produce browse-tree:
189 $tree = $this->pagetree->getBrowsableTree();
190 // Outputting Temporary DB mount notice:
191 if ($this->active_tempMountPoint) {
192 $flashText = '
193 <a href="' . htmlspecialchars(GeneralUtility::linkThisScript(array('setTempDBmount' => 0))) . '">' . $this->getLanguageService()->sl('LLL:EXT:lang/locallang_core.xlf:labels.temporaryDBmount', TRUE) . '</a> <br />' . $this->getLanguageService()->sl('LLL:EXT:lang/locallang_core.xlf:labels.path', TRUE) . ': <span title="' . htmlspecialchars($this->active_tempMountPoint['_thePathFull']) . '">' . htmlspecialchars(GeneralUtility::fixed_lgd_cs($this->active_tempMountPoint['_thePath'], -50)) . '</span>
194 ';
195 $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $flashText, '', FlashMessage::INFO);
196 $this->content .= $flashMessage->render();
197 }
198 // Outputting page tree:
199 $this->content .= '<div id="PageTreeDiv">' . $tree . '</div>';
200 // Setting up the buttons and markers for docheader
201 $docHeaderButtons = $this->getButtons();
202 $markers = array(
203 'WORKSPACEINFO' => $this->getWorkspaceInfo(),
204 'CONTENT' => $this->content
205 );
206 // Build the <body> for the module
207 $this->content = $this->doc->startPage('TYPO3 Page Tree');
208 $this->content .= $this->doc->moduleBody(array(), $docHeaderButtons, $markers);
209 $this->content .= $this->doc->endPage();
210 $this->content = $this->doc->insertStylesAndJS($this->content);
211 }
212
213 /**
214 * Outputting the accumulated content to screen
215 *
216 * @return void
217 */
218 public function printContent() {
219 echo $this->content;
220 }
221
222 /**
223 * Create the panel of buttons for submitting the form or otherwise perform operations.
224 *
225 * @return array All available buttons as an assoc. array
226 */
227 protected function getButtons() {
228 $buttons = array(
229 'csh' => '',
230 'new_page' => '',
231 'refresh' => ''
232 );
233 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
234 // New Page
235 $onclickNewPageWizard = 'top.content.list_frame.location.href=' . GeneralUtility::quoteJSvalue(BackendUtility::getModuleUrl('db_new', ['pagesOnly' => 1, 'id' => ''])) . '+Tree.pageID;';
236 $buttons['new_page'] = '<a href="#" onclick="' . $onclickNewPageWizard . '" title="' . $this->getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_layout.xlf:newPage', TRUE) . '">'
237 . $this->iconFactory->getIcon('actions-page-new', Icon::SIZE_SMALL)->render()
238 . '</a>';
239 // Refresh
240 $buttons['refresh'] = '<a href="' . htmlspecialchars(GeneralUtility::getIndpEnv('REQUEST_URI')) . '" title="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.refresh', TRUE) . '">' . $iconFactory->getIcon('actions-refresh', Icon::SIZE_SMALL)->render() . '</a>';
241 // CSH
242 $buttons['csh'] = str_replace('typo3-csh-inline', 'typo3-csh-inline show-right', BackendUtility::cshItem('xMOD_csh_corebe', 'pagetree'));
243 return $buttons;
244 }
245
246 /**
247 * Create the workspace information
248 *
249 * @return string HTML containing workspace info
250 */
251 protected function getWorkspaceInfo() {
252 if (ExtensionManagementUtility::isLoaded('workspaces') && ($this->getBackendUser()->workspace !== 0 || $this->getBackendUser()->getTSConfigVal('options.pageTree.onlineWorkspaceInfo'))) {
253 $wsTitle = htmlspecialchars(WorkspaceService::getWorkspaceTitle($this->getBackendUser()->workspace));
254
255 $workspaceInfo = '<div class="bgColor4 workspace-info"><span title="' . $wsTitle . '" onclick="top.goToModule(\'web_WorkspacesWorkspaces\');" style="cursor:pointer;">'
256 . $this->iconFactory->getIcon('apps-toolbar-menu-workspace', Icon::SIZE_SMALL)->render() . '</span>'
257 . $wsTitle . '</div>';
258 } else {
259 $workspaceInfo = '';
260 }
261 return $workspaceInfo;
262 }
263
264 /**********************************
265 *
266 * Temporary DB mounts
267 *
268 **********************************/
269 /**
270 * Getting temporary DB mount
271 *
272 * @return void
273 */
274 public function initializeTemporaryDBmount() {
275 $beUser = $this->getBackendUser();
276 // Set/Cancel Temporary DB Mount:
277 if ((string)$this->setTempDBmount !== '') {
278 $set = MathUtility::forceIntegerInRange($this->setTempDBmount, 0);
279 if ($set > 0 && $beUser->isInWebMount($set)) {
280 // Setting...:
281 $this->settingTemporaryMountPoint($set);
282 } else {
283 // Clear:
284 $this->settingTemporaryMountPoint(0);
285 }
286 }
287 // Getting temporary mount point ID:
288 $temporaryMountPoint = (int)$beUser->getSessionData('pageTree_temporaryMountPoint');
289 // If mount point ID existed and is within users real mount points, then set it temporarily:
290 if ($temporaryMountPoint > 0 && $beUser->isInWebMount($temporaryMountPoint)) {
291 if ($this->active_tempMountPoint = BackendUtility::readPageAccess($temporaryMountPoint, $beUser->getPagePermsClause(1))) {
292 $this->pagetree->MOUNTS = array($temporaryMountPoint);
293 } else {
294 // Clear temporary mount point as we have no access to it any longer
295 $this->settingTemporaryMountPoint(0);
296 }
297 }
298 }
299
300 /**
301 * Setting temporary page id as DB mount
302 *
303 * @param int $pageId The page id to set as DB mount
304 * @return void
305 */
306 public function settingTemporaryMountPoint($pageId) {
307 $this->getBackendUser()->setAndSaveSessionData('pageTree_temporaryMountPoint', (int)$pageId);
308 }
309
310 /**********************************
311 *
312 * AJAX Calls
313 *
314 **********************************/
315 /**
316 * Makes the AJAX call to expand or collapse the pagetree.
317 * Called by an AJAX Route, see AjaxRequestHandler
318 *
319 * @param ServerRequestInterface $request
320 * @param ResponseInterface $response
321 * @return ResponseInterface
322 */
323 public function ajaxExpandCollapse(ServerRequestInterface $request, ResponseInterface $response) {
324 $this->init();
325 $tree = $this->pagetree->getBrowsableTree();
326 if (!$this->pagetree->ajaxStatus) {
327 $response->withStatus(500);
328 } else {
329 $response->getBody()->write(json_encode($tree));
330 }
331
332 return $response;
333 }
334
335 /**
336 * Returns LanguageService
337 *
338 * @return \TYPO3\CMS\Lang\LanguageService
339 */
340 protected function getLanguageService() {
341 return $GLOBALS['LANG'];
342 }
343
344 /**
345 * Returns the current BE user.
346 *
347 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
348 */
349 protected function getBackendUser() {
350 return $GLOBALS['BE_USER'];
351 }
352
353 }