TYPO3 CMS  TYPO3_6-2
HistoryService.php
Go to the documentation of this file.
1 <?php
3 
18 
25 
29  protected $backendUserNames;
30 
34  protected $historyObjects = array();
35 
39  protected $differencesObject;
40 
44  public function __construct() {
45  $this->backendUserNames = BackendUtility::getUserNames();
46  }
47 
55  public function getHistory($table, $id) {
56  $history = array();
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  if (!empty($entry['action'])) {
77  $differences = $entry['action'];
78  } else {
79  $differences = $this->getDifferences($entry);
80  }
81  return array(
82  'datetime' => htmlspecialchars(BackendUtility::datetime($entry['tstamp'])),
83  'user' => htmlspecialchars($this->getUserName($entry['user'])),
84  'differences' => $differences
85  );
86  }
87 
95  protected function getDifferences(array $entry) {
96  $differences = array();
97  $tableName = $entry['tablename'];
98  if (is_array($entry['newRecord'])) {
99  $fields = array_keys($entry['newRecord']);
100  foreach ($fields as $field) {
101  if (!empty($GLOBALS['TCA'][$tableName]['columns'][$field]['config']['type']) && $GLOBALS['TCA'][$tableName]['columns'][$field]['config']['type'] !== 'passthrough') {
102  // Create diff-result:
103  $fieldDifferences = $this->getDifferencesObject()->makeDiffDisplay(
104  BackendUtility::getProcessedValue($tableName, $field, $entry['oldRecord'][$field], 0, TRUE),
105  BackendUtility::getProcessedValue($tableName, $field, $entry['newRecord'][$field], 0, TRUE)
106  );
107  if (!empty($fieldDifferences)) {
108  $differences[] = array(
109  'label' => $this->getLanguageService()->sl((string)BackendUtility::getItemLabel($tableName, $field)),
110  'html' => nl2br(trim($fieldDifferences)),
111  );
112  }
113  }
114  }
115  }
116  return $differences;
117  }
118 
125  protected function getUserName($user) {
126  $userName = 'unknown';
127  if (!empty($this->backendUserNames[$user]['username'])) {
128  $userName = $this->backendUserNames[$user]['username'];
129  }
130  return $userName;
131  }
132 
140  protected function getHistoryObject($table, $id) {
141  if (!isset($this->historyObjects[$table][$id])) {
143  $historyObject = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\History\\RecordHistory');
144  $historyObject->element = $table . ':' . $id;
145  $historyObject->createChangeLog();
146  $this->historyObjects[$table][$id] = $historyObject;
147  }
148  return $this->historyObjects[$table][$id];
149  }
150 
156  protected function getDifferencesObject() {
157  if (!isset($this->differencesObject)) {
158  $this->differencesObject = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Utility\\DiffUtility');
159  }
161  }
162 
166  protected function getLanguageService() {
167  return $GLOBALS['LANG'];
168  }
169 
170 }
static getItemLabel($table, $col, $printAllWrap='')
static getUserNames($fields='username, usergroup, usergroup_cached_list, uid', $where='')
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]