[!!!][FEATURE] Introduce PSR-7-based Routing for Backend AJAX Requests
[Packages/TYPO3.CMS.git] / typo3 / sysext / workspaces / Classes / Controller / AjaxController.php
1 <?php
2 namespace TYPO3\CMS\Workspaces\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\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Backend\Utility\BackendUtility;
21
22 /**
23 * Implements the AJAX functionality for the various asynchronous calls
24 */
25 class AjaxController {
26
27 /**
28 * Sets the TYPO3 Backend context to a certain workspace,
29 * called by the Backend toolbar menu
30 *
31 * @param ServerRequestInterface $request
32 * @param ResponseInterface $response
33 * @return ResponseInterface
34 */
35 public function switchWorkspaceAction(ServerRequestInterface $request, ResponseInterface $response) {
36 $parsedBody = $request->getParsedBody();
37 $queryParams = $request->getQueryParams();
38 $workspaceId = (int)(isset($parsedBody['workspaceId']) ? $parsedBody['workspaceId'] : $queryParams['workspaceId']);
39 $pageId = (int)(isset($parsedBody['pageId']) ? $parsedBody['pageId'] : $queryParams['pageId']);
40 $finalPageUid = 0;
41 $originalPageId = $pageId;
42
43 $this->getBackendUser()->setWorkspace($workspaceId);
44
45 while ($pageId) {
46 $page = BackendUtility::getRecordWSOL('pages', $pageId, '*',
47 ' AND pages.t3ver_wsid IN (0, ' . $workspaceId . ')');
48 if ($page) {
49 if ($this->getBackendUser()->doesUserHaveAccess($page, 1)) {
50 break;
51 }
52 } else {
53 $page = BackendUtility::getRecord('pages', $pageId);
54 }
55 $pageId = $page['pid'];
56 }
57
58 if (isset($page['uid'])) {
59 $finalPageUid = (int)$page['uid'];
60 }
61
62 $ajaxResponse = array(
63 'title' => \TYPO3\CMS\Workspaces\Service\WorkspaceService::getWorkspaceTitle($workspaceId),
64 'workspaceId' => $workspaceId,
65 'pageId' => ($finalPageUid && $originalPageId == $finalPageUid) ? NULL : $finalPageUid
66 );
67 $response->getBody()->write(json_encode($ajaxResponse));
68 return $response;
69 }
70
71 /**
72 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
73 */
74 protected function getBackendUser() {
75 return $GLOBALS['BE_USER'];
76 }
77
78 }