ENHANCEMENT: Added API docs and changedFieldNames() method to DataDifferencer

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@78329 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2009-06-03 09:45:05 +00:00
parent da758f9f6e
commit 99d12ac936

View File

@ -52,6 +52,10 @@ class DataDifferencer extends ViewableData {
$this->ignoredFields = array_merge($this->ignoredFields, $ignoredFields);
}
/**
* Get a DataObject with altered values replaced with HTML diff strings, incorporating
* <ins> and <del> tags.
*/
function diffedData() {
$diffed = clone $this->fromRecord;
$fields = array_keys($diffed->getAllFields());
@ -67,6 +71,15 @@ class DataDifferencer extends ViewableData {
return $diffed;
}
/**
* 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
*/
function ChangedFields() {
$changedFields = new DataObjectSet();
$fields = array_keys($this->fromRecord->getAllFields());
@ -76,12 +89,35 @@ class DataDifferencer extends ViewableData {
if($this->fromRecord->$field != $this->toRecord->$field) {
$changedFields->push(new ArrayData(array(
'Name' => $field,
'Title' => $this->fromRecord->fieldLabel($field),
'Diff' => Diff::compareHTML($this->fromRecord->$field, $this->toRecord->$field),
'From' => $this->fromRecord->$field,
'To' => $this->toRecord->$field,
)));
}
}
return $changedFields;
}
/**
* 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;
}
}