[TASK] Re-work/simplify copyright header in PHP files - Part 9
[Packages/TYPO3.CMS.git] / typo3 / sysext / workspaces / Classes / Service / HistoryService.php
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
17 use TYPO3\CMS\Backend\Utility\BackendUtility;
18
19 /**
20 * Service for history
21 *
22 * @author Oliver Hader <oliver.hader@typo3.org>
23 */
24 class HistoryService implements \TYPO3\CMS\Core\SingletonInterface {
25
26 /**
27 * @var array
28 */
29 protected $backendUserNames;
30
31 /**
32 * @var array
33 */
34 protected $historyObjects = array();
35
36 /**
37 * @var \TYPO3\CMS\Core\Utility\DiffUtility
38 */
39 protected $differencesObject;
40
41 /**
42 * Creates this object.
43 */
44 public function __construct() {
45 $this->backendUserNames = BackendUtility::getUserNames();
46 }
47
48 /**
49 * Gets the editing history of a record.
50 *
51 * @param string $table Name of the table
52 * @param integer $id Uid of the record
53 * @return array Record history entries
54 */
55 public function getHistory($table, $id) {
56 $history = array();
57 $i = 0;
58 foreach ($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
67 /**
68 * Gets the human readable representation of one
69 * record history entry.
70 *
71 * @param array $entry Record history entry
72 * @return array
73 * @see getHistory
74 */
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
88 /**
89 * Gets the differences between two record versions out
90 * of one record history entry.
91 *
92 * @param array $entry Record history entry
93 * @return array
94 */
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
119 /**
120 * Gets the username of a backend user.
121 *
122 * @param string $user
123 * @return string
124 */
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
133 /**
134 * Gets an instance of the record history service.
135 *
136 * @param string $table Name of the table
137 * @param integer $id Uid of the record
138 * @return \TYPO3\CMS\Backend\History\RecordHistory
139 */
140 protected function getHistoryObject($table, $id) {
141 if (!isset($this->historyObjects[$table][$id])) {
142 /** @var $historyObject \TYPO3\CMS\Backend\History\RecordHistory */
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
151 /**
152 * Gets an instance of the record differences utility.
153 *
154 * @return \TYPO3\CMS\Core\Utility\DiffUtility
155 */
156 protected function getDifferencesObject() {
157 if (!isset($this->differencesObject)) {
158 $this->differencesObject = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Utility\\DiffUtility');
159 }
160 return $this->differencesObject;
161 }
162
163 /**
164 * @return \TYPO3\CMS\Lang\LanguageService
165 */
166 protected function getLanguageService() {
167 return $GLOBALS['LANG'];
168 }
169
170 }