‪TYPO3CMS  ‪main
ClipboardController.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\ResponseFactoryInterface;
21 use Psr\Http\Message\ResponseInterface;
22 use Psr\Http\Message\ServerRequestInterface;
23 use Psr\Http\Message\StreamFactoryInterface;
28 
35 #[AsController]
37 {
38  private const ‪ALLOWED_ACTIONS = ['getClipboardData'];
39 
40  protected ResponseFactoryInterface ‪$responseFactory;
41  protected StreamFactoryInterface ‪$streamFactory;
43 
44  public function ‪__construct(ResponseFactoryInterface ‪$responseFactory, StreamFactoryInterface ‪$streamFactory)
45  {
46  $this->responseFactory = ‪$responseFactory;
47  $this->streamFactory = ‪$streamFactory;
48  $this->clipboard = GeneralUtility::makeInstance(Clipboard::class);
49  }
50 
54  public function ‪processRequest(ServerRequestInterface $request): ResponseInterface
55  {
56  $this->clipboard->initializeClipboard($request);
57 
58  $CB = (array)($request->getParsedBody()['CB'] ?? []);
59  if ($CB !== []) {
60  // Execute commands.
61  $this->clipboard->setCmd($CB);
62  }
63 
64  // Clean up pad
65  $this->clipboard->cleanCurrent();
66  // Save the clipboard content
67  $this->clipboard->endClipboard();
68 
69  $action = (string)($request->getQueryParams()['action'] ?? '');
70  if (in_array($action, self::ALLOWED_ACTIONS, true)) {
71  return $this->{$action . 'Action'}($request);
72  }
73 
74  // Default response in case no dedicated action is requested.
75  // This is usually done if only internal clipboard state is changed.
76  return $this->‪createResponse(['success' => true, 'data' => []]);
77  }
78 
79  protected function ‪getClipboardDataAction(ServerRequestInterface $request): ResponseInterface
80  {
81  $clipboardData = $this->clipboard->getClipboardData($request->getParsedBody()['table'] ?? '');
82 
83  // Add labels for the panel
84  $lang = $this->‪getLanguageService();
85  $clipboardLabels = [
86  'clipboard' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:buttons.clipboard'),
87  'copyElements' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_misc.xlf:copyElements'),
88  'moveElements' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_misc.xlf:moveElements'),
89  'copy' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:cm.copy'),
90  'cut' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:cm.cut'),
91  'info' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:cm.info'),
92  'removeAll' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:buttons.removeAll'),
93  'removeItem' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.removeItem'),
94  ];
95 
96  return $this->‪createResponse([
97  'success' => $clipboardData !== [],
98  'data' => array_merge($clipboardData, ['labels' => $clipboardLabels]),
99  ]);
100  }
101 
102  protected function ‪createResponse(array $data): ResponseInterface
103  {
104  return $this->responseFactory->createResponse()
105  ->withHeader('Content-Type', 'application/json; charset=utf-8')
106  ->withBody($this->streamFactory->createStream(json_encode($data)));
107  }
108 
110  {
111  return ‪$GLOBALS['LANG'];
112  }
113 }
‪TYPO3\CMS\Backend\Clipboard\Clipboard
Definition: Clipboard.php:48
‪TYPO3\CMS\Backend\Controller\ClipboardController\createResponse
‪createResponse(array $data)
Definition: ClipboardController.php:102
‪TYPO3\CMS\Backend\Controller\ClipboardController\ALLOWED_ACTIONS
‪const ALLOWED_ACTIONS
Definition: ClipboardController.php:38
‪TYPO3\CMS\Backend\Controller\ClipboardController\$streamFactory
‪StreamFactoryInterface $streamFactory
Definition: ClipboardController.php:41
‪TYPO3\CMS\Backend\Controller\ClipboardController\getClipboardDataAction
‪getClipboardDataAction(ServerRequestInterface $request)
Definition: ClipboardController.php:79
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Backend\Controller\ClipboardController
Definition: ClipboardController.php:37
‪TYPO3\CMS\Backend\Attribute\AsController
Definition: AsController.php:25
‪TYPO3\CMS\Backend\Controller\ClipboardController\processRequest
‪processRequest(ServerRequestInterface $request)
Definition: ClipboardController.php:54
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Backend\Controller
Definition: AboutController.php:18
‪TYPO3\CMS\Backend\Controller\ClipboardController\getLanguageService
‪getLanguageService()
Definition: ClipboardController.php:109
‪TYPO3\CMS\Backend\Controller\ClipboardController\$clipboard
‪Clipboard $clipboard
Definition: ClipboardController.php:42
‪TYPO3\CMS\Backend\Controller\ClipboardController\$responseFactory
‪ResponseFactoryInterface $responseFactory
Definition: ClipboardController.php:40
‪TYPO3\CMS\Backend\Controller\ClipboardController\__construct
‪__construct(ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory)
Definition: ClipboardController.php:44