[FEATURE] Integrate element history service
authorOliver Hader <oliver@typo3.org>
Sun, 19 Aug 2012 13:04:50 +0000 (15:04 +0200)
committerOliver Hader <oliver.hader@typo3.org>
Mon, 20 Aug 2012 14:26:10 +0000 (16:26 +0200)
The old workspace module had a possibility to show the
chronological changes of elements in the workspace module.
This feature thus reintroduces the behaviour by using the
already available record history that is shown in a new modal
window now.

Change-Id: I0f8d4ed4e6d3ea6ceea4031076a06b8395d76b59
Resolves: #39966
Releases: 6.0
Reviewed-on: http://review.typo3.org/13928
Reviewed-by: Wouter Wolters
Reviewed-by: Oliver Hader
Tested-by: Oliver Hader
typo3/sysext/workspaces/Classes/ExtDirect/Server.php
typo3/sysext/workspaces/Classes/Service/History.php [new file with mode: 0644]
typo3/sysext/workspaces/Resources/Private/Language/locallang.xlf
typo3/sysext/workspaces/Resources/Public/JavaScript/configuration.js
typo3/sysext/workspaces/Resources/Public/JavaScript/helpers.js
typo3/sysext/workspaces/ext_autoload.php

index c085ee0..3a47205 100644 (file)
@@ -92,6 +92,23 @@ class Tx_Workspaces_ExtDirect_Server extends Tx_Workspaces_ExtDirect_AbstractHan
        }
 
        /**
+        * Gets the editing history of a record.
+        *
+        * @param stdClass $parameters
+        * @return array
+        */
+       public function getHistory($parameters) {
+               /** @var $historyService Tx_Workspaces_Service_History */
+               $historyService = t3lib_div::makeInstance('Tx_Workspaces_Service_History');
+               $history = $historyService->getHistory($parameters->table, $parameters->versionId);
+
+               return array(
+                       'data' => $history,
+                       'total' => count($history),
+               );
+       }
+
+       /**
         * Get List of available workspace actions
         *
         * @param object $parameter
diff --git a/typo3/sysext/workspaces/Classes/Service/History.php b/typo3/sysext/workspaces/Classes/Service/History.php
new file mode 100644 (file)
index 0000000..85323ab
--- /dev/null
@@ -0,0 +1,186 @@
+<?php
+/***************************************************************
+ * Copyright notice
+ *
+ * (c) 2012 Oliver Hader <oliver.hader@typo3.org>
+ * All rights reserved
+ *
+ * This script is part of the TYPO3 project. The TYPO3 project is
+ * free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * The GNU General Public License can be found at
+ * http://www.gnu.org/copyleft/gpl.html.
+ * A copy is found in the textfile GPL.txt and important notices to the license
+ * from the author is found in LICENSE.txt distributed with these scripts.
+ *
+ *
+ * This script is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * This copyright notice MUST APPEAR in all copies of the script!
+ ***************************************************************/
+
+/**
+ * @author Oliver Hader <oliver.hader@typo3.org>
+ * @package Workspaces
+ * @subpackage Service
+ */
+class Tx_Workspaces_Service_History implements t3lib_Singleton {
+       /**
+        * @var array
+        */
+       protected $backendUserNames;
+
+       /**
+        * @var array
+        */
+       protected $historyObjects = array();
+
+       /**
+        * @var t3lib_diff
+        */
+       protected $differencesObject;
+
+       /**
+        * Creates this object.
+        */
+       public function __construct() {
+               require_once PATH_typo3 . 'class.show_rechis.inc';
+               $this->backendUserNames = t3lib_BEfunc::getUserNames();
+       }
+
+       /**
+        * Gets the editing history of a record.
+        *
+        * @param string $table Name of the table
+        * @param integer $id Uid of the record
+        * @return array Record history entries
+        */
+       public function getHistory($table, $id) {
+               $history = array();
+               $i = 0;
+
+               foreach ($this->getHistoryObject($table, $id)->changeLog as $entry) {
+                       if ($i++ > 20) {
+                               break;
+                       }
+
+                       $history[] = $this->getHistoryEntry($entry);
+               }
+
+               return $history;
+       }
+
+       /**
+        * Gets the human readable representation of one
+        * record history entry.
+        *
+        * @param array $entry Record history entry
+        * @return array
+        * @see getHistory
+        */
+       protected function getHistoryEntry(array $entry) {
+               if (!empty($entry['action'])) {
+                       $differences = $entry['action'];
+               } else {
+                       $differences = implode(
+                               '<br/>',
+                               $this->getDifferences($entry)
+                       );
+               }
+
+               return array(
+                       'datetime' => htmlspecialchars(t3lib_BEfunc::datetime($entry['tstamp'])),
+                       'user' => htmlspecialchars($this->getUserName($entry['user'])),
+                       'differences' => $differences,
+               );
+       }
+
+       /**
+        * Gets the differences between two record versions out
+        * of one record history entry.
+        *
+        * @param array $entry Record history entry
+        * @return array
+        */
+       protected function getDifferences(array $entry) {
+               $differences = array();
+               $tableName = $entry['tablename'];
+
+               if (is_array($entry['newRecord'])) {
+                       $fields = array_keys($entry['newRecord']);
+
+                       foreach ($fields as $field) {
+                               t3lib_div::loadTCA($tableName);
+
+                               if (!empty($GLOBALS['TCA'][$tableName]['columns'][$field]['config']['type']) && $GLOBALS['TCA'][$tableName]['columns'][$field]['config']['type'] !== 'passthrough') {
+
+                                               // Create diff-result:
+                                       $fieldDifferences = $this->getDifferencesObject()->makeDiffDisplay(
+                                               t3lib_BEfunc::getProcessedValue($tableName, $field, $entry['oldRecord'][$field], 0, TRUE),
+                                               t3lib_BEfunc::getProcessedValue($tableName ,$field, $entry['newRecord'][$field], 0, TRUE)
+                                       );
+
+                                       $differences[] = nl2br($fieldDifferences);
+                               }
+                       }
+               }
+
+               return $differences;
+       }
+
+       /**
+        * Gets the username of a backend user.
+        *
+        * @param string $user
+        * @return string
+        */
+       protected function getUserName($user) {
+               $userName = 'unknown';
+
+               if (!empty($this->backendUserNames[$user]['username'])) {
+                       $userName = $this->backendUserNames[$user]['username'];
+               }
+
+               return $userName;
+       }
+
+       /**
+        * Gets an instance of the record history service.
+        *
+        * @param string $table Name of the table
+        * @param integer $id Uid of the record
+        * @return recordHistory
+        */
+       protected function getHistoryObject($table, $id) {
+               if (!isset($this->historyObjects[$table][$id])) {
+                       /** @var $historyObject recordHistory */
+                       $historyObject = t3lib_div::makeInstance('recordHistory');
+                       $historyObject->element = $table . ':' . $id;
+                       $historyObject->createChangeLog();
+
+                       $this->historyObjects[$table][$id] = $historyObject;
+               }
+
+               return $this->historyObjects[$table][$id];
+       }
+
+       /**
+        * Gets an instance of the record differences utility.
+        *
+        * @return t3lib_diff
+        */
+       protected function getDifferencesObject() {
+               if (!isset($this->differencesObject)) {
+                       $this->differencesObject = t3lib_div::makeInstance('t3lib_diff');
+               }
+
+               return $this->differencesObject;
+       }
+}
+?>
\ No newline at end of file
index 95b5f6e..48f3fd8 100644 (file)
                        <trans-unit id="tooltip.swap" xml:space="preserve">
                                <source>Swap live and workspace versions of record</source>
                        </trans-unit>
+                       <trans-unit id="tooltip.showHistory" xml:space="preserve">
+                               <source>Show history of workspace version</source>
+                       </trans-unit>
                        <trans-unit id="window.discard.title" xml:space="preserve">
                                <source>Discard workspace version of record.</source>
                        </trans-unit>
index 961d497..0fabff2 100644 (file)
@@ -331,6 +331,17 @@ TYPO3.Workspaces.Configuration.RowButtons = {
                                        return '';
                                }
                        }
+               },
+               {
+                       iconCls: 't3-icon t3-icon-actions t3-icon-actions-document t3-icon-document-history-open',
+                       tooltip: TYPO3.l10n.localize('tooltip.showHistory'),
+                       handler: function(grid, rowIndex, colIndex) {
+                               var record = TYPO3.Workspaces.MainStore.getAt(rowIndex);
+                               TYPO3.Workspaces.Helpers.getHistoryWindow({
+                                       table: record.json.table,
+                                       versionId: record.json.uid
+                               });
+                       }
                }
        ]
 };
index 5128a28..8673a28 100644 (file)
@@ -105,5 +105,77 @@ TYPO3.Workspaces.Helpers = {
                });
 
                return elements;
+       },
+
+       getHistoryWindow: function(configuration) {
+               top.TYPO3.Windows.close('historyWindow');
+               return top.TYPO3.Windows.showWindow({
+                       id: 'historyWindow',
+                       title: 'Record History',
+                       stateful: false,
+                       modal: false,
+
+                       autoHeight: true,
+                       boxMaxHeight: 500,
+                       width: 700,
+
+                       buttons: [
+                               {
+                                       text: TYPO3.l10n.localize('ok'),
+                                       handler: function(event) {
+                                               top.TYPO3.Windows.close('historyWindow');
+                                       }
+                               }
+                       ],
+
+                       items: [
+                               {
+                                       xtype: 'grid',
+
+                                       border : false,
+                                       loadMask : true,
+                                       stripeRows: true,
+                                       autoHeight: true,
+                                       style: 'min-height: 100px',
+
+                                       viewConfig: {
+                                               forceFit: true
+                                       },
+
+                                       store: {
+                                               xtype: 'directstore',
+                                               autoLoad: true,
+                                               autoDestroy: true,
+                                               reader: new Ext.data.JsonReader({
+                                                       idProperty : 'id',
+                                                       root : 'data',
+                                                       totalProperty : 'total',
+                                                       fields: [
+                                                               { name: 'datetime' },
+                                                               { name: 'user' },
+                                                               { name: 'differences' }
+                                                       ]
+                                               }),
+                                               proxy: new Ext.data.DirectProxy({
+                                                       directFn : TYPO3.Workspaces.ExtDirect.getHistory
+                                               }),
+                                               listeners: {
+                                                       beforeload: function(store, options) {
+                                                               store.setBaseParam('table', configuration.table);
+                                                               store.setBaseParam('versionId', configuration.versionId);
+                                                       }
+                                               }
+                                       },
+
+                                       colModel: new Ext.grid.ColumnModel({
+                                               columns: [
+                                                       { width: 30, id: 'datetime', header: 'Date' },
+                                                       { width: 20, id: 'user', header: 'User', dataIndex: 'user' },
+                                                       { id: 'differences', header: 'Differences', dataIndex: 'differences' }
+                                               ]
+                                       })
+                               }
+                       ]
+               });
        }
 };
\ No newline at end of file
index 898856f..913e7fc 100644 (file)
@@ -20,6 +20,7 @@ return array(
        'tx_workspaces_service_cleanuppreviewlinktask' => $extensionPath . 'Classes/Service/CleanupPreviewLinkTask.php',
        'tx_workspaces_service_befunc' => $extensionPath . 'Classes/Service/Befunc.php',
        'tx_workspaces_service_griddata' => $extensionPath . 'Classes/Service/GridData.php',
+       'tx_workspaces_service_history' => $extensionPath . 'Classes/Service/History.php',
        'tx_workspaces_service_integrity' => $extensionPath . 'Classes/Service/Integrity.php',
        'tx_workspaces_service_stages' => $extensionPath . 'Classes/Service/Stages.php',
        'tx_workspaces_service_tcemain' => $extensionPath . 'Classes/Service/Tcemain.php',