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