Passed
Push — master ( 5cd879...e6bc9d )
by Nicolaas
02:12
created

ElementalSwitchTabsExtension::NextBlock()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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