‪TYPO3CMS  ‪main
ShortcutController.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
28 
34 #[AsController]
36 {
37  public function ‪__construct(
38  protected readonly ‪ShortcutToolbarItem $shortcutToolbarItem,
39  protected readonly ‪ShortcutRepository $shortcutRepository,
40  protected readonly ‪BackendViewFactory $backendViewFactory,
41  ) {}
42 
46  public function ‪menuAction(ServerRequestInterface $request): ResponseInterface
47  {
48  $this->shortcutToolbarItem->setRequest($request);
49  return new ‪HtmlResponse($this->shortcutToolbarItem->getDropDown());
50  }
51 
55  public function ‪addAction(ServerRequestInterface $request): ResponseInterface
56  {
57  $result = 'success';
58  $responseCode = 201;
59  $parsedBody = $request->getParsedBody();
60  $routeIdentifier = $parsedBody['routeIdentifier'] ?? '';
61  $arguments = $parsedBody['arguments'] ?? '';
62  if ($routeIdentifier === '') {
63  $result = 'missingRoute';
64  $responseCode = 400;
65  } elseif ($this->shortcutRepository->shortcutExists($routeIdentifier, $arguments)) {
66  $result = 'alreadyExists';
67  $responseCode = 200;
68  } else {
69  $shortcutName = $parsedBody['displayName'] ?? '';
70  $success = $this->shortcutRepository->addShortcut($routeIdentifier, $arguments, $shortcutName);
71  if (!$success) {
72  $result = 'failed';
73  $responseCode = 500;
74  }
75  }
76  return new ‪JsonResponse(['result' => $result], $responseCode);
77  }
78 
82  public function ‪showEditFormAction(ServerRequestInterface $request): ResponseInterface
83  {
84  $queryParams = $request->getQueryParams();
85  $selectedShortcutId = (int)($queryParams['shortcutId'] ?? 0);
86  $selectedShortcutGroupId = (int)($queryParams['shortcutGroup'] ?? '');
87  $selectedShortcut = $this->shortcutRepository->getShortcutById($selectedShortcutId);
88  $shortcutGroups = $this->shortcutRepository->getShortcutGroups();
89  $view = $this->backendViewFactory->create($request);
90  $view->assignMultiple([
91  'selectedShortcutId' => $selectedShortcutId,
92  'selectedShortcutGroupId' => $selectedShortcutGroupId,
93  'selectedShortcut' => $selectedShortcut,
94  'shortcutGroups' => $shortcutGroups,
95  ]);
96  return new ‪HtmlResponse($view->render('ToolbarItems/ShortcutToolbarItemEditForm'));
97  }
98 
103  public function ‪updateAction(ServerRequestInterface $request): ResponseInterface
104  {
105  $parsedBody = $request->getParsedBody();
106  $shortcutId = (int)($parsedBody['shortcutId'] ?? 0);
107  $shortcutTitle = strip_tags($parsedBody['shortcutTitle'] ?? '');
108  $shortcutGroupId = (int)($parsedBody['shortcutGroup'] ?? 0);
109  $success = $this->shortcutRepository->updateShortcut($shortcutId, $shortcutTitle, $shortcutGroupId);
110  return new ‪HtmlResponse($success ? $shortcutTitle : 'failed');
111  }
112 
116  public function ‪removeAction(ServerRequestInterface $request): ResponseInterface
117  {
118  $shortcut = $this->shortcutRepository->getShortcutById((int)($request->getParsedBody()['shortcutId'] ?? 0));
119  $success = $this->shortcutRepository->removeShortcut($shortcut['raw']['uid'] ?? 0);
120 
121  return new ‪JsonResponse([
122  'success' => $success,
123  'data' => [
124  'route' => $shortcut['route'] ?? null,
125  'args' => $shortcut['raw']['arguments'] ?? null,
126  ],
127  ]);
128  }
129 }
‪TYPO3\CMS\Backend\Backend\Shortcut\ShortcutRepository
Definition: ShortcutRepository.php:42
‪TYPO3\CMS\Backend\Controller\ShortcutController
Definition: ShortcutController.php:36
‪TYPO3\CMS\Backend\Controller\ShortcutController\menuAction
‪menuAction(ServerRequestInterface $request)
Definition: ShortcutController.php:46
‪TYPO3\CMS\Backend\View\BackendViewFactory
Definition: BackendViewFactory.php:35
‪TYPO3\CMS\Backend\Controller\ShortcutController\addAction
‪addAction(ServerRequestInterface $request)
Definition: ShortcutController.php:55
‪TYPO3\CMS\Backend\Controller\ShortcutController\__construct
‪__construct(protected readonly ShortcutToolbarItem $shortcutToolbarItem, protected readonly ShortcutRepository $shortcutRepository, protected readonly BackendViewFactory $backendViewFactory,)
Definition: ShortcutController.php:37
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ShortcutToolbarItem
Definition: ShortcutToolbarItem.php:33
‪TYPO3\CMS\Core\Http\JsonResponse
Definition: JsonResponse.php:28
‪TYPO3\CMS\Backend\Attribute\AsController
Definition: AsController.php:25
‪TYPO3\CMS\Backend\Controller\ShortcutController\removeAction
‪removeAction(ServerRequestInterface $request)
Definition: ShortcutController.php:116
‪TYPO3\CMS\Backend\Controller\ShortcutController\showEditFormAction
‪showEditFormAction(ServerRequestInterface $request)
Definition: ShortcutController.php:82
‪TYPO3\CMS\Backend\Controller\ShortcutController\updateAction
‪updateAction(ServerRequestInterface $request)
Definition: ShortcutController.php:103
‪TYPO3\CMS\Backend\Controller
Definition: AboutController.php:18
‪TYPO3\CMS\Core\Http\HtmlResponse
Definition: HtmlResponse.php:28