TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
HistoryService.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Workspaces\Service;
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 
20 
25 {
29  protected $backendUserNames;
30 
34  protected $historyObjects = [];
35 
39  protected $differencesObject;
40 
44  public function __construct()
45  {
46  $this->backendUserNames = BackendUtility::getUserNames();
47  }
48 
56  public function getHistory($table, $id)
57  {
58  $history = [];
59  $i = 0;
60  foreach ((array)$this->getHistoryObject($table, $id)->changeLog as $entry) {
61  if ($i++ > 20) {
62  break;
63  }
64  $history[] = $this->getHistoryEntry($entry);
65  }
66  return $history;
67  }
68 
77  protected function getHistoryEntry(array $entry)
78  {
79  if (!empty($entry['action'])) {
80  $differences = $entry['action'];
81  } else {
82  $differences = $this->getDifferences($entry);
83  }
84 
86  $avatar = GeneralUtility::makeInstance(Avatar::class);
87  $beUserRecord = BackendUtility::getRecord('be_users', $entry['user']);
88 
89  return [
90  'datetime' => htmlspecialchars(BackendUtility::datetime($entry['tstamp'])),
91  'user' => htmlspecialchars($this->getUserName($entry['user'])),
92  'user_avatar' => $avatar->render($beUserRecord),
93  'differences' => $differences
94  ];
95  }
96 
104  protected function getDifferences(array $entry)
105  {
106  $differences = [];
107  $tableName = $entry['tablename'];
108  if (is_array($entry['newRecord'])) {
109  $fields = array_keys($entry['newRecord']);
110  foreach ($fields as $field) {
111  if (!empty($GLOBALS['TCA'][$tableName]['columns'][$field]['config']['type']) && $GLOBALS['TCA'][$tableName]['columns'][$field]['config']['type'] !== 'passthrough') {
112  // Create diff-result:
113  $fieldDifferences = $this->getDifferencesObject()->makeDiffDisplay(
114  BackendUtility::getProcessedValue($tableName, $field, $entry['oldRecord'][$field], 0, true),
115  BackendUtility::getProcessedValue($tableName, $field, $entry['newRecord'][$field], 0, true)
116  );
117  if (!empty($fieldDifferences)) {
118  $differences[] = [
119  'label' => $this->getLanguageService()->sL((string)BackendUtility::getItemLabel($tableName, $field)),
120  'html' => nl2br(trim($fieldDifferences)),
121  ];
122  }
123  }
124  }
125  }
126  return $differences;
127  }
128 
135  protected function getUserName($user)
136  {
137  $userName = 'unknown';
138  if (!empty($this->backendUserNames[$user]['username'])) {
139  $userName = $this->backendUserNames[$user]['username'];
140  }
141  return $userName;
142  }
143 
151  protected function getHistoryObject($table, $id)
152  {
153  if (!isset($this->historyObjects[$table][$id])) {
155  $historyObject = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Backend\History\RecordHistory::class);
156  $historyObject->element = $table . ':' . $id;
157  $historyObject->createChangeLog();
158  $this->historyObjects[$table][$id] = $historyObject;
159  }
160  return $this->historyObjects[$table][$id];
161  }
162 
168  protected function getDifferencesObject()
169  {
170  if (!isset($this->differencesObject)) {
171  $this->differencesObject = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Utility\DiffUtility::class);
172  }
174  }
175 
179  protected function getLanguageService()
180  {
181  return $GLOBALS['LANG'];
182  }
183 }
static getRecord($table, $uid, $fields= '*', $where= '', $useDeleteClause=true)
if(TYPO3_MODE=== 'BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static makeInstance($className,...$constructorArguments)
static getUserNames($fields= 'username, usergroup, usergroup_cached_list, uid', $where= '')