Passed
Push — master ( 08cb4a...af336d )
by Robbie
02:59
created

HistoryViewerField::getContextKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
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
use SilverStripe\View\Requirements;
9
10
class HistoryViewerField extends FormField
11
{
12
    /**
13
     * Default to using the SiteTree component
14
     *
15
     * @var string
16
     */
17
    protected $schemaComponent = 'PageHistoryViewer';
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
    public function __construct($name, $title = null, $value = null)
27
    {
28
        Requirements::javascript('silverstripe/versioned-admin:client/dist/js/bundle.js');
29
        Requirements::css('silverstripe/versioned-admin:client/dist/styles/bundle.css');
30
31
        parent::__construct($name, $title, $value);
32
    }
33
34
    /**
35
     * Get the source record to view history for
36
     *
37
     * @return DataObject|null
38
     */
39
    public function getSourceRecord()
40
    {
41
        return $this->getForm() ? $this->getForm()->getRecord() : null;
42
    }
43
44
    /**
45
     * Get whether the record is previewable
46
     *
47
     * @return boolean
48
     */
49
    public function getPreviewEnabled()
50
    {
51
        $record = $this->getSourceRecord();
52
53
        return $record && $record instanceof CMSPreviewable;
54
    }
55
56
    public function getContextKey()
57
    {
58
        if ($this->contextKey) {
59
            return $this->contextKey;
60
        }
61
62
        // Default to using the DataObject's DB table name as the unique identifier
63
        return DataObject::getSchema()->baseDataTable(get_class($this->getSourceRecord()));
64
    }
65
66
    public function setContextKey($contextKey)
67
    {
68
        $this->contextKey = (string) $contextKey;
69
        return $this;
70
    }
71
}
72