Passed
Pull Request — master (#18)
by Robbie
03:31
created

CMSPageHistoryViewerController::isEnabled()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 4
nc 6
nop 1
dl 0
loc 8
rs 9.2
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 24 and the first side effect is on line 16.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace SilverStripe\VersionedAdmin\Controllers;
4
5
use SilverStripe\CMS\Controllers\CMSMain;
6
use SilverStripe\CMS\Controllers\CMSPageHistoryController;
7
use SilverStripe\CMS\Model\SiteTree;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\Form;
10
use SilverStripe\Forms\HiddenField;
11
use SilverStripe\ORM\DataObject;
12
use SilverStripe\VersionedAdmin\Forms\HistoryViewerField;
13
use SilverStripe\View\SSViewer;
14
15
if (!class_exists(CMSMain::class)) {
16
    return;
17
}
18
19
/**
20
 * The history viewer controller replaces the {@link CMSPageHistoryController} and uses the React based
21
 * {@link HistoryViewerField} to display the history for a {@link DataObject} that has the {@link Versioned}
22
 * extension.
23
 */
24
class CMSPageHistoryViewerController extends CMSPageHistoryController
25
{
26
    private static $url_segment = 'pages/history';
0 ignored issues
show
introduced by
The private property $url_segment is not used, and could be removed.
Loading history...
27
28
    private static $url_rule = '/$Action/$ID/$VersionID/$OtherVersionID';
0 ignored issues
show
introduced by
The private property $url_rule is not used, and could be removed.
Loading history...
29
30
    private static $url_priority = 42;
0 ignored issues
show
introduced by
The private property $url_priority is not used, and could be removed.
Loading history...
31
32
    private static $required_permission_codes = 'CMS_ACCESS_CMSMain';
0 ignored issues
show
introduced by
The private property $required_permission_codes is not used, and could be removed.
Loading history...
33
34
    public function getEditForm($id = null, $fields = null, $versionID = null, $compareID = null)
35
    {
36
        /** @var DataObject $record */
37
        $record = $this->getRecord($id ?: $this->currentPageID());
38
39
        if (!$this->isEnabled($record)) {
40
            return parent::getEditForm($id, $fields, $versionID, $compareID);
41
        }
42
43
        /** @var Form $form */
44
        $form = parent::getEditForm($id);
45
        $form->addExtraClass('history-viewer__form');
46
        // Disable default CMS preview
47
        $form->removeExtraClass('cms-previewable');
48
49
        if ($record) {
0 ignored issues
show
introduced by
$record is of type SilverStripe\ORM\DataObject, thus it always evaluated to true. If $record can have other possible types, add them to src/Controllers/CMSPageHistoryViewerController.php:36
Loading history...
50
            $fieldList = FieldList::create(
51
                HiddenField::create('ID', null, $record->ID),
52
                HistoryViewerField::create('PageHistory')
53
                    ->setForm($form)
54
            );
55
            $form->setFields($fieldList);
56
        }
57
58
        return $form;
59
    }
60
61
    /**
62
     * Only activate for pages that have a history viewer capability applied. Extensions can provide their
63
     * own two cents about this criteria.
64
     *
65
     * @param SiteTree $record
66
     * @return bool
67
     */
68
    public function isEnabled(SiteTree $record = null)
69
    {
70
        if (!$record) {
71
            $record = $this->currentPage();
72
        }
73
74
        $enabledResults = array_filter($this->extend('updateIsEnabled', $record));
75
        return ($record && !empty($enabledResults) && max($enabledResults) === true);
76
    }
77
78
    public function getTabIdentifier()
79
    {
80
        return 'history';
81
    }
82
83
    /**
84
     * Disables the Tools panel when history viewer is enabled
85
     *
86
     * {@inheritDoc}
87
     */
88
    public function Tools()
89
    {
90
        if ($this->isEnabled()) {
91
            return false;
92
        }
93
        return parent::Tools();
94
    }
95
96
    /**
97
     * If the history viewer is not enabled for the current record, do not include its templates in the list. This
98
     * is an automatic template override by controller name, so also needs to be excluded manually.
99
     *
100
     * {@inheritDoc}
101
     */
102
    public function getTemplatesWithSuffix($suffix)
103
    {
104
        if ($this->isEnabled()) {
105
            return parent::getTemplatesWithSuffix($suffix);
106
        }
107
108
        $templates = SSViewer::get_templates_by_class(get_parent_class($this), $suffix, __CLASS__);
109
        return SSViewer::chooseTemplate($templates);
110
    }
111
}
112