Completed
Pull Request — master (#62)
by Jason
12:32
created

AccordionBlockController::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Dynamic\DynamicBlocks\Block;
4
5
use Dynamic\DynamicBlocks\Model\AccordionPanel;
6
use League\Flysystem\Config;
7
use SheaDawson\Blocks\Controllers\BlockController;
8 12
use SheaDawson\Blocks\Model\Block;
9
use SilverStripe\Forms\GridField\GridField;
10 12
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
11
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
12
use SilverStripe\View\Requirements;
13
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
14
15
class AccordionBlock extends Block
16 1
{
17
    /**
18 1
     * @return string
19
     */
20
    public function singular_name()
21
    {
22
        return _t('AccordionBlock.SINGULARNAME', 'Accordion Block');
23
    }
24
25
    /**
26
     * @return string
27
     */
28
    public function plural_name()
29
    {
30
        return _t('AccordionBlock.PLURALNAME', 'Accordion Blocks');
31
    }
32
33
    /**
34
     * @var array
35
     */
36
    private static $db = array(
37
        'Content' => 'HTMLText',
38
        'SortOrder' => 'Int',
39 1
    );
40
41 1
    /**
42
     * @var array
43 1
     */
44 1
    private static $has_many = array(
45 1
        'Panels' => AccordionPanel::class,
46 1
    );
47
48 1
    /**
49 1
     * @return FieldList
50 1
     */
51 1
    public function getCMSFields()
52 1
    {
53 1
        $fields = parent::getCMSFields();
54 1
55
        $fields->removeByName(array(
56 1
            'SortOrder',
57 1
            'Panels',
58 1
        ));
59 1
60 1
        $config = GridFieldConfig_RecordEditor::create();
61
        if (class_exists(GridFieldOrderableRows::class)) {
62 1
            $config->addComponent(new GridFieldOrderableRows());
63
        }
64
        $config->removeComponentsByType('GridFieldAddExistingAutocompleter');
65
        $config->removeComponentsByType(GridFieldDeleteAction::class);
66
        $config->addComponent(new GridFieldDeleteAction(false));
67
68 1
        if ($this->ID) {
69
            $fields->addFieldsToTab('Root.Panels', array(
70 1
                GridField::create('Panels', 'Accordion Panels', $this->Panels()->sort('Sort'), $config),
0 ignored issues
show
Documentation Bug introduced by
The method Panels does not exist on object<Dynamic\DynamicBl...s\Block\AccordionBlock>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
71 1
            ));
72
        }
73
74
        return $fields;
75
    }
76
77
    /**
78
     * @return DataList
79
     */
80
    public function getPanelList()
81
    {
82
        if ($this->Panels()->exists()) {
0 ignored issues
show
Documentation Bug introduced by
The method Panels does not exist on object<Dynamic\DynamicBl...s\Block\AccordionBlock>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
83
            return $this->Panels()->sort('Sort');
0 ignored issues
show
Documentation Bug introduced by
The method Panels does not exist on object<Dynamic\DynamicBl...s\Block\AccordionBlock>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
84
        }
85
        return $this->Panels();
0 ignored issues
show
Documentation Bug introduced by
The method Panels does not exist on object<Dynamic\DynamicBl...s\Block\AccordionBlock>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
86
    }
87
}
88
89
class AccordionBlockController extends BlockController
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
90
{
91
    /**
92
     * @var string
93
     */
94
    private static $accordion_class = 'accordion-block';
0 ignored issues
show
Unused Code introduced by
The property $accordion_class is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
95
96
    /**
97
     *
98
     */
99
    public function init()
100
    {
101
        $class = $this->AccordionClass();
102
        Requirements::javascript(THIRDPARTY_DIR . '/jquery-ui/jquery-ui.js');
103
        Requirements::customScript('
104
            $(function() {
105
                $( ".' . $class . '" ).accordion({
106
                    header: ".accord-header",
107
                    collapsible: true, heightStyle: "content"
108
                });
109
            });
110
        ');
111
    }
112
113
    /**
114
     * @return array
115
     */
116
    public function AccordionClass()
117
    {
118
        return Config::inst()->get('AccordionBlockController', 'accordion_class');
0 ignored issues
show
Bug introduced by
The method inst() does not seem to exist on object<League\Flysystem\Config>.

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...
119
    }
120
}