Passed
Push — master ( 2748a9...0d799e )
by Nicolaas
01:58
created

ElementalSwitchTabsExtension::getJsFoTabSwitch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 7
c 2
b 0
f 0
dl 0
loc 10
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Sunnysideup\ElementalSwitchTabs\Extensions;
4
5
use DNADesign\Elemental\Models\BaseElement;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\LiteralField;
8
use SilverStripe\ORM\DataExtension;
9
10
class ElementalSwitchTabsExtension extends DataExtension
11
{
12
13
    public function updateCMSFields(FieldList $fields)
14
    {
15
        $owner = $this->getOwner();
16
        $fields->addFieldsToTab(
17
            'Root.Main',
18
            [
19
                LiteralField::create(
20
                    'AllSettings',
21
                    '<a
22
                        href="' . $owner->MyCMSEditLink(true) . '"
23
                        style="float: right; display: block; width: auto;"
24
                    >Edit All Settings</a>'
25
                ),
26
            ],
27
            'Title'
28
        );
29
    }
30
31
32
    public function getLinksField(string $nameOfTab, string $label)
33
    {
34
        return LiteralField::create(
35
            'LinkToLink' . $nameOfTab,
36
            '<a href="#" onclick="' . $this->getJsFoTabSwitch($nameOfTab) . '">' . $label . '</a>'
37
        );
38
    }
39
40
    /**
41
     * @return BaseElement|null
42
     */
43
    public function PreviousBlock()
44
    {
45
        $owner = $this->getOwner();
46
        if ($owner->exists()) {
47
            $parent = $owner->Parent();
48
            if ($parent) {
49
                return $parent->Elements()
50
                    ->filter(['Sort:LessThanOrEqual' => $owner->Sort])
51
                    ->exclude(['ID' => $owner->ID])
52
                    ->sort(['Sort' => 'ASC'])
53
                    ->last()
54
                ;
55
            }
56
        }
57
        return null;
58
    }
59
60
    public function MyCMSEditLink(): string
61
    {
62
        $owner = $this->getOwner();
63
        return (string) $owner->CMSEditLink(true);
64
    }
65
66
    /**
67
     * @return BaseElement|null
68
     */
69
    public function NextBlock()
70
    {
71
        $owner = $this->getOwner();
72
        if ($owner->exists()) {
73
            $parent = $owner->Parent();
74
            if ($parent) {
75
                return $parent->Elements()
76
                    ->filter(['Sort:GreaterThanOrEqual' => $owner->Sort])
77
                    ->exclude(['ID' => $owner->ID])
78
                    ->sort(['Sort' => 'ASC'])
79
                    ->first()
80
                ;
81
            }
82
        }
83
        return null;
84
    }
85
86
    protected function getJsFoTabSwitch(string $nameOfTab): string
87
    {
88
        return <<<js
89
        if(jQuery(this).closest('div.element-editor__element').length > 0) {
90
            jQuery(this)
91
                .closest('div.element-editor__element')
92
                .find('button[name=\\'{$nameOfTab}\\']')
93
                .click();
94
        } else {
95
            jQuery('li[aria-controls=\\'Root_{$nameOfTab}\\'] a').click();
96
        }
97
        return false;
98
js;
99
    }
100
}
101