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