2009-05-23 05:28:18 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility class to render views of the differences between two data objects (or two versions of the
|
|
|
|
* same data object).
|
|
|
|
*
|
|
|
|
* Construcing a diff object is done as follows:
|
|
|
|
* <code>
|
|
|
|
* $fromRecord = Versioned::get_version('SiteTree', $pageID, $fromVersion);
|
|
|
|
* $toRecord = Versioned::get_version('SiteTree, $pageID, $toVersion);
|
|
|
|
* $diff = new DataDifferencer($fromRecord, $toRecord);
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* And then it can be used in a number of ways. You can use the ChangedFields() method in a template:
|
|
|
|
* <pre>
|
|
|
|
* <dl class="diff">
|
|
|
|
* <% control Diff.ChangedFields %>
|
|
|
|
* <dt>$Title</dt>
|
|
|
|
* <dd>$Diff</dd>
|
|
|
|
* <% end_control %>
|
|
|
|
* </dl>
|
|
|
|
* </pre>
|
|
|
|
*
|
|
|
|
* Or you can get the diff'ed content as another DataObject, that you can insert into a form.
|
|
|
|
* <code>
|
|
|
|
* $form->loadDataFrom($diff->diffedData());
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* If there are fields whose changes you aren't interested in, you can ignore them like so:
|
|
|
|
* <code>
|
|
|
|
* $diff->ignoreFields('AuthorID', 'Status');
|
|
|
|
* </code>
|
|
|
|
*/
|
|
|
|
class DataDifferencer extends ViewableData {
|
|
|
|
protected $fromRecord;
|
|
|
|
protected $toRecord;
|
|
|
|
|
|
|
|
protected $ignoredFields = array("ID","Version","RecordID");
|
|
|
|
|
2009-06-17 05:17:29 +02:00
|
|
|
/**
|
|
|
|
* Construct a DataDifferencer to show the changes between $fromRecord and $toRecord.
|
|
|
|
* If $fromRecord is null, this will represent a "creation".
|
|
|
|
*/
|
2009-05-23 05:28:18 +02:00
|
|
|
function __construct($fromRecord, $toRecord) {
|
2009-06-17 05:17:29 +02:00
|
|
|
if(!$toRecord) user_error("DataDifferencer constructed without a toRecord", E_USER_WARNING);
|
2009-05-23 05:28:18 +02:00
|
|
|
$this->fromRecord = $fromRecord;
|
|
|
|
$this->toRecord = $toRecord;
|
2009-09-18 05:02:19 +02:00
|
|
|
parent::__construct();
|
2009-05-23 05:28:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specify some fields to ignore changes from. Repeated calls are cumulative.
|
|
|
|
* @param $ignoredFields An array of field names to ignore. Alternatively, pass the field names as
|
|
|
|
* separate args.
|
|
|
|
*/
|
|
|
|
function ignoreFields($ignoredFields) {
|
|
|
|
if(!is_array($ignoredFields)) $ignoredFields = func_get_args();
|
|
|
|
$this->ignoredFields = array_merge($this->ignoredFields, $ignoredFields);
|
|
|
|
}
|
|
|
|
|
2009-06-03 11:45:05 +02:00
|
|
|
/**
|
|
|
|
* Get a DataObject with altered values replaced with HTML diff strings, incorporating
|
|
|
|
* <ins> and <del> tags.
|
|
|
|
*/
|
2009-05-23 05:28:18 +02:00
|
|
|
function diffedData() {
|
2009-06-17 05:17:29 +02:00
|
|
|
if($this->fromRecord) {
|
|
|
|
$diffed = clone $this->fromRecord;
|
2010-01-13 00:17:02 +01:00
|
|
|
$fields = array_keys($diffed->getAllFields() + $this->toRecord->getAllFields());
|
2009-06-17 05:17:29 +02:00
|
|
|
} else {
|
|
|
|
$diffed = clone $this->toRecord;
|
|
|
|
$fields = array_keys($this->toRecord->getAllFields());
|
|
|
|
}
|
2009-05-23 05:28:18 +02:00
|
|
|
|
|
|
|
foreach($fields as $field) {
|
|
|
|
if(in_array($field, $this->ignoredFields)) continue;
|
|
|
|
|
2009-06-17 05:17:29 +02:00
|
|
|
if(!$this->fromRecord) {
|
|
|
|
$diffed->$field = "<ins>" . $this->toRecord->$field . "</ins>";
|
|
|
|
} else if($this->fromRecord->$field != $this->toRecord->$field) {
|
2009-05-23 05:28:18 +02:00
|
|
|
$diffed->$field = Diff::compareHTML($this->fromRecord->$field, $this->toRecord->$field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $diffed;
|
|
|
|
}
|
|
|
|
|
2009-06-03 11:45:05 +02:00
|
|
|
/**
|
|
|
|
* Get a DataObjectSet of the changed fields.
|
|
|
|
* Each element is an array data containing
|
|
|
|
* - Name: The field name
|
|
|
|
* - Title: The human-readable field title
|
|
|
|
* - Diff: An HTML diff showing the changes
|
|
|
|
* - From: The older version of the field
|
|
|
|
* - To: The newer version of the field
|
|
|
|
*/
|
2009-05-23 05:28:18 +02:00
|
|
|
function ChangedFields() {
|
|
|
|
$changedFields = new DataObjectSet();
|
2009-06-17 05:17:29 +02:00
|
|
|
|
|
|
|
if($this->fromRecord) {
|
|
|
|
$base = $this->fromRecord;
|
|
|
|
$fields = array_keys($this->fromRecord->getAllFields());
|
|
|
|
} else {
|
|
|
|
$base = $this->toRecord;
|
|
|
|
$fields = array_keys($this->toRecord->getAllFields());
|
|
|
|
}
|
2009-05-23 05:28:18 +02:00
|
|
|
|
|
|
|
foreach($fields as $field) {
|
|
|
|
if(in_array($field, $this->ignoredFields)) continue;
|
|
|
|
|
2009-06-17 05:17:29 +02:00
|
|
|
if(!$this->fromRecord || $this->fromRecord->$field != $this->toRecord->$field) {
|
2009-05-23 05:28:18 +02:00
|
|
|
$changedFields->push(new ArrayData(array(
|
2009-06-03 11:45:05 +02:00
|
|
|
'Name' => $field,
|
2009-06-17 05:17:29 +02:00
|
|
|
'Title' => $base->fieldLabel($field),
|
2010-02-25 00:32:40 +01:00
|
|
|
'Diff' => $this->fromRecord
|
|
|
|
? Diff::compareHTML($this->fromRecord->$field, $this->toRecord->$field)
|
|
|
|
: "<ins>" . $this->toRecord->$field . "</ins>",
|
2009-06-17 05:42:24 +02:00
|
|
|
'From' => $this->fromRecord ? $this->fromRecord->$field : null,
|
|
|
|
'To' => $this->toRecord ? $this->toRecord->$field : null,
|
2009-05-23 05:28:18 +02:00
|
|
|
)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $changedFields;
|
|
|
|
}
|
2009-06-03 11:45:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an array of the names of every fields that has changed.
|
|
|
|
* This is simpler than {@link ChangedFields()}
|
|
|
|
*/
|
|
|
|
function changedFieldNames() {
|
|
|
|
$diffed = clone $this->fromRecord;
|
|
|
|
$fields = array_keys($diffed->getAllFields());
|
|
|
|
|
|
|
|
$changedFields = array();
|
|
|
|
|
|
|
|
foreach($fields as $field) {
|
|
|
|
if(in_array($field, $this->ignoredFields)) continue;
|
|
|
|
if($this->fromRecord->$field != $this->toRecord->$field) {
|
|
|
|
$changedFields[] = $field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $changedFields;
|
|
|
|
}
|
2009-06-17 05:17:29 +02:00
|
|
|
}
|