1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
36 class DataDifferencer extends ViewableData {
37 protected $fromRecord;
38 protected $toRecord;
39
40 protected $ignoredFields = array("ID","Version","RecordID");
41
42 43 44 45
46 function __construct($fromRecord, $toRecord) {
47 if(!$toRecord) user_error("DataDifferencer constructed without a toRecord", E_USER_WARNING);
48 $this->fromRecord = $fromRecord;
49 $this->toRecord = $toRecord;
50 parent::__construct();
51 }
52
53 54 55 56 57
58 function ignoreFields($ignoredFields) {
59 if(!is_array($ignoredFields)) $ignoredFields = func_get_args();
60 $this->ignoredFields = array_merge($this->ignoredFields, $ignoredFields);
61 }
62
63 64 65 66
67 function diffedData() {
68 if($this->fromRecord) {
69 $diffed = clone $this->fromRecord;
70 $fields = array_keys($diffed->getAllFields() + $this->toRecord->getAllFields());
71 } else {
72 $diffed = clone $this->toRecord;
73 $fields = array_keys($this->toRecord->getAllFields());
74 }
75
76 foreach($fields as $field) {
77 if(in_array($field, $this->ignoredFields)) continue;
78
79 if(!$this->fromRecord) {
80 $diffed->$field = "<ins>" . $this->toRecord->$field . "</ins>";
81 } else if($this->fromRecord->$field != $this->toRecord->$field) {
82 $diffed->$field = Diff::compareHTML($this->fromRecord->$field, $this->toRecord->$field);
83 }
84 }
85
86 return $diffed;
87 }
88
89 90 91 92 93 94 95 96 97
98 function ChangedFields() {
99 $changedFields = new DataObjectSet();
100
101 if($this->fromRecord) {
102 $base = $this->fromRecord;
103 $fields = array_keys($this->fromRecord->getAllFields());
104 } else {
105 $base = $this->toRecord;
106 $fields = array_keys($this->toRecord->getAllFields());
107 }
108
109 foreach($fields as $field) {
110 if(in_array($field, $this->ignoredFields)) continue;
111
112 if(!$this->fromRecord || $this->fromRecord->$field != $this->toRecord->$field) {
113 $changedFields->push(new ArrayData(array(
114 'Name' => $field,
115 'Title' => $base->fieldLabel($field),
116 'Diff' => $this->fromRecord
117 ? Diff::compareHTML($this->fromRecord->$field, $this->toRecord->$field)
118 : "<ins>" . $this->toRecord->$field . "</ins>",
119 'From' => $this->fromRecord ? $this->fromRecord->$field : null,
120 'To' => $this->toRecord ? $this->toRecord->$field : null,
121 )));
122 }
123 }
124
125 return $changedFields;
126 }
127
128 129 130 131
132 function changedFieldNames() {
133 $diffed = clone $this->fromRecord;
134 $fields = array_keys($diffed->getAllFields());
135
136 $changedFields = array();
137
138 foreach($fields as $field) {
139 if(in_array($field, $this->ignoredFields)) continue;
140 if($this->fromRecord->$field != $this->toRecord->$field) {
141 $changedFields[] = $field;
142 }
143 }
144
145 return $changedFields;
146 }
147 }
148
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-