Passed
Push — master ( 559468...0877be )
by Nicolaas
08:53
created

ElementalSwitchTabsExtension::getClassDropdown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Sunnysideup\ElementalSwitchTabs\Extensions;
4
5
use SilverStripe\Forms\FieldList;
6
use DNADesign\Elemental\Models\BaseElement;
7
use DNADesign\Elemental\Controllers\ElementalAreaController;
8
use SilverStripe\Forms\LiteralField;
9
use SilverStripe\Forms\ReadonlyField;
10
use SilverStripe\ORM\DataExtension;
11
use SilverStripe\Control\Controller;
12
use SilverStripe\Admin\LeftAndMain;
13
use SilverStripe\CMS\Controllers\CMSPageEditController;
14
use SilverStripe\Core\ClassInfo;
15
use SilverStripe\Forms\DropdownField;
16
17
class ElementalSwitchTabsExtension extends DataExtension
18
{
19
    public function updateCMSFields(FieldList $fields)
20
    {
21
        $owner = $this->getOwner();
22
        $controller = Controller::curr();
23
        if (($controller && $controller instanceof ElementalAreaController)) {
24
            $fields->addFieldsToTab(
25
                'Root.Main',
26
                [
27
                    LiteralField::create(
28
                        'AllSettings',
29
                        '<a
30
                            href="' . $owner->MyCMSEditLink() . '"
31
                            style="float: right; display: block; width: auto;"
32
                        >Edit All Settings</a>'
33
                    ),
34
                ],
35
                'Title'
36
            );
37
        } elseif ($controller && ! ($controller instanceof CMSPageEditController)) {
38
            $page = $owner->getPage();
39
            $pageTitle = 'Page not found';
40
            if ($page) {
41
                $pageTitle = $page->MenuTitle;
42
            }
43
            $fields->addFieldsToTab(
44
                'Root.Main',
45
                [
46
                    LiteralField::create(
47
                        'AllSettings',
48
                        '<a
49
                            href="' . $owner->CMSEditLink(false) . '"
50
                            style="text-align: right; display: block; padding-bottom: 20px;"
51
                        >Edit on the "'.$pageTitle.'" page</a>'
52
                    ),
53
                ],
54
                'Title'
55
            );
56
        }
57
        $fields->addFieldsToTab(
58
            'Root.Settings',
59
            [
60
                DropdownField::create(
61
                    'ClassName',
62
                    'Background Colour',
63
                    $this->getClassDropdown()
64
                ),
65
            ]
66
        );
67
    }
68
69
70
71
    protected function getClassDropdown(): array
72
    {
73
        $owner = $this->getOwner();
74
        $page = $owner->getPage();
75
        return $page->getElementalTypes();
76
77
78
    }
79
80
    public function getLinksField(string $nameOfTab, string $label)
81
    {
82
        return LiteralField::create(
83
            'LinkToLink' . $nameOfTab,
84
            '<a href="#" onclick="' . $this->getJsFoTabSwitch($nameOfTab) . '">' . $label . '</a>'
85
        );
86
    }
87
88
    /**
89
     * @return BaseElement|null
90
     */
91
    public function PreviousBlock()
92
    {
93
        $owner = $this->getOwner();
94
        if ($owner->exists()) {
95
            $parent = $owner->Parent();
96
            if ($parent) {
97
                return $parent->Elements()
98
                    ->filter(['Sort:LessThanOrEqual' => $owner->Sort])
99
                    ->exclude(['ID' => $owner->ID])
100
                    ->sort(['Sort' => 'ASC'])
101
                    ->last()
102
                ;
103
            }
104
        }
105
        return null;
106
    }
107
108
    public function MyCMSEditLink(): string
109
    {
110
        $owner = $this->getOwner();
111
        return (string) $owner->CMSEditLink(true);
112
    }
113
114
    /**
115
     * @return BaseElement|null
116
     */
117
    public function NextBlock()
118
    {
119
        $owner = $this->getOwner();
120
        if ($owner->exists()) {
121
            $parent = $owner->Parent();
122
            if ($parent) {
123
                return $parent->Elements()
124
                    ->filter(['Sort:GreaterThanOrEqual' => $owner->Sort])
125
                    ->exclude(['ID' => $owner->ID])
126
                    ->sort(['Sort' => 'ASC'])
127
                    ->first()
128
                ;
129
            }
130
        }
131
        return null;
132
    }
133
134
    protected function getJsFoTabSwitch(string $nameOfTab): string
135
    {
136
        return <<<js
137
        if(jQuery(this).closest('div.element-editor__element').length > 0) {
138
            jQuery(this)
139
                .closest('div.element-editor__element')
140
                .find('button[name=\\'{$nameOfTab}\\']')
141
                .click();
142
        } else {
143
            jQuery('li[aria-controls=\\'Root_{$nameOfTab}\\'] a').click();
144
        }
145
        return false;
146
js;
147
    }
148
}
149