PageExtension::IsGetSiteControlPage()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Sunnysideup\GetSiteControl\Extensions;
4
5
use SilverStripe\CMS\Model\RedirectorPage;
6
use SilverStripe\CMS\Model\SiteTreeExtension;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\ErrorPage\ErrorPage;
9
use SilverStripe\Forms\CheckboxField;
10
use SilverStripe\Forms\FieldList;
11
12
class PageExtension extends SiteTreeExtension
13
{
14
    private static $db = [
15
        'ActiveGetSiteControl' => 'Boolean(1)',
16
    ];
17
18
    /**
19
     * list of clasess to be explicityly included
20
     * to avoid including all by default.
21
     *
22
     * @var array
23
     */
24
    private static $page_classes_included_from_get_site_control = [];
25
26
    /**
27
     * list of clasess to be excluded.
28
     *
29
     * @var array
30
     */
31
    private static $page_classes_excluded_from_get_site_control = [
32
        ErrorPage::class,
33
        RedirectorPage::class,
34
    ];
35
36
    public function updateCMSFields(FieldList $fields)
37
    {
38
        // Add get site control field only if is Published or can Publish
39
        $a = ($this->getOwner()->isPublished() && $this->getOwner()->isOnDraft());
40
        $b = $this->getOwner()->canPublish();
41
        if ($a || $b) {
42
            $fields->addFieldToTab(
43
                'Root.GetSiteControl',
44
                CheckboxField::create('ActiveGetSiteControl', 'Use GetSiteControl')
45
            );
46
        }
47
    }
48
49
    public function IsGetSiteControlPage(): bool
50
    {
51
        return $this->getOwner()->IsGetSiteControlEnabledOnPageLevel() && $this->getOwner()->ActiveGetSiteControl;
52
    }
53
54
    public function IsGetSiteControlEnabledOnPageLevel(): bool
55
    {
56
        $included = Config::inst()->get(PageExtension::class, 'page_classes_included_from_get_site_control');
57
        if (! empty($included)) {
58
            foreach ($included as $className) {
59
                if ($this instanceof $className) {
60
                    return true;
61
                }
62
            }
63
64
            return false;
65
        }
66
67
        $excluded = Config::inst()->get(PageExtension::class, 'page_classes_excluded_from_get_site_control');
68
        foreach ($excluded as $className) {
69
            if ($this instanceof $className) {
70
                return false;
71
            }
72
        }
73
74
        return true;
75
    }
76
}
77