Completed
Branch master (003c26)
by Pixelneat
02:56
created

SliderBlock::plural_name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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
     * @return string
22
     */
23
    public function singular_name() {
24
        return _t('SliderBlock.SINGULARNAME', 'Slider Block');
25
    }
26
27
    /**
28
     * @return string
29
     */
30
    public function plural_name() {
31
        return _t('SliderBlock.PLURALNAME', 'Slider Blocks');
32
    }
33
34
    /**
35
     * @return \FieldList
36
     */
37
    public function getCMSFields() {
38
        $fields = parent::getCMSFields();
39
        $fields->removeByName(['Content']);
40
        $fields->findOrMakeTab('Root.Sliders', _t('SliderItem.PLURALNAME', 'Sliders'));
41
42
        if ($this->ID) {
43
            $sliderField = $this->createSliderGridField();
44
        } else {
45
            $label = _t('SliderBlock.PLEASE_SAVE_BEFORE_ADD_SLIDERS', 'Please save block before add sliders');
46
            $sliderField = LiteralField::create("PleaseSaveBeforeAddSliders", "<p class=\"message notice\">{$label}</p>");
47
        }
48
49
        $fields->addFieldToTab('Root.Sliders', $sliderField);
50
51
        return $fields;
52
    }
53
54
    /**
55
     * @return GridField
56
     */
57
    protected function createSliderGridField() {
58
        $config = GridFieldConfig::create()->addComponents(
59
            new GridFieldToolbarHeader(),
60
            new GridFieldSortableHeader(),
61
            new GridFieldDataColumns(),
62
            new GridFieldPaginator(20),
63
            new GridFieldDetailForm()
64
        );
65
66
        if ($this->ID) {
67
            $config->addComponent(new GridFieldOrderableRows('SortOrder'));
68
        }
69
70
        if ($this->canEdit()) {
71
            $multiClass = new GridFieldAddNewMultiClass();
72
            $multiClass->setClasses($this->getSliderClasses());
73
74
            $config->addComponents(
75
                $multiClass,
76
                new GridFieldEditButton()
77
            );
78
        }
79
80
        if ($this->canDelete()) {
81
            $config->addComponent(new GridFieldDeleteAction());
82
        }
83
84
        $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...
85
86
        return $sliders;
87
    }
88
89
    /**
90
     * Get slider types which are sub classes for BaseSliderItem
91
     *
92
     * @return array
93
     */
94
    public function getSliderClasses() {
95
        $classes = ArrayLib::valuekey(ClassInfo::subclassesFor('BaseSliderItem'));
96
        array_shift($classes);
97
        foreach ($classes as $k => $v) {
98
            $classes[$k] = singleton($k)->singular_name();
99
        }
100
101
        return $classes;
102
    }
103
}