Passed
Push — master ( 2d29fe...73ce0d )
by Nicolaas
08:00
created

ElementalSwitchTabsExtension::PreviousBlock()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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