‪TYPO3CMS  ‪main
SimpleDataHandlerController.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;
23 use TYPO3\CMS\Backend\Utility\BackendUtility;
33 
43 {
49  protected ‪$flags;
50 
56  protected ‪$data;
57 
64  protected ‪$cmd;
65 
71  protected ‪$mirror;
72 
78  protected ‪$cacheCmd;
79 
85  protected ‪$redirect;
86 
92  protected ‪$CB;
93 
99  protected ‪$tce;
100 
108  public function ‪mainAction(ServerRequestInterface $request): ResponseInterface
109  {
110  $this->‪init($request);
111 
112  $this->‪initializeClipboard($request);
113  $this->‪processRequest();
114 
115  // Write errors to flash message queue
116  $this->tce->printLogErrorMessages();
117  if ($this->redirect) {
118  return new ‪RedirectResponse(GeneralUtility::locationHeaderUrl($this->redirect), 303);
119  }
120  return new ‪HtmlResponse('');
121  }
122 
126  public function ‪processAjaxRequest(ServerRequestInterface $request): ResponseInterface
127  {
128  $this->‪init($request);
129 
130  // do the regular / main logic
131  $this->‪initializeClipboard($request);
132  $this->‪processRequest();
133 
134  $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
135 
136  $content = [
137  'redirect' => ‪$this->redirect,
138  'messages' => [],
139  'hasErrors' => false,
140  ];
141 
142  // Prints errors (= write them to the message queue)
143  $this->tce->printLogErrorMessages();
144 
145  $messages = $flashMessageService->getMessageQueueByIdentifier()->getAllMessagesAndFlush();
146  if (!empty($messages)) {
147  foreach ($messages as $message) {
148  $content['messages'][] = [
149  'title' => $message->getTitle(),
150  'message' => $message->getMessage(),
151  'severity' => $message->getSeverity(),
152  ];
153  if ($message->getSeverity() === ContextualFeedbackSeverity::ERROR) {
154  $content['hasErrors'] = true;
155  }
156  }
157  }
158  return new JsonResponse($content);
159  }
160 
164  protected function ‪init(ServerRequestInterface $request): void
165  {
166  $beUser = $this->‪getBackendUser();
167 
168  $parsedBody = $request->getParsedBody();
169  $queryParams = $request->getQueryParams();
170 
171  // GPvars:
172  $this->flags = (array)($parsedBody['flags'] ?? $queryParams['flags'] ?? []);
173  $this->data = (array)($parsedBody['data'] ?? $queryParams['data'] ?? []);
174  $this->cmd = (array)($parsedBody['cmd'] ?? $queryParams['cmd'] ?? []);
175  $this->mirror = (array)($parsedBody['mirror'] ?? $queryParams['mirror'] ?? []);
176  $this->cacheCmd = (string)($parsedBody['cacheCmd'] ?? $queryParams['cacheCmd'] ?? '');
177  $this->CB = (array)($parsedBody['CB'] ?? $queryParams['CB'] ?? []);
178  $this->redirect = GeneralUtility::sanitizeLocalUrl((string)($parsedBody['redirect'] ?? $queryParams['redirect'] ?? ''));
179  // Creating DataHandler object
180  $this->tce = GeneralUtility::makeInstance(DataHandler::class);
181  // Configuring based on user prefs.
182  if ($beUser->uc['copyLevels'] ?? false) {
183  // Set to number of page-levels to copy.
184  $this->tce->copyTree = ‪MathUtility::forceIntegerInRange($beUser->uc['copyLevels'], 0, 100);
185  }
186  if ($beUser->uc['neverHideAtCopy'] ?? false) {
187  $this->tce->neverHideAtCopy = true;
188  }
189  // Reverse order.
190  if ($this->flags['reverseOrder'] ?? false) {
191  $this->tce->reverseOrder = true;
192  }
193  }
194 
198  protected function ‪initializeClipboard(ServerRequestInterface $request): void
199  {
200  if ($this->CB !== []) {
201  $clipObj = GeneralUtility::makeInstance(Clipboard::class);
202  $clipObj->initializeClipboard($request);
203  if ($this->CB['paste'] ?? false) {
204  $clipObj->setCurrentPad((string)($this->CB['pad'] ?? ''));
205  $this->‪setPasteCmd($clipObj);
206  }
207  if ($this->CB['delete'] ?? false) {
208  $clipObj->setCurrentPad((string)($this->CB['pad'] ?? ''));
209  $this->‪setDeleteCmd($clipObj);
210  }
211  }
212  }
213 
217  protected function ‪processRequest(): void
218  {
219  // LOAD DataHandler with data and cmd arrays:
220  $this->tce->start($this->data, $this->cmd);
221  if ($this->mirror !== []) {
222  $this->tce->setMirror($this->mirror);
223  }
224  // Execute actions:
225  $this->tce->process_datamap();
226  $this->tce->process_cmdmap();
227  // Clearing cache:
228  if (!empty($this->cacheCmd)) {
229  $this->tce->clear_cacheCmd($this->cacheCmd);
230  }
231  // Update page tree?
232  if (isset($this->data['pages']) || isset($this->cmd['pages'])) {
233  BackendUtility::setUpdateSignal('updatePageTree');
234  }
235  }
236 
248  protected function ‪setPasteCmd(Clipboard $clipboard): void
249  {
250  [$pasteTable, $pasteUid] = explode('|', (string)$this->CB['paste']);
251  $pasteUid = (int)$pasteUid;
252  // pUid must be set and if pTable is not set (that means paste ALL elements)
253  // the uid MUST be positive/zero (pointing to page id)
254  if (!$pasteTable && $pasteUid < 0) {
255  return;
256  }
257  $elements = $clipboard->elFromTable($pasteTable);
258  // So the order is preserved.
259  $elements = array_reverse($elements);
260  $mode = $clipboard->currentMode() === 'copy' ? 'copy' : 'move';
261  // Traverse elements and make CMD array
262  foreach ($elements as $key => $value) {
263  [$table, ‪$uid] = explode('|', $key);
264  if (!is_array($this->cmd[$table] ?? null)) {
265  $this->cmd[$table] = [];
266  }
267  if (is_array($this->CB['update'] ?? false)) {
268  $this->cmd[$table][‪$uid][$mode] = [
269  'action' => 'paste',
270  'target' => $pasteUid,
271  'update' => $this->CB['update'],
272  ];
273  } else {
274  $this->cmd[$table][‪$uid][$mode] = $pasteUid;
275  }
276  if ($mode === 'move') {
277  $clipboard->removeElement($key);
278  }
279  }
280  $clipboard->endClipboard();
281  }
282 
286  protected function ‪setDeleteCmd(Clipboard $clipboard): void
287  {
288  foreach ($clipboard->elFromTable() as $key => $value) {
289  [$table, ‪$uid] = explode('|', $key);
290  if (!is_array($this->cmd[$table])) {
291  $this->cmd[$table] = [];
292  }
293  $this->cmd[$table][‪$uid]['delete'] = 1;
294  $clipboard->removeElement($key);
295  }
296  $clipboard->endClipboard();
297  }
298 
299  protected function ‪getBackendUser(): BackendUserAuthentication
300  {
301  return ‪$GLOBALS['BE_USER'];
302  }
303 }
‪TYPO3\CMS\Core\DataHandling\DataHandler
Definition: DataHandler.php:94
‪TYPO3\CMS\Backend\Controller\SimpleDataHandlerController\$data
‪array $data
Definition: SimpleDataHandlerController.php:54
‪TYPO3\CMS\Backend\Clipboard\Clipboard
Definition: Clipboard.php:48
‪TYPO3\CMS\Backend\Controller\SimpleDataHandlerController\$cmd
‪array $cmd
Definition: SimpleDataHandlerController.php:61
‪TYPO3\CMS\Backend\Controller\SimpleDataHandlerController\mainAction
‪ResponseInterface mainAction(ServerRequestInterface $request)
Definition: SimpleDataHandlerController.php:100
‪TYPO3\CMS\Backend\Controller\SimpleDataHandlerController\$cacheCmd
‪string $cacheCmd
Definition: SimpleDataHandlerController.php:73
‪TYPO3\CMS\Backend\Controller\SimpleDataHandlerController\getBackendUser
‪getBackendUser()
Definition: SimpleDataHandlerController.php:291
‪TYPO3\CMS\Backend\Controller\SimpleDataHandlerController\$redirect
‪string $redirect
Definition: SimpleDataHandlerController.php:79
‪TYPO3\CMS\Core\Type\ContextualFeedbackSeverity
‪ContextualFeedbackSeverity
Definition: ContextualFeedbackSeverity.php:25
‪TYPO3\CMS\Backend\Clipboard\Clipboard\currentMode
‪string currentMode()
Definition: Clipboard.php:656
‪TYPO3\CMS\Backend\Controller\SimpleDataHandlerController\init
‪init(ServerRequestInterface $request)
Definition: SimpleDataHandlerController.php:156
‪TYPO3\CMS\Backend\Controller\SimpleDataHandlerController\initializeClipboard
‪initializeClipboard(ServerRequestInterface $request)
Definition: SimpleDataHandlerController.php:190
‪TYPO3\CMS\Backend\Controller\SimpleDataHandlerController\$flags
‪array $flags
Definition: SimpleDataHandlerController.php:48
‪TYPO3\CMS\Backend\Controller\SimpleDataHandlerController\processAjaxRequest
‪processAjaxRequest(ServerRequestInterface $request)
Definition: SimpleDataHandlerController.php:118
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:61
‪TYPO3\CMS\Backend\Clipboard\Clipboard\removeElement
‪removeElement(string $elementKey)
Definition: Clipboard.php:636
‪TYPO3\CMS\Backend\Controller\SimpleDataHandlerController\setDeleteCmd
‪setDeleteCmd(Clipboard $clipboard)
Definition: SimpleDataHandlerController.php:278
‪TYPO3\CMS\Backend\Clipboard\Clipboard\endClipboard
‪endClipboard()
Definition: Clipboard.php:207
‪TYPO3\CMS\Core\Http\RedirectResponse
Definition: RedirectResponse.php:30
‪TYPO3\CMS\Webhooks\Message\$uid
‪identifier readonly int $uid
Definition: PageModificationMessage.php:35
‪TYPO3\CMS\Core\Http\JsonResponse
Definition: JsonResponse.php:28
‪TYPO3\CMS\Backend\Controller\SimpleDataHandlerController
Definition: SimpleDataHandlerController.php:43
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Backend\Controller\SimpleDataHandlerController\$tce
‪TYPO3 CMS Core DataHandling DataHandler $tce
Definition: SimpleDataHandlerController.php:91
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Backend\Controller\SimpleDataHandlerController\setPasteCmd
‪setPasteCmd(Clipboard $clipboard)
Definition: SimpleDataHandlerController.php:240
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange(mixed $theInt, int $min, int $max=2000000000, int $defaultValue=0)
Definition: MathUtility.php:34
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Backend\Controller
Definition: AboutController.php:18
‪TYPO3\CMS\Backend\Controller\SimpleDataHandlerController\processRequest
‪processRequest()
Definition: SimpleDataHandlerController.php:209
‪TYPO3\CMS\Backend\Clipboard\Clipboard\elFromTable
‪array elFromTable(string $matchTable='', string $padIdentifier='')
Definition: Clipboard.php:699
‪TYPO3\CMS\Core\Messaging\FlashMessageService
Definition: FlashMessageService.php:27
‪TYPO3\CMS\Core\Http\HtmlResponse
Definition: HtmlResponse.php:28
‪TYPO3\CMS\Backend\Controller\SimpleDataHandlerController\$mirror
‪array $mirror
Definition: SimpleDataHandlerController.php:67
‪TYPO3\CMS\Backend\Controller\SimpleDataHandlerController\$CB
‪array $CB
Definition: SimpleDataHandlerController.php:85