DiffField   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 75
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setComparisonField() 0 5 1
A getSchemaDataDefaults() 0 5 1
A Value() 0 5 1
A getOutdatedField() 0 5 1
A getComparisonField() 0 3 1
A getSchemaStateDefaults() 0 17 1
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
    public function setComparisonField(FormField $field)
29
    {
30
        $this->comparisonField = $field;
31
        $this->setFailover($this->comparisonField);
32
        return $this;
33
    }
34
35
    /**
36
     * @return FormField
37
     */
38
    public function getComparisonField()
39
    {
40
        return $this->comparisonField;
41
    }
42
43
    public function Value()
44
    {
45
        $oldValue = $this->getOutdatedField()->Value();
46
        $newValue = $this->getComparisonField()->Value();
47
        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
    public function getOutdatedField()
61
    {
62
        $newField = clone $this->getComparisonField();
63
        $newField->setValue($this->value);
64
        return $newField;
65
    }
66
67
    public function getSchemaDataDefaults()
68
    {
69
        return array_merge(
70
            $this->getComparisonField()->getSchemaDataDefaults(),
71
            parent::getSchemaDataDefaults()
72
        );
73
    }
74
75
    public function getSchemaStateDefaults()
76
    {
77
        $fromValue = $this->getOutdatedField()->Value();
78
        $toField = $this->getComparisonField();
79
        $toValue = $toField->Value();
80
81
        $state = array_merge(
82
            $toField->getSchemaStateDefaults(),
83
            parent::getSchemaStateDefaults(),
84
            ['value' => $this->Value()]
85
        );
86
        $state['data']['diff'] = [
87
            'from' => $fromValue,
88
            'to' => $toValue,
89
        ];
90
91
        return $state;
92
    }
93
}
94