Passed
Push — master ( 09b78f...c47642 )
by Nicolaas
07:35
created

ElementalSwitchTabsExtension   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 8
eloc 27
c 4
b 0
f 0
dl 0
loc 56
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A PreviousBlock() 0 10 3
A getLinksField() 0 5 1
A NextBlock() 0 10 3
A getJsFoTabSwitch() 0 10 1
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
        if($this->exists()) {
0 ignored issues
show
Bug introduced by
The method exists() does not exist on Sunnysideup\ElementalSwi...ntalSwitchTabsExtension. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
        if($this->/** @scrutinizer ignore-call */ exists()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42
            $parent = $this->getOwner()->Parent();
43
            if($parent) {
44
                return BaseElement::get()
45
                    ->filter(['Sort:LessThanOrEqual' => $this->Sort])
0 ignored issues
show
Bug Best Practice introduced by
The property Sort does not exist on Sunnysideup\ElementalSwi...ntalSwitchTabsExtension. Did you maybe forget to declare it?
Loading history...
46
                    ->exclude(['ID' => $this->ID])
0 ignored issues
show
Bug Best Practice introduced by
The property ID does not exist on Sunnysideup\ElementalSwi...ntalSwitchTabsExtension. Did you maybe forget to declare it?
Loading history...
47
                    ->sort(['Sort' => 'ASC'])
48
                    ->last();
49
            }
50
        }
51
    }
52
53
    /**
54
     * @return BaseElement|null
55
     */
56
    public function NextBlock()
57
    {
58
        if($this->exists()) {
59
            $parent = $this->getOwner()->Parent();
60
            if($parent) {
61
                return BaseElement::get()
62
                    ->filter(['Sort:GreaterThanOrEqual' => $this->Sort])
0 ignored issues
show
Bug Best Practice introduced by
The property Sort does not exist on Sunnysideup\ElementalSwi...ntalSwitchTabsExtension. Did you maybe forget to declare it?
Loading history...
63
                    ->exclude(['ID' => $this->ID])
0 ignored issues
show
Bug Best Practice introduced by
The property ID does not exist on Sunnysideup\ElementalSwi...ntalSwitchTabsExtension. Did you maybe forget to declare it?
Loading history...
64
                    ->sort(['Sort' => 'ASC'])
65
                    ->first();
66
            }
67
        }
68
    }
69
}
70