1
<?php
2

3
namespace SilverStripe\VersionedAdmin\Forms;
4

5
use SilverStripe\Forms\FormField;
6
use SilverStripe\Forms\HTMLReadonlyField;
7
use SilverStripe\View\Parsers\Diff;
8

9
/**
10
 * This form field is both a field object in it's own right, and a decorator for another field type.
11
 * It is used to render differences between two data entries into the field type it wraps
12
 * e.g. a TextField with the value "Old data" can be transformed with {@see DiffTransformation}
13
 * and then having setValue called again to load in "New data" - this field will then render
14
 * the value "<ins>New</ins> <del>Old</del> data". Most useful in historic version comparisons
15
 * {@see SilverStripe\Versioned\Versioned}
16
 */
17
class DiffField extends HTMLReadonlyField
18
{
19
    /**
20
     * @var FormField Holds the field used as the 'To' (the newer) data for the diff.
21
     */
22
    protected $comparisonField;
23

24
    /**
25
     * @param FormField $field
26
     * @return $this
27
     */
28 1
    public function setComparisonField(FormField $field)
29
    {
30 1
        $this->comparisonField = $field;
31 1
        $this->setFailover($this->comparisonField);
32 1
        return $this;
33
    }
34

35
    /**
36
     * @return FormField
37
     */
38 1
    public function getComparisonField()
39
    {
40 1
        return $this->comparisonField;
41
    }
42

43 1
    public function Value()
44
    {
45 1
        $oldValue = $this->getOutdatedField()->Value();
46 1
        $newValue = $this->getComparisonField()->Value();
47 1
        return Diff::compareHTML($oldValue, $newValue);
48
    }
49

50
    /**
51
     * This function is so named not in the manner of chronology,
52
     * but rather in terms of succession.
53
     * That is to say the 'outdated' field may in fact be for a newer
54
     * value in terms of chronology, but a diff shows what it takes to
55
     * turn one value _into_ another... and in this case the 'from' field
56
     * value is succeeded by the 'to' value - it becomes 'outdated'.
57
     *
58
     * @return FormField
59
     */
60 1
    public function getOutdatedField()
61
    {
62 1
        $newField = clone $this->getComparisonField();
63 1
        $newField->setValue($this->value);
64 1
        return $newField;
65
    }
66

67 0
    public function getSchemaDataDefaults()
68
    {
69 0
        return array_merge(
70 0
            $this->getComparisonField()->getSchemaDataDefaults(),
71 0
            parent::getSchemaDataDefaults()
72
        );
73
    }
74

75 0
    public function getSchemaStateDefaults()
76
    {
77 0
        $fromValue = $this->getOutdatedField()->Value();
78 0
        $toField = $this->getComparisonField();
79 0
        $toValue = $toField->Value();
80

81 0
        $state = array_merge(
82 0
            $toField->getSchemaStateDefaults(),
83 0
            parent::getSchemaStateDefaults(),
84 0
            ['value' => $this->Value()]
85
        );
86 0
        $state['data']['diff'] = [
87 0
            'from' => $fromValue,
88 0
            'to' => $toValue,
89
        ];
90

91 0
        return $state;
92
    }
93
}

Read our documentation on viewing source code .

Loading