‪TYPO3CMS  ‪main
HistoryService.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 
22 use TYPO3\CMS\Backend\Utility\BackendUtility;
30 
35 {
36  protected array ‪$backendUserNames;
37  protected array ‪$historyEntries = [];
38 
39  public function ‪__construct()
40  {
41  $this->backendUserNames = BackendUtility::getUserNames();
42  }
43 
51  public function ‪getHistory(string $table, int $id): array
52  {
53  $history = [];
54  $i = 0;
55  foreach ($this->‪getHistoryEntries($table, $id) as $entry) {
56  if ((int)($entry['actiontype'] ?? 0) === ‪RecordHistoryStore::ACTION_STAGECHANGE) {
57  continue;
58  }
59  if ($i++ > 20) {
60  break;
61  }
62  $history[] = $this->‪getHistoryEntry($entry);
63  }
64  return $history;
65  }
66 
67  public function ‪getStageChanges(string $table, int $id): array
68  {
69  $stageChanges = [];
70  foreach ($this->‪getHistoryEntries($table, $id) as $entry) {
71  if ((int)($entry['actiontype'] ?? 0) !== ‪RecordHistoryStore::ACTION_STAGECHANGE) {
72  continue;
73  }
74  $stageChanges[] = $entry;
75  }
76 
77  return $stageChanges;
78  }
79 
87  protected function ‪getHistoryEntry(array $entry): array
88  {
89  if (!empty($entry['action'])) {
90  $differences = $entry['action'];
91  } else {
92  $differences = $this->‪getDifferences($entry);
93  }
94 
95  $avatar = GeneralUtility::makeInstance(Avatar::class);
96  $beUserRecord = BackendUtility::getRecord('be_users', $entry['userid']);
97 
98  return [
99  'datetime' => htmlspecialchars(BackendUtility::datetime($entry['tstamp'])),
100  'user' => htmlspecialchars($this->‪getUserName($entry['userid'])),
101  'user_avatar' => $avatar->render($beUserRecord),
102  'differences' => $differences,
103  ];
104  }
105 
112  protected function ‪getDifferences(array $entry): array
113  {
114  $diffUtility = GeneralUtility::makeInstance(DiffUtility::class);
115  $differences = [];
116  $tableName = $entry['tablename'];
117  if (is_array($entry['newRecord'] ?? false)) {
118  ‪$fields = array_keys($entry['newRecord']);
119 
121  foreach (‪$fields as $field) {
122  $tcaType = ‪$GLOBALS['TCA'][$tableName]['columns'][$field]['config']['type'] ?? '';
123  if (!empty(‪$GLOBALS['TCA'][$tableName]['columns'][$field]['config']['type']) && $tcaType !== 'passthrough') {
124  // Create diff-result:
125  if ($tcaType === 'flex') {
126  $granularity = ‪DiffGranularity::CHARACTER;
127  $flexFormValueFormatter = GeneralUtility::makeInstance(FlexFormValueFormatter::class);
128  $colConfig = ‪$GLOBALS['TCA'][$tableName]['columns'][$field]['config'] ?? [];
129  $old = $flexFormValueFormatter->format($tableName, $field, $entry['oldRecord'][$field], $entry['recuid'], $colConfig);
130  $new = $flexFormValueFormatter->format($tableName, $field, $entry['newRecord'][$field], $entry['recuid'], $colConfig);
131  } else {
132  $granularity = DiffGranularity::WORD;
133  $old = (string)BackendUtility::getProcessedValue($tableName, $field, $entry['oldRecord'][$field], 0, true);
134  $new = (string)BackendUtility::getProcessedValue($tableName, $field, $entry['newRecord'][$field], 0, true);
135  }
136  $fieldDifferences = $diffUtility->makeDiffDisplay($old, $new, $granularity);
137  if (!empty($fieldDifferences)) {
138  $differences[] = [
139  'label' => $this->‪getLanguageService()->sL((string)BackendUtility::getItemLabel($tableName, (string)$field)),
140  'html' => trim($fieldDifferences),
141  ];
142  }
143  }
144  }
145  }
146  return $differences;
147  }
148 
152  protected function ‪getUserName(int $user): string
153  {
154  $userName = 'unknown';
155  if (!empty($this->backendUserNames[$user]['username'])) {
156  $userName = $this->backendUserNames[$user]['username'];
157  }
158  return $userName;
159  }
160 
167  protected function ‪getHistoryEntries(string $table, int $id): array
168  {
169  if (!isset($this->historyEntries[$table][$id])) {
170  $this->historyEntries[$table][$id] = GeneralUtility::makeInstance(RecordHistory::class)
171  ->getHistoryDataForRecord($table, $id);
172  }
173  return $this->historyEntries[$table][$id];
174  }
175 
177  {
178  return ‪$GLOBALS['LANG'];
179  }
180 }
‪TYPO3\CMS\Core\Utility\DiffUtility
Definition: DiffUtility.php:28
‪TYPO3\CMS\Workspaces\Service
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar
Definition: Avatar.php:35
‪TYPO3\CMS\Workspaces\Service\HistoryService\getStageChanges
‪getStageChanges(string $table, int $id)
Definition: HistoryService.php:67
‪TYPO3\CMS\Workspaces\Service\HistoryService\getUserName
‪getUserName(int $user)
Definition: HistoryService.php:152
‪TYPO3\CMS\Workspaces\Service\HistoryService\getLanguageService
‪getLanguageService()
Definition: HistoryService.php:176
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\ACTION_STAGECHANGE
‪const ACTION_STAGECHANGE
Definition: RecordHistoryStore.php:37
‪$fields
‪$fields
Definition: pages.php:5
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore
Definition: RecordHistoryStore.php:31
‪TYPO3\CMS\Workspaces\Service\HistoryService\getHistory
‪array getHistory(string $table, int $id)
Definition: HistoryService.php:51
‪TYPO3\CMS\Backend\History\RecordHistory
Definition: RecordHistory.php:32
‪TYPO3\CMS\Backend\View\ValueFormatter\FlexFormValueFormatter
Definition: FlexFormValueFormatter.php:34
‪TYPO3\CMS\Workspaces\Service\HistoryService
Definition: HistoryService.php:35
‪TYPO3\CMS\Workspaces\Service\HistoryService\getDifferences
‪getDifferences(array $entry)
Definition: HistoryService.php:112
‪TYPO3\CMS\Workspaces\Service\HistoryService\getHistoryEntries
‪getHistoryEntries(string $table, int $id)
Definition: HistoryService.php:167
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Utility\DiffGranularity
‪DiffGranularity
Definition: DiffGranularity.php:21
‪TYPO3\CMS\Workspaces\Service\HistoryService\$historyEntries
‪array $historyEntries
Definition: HistoryService.php:37
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Workspaces\Service\HistoryService\getHistoryEntry
‪getHistoryEntry(array $entry)
Definition: HistoryService.php:87
‪TYPO3\CMS\Workspaces\Service\HistoryService\__construct
‪__construct()
Definition: HistoryService.php:39
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\CHARACTER
‪@ CHARACTER
Definition: DiffGranularity.php:25
‪TYPO3\CMS\Workspaces\Service\HistoryService\$backendUserNames
‪array $backendUserNames
Definition: HistoryService.php:36