From 7e3652216b871d6b02cf527175077363020c3484 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Wed, 17 Jun 2009 03:17:29 +0000 Subject: [PATCH] ENHANCEMENT: Added better support for using DataDifferencer to look at new records. git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@79400 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/DataDifferencer.php | 37 ++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/core/model/DataDifferencer.php b/core/model/DataDifferencer.php index c107873e3..96c852cfd 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; } @@ -57,13 +62,20 @@ class DataDifferencer extends ViewableData { * 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); } } @@ -82,16 +94,25 @@ class DataDifferencer extends ViewableData { */ 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( 'Name' => $field, - 'Title' => $this->fromRecord->fieldLabel($field), - 'Diff' => Diff::compareHTML($this->fromRecord->$field, $this->toRecord->$field), + 'Title' => $base->fieldLabel($field), + 'Diff' => $this->fromRecord + ? Diff::compareHTML($this->fromRecord->$field, $this->toRecord->$field) + : "" . $this->toRecord->$field . "", 'From' => $this->fromRecord->$field, 'To' => $this->toRecord->$field, ))); @@ -120,4 +141,4 @@ class DataDifferencer extends ViewableData { return $changedFields; } -} \ No newline at end of file +}