Passed
Push — master ( 9cdfee...3165c7 )
by Nicolaas
01:45
created

ElementalSwitchTabsExtension::getLinksField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 3
c 2
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Sunnysideup\ElementalSwitchTabs\Extensions;
4
5
use SilverStripe\Forms\LiteralField;
6
use SilverStripe\ORM\DataExtension;
7
8
use DNADesign\Elemental\Models\BaseElement;
9
10
class ElementalSwitchTabsExtension extends DataExtension
11
{
12
    public function getLinksField(string $nameOfTab, string $label)
13
    {
14
        return LiteralField::create(
15
            'LinkToLink' . $nameOfTab,
16
            '<a href="#" onclick="' . $this->getJsFoTabSwitch($nameOfTab) . '">' . $label . '</a>'
17
        );
18
    }
19
20
    protected function getJsFoTabSwitch(string $nameOfTab): string
21
    {
22
        return <<<js
23
        if(jQuery(this).closest('div.element-editor__element').length > 0) {
24
            jQuery(this)
25
                .closest('div.element-editor__element')
26
                .find('button[name=\\'{$nameOfTab}\\']')
27
                .click();
28
        } else {
29
            jQuery('li[aria-controls=\\'Root_{$nameOfTab}\\'] a').click();
30
        }
31
        return false;
32
js;
33
    }
34
35
36
    /**
37
     * @return BaseElement|null
38
     */
39
    public function PreviousBlock()
40
    {
41
        $owner = $this->getOwner();
42
        if($owner->exists()) {
43
            $parent = $owner->Parent();
44
            if($parent) {
45
                return $parent->Elements()
46
                    ->filter(['Sort:LessThanOrEqual' => $owner->Sort])
47
                    ->exclude(['ID' => $owner->ID])
48
                    ->sort(['Sort' => 'ASC'])
49
                    ->last();
50
            }
51
        }
52
    }
53
54
    public function MyCMSEditLink() : string
55
    {
56
        $owner = $this->getOwner();
57
        $page = $owner->getPage();
58
59
        return '/admin/pages/edit/EditForm/'.$page->ID.'/field/ElementalArea/item/'.$owner->ID.'/edit';
60
    }
61
62
    /**
63
     * @return BaseElement|null
64
     */
65
    public function NextBlock()
66
    {
67
        $owner = $this->getOwner();
68
        if($owner->exists()) {
69
            $parent = $owner->Parent();
70
            if($parent) {
71
                return $parent->Elements()
72
                    ->filter(['Sort:GreaterThanOrEqual' => $owner->Sort])
73
                    ->exclude(['ID' => $owner->ID])
74
                    ->sort(['Sort' => 'ASC'])
75
                    ->first();
76
            }
77
        }
78
    }
79
}
80