1
<?php
2

3
namespace SilverStripe\VersionedAdmin\Forms;
4

5
use SilverStripe\Forms\FormField;
6
use SilverStripe\ORM\CMSPreviewable;
7
use SilverStripe\ORM\DataObject;
8

9
class HistoryViewerField extends FormField
10
{
11
    /**
12
     * The default pagination page size
13
     *
14
     * @config
15
     * @var int
16
     */
17
    private static $default_page_size = 30;
18

19
    /**
20
     * Unique context key used to differentiate the different use cases for HistoryViewer
21
     *
22
     * @var string
23
     */
24
    protected $contextKey;
25

26
    protected $schemaComponent = 'HistoryViewer';
27

28
    protected $inputType = '';
29

30 1
    public function __construct($name, $title = null, $value = null)
31
    {
32 1
        parent::__construct($name, $title, $value);
33
    }
34

35 1
    protected function setupDefaultClasses()
36
    {
37 1
        parent::setupDefaultClasses();
38

39 1
        $this->addExtraClass('fill-height');
40
    }
41

42
    /**
43
     * Get the source record to view history for
44
     *
45
     * @return DataObject|null
46
     */
47 1
    public function getSourceRecord()
48
    {
49 1
        return $this->getForm() ? $this->getForm()->getRecord() : null;
50
    }
51

52
    /**
53
     * Get whether the record is previewable
54
     *
55
     * @return boolean
56
     */
57 0
    public function getPreviewEnabled()
58
    {
59 0
        $record = $this->getSourceRecord();
60 0
        $previewEnabled = $record && $record instanceof CMSPreviewable;
61

62 0
        $this->extend('updatePreviewEnabled', $previewEnabled, $record);
63

64 0
        return $previewEnabled;
65
    }
66

67 0
    public function getContextKey()
68
    {
69 0
        if ($this->contextKey) {
70 0
            return $this->contextKey;
71
        }
72

73
        // Default to using the DataObject's DB table name as the unique identifier
74 0
        return DataObject::getSchema()->baseDataTable(get_class($this->getSourceRecord()));
75
    }
76

77 0
    public function setContextKey($contextKey)
78
    {
79 0
        $this->contextKey = (string) $contextKey;
80 0
        return $this;
81
    }
82

83
    /**
84
     * Provide the necessary input data for React to power the history viewer
85
     *
86
     * {@inheritDoc}
87
     */
88 0
    public function getSchemaDataDefaults()
89
    {
90 0
        $data = parent::getSchemaDataDefaults();
91

92 0
        $sourceRecord = $this->getSourceRecord();
93

94 0
        $data['data'] = array_merge($data['data'], [
95 0
            'recordId' => $sourceRecord ? $sourceRecord->ID : null,
96 0
            'recordClass' => $sourceRecord ? $sourceRecord->ClassName : null,
97 0
            'contextKey' => $this->getContextKey(),
98 0
            'isPreviewable' => $this->getPreviewEnabled(),
99 0
            'limit' => $this->config()->get('default_page_size'),
100 0
            'offset' => 0,
101 0
            'page' => 0,
102
        ]);
103

104 0
        return $data;
105
    }
106

107
    /**
108
     * When not used in a React form factory context, this adds the schema data to SilverStripe template
109
     * rendered attributes lists
110
     *
111
     * @return array
112
     */
113 0
    public function getAttributes()
114
    {
115 0
        $attributes = parent::getAttributes();
116

117 0
        $attributes['data-schema'] = json_encode($this->getSchemaData());
118

119 0
        return $attributes;
120
    }
121

122 0
    public function Type()
123
    {
124 0
        return 'history-viewer__container';
125
    }
126
}

Read our documentation on viewing source code .

Loading