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