Completed
Push — master ( 003c26...a1c350 )
by Donata
02:15
created

SliderBlock_Controller::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
/**
4
 * @author    Donatas Navidonskis <[email protected]>
5
 * @since     2017
6
 * @class     SliderBlock
7
 *
8
 * @method DataList Sliders
9
 */
10
class SliderBlock extends BaseBlock {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
11
12
    /**
13
     * @var array
14
     * @config
15
     */
16
    private static $has_many = [
1 ignored issue
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $has_many 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...
17
        'Sliders' => 'BaseSliderItem',
18
    ];
19
20
    /**
21
     * When slider is rendering, load javascript module for the slider.
22
     *
23
     * @var bool
24
     * @config
25
     */
26
    private static $load_slider_script = true;
1 ignored issue
show
Unused Code introduced by
The property $load_slider_script 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...
27
28
    /**
29
     * @return string
30
     */
31
    public function singular_name() {
32
        return _t('SliderBlock.SINGULARNAME', 'Slider Block');
33
    }
34
35
    /**
36
     * @return string
37
     */
38
    public function plural_name() {
39
        return _t('SliderBlock.PLURALNAME', 'Slider Blocks');
40
    }
41
42
    /**
43
     * @return \FieldList
44
     */
45
    public function getCMSFields() {
46
        $fields = parent::getCMSFields();
47
        $fields->removeByName(['Content']);
48
        $fields->findOrMakeTab('Root.Sliders', _t('SliderItem.PLURALNAME', 'Sliders'));
49
50
        if ($this->ID) {
51
            $sliderField = $this->createSliderGridField();
52
        } else {
53
            $label = _t('SliderBlock.PLEASE_SAVE_BEFORE_ADD_SLIDERS', 'Please save block before add sliders');
54
            $sliderField = LiteralField::create("PleaseSaveBeforeAddSliders", "<p class=\"message notice\">{$label}</p>");
55
        }
56
57
        $fields->addFieldToTab('Root.Sliders', $sliderField);
58
59
        return $fields;
60
    }
61
62
    /**
63
     * @return GridField
64
     */
65
    protected function createSliderGridField() {
66
        $config = GridFieldConfig::create()->addComponents(
67
            new GridFieldToolbarHeader(),
68
            new GridFieldSortableHeader(),
69
            new GridFieldDataColumns(),
70
            new GridFieldPaginator(20),
71
            new GridFieldDetailForm()
72
        );
73
74
        if ($this->ID) {
75
            $config->addComponent(new GridFieldOrderableRows('SortOrder'));
76
        }
77
78
        if ($this->canEdit()) {
79
            $multiClass = new GridFieldAddNewMultiClass();
80
            $multiClass->setClasses($this->getSliderClasses());
81
82
            $config->addComponents(
83
                $multiClass,
84
                new GridFieldEditButton()
85
            );
86
        }
87
88
        if ($this->canDelete()) {
89
            $config->addComponent(new GridFieldDeleteAction());
90
        }
91
92
        $sliders = new GridField('Sliders', null, $this->Sliders(), $config);
0 ignored issues
show
Documentation Bug introduced by
The method Sliders does not exist on object<SliderBlock>? 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...
93
94
        return $sliders;
95
    }
96
97
    /**
98
     * Get slider types which are sub classes for BaseSliderItem
99
     *
100
     * @return array
101
     */
102
    public function getSliderClasses() {
103
        $classes = ArrayLib::valuekey(ClassInfo::subclassesFor('BaseSliderItem'));
104
        array_shift($classes);
105
        foreach ($classes as $k => $v) {
106
            $classes[$k] = singleton($k)->singular_name();
107
        }
108
109
        return $classes;
110
    }
111
}
112
113
class SliderBlock_Controller extends Block_Controller {
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...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
114
115
    public function init() {
116
        if (SliderBlock::config()->load_slider_script) {
117
            Requirements::javascript("//cdnjs.cloudflare.com/ajax/libs/lory.js/2.2.0/lory.min.js");
118
            Requirements::javascript(CONTENT_BLOCKS_DIR."/assets/javascript/universal-slider.js");
119
        }
120
121
        parent::init();
122
    }
123
124
}