Passed
Push — master ( 72cc10...fd5ee6 )
by Robbie
03:14
created

HistoryViewerField::getSchemaDataDefaults()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 1
nop 0
dl 0
loc 17
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\VersionedAdmin\Forms;
4
5
use SilverStripe\Core\Convert;
6
use SilverStripe\Forms\FormField;
7
use SilverStripe\ORM\CMSPreviewable;
8
use SilverStripe\ORM\DataObject;
9
use SilverStripe\View\Requirements;
10
11
class HistoryViewerField extends FormField
12
{
13
    /**
14
     * The default pagination page size
15
     *
16
     * @config
17
     * @var int
18
     */
19
    private static $default_page_size = 30;
0 ignored issues
show
introduced by
The private property $default_page_size is not used, and could be removed.
Loading history...
20
21
    /**
22
     * Unique context key used to differentiate the different use cases for HistoryViewer
23
     *
24
     * @var string
25
     */
26
    protected $contextKey;
27
28
    protected $schemaComponent = 'HistoryViewer';
29
30
    protected $inputType = '';
31
32
    public function __construct($name, $title = null, $value = null)
33
    {
34
        Requirements::javascript('silverstripe/versioned-admin:client/dist/js/bundle.js');
35
        Requirements::css('silverstripe/versioned-admin:client/dist/styles/bundle.css');
36
37
        parent::__construct($name, $title, $value);
38
    }
39
40
    protected function setupDefaultClasses()
41
    {
42
        parent::setupDefaultClasses();
43
44
        $this->addExtraClass('fill-height');
45
    }
46
47
    /**
48
     * Get the source record to view history for
49
     *
50
     * @return DataObject|null
51
     */
52
    public function getSourceRecord()
53
    {
54
        return $this->getForm() ? $this->getForm()->getRecord() : null;
55
    }
56
57
    /**
58
     * Get whether the record is previewable
59
     *
60
     * @return boolean
61
     */
62
    public function getPreviewEnabled()
63
    {
64
        $record = $this->getSourceRecord();
65
66
        return $record && $record instanceof CMSPreviewable;
67
    }
68
69
    public function getContextKey()
70
    {
71
        if ($this->contextKey) {
72
            return $this->contextKey;
73
        }
74
75
        // Default to using the DataObject's DB table name as the unique identifier
76
        return DataObject::getSchema()->baseDataTable(get_class($this->getSourceRecord()));
77
    }
78
79
    public function setContextKey($contextKey)
80
    {
81
        $this->contextKey = (string) $contextKey;
82
        return $this;
83
    }
84
85
    /**
86
     * Provide the necessary input data for React to power the history viewer
87
     *
88
     * {@inheritDoc}
89
     */
90
    public function getSchemaDataDefaults()
91
    {
92
        $data = parent::getSchemaDataDefaults();
93
94
        $sourceRecord = $this->getSourceRecord();
95
96
        $data['data'] = array_merge($data['data'], [
97
            'recordId' => $sourceRecord ? $sourceRecord->ID : null,
98
            'recordClass' => $sourceRecord ? $sourceRecord->ClassName : null,
99
            'contextKey' => $this->getContextKey(),
100
            'isPreviewable' => $this->getPreviewEnabled(),
101
            'limit' => $this->config()->get('default_page_size'),
102
            'offset' => 0,
103
            'page' => 0,
104
        ]);
105
106
        return $data;
107
    }
108
109
    /**
110
     * When not used in a React form factory context, this adds the schema data to SilverStripe template
111
     * rendered attributes lists
112
     *
113
     * @return array
114
     */
115
    public function getAttributes()
116
    {
117
        $attributes = parent::getAttributes();
118
119
        $attributes['data-schema'] = Convert::raw2json($this->getSchemaData());
120
121
        return $attributes;
122
    }
123
124
    public function Type()
125
    {
126
        return 'history-viewer__container';
127
    }
128
}
129