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
|
|
|
public function __construct($name, $title = null, $value = null) |
31
|
|
|
{ |
32
|
|
|
parent::__construct($name, $title, $value); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function setupDefaultClasses() |
36
|
|
|
{ |
37
|
|
|
parent::setupDefaultClasses(); |
38
|
|
|
|
39
|
|
|
$this->addExtraClass('fill-height'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get the source record to view history for |
44
|
|
|
* |
45
|
|
|
* @return DataObject|null |
46
|
|
|
*/ |
47
|
|
|
public function getSourceRecord() |
48
|
|
|
{ |
49
|
|
|
return $this->getForm() ? $this->getForm()->getRecord() : null; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get whether the record is previewable |
54
|
|
|
* |
55
|
|
|
* @return boolean |
56
|
|
|
*/ |
57
|
|
|
public function getPreviewEnabled() |
58
|
|
|
{ |
59
|
|
|
$record = $this->getSourceRecord(); |
60
|
|
|
$previewEnabled = $record && $record instanceof CMSPreviewable; |
61
|
|
|
|
62
|
|
|
$this->extend('updatePreviewEnabled', $previewEnabled, $record); |
63
|
|
|
|
64
|
|
|
return $previewEnabled; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getContextKey() |
68
|
|
|
{ |
69
|
|
|
if ($this->contextKey) { |
70
|
|
|
return $this->contextKey; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// Default to using the DataObject's DB table name as the unique identifier |
74
|
|
|
return DataObject::getSchema()->baseDataTable(get_class($this->getSourceRecord())); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function setContextKey($contextKey) |
78
|
|
|
{ |
79
|
|
|
$this->contextKey = (string) $contextKey; |
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Provide the necessary input data for React to power the history viewer |
85
|
|
|
* |
86
|
|
|
* {@inheritDoc} |
87
|
|
|
*/ |
88
|
|
|
public function getSchemaDataDefaults() |
89
|
|
|
{ |
90
|
|
|
$data = parent::getSchemaDataDefaults(); |
91
|
|
|
|
92
|
|
|
$sourceRecord = $this->getSourceRecord(); |
93
|
|
|
|
94
|
|
|
$data['data'] = array_merge($data['data'], [ |
95
|
|
|
'recordId' => $sourceRecord ? $sourceRecord->ID : null, |
96
|
|
|
'recordClass' => $sourceRecord ? $sourceRecord->ClassName : null, |
97
|
|
|
'contextKey' => $this->getContextKey(), |
98
|
|
|
'isPreviewable' => $this->getPreviewEnabled(), |
99
|
|
|
'limit' => $this->config()->get('default_page_size'), |
100
|
|
|
'offset' => 0, |
101
|
|
|
'page' => 0, |
102
|
|
|
]); |
103
|
|
|
|
104
|
|
|
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
|
|
|
public function getAttributes() |
114
|
|
|
{ |
115
|
|
|
$attributes = parent::getAttributes(); |
116
|
|
|
|
117
|
|
|
$attributes['data-schema'] = json_encode($this->getSchemaData()); |
118
|
|
|
|
119
|
|
|
return $attributes; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function Type() |
123
|
|
|
{ |
124
|
|
|
return 'history-viewer__container'; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|