‪TYPO3CMS  ‪main
RecordHistoryRollback.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\EventDispatcher\EventDispatcherInterface;
23 use TYPO3\CMS\Backend\Utility\BackendUtility;
27 
29 {
33  protected ‪$eventDispatcher;
34 
35  public function ‪__construct(EventDispatcherInterface ‪$eventDispatcher)
36  {
37  $this->eventDispatcher = ‪$eventDispatcher;
38  }
39 
43  public function ‪performRollback(string $rollbackFields, array $diff, ?‪BackendUserAuthentication $backendUserAuthentication = null): void
44  {
45  $this->eventDispatcher->dispatch(new ‪BeforeHistoryRollbackStartEvent($rollbackFields, $diff, $this, $backendUserAuthentication));
46  $rollbackData = explode(':', $rollbackFields);
47  $rollbackDataCount = count($rollbackData);
48  // PROCESS INSERTS AND DELETES
49  // rewrite inserts and deletes
50  $commandMapArray = [];
51  $data = [];
52  if ($diff['insertsDeletes']) {
53  if ($rollbackDataCount === 1) {
54  // all tables
55  $data = $diff['insertsDeletes'];
56  } elseif ($rollbackDataCount === 2 && $diff['insertsDeletes'][$rollbackFields]) {
57  // one record
58  $data[$rollbackFields] = $diff['insertsDeletes'][$rollbackFields];
59  }
60  if (!empty($data)) {
61  foreach ($data as $key => $action) {
62  $elParts = explode(':', $key);
63  if ((int)$action === 1) {
64  // inserted records should be deleted
65  $commandMapArray[$elParts[0]][$elParts[1]]['delete'] = 1;
66  // When the record is deleted, the contents of the record do not need to be updated
67  unset(
68  $diff['oldData'][$key],
69  $diff['newData'][$key]
70  );
71  } elseif ((int)$action === -1) {
72  // deleted records should be inserted again
73  $commandMapArray[$elParts[0]][$elParts[1]]['undelete'] = 1;
74  }
75  }
76  }
77  }
78  // Writes the data:
79  if ($commandMapArray) {
80  $tce = GeneralUtility::makeInstance(DataHandler::class);
81  $tce->dontProcessTransformations = true;
82  $tce->start([], $commandMapArray, $backendUserAuthentication);
83  $tce->process_cmdmap();
84  unset($tce);
85  }
86  if (!$diff['insertsDeletes']) {
87  // PROCESS CHANGES
88  // create an array for process_datamap
89  $diffModified = [];
90  foreach ($diff['oldData'] as $key => $value) {
91  $splitKey = explode(':', $key);
92  $diffModified[$splitKey[0]][$splitKey[1]] = $value;
93  }
94  if ($rollbackDataCount === 1) {
95  // all tables
96  $data = $diffModified;
97  } elseif ($rollbackDataCount === 2) {
98  // one record
99  $data[$rollbackData[0]][$rollbackData[1]] = $diffModified[$rollbackData[0]][$rollbackData[1]];
100  } elseif ($rollbackDataCount === 3) {
101  // one field in one record
102  $data[$rollbackData[0]][$rollbackData[1]][$rollbackData[2]] = $diffModified[$rollbackData[0]][$rollbackData[1]][$rollbackData[2]];
103  }
104  // Writes the data:
105  $tce = GeneralUtility::makeInstance(DataHandler::class);
106  $tce->dontProcessTransformations = true;
107  $tce->start($data, [], $backendUserAuthentication);
108  $tce->process_datamap();
109  unset($tce);
110  }
111  if (isset($data['pages']) || isset($commandMapArray['pages'])) {
112  BackendUtility::setUpdateSignal('updatePageTree');
113  }
114  $this->eventDispatcher->dispatch(new AfterHistoryRollbackFinishedEvent($rollbackFields, $diff, $data, $this, $backendUserAuthentication));
115  }
116 }
‪TYPO3\CMS\Core\DataHandling\DataHandler
Definition: DataHandler.php:94
‪TYPO3\CMS\Backend\History\RecordHistoryRollback\__construct
‪__construct(EventDispatcherInterface $eventDispatcher)
Definition: RecordHistoryRollback.php:34
‪TYPO3\CMS\Backend\History\RecordHistoryRollback
Definition: RecordHistoryRollback.php:29
‪TYPO3\CMS\Backend\History\RecordHistoryRollback\$eventDispatcher
‪EventDispatcherInterface $eventDispatcher
Definition: RecordHistoryRollback.php:32
‪TYPO3\CMS\Backend\History
‪TYPO3\CMS\Backend\History\Event\BeforeHistoryRollbackStartEvent
Definition: BeforeHistoryRollbackStartEvent.php:27
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:61
‪TYPO3\CMS\Backend\History\Event\AfterHistoryRollbackFinishedEvent
Definition: AfterHistoryRollbackFinishedEvent.php:27
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Backend\History\RecordHistoryRollback\performRollback
‪performRollback(string $rollbackFields, array $diff, ?BackendUserAuthentication $backendUserAuthentication=null)
Definition: RecordHistoryRollback.php:42