[FEATURE] Integrate element history service
[Packages/TYPO3.CMS.git] / typo3 / sysext / workspaces / Classes / Service / History.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2012 Oliver Hader <oliver.hader@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * @author Oliver Hader <oliver.hader@typo3.org>
30 * @package Workspaces
31 * @subpackage Service
32 */
33 class Tx_Workspaces_Service_History implements t3lib_Singleton {
34 /**
35 * @var array
36 */
37 protected $backendUserNames;
38
39 /**
40 * @var array
41 */
42 protected $historyObjects = array();
43
44 /**
45 * @var t3lib_diff
46 */
47 protected $differencesObject;
48
49 /**
50 * Creates this object.
51 */
52 public function __construct() {
53 require_once PATH_typo3 . 'class.show_rechis.inc';
54 $this->backendUserNames = t3lib_BEfunc::getUserNames();
55 }
56
57 /**
58 * Gets the editing history of a record.
59 *
60 * @param string $table Name of the table
61 * @param integer $id Uid of the record
62 * @return array Record history entries
63 */
64 public function getHistory($table, $id) {
65 $history = array();
66 $i = 0;
67
68 foreach ($this->getHistoryObject($table, $id)->changeLog as $entry) {
69 if ($i++ > 20) {
70 break;
71 }
72
73 $history[] = $this->getHistoryEntry($entry);
74 }
75
76 return $history;
77 }
78
79 /**
80 * Gets the human readable representation of one
81 * record history entry.
82 *
83 * @param array $entry Record history entry
84 * @return array
85 * @see getHistory
86 */
87 protected function getHistoryEntry(array $entry) {
88 if (!empty($entry['action'])) {
89 $differences = $entry['action'];
90 } else {
91 $differences = implode(
92 '<br/>',
93 $this->getDifferences($entry)
94 );
95 }
96
97 return array(
98 'datetime' => htmlspecialchars(t3lib_BEfunc::datetime($entry['tstamp'])),
99 'user' => htmlspecialchars($this->getUserName($entry['user'])),
100 'differences' => $differences,
101 );
102 }
103
104 /**
105 * Gets the differences between two record versions out
106 * of one record history entry.
107 *
108 * @param array $entry Record history entry
109 * @return array
110 */
111 protected function getDifferences(array $entry) {
112 $differences = array();
113 $tableName = $entry['tablename'];
114
115 if (is_array($entry['newRecord'])) {
116 $fields = array_keys($entry['newRecord']);
117
118 foreach ($fields as $field) {
119 t3lib_div::loadTCA($tableName);
120
121 if (!empty($GLOBALS['TCA'][$tableName]['columns'][$field]['config']['type']) && $GLOBALS['TCA'][$tableName]['columns'][$field]['config']['type'] !== 'passthrough') {
122
123 // Create diff-result:
124 $fieldDifferences = $this->getDifferencesObject()->makeDiffDisplay(
125 t3lib_BEfunc::getProcessedValue($tableName, $field, $entry['oldRecord'][$field], 0, TRUE),
126 t3lib_BEfunc::getProcessedValue($tableName ,$field, $entry['newRecord'][$field], 0, TRUE)
127 );
128
129 $differences[] = nl2br($fieldDifferences);
130 }
131 }
132 }
133
134 return $differences;
135 }
136
137 /**
138 * Gets the username of a backend user.
139 *
140 * @param string $user
141 * @return string
142 */
143 protected function getUserName($user) {
144 $userName = 'unknown';
145
146 if (!empty($this->backendUserNames[$user]['username'])) {
147 $userName = $this->backendUserNames[$user]['username'];
148 }
149
150 return $userName;
151 }
152
153 /**
154 * Gets an instance of the record history service.
155 *
156 * @param string $table Name of the table
157 * @param integer $id Uid of the record
158 * @return recordHistory
159 */
160 protected function getHistoryObject($table, $id) {
161 if (!isset($this->historyObjects[$table][$id])) {
162 /** @var $historyObject recordHistory */
163 $historyObject = t3lib_div::makeInstance('recordHistory');
164 $historyObject->element = $table . ':' . $id;
165 $historyObject->createChangeLog();
166
167 $this->historyObjects[$table][$id] = $historyObject;
168 }
169
170 return $this->historyObjects[$table][$id];
171 }
172
173 /**
174 * Gets an instance of the record differences utility.
175 *
176 * @return t3lib_diff
177 */
178 protected function getDifferencesObject() {
179 if (!isset($this->differencesObject)) {
180 $this->differencesObject = t3lib_div::makeInstance('t3lib_diff');
181 }
182
183 return $this->differencesObject;
184 }
185 }
186 ?>