TYPO3 CMS  TYPO3_7-6
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 
18 
23 {
27  protected $backendUserNames;
28 
32  protected $historyObjects = [];
33 
37  protected $differencesObject;
38 
42  public function __construct()
43  {
44  $this->backendUserNames = BackendUtility::getUserNames();
45  }
46 
54  public function getHistory($table, $id)
55  {
56  $history = [];
57  $i = 0;
58  foreach ((array)$this->getHistoryObject($table, $id)->changeLog as $entry) {
59  if ($i++ > 20) {
60  break;
61  }
62  $history[] = $this->getHistoryEntry($entry);
63  }
64  return $history;
65  }
66 
75  protected function getHistoryEntry(array $entry)
76  {
77  if (!empty($entry['action'])) {
78  $differences = $entry['action'];
79  } else {
80  $differences = $this->getDifferences($entry);
81  }
82  return [
83  'datetime' => htmlspecialchars(BackendUtility::datetime($entry['tstamp'])),
84  'user' => htmlspecialchars($this->getUserName($entry['user'])),
85  'differences' => $differences
86  ];
87  }
88 
96  protected function getDifferences(array $entry)
97  {
98  $differences = [];
99  $tableName = $entry['tablename'];
100  if (is_array($entry['newRecord'])) {
101  $fields = array_keys($entry['newRecord']);
102  foreach ($fields as $field) {
103  if (!empty($GLOBALS['TCA'][$tableName]['columns'][$field]['config']['type']) && $GLOBALS['TCA'][$tableName]['columns'][$field]['config']['type'] !== 'passthrough') {
104  // Create diff-result:
105  $fieldDifferences = $this->getDifferencesObject()->makeDiffDisplay(
106  BackendUtility::getProcessedValue($tableName, $field, $entry['oldRecord'][$field], 0, true),
107  BackendUtility::getProcessedValue($tableName, $field, $entry['newRecord'][$field], 0, true)
108  );
109  if (!empty($fieldDifferences)) {
110  $differences[] = [
111  'label' => $this->getLanguageService()->sl((string)BackendUtility::getItemLabel($tableName, $field)),
112  'html' => nl2br(trim($fieldDifferences)),
113  ];
114  }
115  }
116  }
117  }
118  return $differences;
119  }
120 
127  protected function getUserName($user)
128  {
129  $userName = 'unknown';
130  if (!empty($this->backendUserNames[$user]['username'])) {
131  $userName = $this->backendUserNames[$user]['username'];
132  }
133  return $userName;
134  }
135 
143  protected function getHistoryObject($table, $id)
144  {
145  if (!isset($this->historyObjects[$table][$id])) {
147  $historyObject = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Backend\History\RecordHistory::class);
148  $historyObject->element = $table . ':' . $id;
149  $historyObject->createChangeLog();
150  $this->historyObjects[$table][$id] = $historyObject;
151  }
152  return $this->historyObjects[$table][$id];
153  }
154 
160  protected function getDifferencesObject()
161  {
162  if (!isset($this->differencesObject)) {
163  $this->differencesObject = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Utility\DiffUtility::class);
164  $this->differencesObject->stripTags = false;
165  }
167  }
168 
172  protected function getLanguageService()
173  {
174  return $GLOBALS['LANG'];
175  }
176 }
static getItemLabel($table, $col, $printAllWrap='')
static getUserNames($fields='username, usergroup, usergroup_cached_list, uid', $where='')
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']