‪TYPO3CMS  10.4
HistoryService.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
25 
30 {
34  protected ‪$backendUserNames;
35 
39  protected ‪$historyEntries = [];
40 
44  protected ‪$differencesObject;
45 
49  public function ‪__construct()
50  {
51  $this->backendUserNames = ‪BackendUtility::getUserNames();
52  }
53 
61  public function ‪getHistory($table, $id)
62  {
63  $history = [];
64  $i = 0;
65  foreach ((array)$this->‪getHistoryEntries($table, $id) as $entry) {
66  if ($i++ > 20) {
67  break;
68  }
69  $history[] = $this->‪getHistoryEntry($entry);
70  }
71  return $history;
72  }
73 
82  protected function ‪getHistoryEntry(array $entry)
83  {
84  if (!empty($entry['action'])) {
85  $differences = $entry['action'];
86  } else {
87  $differences = $this->‪getDifferences($entry);
88  }
89 
90  $avatar = GeneralUtility::makeInstance(Avatar::class);
91  $beUserRecord = ‪BackendUtility::getRecord('be_users', $entry['userid']);
92 
93  return [
94  'datetime' => htmlspecialchars(‪BackendUtility::datetime($entry['tstamp'])),
95  'user' => htmlspecialchars($this->‪getUserName($entry['userid'])),
96  'user_avatar' => $avatar->render($beUserRecord),
97  'differences' => $differences
98  ];
99  }
100 
108  protected function ‪getDifferences(array $entry)
109  {
110  $differences = [];
111  $tableName = $entry['tablename'];
112  if (is_array($entry['newRecord'])) {
113  ‪$fields = array_keys($entry['newRecord']);
114  foreach (‪$fields as $field) {
115  if (!empty(‪$GLOBALS['TCA'][$tableName]['columns'][$field]['config']['type']) && ‪$GLOBALS['TCA'][$tableName]['columns'][$field]['config']['type'] !== 'passthrough') {
116  // Create diff-result:
117  $fieldDifferences = $this->‪getDifferencesObject()->‪makeDiffDisplay(
118  ‪BackendUtility::getProcessedValue($tableName, $field, $entry['oldRecord'][$field], 0, true),
119  ‪BackendUtility::getProcessedValue($tableName, $field, $entry['newRecord'][$field], 0, true)
120  );
121  if (!empty($fieldDifferences)) {
122  $differences[] = [
123  'label' => $this->‪getLanguageService()->‪sL((string)‪BackendUtility::getItemLabel($tableName, $field)),
124  'html' => nl2br(trim($fieldDifferences)),
125  ];
126  }
127  }
128  }
129  }
130  return $differences;
131  }
132 
139  protected function ‪getUserName($user)
140  {
141  $userName = 'unknown';
142  if (!empty($this->backendUserNames[$user]['username'])) {
143  $userName = $this->backendUserNames[$user]['username'];
144  }
145  return $userName;
146  }
147 
155  protected function ‪getHistoryEntries($table, $id)
156  {
157  if (!isset($this->historyEntries[$table][$id])) {
158  $this->historyEntries[$table][$id] = GeneralUtility::makeInstance(RecordHistory::class)
159  ->getHistoryDataForRecord($table, $id);
160  }
161  return $this->historyEntries[$table][$id];
162  }
163 
169  protected function ‪getDifferencesObject()
170  {
171  if (!isset($this->differencesObject)) {
172  $this->differencesObject = GeneralUtility::makeInstance(DiffUtility::class);
173  $this->differencesObject->stripTags = false;
174  }
176  }
177 
181  protected function ‪getLanguageService()
182  {
183  return ‪$GLOBALS['LANG'];
184  }
185 }
‪TYPO3\CMS\Core\Utility\DiffUtility
Definition: DiffUtility.php:25
‪TYPO3\CMS\Workspaces\Service\HistoryService\getHistoryEntries
‪array getHistoryEntries($table, $id)
Definition: HistoryService.php:152
‪TYPO3\CMS\Workspaces\Service\HistoryService\getUserName
‪string getUserName($user)
Definition: HistoryService.php:136
‪TYPO3\CMS\Backend\Utility\BackendUtility\datetime
‪static string datetime($value)
Definition: BackendUtility.php:989
‪TYPO3\CMS\Workspaces\Service
Definition: AdditionalColumnService.php:16
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar
Definition: Avatar.php:33
‪TYPO3\CMS\Backend\Utility\BackendUtility\getItemLabel
‪static string getItemLabel($table, $col)
Definition: BackendUtility.php:1521
‪TYPO3\CMS\Backend\Utility\BackendUtility\getUserNames
‪static array getUserNames($fields='username, usergroup, usergroup_cached_list, uid', $where='')
Definition: BackendUtility.php:818
‪TYPO3\CMS\Core\Localization\LanguageService\sL
‪string sL($input)
Definition: LanguageService.php:194
‪$fields
‪$fields
Definition: pages.php:5
‪TYPO3\CMS\Workspaces\Service\HistoryService\$differencesObject
‪DiffUtility $differencesObject
Definition: HistoryService.php:41
‪TYPO3\CMS\Workspaces\Service\HistoryService\getDifferencesObject
‪DiffUtility getDifferencesObject()
Definition: HistoryService.php:166
‪TYPO3\CMS\Backend\History\RecordHistory
Definition: RecordHistory.php:33
‪TYPO3\CMS\Workspaces\Service\HistoryService
Definition: HistoryService.php:30
‪TYPO3\CMS\Workspaces\Service\HistoryService\getHistory
‪array getHistory($table, $id)
Definition: HistoryService.php:58
‪TYPO3\CMS\Workspaces\Service\HistoryService\getLanguageService
‪LanguageService getLanguageService()
Definition: HistoryService.php:178
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:75
‪TYPO3\CMS\Backend\Utility\BackendUtility\getRecord
‪static array null getRecord($table, $uid, $fields=' *', $where='', $useDeleteClause=true)
Definition: BackendUtility.php:95
‪TYPO3\CMS\Core\Utility\DiffUtility\makeDiffDisplay
‪string makeDiffDisplay($str1, $str2)
Definition: DiffUtility.php:39
‪TYPO3\CMS\Workspaces\Service\HistoryService\getHistoryEntry
‪array getHistoryEntry(array $entry)
Definition: HistoryService.php:79
‪TYPO3\CMS\Workspaces\Service\HistoryService\getDifferences
‪array getDifferences(array $entry)
Definition: HistoryService.php:105
‪TYPO3\CMS\Backend\Utility\BackendUtility\getProcessedValue
‪static string null getProcessedValue( $table, $col, $value, $fixed_lgd_chars=0, $defaultPassthrough=false, $noRecordLookup=false, $uid=0, $forceResult=true, $pid=0)
Definition: BackendUtility.php:1664
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:23
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Workspaces\Service\HistoryService\$historyEntries
‪array $historyEntries
Definition: HistoryService.php:37
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Workspaces\Service\HistoryService\__construct
‪__construct()
Definition: HistoryService.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Workspaces\Service\HistoryService\$backendUserNames
‪array $backendUserNames
Definition: HistoryService.php:33