Index: trunk/extensions/EducationProgram/actions/EPRestoreAction.php |
— | — | @@ -64,8 +64,21 @@ |
65 | 65 | $success = $this->doRestore( $object, $revision ); |
66 | 66 | } |
67 | 67 | else { |
68 | | - $this->displayForm( $object, $revision ); |
69 | | - $success = null; |
| 68 | + $diff = $object->getRestoreDiff( $revision ); |
| 69 | + |
| 70 | + if ( $diff->isValid() ) { |
| 71 | + if ( $diff->hasChanges() ) { |
| 72 | + $diff->setContext( $this->getContext() ); |
| 73 | + $diff->display(); |
| 74 | + |
| 75 | + $this->displayForm( $object, $revision ); |
| 76 | + } |
| 77 | + else { |
| 78 | + // TODO |
| 79 | + } |
| 80 | + |
| 81 | + $success = null; |
| 82 | + } |
70 | 83 | } |
71 | 84 | } |
72 | 85 | } |
— | — | @@ -102,7 +115,7 @@ |
103 | 116 | * @return boolean Success indicator |
104 | 117 | */ |
105 | 118 | protected function doRestore( EPPageObject $object, EPRevision $revision ) { |
106 | | - $success = $object->restoreToRevision( $revision, $object->getTable()->getRevertableFields() ); |
| 119 | + $success = $object->restoreToRevision( $revision ); |
107 | 120 | |
108 | 121 | if ( $success ) { |
109 | 122 | $revAction = new EPRevisionAction(); |
Index: trunk/extensions/EducationProgram/actions/EPUndoAction.php |
— | — | @@ -115,7 +115,7 @@ |
116 | 116 | * @return boolean Success indicator |
117 | 117 | */ |
118 | 118 | protected function doUndo( EPPageObject $object, EPRevision $revision ) { |
119 | | - $success = $object->undoRevision( $revision, $object->getTable()->getRevertableFields() ); |
| 119 | + $success = $object->undoRevision( $revision ); |
120 | 120 | |
121 | 121 | if ( $success ) { |
122 | 122 | $revAction = new EPRevisionAction(); |
Index: trunk/extensions/EducationProgram/includes/EPRevisionDiff.php |
— | — | @@ -19,6 +19,34 @@ |
20 | 20 | |
21 | 21 | protected $isValid = true; |
22 | 22 | |
| 23 | + public static function newFromRestoreRevision( EPRevisionedObject $sourceObject, EPRevision $revison, array $fields = null ) { |
| 24 | + $changedFields = array(); |
| 25 | + |
| 26 | + $targetObject = $revison->getObject(); |
| 27 | + $fields = is_null( $fields ) ? $targetObject->getFieldNames() : $fields; |
| 28 | + |
| 29 | + foreach ( $fields as $fieldName ) { |
| 30 | + $sourceHasField = $sourceObject->hasField( $fieldName ); |
| 31 | + $targetHasField = $targetObject->hasField( $fieldName ); |
| 32 | + |
| 33 | + if ( ( $sourceHasField XOR $targetHasField ) |
| 34 | + || $sourceObject->getField( $fieldName, null ) !== $targetObject->getField( $fieldName, null ) ) { |
| 35 | + |
| 36 | + $changedFields[$fieldName] = array(); |
| 37 | + |
| 38 | + if ( $sourceHasField ) { |
| 39 | + $changedFields[$fieldName]['source'] = $sourceObject->getField( $fieldName ); |
| 40 | + } |
| 41 | + |
| 42 | + if ( $targetHasField ) { |
| 43 | + $changedFields[$fieldName]['target'] = $targetObject->getField( $fieldName ); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return new self( $changedFields ); |
| 49 | + } |
| 50 | + |
23 | 51 | public static function newFromUndoRevision( EPRevisionedObject $currentObject, EPRevision $revison, array $fields = null ) { |
24 | 52 | $changedFields = array(); |
25 | 53 | |
— | — | @@ -30,13 +58,25 @@ |
31 | 59 | $fields = is_null( $fields ) ? $sourceObject->getFieldNames() : $fields; |
32 | 60 | |
33 | 61 | foreach ( $fields as $fieldName ) { |
34 | | - if ( $currentObject->getField( $fieldName ) === $sourceObject->getField( $fieldName ) |
35 | | - && $sourceObject->getField( $fieldName ) !== $targetObject->getField( $fieldName ) ) { |
| 62 | + $sourceHasField = $sourceObject->hasField( $fieldName ); |
| 63 | + $targetHasField = $targetObject->hasField( $fieldName ); |
36 | 64 | |
37 | | - $changedFields[$fieldName] = array( |
38 | | - $sourceObject->getField( $fieldName ), |
39 | | - $targetObject->getField( $fieldName ) |
40 | | - ); |
| 65 | + if ( $currentObject->getField( $fieldName, null ) === $sourceObject->getField( $fieldName, null ) |
| 66 | + && |
| 67 | + ( ( $sourceHasField XOR $targetHasField ) |
| 68 | + || |
| 69 | + $sourceObject->getField( $fieldName, null ) !== $targetObject->getField( $fieldName, null ) |
| 70 | + ) ) { |
| 71 | + |
| 72 | + $changedFields[$fieldName] = array(); |
| 73 | + |
| 74 | + if ( $sourceHasField ) { |
| 75 | + $changedFields[$fieldName]['source'] = $sourceObject->getField( $fieldName ); |
| 76 | + } |
| 77 | + |
| 78 | + if ( $targetHasField ) { |
| 79 | + $changedFields[$fieldName]['target'] = $targetObject->getField( $fieldName ); |
| 80 | + } |
41 | 81 | } |
42 | 82 | } |
43 | 83 | } |
— | — | @@ -64,13 +104,14 @@ |
65 | 105 | $out->addHTML( '</tr>' ); |
66 | 106 | |
67 | 107 | foreach ( $this->changedFields as $field => $values ) { |
68 | | - list( $old, $new ) = $values; |
69 | | - |
70 | 108 | $out->addHtml( '<tr>' ); |
71 | 109 | |
| 110 | + $source = array_key_exists( 'source', $values ) ? $values['source'] : ''; |
| 111 | + $target = array_key_exists( 'target', $values ) ? $values['target'] : ''; |
| 112 | + |
72 | 113 | $out->addElement( 'th', array(), $field ); |
73 | | - $out->addElement( 'td', array(), $old ); |
74 | | - $out->addElement( 'td', array(), $new ); |
| 114 | + $out->addElement( 'td', array(), $source ); |
| 115 | + $out->addElement( 'td', array(), $target ); |
75 | 116 | |
76 | 117 | $out->addHtml( '</tr>' ); |
77 | 118 | } |
Index: trunk/extensions/EducationProgram/includes/EPRevisionedObject.php |
— | — | @@ -345,21 +345,31 @@ |
346 | 346 | * |
347 | 347 | * @param EPRevision $revison |
348 | 348 | * @param array|null $fields |
349 | | - * |
| 349 | + * |
350 | 350 | * @return boolean Success indicator |
351 | 351 | */ |
352 | 352 | public function restoreToRevision( EPRevision $revison, array $fields = null ) { |
353 | | - $object = $revison->getObject(); |
354 | | - $fields = is_null( $fields ) ? $object->getFieldNames() : $fields; |
355 | | - |
356 | | - foreach ( $fields as $fieldName ) { |
357 | | - $this->restoreField( $fieldName, $object->getField( $fieldName ) ); |
358 | | - } |
359 | | - |
360 | | - return true; |
| 353 | + $diff = $this->getRestoreDiff( $revison, $fields ); |
| 354 | + $this->applyDiff( $diff ); |
| 355 | + return $diff->isValid(); |
361 | 356 | } |
362 | 357 | |
363 | 358 | /** |
| 359 | + * Get a diff for the changes that will happen when retoring to the provided revision. |
| 360 | + * |
| 361 | + * @since 0.1 |
| 362 | + * |
| 363 | + * @param EPRevision $revison |
| 364 | + * @param array|null $fields |
| 365 | + * |
| 366 | + * @return EPRevisionDiff |
| 367 | + */ |
| 368 | + public function getRestoreDiff( EPRevision $revison, array $fields = null ) { |
| 369 | + $fields = is_null( $fields ) ? $this->table->getRevertableFields() : $fields; |
| 370 | + return EPRevisionDiff::newFromRestoreRevision( $this, $revison, $fields ); |
| 371 | + } |
| 372 | + |
| 373 | + /** |
364 | 374 | * Undo the changes of a single revision to this object. |
365 | 375 | * Changes are compared on field level. If a field is no |
366 | 376 | * longer the same as in the revision being undone, it |
— | — | @@ -377,12 +387,19 @@ |
378 | 388 | */ |
379 | 389 | public function undoRevision( EPRevision $revison, array $fields = null ) { |
380 | 390 | $diff = $this->getUndoDiff( $revison, $fields ); |
| 391 | + $this->applyDiff( $diff ); |
| 392 | + return $diff->isValid(); |
| 393 | + } |
381 | 394 | |
| 395 | + public function applyDiff( EPRevisionDiff $diff ) { |
382 | 396 | foreach ( $diff->getChangedFields() as $fieldName => $values ) { |
383 | | - $this->restoreField( $fieldName, $values[1] ); |
| 397 | + if ( array_key_exists( 'target', $values ) ) { |
| 398 | + $this->restoreField( $fieldName, $values['target'] ); |
| 399 | + } |
| 400 | + else { |
| 401 | + $this->removeField( $fieldName ); |
| 402 | + } |
384 | 403 | } |
385 | | - |
386 | | - return $diff->isValid(); |
387 | 404 | } |
388 | 405 | |
389 | 406 | /** |
— | — | @@ -396,6 +413,7 @@ |
397 | 414 | * @return EPRevisionDiff |
398 | 415 | */ |
399 | 416 | public function getUndoDiff( EPRevision $revison, array $fields = null ) { |
| 417 | + $fields = is_null( $fields ) ? $this->table->getRevertableFields() : $fields; |
400 | 418 | return EPRevisionDiff::newFromUndoRevision( $this, $revison, $fields ); |
401 | 419 | } |
402 | 420 | |