From 1d31b59b2404570d8024722862f3678381686924 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Thu, 9 Jul 2009 06:38:43 +0000 Subject: [PATCH] ENHANCEMENT: Improvements to DataDifferencer for cmsworkflow. git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@81475 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/DataDifferencer.php | 73 ++++++++++++++++++++++++++++++---- 1 file changed, 65 insertions(+), 8 deletions(-) diff --git a/core/model/DataDifferencer.php b/core/model/DataDifferencer.php index 1f7bdbb77..2d87670e9 100644 --- a/core/model/DataDifferencer.php +++ b/core/model/DataDifferencer.php @@ -37,7 +37,12 @@ class DataDifferencer extends ViewableData { protected $ignoredFields = array("ID","Version","RecordID"); + /** + * Construct a DataDifferencer to show the changes between $fromRecord and $toRecord. + * If $fromRecord is null, this will represent a "creation". + */ function __construct($fromRecord, $toRecord) { + if(!$toRecord) user_error("DataDifferencer constructed without a toRecord", E_USER_WARNING); $this->fromRecord = $fromRecord; $this->toRecord = $toRecord; } @@ -52,14 +57,25 @@ class DataDifferencer extends ViewableData { $this->ignoredFields = array_merge($this->ignoredFields, $ignoredFields); } + /** + * Get a DataObject with altered values replaced with HTML diff strings, incorporating + * and tags. + */ function diffedData() { - $diffed = clone $this->fromRecord; - $fields = array_keys($diffed->getAllFields()); + if($this->fromRecord) { + $diffed = clone $this->fromRecord; + $fields = array_keys($diffed->getAllFields()); + } else { + $diffed = clone $this->toRecord; + $fields = array_keys($this->toRecord->getAllFields()); + } foreach($fields as $field) { if(in_array($field, $this->ignoredFields)) continue; - if($this->fromRecord->$field != $this->toRecord->$field) { + if(!$this->fromRecord) { + $diffed->$field = "" . $this->toRecord->$field . ""; + } else if($this->fromRecord->$field != $this->toRecord->$field) { $diffed->$field = Diff::compareHTML($this->fromRecord->$field, $this->toRecord->$field); } } @@ -67,21 +83,62 @@ 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()); + + if($this->fromRecord) { + $base = $this->fromRecord; + $fields = array_keys($this->fromRecord->getAllFields()); + } else { + $base = $this->toRecord; + $fields = array_keys($this->toRecord->getAllFields()); + } foreach($fields as $field) { if(in_array($field, $this->ignoredFields)) continue; - if($this->fromRecord->$field != $this->toRecord->$field) { + if(!$this->fromRecord || $this->fromRecord->$field != $this->toRecord->$field) { $changedFields->push(new ArrayData(array( - 'Title' => $this->fromRecord->fieldLabel($field), - 'Diff' => Diff::compareHTML($this->fromRecord->$field, $this->toRecord->$field), + 'Name' => $field, + 'Title' => $base->fieldLabel($field), + 'Diff' => $this->fromRecord + ? Diff::compareHTML($this->fromRecord->$field, $this->toRecord->$field) + : "" . $this->toRecord->$field . "", + 'From' => $this->fromRecord ? $this->fromRecord->$field : null, + 'To' => $this->toRecord ? $this->toRecord->$field : null, ))); } } return $changedFields; } -} \ No newline at end of file + + /** + * 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; + } +}