Cleanup #11357: Make use of new t3lib_div::redirect function throughout the core
[Packages/TYPO3.CMS.git] / typo3 / class.show_rechis.inc
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2009 Kasper Skaarhoj (kasperYYYY@typo3.com)
6 * (c) 2006-2009 Sebastian Kurfuerst (sebastian@garbage-group.de)
7 * All rights reserved
8 *
9 * This script is part of the TYPO3 project. The TYPO3 project is
10 * free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
17 * A copy is found in the textfile GPL.txt and important notices to the license
18 * from the author is found in LICENSE.txt distributed with these scripts.
19 *
20 *
21 * This script is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
28 /**
29 * Class for the record history display script (show_rechis.php)
30 *
31 * $Id$
32 * XHTML Compliant
33 *
34 * @author Sebastian Kurfuerst <sebastian@garbage-group.de>
35 */
36
37
38 /**
39 * Class for the record history display script (show_rechis.php)
40 *
41 * @author Sebastian Kurfuerst <sebastian@garbage-group.de>
42 * @package TYPO3
43 * @subpackage core
44 */
45 class recordHistory {
46 // External, static:
47 var $maxSteps=20; // Maximum number of sys_history steps to show.
48 var $showDiff=1; // display diff or not (0-no diff, 1-inline)
49 var $showSubElements=1; // on a pages table - show sub elements as well.
50 var $showInsertDelete=1; // show inserts and deletes as well
51
52 // Internal, GPvars
53 var $element; // Element reference, syntax [tablename]:[uid]
54 var $lastSyslogId; // syslog ID which is not shown anymore
55 var $returnUrl;
56
57 // Internal
58 var $changeLog;
59 var $showMarked=FALSE;
60 /**
61 * Constructor for the class
62 *
63 * @return void
64 */
65 function recordHistory() {
66 // GPvars:
67 $this->element = t3lib_div::_GP('element');
68 $this->returnUrl = t3lib_div::_GP('returnUrl');
69 $this->lastSyslogId = t3lib_div::_GP('diff');
70 $this->rollbackFields = t3lib_div::_GP('rollbackFields');
71 // resolve sh_uid if set
72 $this->resolveShUid();
73 }
74
75 /**
76 * Main function for the listing of history.
77 * It detects incoming variables like element reference, history element uid etc. and renders the correct screen.
78 *
79 * @return HTML content for the module
80 */
81 function main() {
82 $content = '';
83
84 // single-click rollback
85 if (t3lib_div::_GP('revert') && t3lib_div::_GP('sumUp')) {
86 $this->rollbackFields = t3lib_div::_GP('revert');
87 $this->showInsertDelete = 0;
88 $this->showSubElements = 0;
89
90 $element = explode(':',$this->element);
91 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*','sys_history', 'tablename='.$GLOBALS['TYPO3_DB']->fullQuoteStr($element[0], 'sys_history').' AND recuid='.intval($element[1]), '', 'uid DESC', '1');
92 $record = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
93 $this->lastSyslogId = $record['sys_log_uid'];
94
95 $this->createChangeLog();
96 $completeDiff = $this->createMultipleDiff();
97 $this->performRollback($completeDiff);
98 t3lib_div::redirect($this->returnUrl);
99 }
100
101 // save snapshot
102 if (t3lib_div::_GP('highlight') && !t3lib_div::_GP('settings')) {
103 $this->toggleHighlight(t3lib_div::_GP('highlight'));
104 }
105
106 $content .= $this->displaySettings();
107 if ($this->createChangeLog()) {
108 if ($this->rollbackFields) {
109 $completeDiff = $this->createMultipleDiff();
110 $content .= $this->performRollback($completeDiff);
111
112 }
113 if ($this->lastSyslogId) {
114 $completeDiff = $this->createMultipleDiff();
115 $content .= $this->displayMultipleDiff($completeDiff);
116 }
117 if ($this->element) {
118 $content .= $this->displayHistory();
119 }
120 }
121 return $content;
122 }
123
124 /*******************************
125 *
126 * database actions
127 *
128 *******************************/
129
130 /**
131 * toggles highlight state of record
132 *
133 * @param integer uid of sys_history entry
134 * @return [type] ...
135 */
136 function toggleHighlight($uid) {
137 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('snapshot','sys_history','uid='.intval($uid));
138 $tmp = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
139 if ($tmp['snapshot']) {
140 $tmp = 0;
141 } else {
142 $tmp = 1;
143 }
144 $updateFields = array('snapshot' => $tmp);
145 $GLOBALS['TYPO3_DB']->exec_UPDATEquery('sys_history','uid='.intval($uid),$updateFields);
146 }
147
148 /**
149 * perform rollback
150 *
151 * @param array diff array to rollback
152 * @return void
153 * @access private
154 */
155 function performRollback($diff) {
156 if (!$this->rollbackFields) {
157 return 0;
158 }
159
160 $reloadPageFrame=0;
161 $rollbackData = explode(':',$this->rollbackFields);
162
163 // PROCESS INSERTS AND DELETES
164 // rewrite inserts and deletes
165 $cmdmapArray = array();
166 if ($diff['insertsDeletes']) {
167
168 switch (count($rollbackData)) {
169 case 1: // all tables
170 $data = $diff['insertsDeletes'];
171 break;
172 case 2: // one record
173 if ($diff['insertsDeletes'][$this->rollbackFields]) {
174 $data[$this->rollbackFields] = $diff['insertsDeletes'][$this->rollbackFields];
175 }
176 break;
177 case 3: // one field in one record -- ignore!
178 break;
179 }
180 if ($data) {
181 foreach ($data as $key => $action) {
182 $elParts = explode(':',$key);
183 if ($action == 1) { // inserted records should be deleted
184 $cmdmapArray[$elParts[0]][$elParts[1]]['delete'] = 1;
185 // when the record is deleted, the contents of the record do not need to be updated
186 unset($diff['oldData'][$key]);
187 unset($diff['newData'][$key]);
188 } elseif ($action == -1) { // deleted records should be inserted again
189 $cmdmapArray[$elParts[0]][$elParts[1]]['undelete'] = 1;
190 }
191 }
192 }
193 }
194 // Writes the data:
195 if ($cmdmapArray) {
196 $tce = t3lib_div::makeInstance('t3lib_TCEmain');
197 $tce->stripslashes_values=0;
198 $tce->debug=0;
199 $tce->dontProcessTransformations=1;
200 $tce->start(array(),$cmdmapArray);
201 $tce->process_cmdmap();
202 unset($tce);
203 if (isset($cmdmapArray['pages'])) {
204 $reloadPageFrame=1;
205 }
206 }
207
208 // PROCESS CHANGES
209 // create an array for process_datamap
210 $diff_modified = array();
211 foreach ($diff['oldData'] as $key => $value) {
212 $splitKey = explode(':',$key);
213 $diff_modified[$splitKey[0]][$splitKey[1]] = $value;
214 }
215 switch (count($rollbackData)) {
216 case 1: // all tables
217 $data = $diff_modified;
218 break;
219 case 2: // one record
220 $data[$rollbackData[0]][$rollbackData[1]] = $diff_modified[$rollbackData[0]][$rollbackData[1]];
221 break;
222 case 3: // one field in one record
223 $data[$rollbackData[0]][$rollbackData[1]][$rollbackData[2]] = $diff_modified[$rollbackData[0]][$rollbackData[1]][$rollbackData[2]];
224 break;
225 }
226 // Removing fields:
227 $data = $this->removeFilefields($rollbackData[0],$data);
228
229 // Writes the data:
230 $tce = t3lib_div::makeInstance('t3lib_TCEmain');
231 $tce->stripslashes_values=0;
232 $tce->debug=0;
233 $tce->dontProcessTransformations=1;
234 $tce->start($data,array());
235 $tce->process_datamap();
236 unset($tce);
237 if (isset($data['pages'])) {
238 $reloadPageFrame=1;
239 }
240
241 // return to normal operation
242 $this->lastSyslogId = FALSE;
243 $this->rollbackFields = FALSE;
244 $this->createChangeLog();
245
246 // reload page frame if necessary
247 if ($reloadPageFrame) {
248 return '<script type="text/javascript">
249 /*<![CDATA[*/
250 if (top.content && top.content.nav_frame && top.content.nav_frame.refresh_nav) {
251 top.content.nav_frame.refresh_nav();
252 }
253 /*]]>*/
254 </script>';
255 }
256 }
257
258 /*******************************
259 *
260 * Display functions
261 *
262 *******************************/
263
264 /**
265 * Displays settings
266 *
267 * @return string HTML code to modify settings
268 */
269 function displaySettings() {
270 global $BE_USER, $LANG, $SOBE;
271 // get current selection from UC, merge data, write it back to UC
272 $currentSelection = is_array($BE_USER->uc['moduleData']['history']) ? $BE_USER->uc['moduleData']['history'] : array('maxSteps' => '', 'showDiff' => 1, 'showSubElements' => 1, 'showInsertDelete' => 1);
273
274 $currentSelectionOverride = t3lib_div::_GP('settings');
275 if ($currentSelectionOverride) {
276 $currentSelection = array_merge($currentSelection,$currentSelectionOverride);
277 $BE_USER->uc['moduleData']['history'] = $currentSelection;
278 $BE_USER->writeUC($BE_USER->uc);
279 }
280
281 // display selector for number of history entries
282 $selector['maxSteps'] = array(
283 10 => 10,
284 20 => 20,
285 50 => 50,
286 100 => 100,
287 '' => 'maxSteps_all',
288 'marked' => 'maxSteps_marked'
289 );
290 $selector['showDiff'] = array(
291 0 => 'showDiff_no',
292 1 => 'showDiff_inline'
293 );
294 $selector['showSubElements'] = array(
295 0 => 'no',
296 1 => 'yes',
297 );
298 $selector['showInsertDelete'] = array(
299 0 => 'no',
300 1 => 'yes',
301 );
302 // render selectors
303 $displayCode = '';
304 foreach ($selector as $key => $values) {
305 $displayCode .= '<tr><td>'.$LANG->getLL($key,1).'</td><td><select name="settings['.$key.']" onChange="document.settings.submit()" style="width:100px">';
306 foreach ($values as $singleKey => $singleVal) {
307 $caption = $LANG->getLL($singleVal,1)?$LANG->getLL($singleVal,1):$singleVal;
308 $displayCode .= '<option value="'.$singleKey.'" '.(($singleKey == $currentSelection[$key])?'selected':'').'> '.$caption.'</option>';
309 }
310 $displayCode .= '</select></td></tr>';
311 }
312 // set values correctly
313 if ($currentSelection['maxSteps'] != 'marked') {
314 $this->maxSteps = $currentSelection['maxSteps']?intval($currentSelection['maxSteps']):'';
315 } else {
316 $this->showMarked = TRUE;
317 $this->maxSteps = FALSE;
318 }
319 $this->showDiff = intval($currentSelection['showDiff']);
320 $this->showSubElements = intval($currentSelection['showSubElements']);
321 $this->showInsertDelete = intval($currentSelection['showInsertDelete']);
322
323 $content = '';
324 // get link to page history if the element history is shown
325 $elParts = explode(':',$this->element);
326 if ($elParts[0] != 'pages') {
327 $content .= '<b>'.$LANG->getLL('elementHistory',1).'</b><br />';
328 $pid = t3lib_BEfunc::getRecordRaw($elParts[0],'uid='.intval($elParts[1]));
329 $content .= $this->linkPage($LANG->getLL('elementHistory_link',1),array('element' => 'pages:'.$pid['pid']));
330 }
331 $content .= '<form name="settings" action="'.t3lib_div::getIndpEnv('TYPO3_REQUEST_URL').'" method="post"><table>'.$displayCode.'</table></form>';
332 return $SOBE->doc->section($LANG->getLL('settings',1),$content,0,1,0,0);
333
334 }
335
336 /**
337 * Shows the full change log
338 *
339 * @return string HTML for list, wrapped in a table.
340 */
341 function displayHistory() {
342 global $LANG;
343 global $SOBE;
344 global $TCA;
345
346 $lines=array();
347
348 // Initialize:
349 $lines[] = '<tr class="bgColor5 c-head">
350 <td> </td>
351 <td>'.$LANG->getLL('time',1).'</td>
352 <td>'.$LANG->getLL('age',1).'</td>
353 <td>'.$LANG->getLL('user',1).'</td>
354 <td>'.$LANG->getLL('tableUid',1).'</td>
355 <td>'.$LANG->getLL('differences',1).'</td>
356 <td>&nbsp;</td>
357 </tr>';
358
359 // get default page TSconfig expiration time
360 $elParts = explode(':',$this->element);
361 if ($elParts[0] != 'pages') {
362 $tmp = t3lib_BEfunc::getRecordRaw($elParts[0],'uid='.intval($elParts[1]));
363 $pid = $tmp['pid'];
364 } else {
365 $pid = $elParts[1];
366 }
367 $tmpTsConfig = $GLOBALS['BE_USER']->getTSConfig('TCEMAIN',t3lib_BEfunc::getPagesTSconfig($pid));
368 $expirationTime = isset($tmpTsConfig['properties']['default.']['history.']['maxAgeDays']) ? $tmpTsConfig['properties']['default.']['history.']['maxAgeDays'] : 30;
369
370 $expirationTimestamp = $expirationTime ? ($GLOBALS['EXEC_TIME'] - 60 * 60 * 24 * $expirationTime) : 0;
371 $expirationWarning = 0;
372
373 $be_user_array = t3lib_BEfunc::getUserNames();
374
375 // Traverse changelog array:
376 if (!$this->changeLog) {
377 return 0;
378 }
379 $i = 0;
380 foreach ($this->changeLog as $sysLogUid => $entry) {
381 // stop after maxSteps
382 if ($i > $this->maxSteps && $this->maxSteps) {
383 break;
384 }
385
386 // display inconsistency warning
387 if ($entry['tstamp'] < $expirationTimestamp && !$expirationWarning) {
388 $expirationWarning = 1;
389
390 $lines[] = '
391 <tr class="bgColor4-20">
392 <td colspan="7"><b>'.$LANG->getLL('consistenceWarning',1).'</b></td>
393 </tr>';
394 }
395
396 // show only marked states
397 if (!$entry['snapshot'] && $this->showMarked) {
398 continue;
399 }
400 $i++;
401 // get user names
402 $userName = ($entry['user']?$be_user_array[$entry['user']]['username']:$LANG->getLL('externalChange',1));
403
404 // build up single line
405 $singleLine = array();
406
407 // diff link
408 $image = '<img'.t3lib_iconWorks::skinImg('','gfx/button_top_right.gif').' align="top" alt="'.$LANG->getLL('sumUpChanges',1).'" title="'.$LANG->getLL('sumUpChanges',1).'" />';
409 $singleLine[] = '<span>'.$this->linkPage($image,array('diff' => $sysLogUid)).'</span>'; // remove first link
410
411 $singleLine[] = htmlspecialchars(t3lib_BEfunc::datetime($entry['tstamp'])); // add time
412 $singleLine[] = htmlspecialchars(t3lib_BEfunc::calcAge($GLOBALS['EXEC_TIME'] - $entry['tstamp'], $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.php:labels.minutesHoursDaysYears'))); // add age
413 $singleLine[] = htmlspecialchars($userName); // add user name
414 $singleLine[] = $this->linkPage($this->generateTitle($entry['tablename'],$entry['recuid']),array('element' => $entry['tablename'].':'.$entry['recuid']),'',$LANG->getLL('linkRecordHistory',1)); // add record UID
415
416 // show insert/delete/diff/changed field names
417 if ($entry['action']) { // insert or delete of element
418 $singleLine[] = '<strong>'.htmlspecialchars($LANG->getLL($entry['action'],1)).'</strong>';
419 } else {
420 if (!$this->showDiff) { // display field names instead of full diff
421 // re-write field names with labels
422 $tmpFieldList = explode(',',$entry['fieldlist']);
423 foreach ($tmpFieldList as $key => $value) {
424 $tmp = str_replace(':','',$LANG->sl(t3lib_BEfunc::getItemLabel($entry['tablename'],$value),1));
425 if($tmp) $tmpFieldList[$key] = $tmp;
426 else unset($tmpFieldList[$key]); // remove fields if no label available
427 }
428 $singleLine[] = htmlspecialchars(implode(',',$tmpFieldList));
429 } else { // display diff
430 $diff = $this->renderDiff($entry,$entry['tablename']);
431 $singleLine[] = $diff;
432 }
433 }
434 // show link to mark/unmark state
435 if (!$entry['action']) {
436 if ($entry['snapshot']) {
437 $image = '<img'.t3lib_iconWorks::skinImg('','gfx/unmarkstate.gif').' align="top" alt="'.$LANG->getLL('unmarkState',1).'" title="'.$LANG->getLL('unmarkState',1).'" />';
438 } else {
439 $image = '<img'.t3lib_iconWorks::skinImg('','gfx/markstate.gif').' align="top" alt="'.$LANG->getLL('markState',1).'" title="'.$LANG->getLL('markState',1).'" />';
440 }
441 $singleLine[] = $this->linkPage($image,array('highlight' => $entry['uid']));
442 } else {
443 $singleLine[] = '';
444 }
445
446 $bgColorClass = $entry['snapshot'] ? 'bgColor2' : 'bgColor4-20';
447 // put line together
448 $lines[] = '
449 <tr class="'.$bgColorClass.'">
450 <td>'.implode('</td><td>',$singleLine).'</td>
451 </tr>';
452 }
453
454 // Finally, put it all together:
455 $theCode = '
456 <!--
457 History (list):
458 -->
459 <table border="0" cellpadding="2" cellspacing="2" id="typo3-history">
460 '.implode('',$lines).'
461 </table>';
462
463 if ($this->lastSyslogId) {
464 $theCode .= '<br />' . $this->linkPage('<img'.t3lib_iconWorks::skinImg('','gfx/group_tobottom.gif').' alt="'.$LANG->getLL('fullView',1).'" title="'.$LANG->getLL('fullView',1).'" />',array('diff' => ''));
465 }
466 // Add message about the difference view.
467 $theCode .= '<br /><img'.t3lib_iconWorks::skinImg('','gfx/icon_note.gif','width="18" height="16"').' align="top" alt="" />'.$LANG->getLL('differenceMsg').'<br /><br />';
468
469 // Add CSH:
470 $theCode .= t3lib_BEfunc::cshItem('xMOD_csh_corebe', 'history_'.($this->sumUp ? 'sum' : 'log'), $GLOBALS['BACK_PATH'],'');
471
472 // Add the whole content as a module section:
473 return $SOBE->doc->section($LANG->getLL('changes'),$theCode,0,1);
474 }
475
476 /**
477 * Displays a diff over multiple fields including rollback links
478 *
479 * @param array difference array
480 * @return string HTML output
481 */
482 function displayMultipleDiff($diff) {
483 global $SOBE, $LANG;
484 $content = '';
485
486 // get all array keys needed
487 $arrayKeys = array_merge(array_keys($diff['newData']),array_keys($diff['insertsDeletes']),array_keys($diff['oldData']));
488 $arrayKeys = array_unique($arrayKeys);
489
490 if ($arrayKeys) {
491 foreach ($arrayKeys as $key) {
492 $record = '';
493 $elParts = explode(':',$key);
494 // turn around diff because it should be a "rollback preview"
495 if ($diff['insertsDeletes'][$key] == 1) { // insert
496 $record .= '<b>'.$LANG->getLL('delete',1).'</b>';
497 $record .= '<br />';
498 } elseif ($diff['insertsDeletes'][$key] == -1) {
499 $record .= '<b>'.$LANG->getLL('insert',1).'</b>';
500 $record .= '<br />';
501 }
502 // build up temporary diff array
503 // turn around diff because it should be a "rollback preview"
504 if ($diff['newData'][$key]) {
505 $tmpArr['newRecord'] = $diff['oldData'][$key];
506 $tmpArr['oldRecord'] = $diff['newData'][$key];
507 $record .= $this->renderDiff($tmpArr, $elParts[0],$elParts[1]);
508 }
509
510 $elParts = explode(':',$key);
511 $titleLine = $this->createRollbackLink($key, $LANG->getLL('revertRecord',1),1) . $this->generateTitle($elParts[0],$elParts[1]);
512 $record = '<div style="margin-left:10px;padding-left:5px;border-left:1px solid black;border-bottom:1px dotted black;padding-bottom:2px;">'.$record.'</div>';
513
514 $content .= $SOBE->doc->section($titleLine,$record,0,0,0,1);
515 }
516 $content = $this->createRollbackLink('ALL', $LANG->getLL('revertAll',1),0) . '<div style="margin-left:10px;padding-left:5px;border-left:1px solid black;border-bottom:1px dotted black;padding-bottom:2px;">'.$content.'</div>';
517 } else {
518 $content = $LANG->getLL('noDifferences',1);
519 }
520 return $SOBE->doc->section($LANG->getLL('mergedDifferences',1),$content,0,1,0,1);
521 }
522
523 /**
524 * Renders HTML table-rows with the comparison information of an sys_history entry record
525 *
526 * @param array sys_history entry record.
527 * @param string The table name
528 * @param integer If set to UID of record, display rollback links
529 * @return string HTML table
530 * @access private
531 */
532 function renderDiff($entry,$table,$rollbackUid=0) {
533 global $SOBE, $LANG, $TCA;
534 $lines=array();
535 if (is_array($entry['newRecord'])) {
536
537 $t3lib_diff_Obj = t3lib_div::makeInstance('t3lib_diff');
538
539 $fieldsToDisplay = array_keys($entry['newRecord']);
540 foreach($fieldsToDisplay as $fN) {
541 t3lib_div::loadTCA($table);
542 if (is_array($TCA[$table]['columns'][$fN]) && $TCA[$table]['columns'][$fN]['config']['type']!='passthrough') {
543
544 // Create diff-result:
545 $diffres = $t3lib_diff_Obj->makeDiffDisplay(
546 t3lib_BEfunc::getProcessedValue($table,$fN,$entry['oldRecord'][$fN],0,1),
547 t3lib_BEfunc::getProcessedValue($table,$fN,$entry['newRecord'][$fN],0,1)
548 );
549 $lines[]='
550 <tr class="bgColor4">
551 '.($rollbackUid?'<td style="width:33px">'.$this->createRollbackLink($table.':'.$rollbackUid.':'.$fN, $LANG->getLL('revertField',1),2).'</td>':'').'
552 <td style="width:90px"><em>'.$LANG->sl(t3lib_BEfunc::getItemLabel($table,$fN),1).'</em></td>
553 <td style="width:300px">'.nl2br($diffres).'</td>
554 </tr>';
555 }
556 }
557 }
558 if ($lines) {
559 $content = '<table border="0" cellpadding="2" cellspacing="2" id="typo3-history-item">
560 '.implode('',$lines).'
561 </table>';
562 return $content;
563 }
564 return NULL; // error fallback
565 }
566
567 /*******************************
568 *
569 * build up history
570 *
571 *******************************/
572
573 /**
574 * Creates a diff between the current version of the records and the selected version
575 *
576 * @return array diff for many elements
577 */
578 function createMultipleDiff() {
579 $insertsDeletes = array();
580 $newArr = array();
581 $differences = array();
582 if (!$this->changeLog) {
583 return 0;
584 }
585
586 // traverse changelog array
587 foreach ($this->changeLog as $key => $value) {
588 $field = $value['tablename'].':'.$value['recuid'];
589 // inserts / deletes
590 if ($value['action']) {
591 if (!$insertsDeletes[$field]) {
592 $insertsDeletes[$field] = 0;
593 }
594 if ($value['action'] == 'insert') {
595 $insertsDeletes[$field]++;
596 } else {
597 $insertsDeletes[$field]--;
598 }
599 // unset not needed fields
600 if ($insertsDeletes[$field] == 0) {
601 unset($insertsDeletes[$field]);
602 }
603 } else {
604 // update fields
605 if (!isset($newArr[$field])) { // first row of field
606 $newArr[$field] = $value['newRecord'];
607 $differences[$field] = $value['oldRecord'];
608 } else { // standard
609 $differences[$field] = array_merge($differences[$field],$value['oldRecord']);
610 }
611 }
612 }
613
614 // remove entries where there were no changes effectively
615 foreach ($newArr as $record => $value) {
616 foreach ($value as $key => $innerVal) {
617 if ($newArr[$record][$key] == $differences[$record][$key]) {
618 unset($newArr[$record][$key]);
619 unset($differences[$record][$key]);
620 }
621 }
622 if (empty($newArr[$record]) && empty($differences[$record])) {
623 unset($newArr[$record]);
624 unset($differences[$record]);
625 }
626 }
627 return array(
628 'newData' => $newArr,
629 'oldData' => $differences,
630 'insertsDeletes' => $insertsDeletes
631 );
632 }
633
634 /**
635 * Creates change log including sub-elements, filling $this->changeLog
636 *
637 * @return [type] ...
638 */
639 function createChangeLog() {
640
641 global $TCA;
642 $elParts = explode(':',$this->element);
643 $changeLog = $this->getHistoryData($elParts[0],$elParts[1]);
644
645 // get history of tables of this page and merge it into changelog
646 if ($elParts[0] == 'pages' && $this->showSubElements) {
647 foreach ($TCA as $tablename => $value) {
648 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid',$tablename,'pid='.intval($elParts[1])); // check if there are records on the page
649 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
650 if ($newChangeLog = $this->getHistoryData($tablename, $row['uid'])) { // if there is history data available, merge it into changelog
651 foreach ($newChangeLog as $key => $value) {
652 $changeLog[$key] = $value;
653 }
654 }
655 }
656 }
657 }
658 if(!$changeLog) {
659 return 0;
660 }
661
662 krsort($changeLog);
663 $this->changeLog = $changeLog;
664
665 return 1;
666 }
667
668 /**
669 * Gets history and delete/insert data from sys_log and sys_history
670 *
671 * @param string DB table name
672 * @param integer UID of record
673 * @return array history data of the record
674 */
675 function getHistoryData($table,$uid) {
676 global $TCA;
677 $uid = $this->resolveElement($table,$uid);
678 // If table is found in $TCA:
679 if ($TCA[$table]) {
680 // Selecting the $this->maxSteps most recent states:
681 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
682 'sys_history.*,sys_log.userid',
683 'sys_history,sys_log',
684 'sys_history.sys_log_uid=sys_log.uid
685 AND sys_history.tablename='.$GLOBALS['TYPO3_DB']->fullQuoteStr($table, 'sys_history').'
686 AND sys_history.recuid='.intval($uid),
687 '',
688 'sys_log.uid DESC',
689 $this->maxSteps
690 );
691
692 // Traversing the result, building up changesArray / changeLog:
693 #$changesArray=array(); // used temporarily to track intermedia changes
694 $changeLog=array();
695 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
696 // only history until a certain syslog ID needed
697 if ($row['sys_log_uid'] < $this->lastSyslogId && $this->lastSyslogId) {
698 continue;
699 }
700 $hisDat = unserialize($row['history_data']);
701 if (is_array($hisDat['newRecord']) && is_array($hisDat['oldRecord'])) {
702
703 // Add hisDat to the changeLog
704 $hisDat['uid']=$row['uid'];
705 $hisDat['tstamp']=$row['tstamp'];
706 $hisDat['user']=$row['userid'];
707 $hisDat['snapshot']=$row['snapshot'];
708 $hisDat['fieldlist']=$row['fieldlist'];
709 $hisDat['tablename']=$row['tablename'];
710 $hisDat['recuid']=$row['recuid'];
711
712 $changeLog[$row['sys_log_uid']]=$hisDat;
713
714 // Update change array
715 // This is used to detect if any intermedia changes have been made.
716 #$changesArray = array_merge($changesArray,$hisDat['oldRecord']);
717 } else {
718 debug('ERROR: [getHistoryData]');
719 return 0; // error fallback
720 }
721 }
722 // SELECT INSERTS/DELETES
723 if ($this->showInsertDelete) {
724 // Select most recent inserts and deletes // WITHOUT snapshots
725 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
726 'uid,userid,action,tstamp',
727 'sys_log',
728 'type=1
729 AND ( action=1 OR action=3 )
730 AND tablename='.$GLOBALS['TYPO3_DB']->fullQuoteStr($table, 'sys_log').'
731 AND recuid='.intval($uid),
732 '',
733 'uid DESC',
734 $this->maxSteps
735 );
736 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
737
738 if ($row['uid'] < $this->lastSyslogId && $this->lastSyslogId) {
739 continue;
740 }
741 $hisDat = array();
742
743 switch ($row['action']) {
744 case 1: // Insert
745 $hisDat['action'] = 'insert';
746 break;
747 case 3: // Delete
748 $hisDat['action'] = 'delete';
749 break;
750 }
751 $hisDat['tstamp']=$row['tstamp'];
752 $hisDat['user']=$row['userid'];
753 $hisDat['tablename'] = $table;
754 $hisDat['recuid'] = $uid;
755 $changeLog[$row['uid']] = $hisDat;
756 }
757 }
758 return $changeLog;
759 }
760 return 0; // error fallback
761 }
762
763
764 /*******************************
765 *
766 * Various helper functions
767 *
768 *******************************/
769
770 /**
771 * generates the title and puts the record title behind
772 *
773 * @param [type] $table: ...
774 * @param [type] $uid: ...
775 * @return [type] ...
776 */
777 function generateTitle($table, $uid) {
778 global $TCA;
779
780 $out = $table.':'.$uid;
781 if ($labelField = $TCA[$table]['ctrl']['label']) {
782 $record = t3lib_BEfunc::getRecordRaw($table, 'uid='.intval($uid));
783 $out .= ' ('.t3lib_BEfunc::getRecordTitle($table, $record, TRUE).')';
784 }
785 return $out;
786 }
787 /**
788 * creates a link for the rollback
789 *
790 * @param sting parameter which is set to rollbackFields
791 * @param string optional, alternative label and title tag of image
792 * @param integer optional, type of rollback: 0 - ALL; 1 - element; 2 - field
793 * @return string HTML output
794 */
795 function createRollbackLink($key, $alt='', $type=0) {
796 global $LANG;
797
798 return $this->linkPage('<img '.t3lib_iconWorks::skinImg('','gfx/revert_'.$type.'.gif','width="33" height="33"').' alt="'.$alt.'" title="'.$alt.'" align="middle" />',array('rollbackFields'=>$key));
799 }
800
801 /**
802 * Creates a link to the same page.
803 *
804 * @param string String to wrap in <a> tags (must be htmlspecialchars()'ed prior to calling function)
805 * @param array Array of key/value pairs to override the default values with.
806 * @param string Possible anchor value.
807 * @param string Possible title.
808 * @return string Link.
809 * @access private
810 */
811 function linkPage($str,$inparams=array(),$anchor='',$title='') {
812
813 // Setting default values based on GET parameters:
814 $params['element']=$this->element;
815 $params['returnUrl']=$this->returnUrl;
816 $params['diff']=$this->lastSyslogId;
817 // Mergin overriding values:
818 $params = array_merge($params,$inparams);
819
820 // Make the link:
821 $Ahref = 'show_rechis.php?'.t3lib_div::implodeArrayForUrl('',$params).($anchor?'#'.$anchor:'');
822 $link = '<a href="'.htmlspecialchars($Ahref).'"'.($title?' title="'.$title.'"':'').'>'.$str.'</a>';
823
824 // Return link:
825 return $link;
826 }
827
828 /**
829 * Will traverse the field names in $dataArray and look in $TCA if the fields are of types which cannot be handled by the sys_history (that is currently group types with internal_type set to "file")
830 *
831 * @param string Table name
832 * @param array The data array
833 * @return array The modified data array
834 * @access private
835 */
836 function removeFilefields($table,$dataArray) {
837 global $TCA;
838
839 if ($TCA[$table]) {
840 t3lib_div::loadTCA($table);
841
842 foreach($TCA[$table]['columns'] as $field => $config) {
843 if ($config['config']['type']=='group' && $config['config']['internal_type']=='file') {
844 unset($dataArray[$field]);
845 }
846 }
847 }
848 return $dataArray;
849 }
850
851 /**
852 * Convert input element reference to workspace version if any.
853 *
854 * @param string table of input element
855 * @param integer UID of record
856 * @return integer converted UID of record
857 */
858 function resolveElement($table,$uid) {
859 if (isset($GLOBALS['TCA'][$table])) {
860 if ($workspaceVersion = t3lib_BEfunc::getWorkspaceVersionOfRecord($GLOBALS['BE_USER']->workspace, $table, $uid, 'uid')) {
861 $uid = $workspaceVersion['uid'];
862 }
863 }
864 return $uid;
865 }
866
867 /**
868 * resolve sh_uid (used from log)
869 *
870 * @return [type] ...
871 */
872 function resolveShUid() {
873 if (t3lib_div::_GP('sh_uid')) {
874 $sh_uid = t3lib_div::_GP('sh_uid');
875 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*','sys_history', 'uid='.intval($sh_uid));
876 $record = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
877 $this->element = $record['tablename'].':'.$record['recuid'];
878 $this->lastSyslogId = $record['sys_log_uid']-1;
879 }
880 }
881 }
882 ?>