Completed
Push — master ( ed7f79...6ebeeb )
by Donata
02:21
created

SliderBlock::getSliderOptions()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 1
nop 0
1
<?php
2
3
/**
4
 * @author    Donatas Navidonskis <[email protected]>
5
 * @since     2017
6
 * @class     SliderBlock
7
 *
8
 * @property int     SlideSpeed
9
 * @property boolean EnableMouseEvents
10
 * @property boolean Infinite
11
 *
12
 * @method DataList Sliders
13
 */
14
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...
15
16
    /**
17
     * @var array
18
     * @config
19
     */
20
    private static $db = [
0 ignored issues
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 $db 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...
21
        'SlideSpeed'        => 'Int(600)',
22
        'EnableMouseEvents' => 'Boolean(true)',
23
        'Infinite'          => 'Boolean(true)',
24
    ];
25
26
    /**
27
     * @var array
28
     * @config
29
     */
30
    private static $has_many = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
31
        'Sliders' => 'BaseSliderItem',
32
    ];
33
34
    /**
35
     * When slider is rendering, load javascript module for the slider.
36
     *
37
     * @var bool
38
     * @config
39
     */
40
    private static $load_slider_script = true;
41
42
    /**
43
     * @var array
44
     * @config
45
     */
46
    private static $defaults = [
0 ignored issues
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 $defaults 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...
47
        'SlideSpeed'        => 600,
48
        'EnableMouseEvents' => true,
49
        'Infinite'          => true,
50
    ];
51
52
    /**
53
     * @return string
54
     */
55
    public function singular_name() {
56
        return _t('SliderBlock.SINGULARNAME', 'Slider Block');
57
    }
58
59
    /**
60
     * @return string
61
     */
62
    public function plural_name() {
63
        return _t('SliderBlock.PLURALNAME', 'Slider Blocks');
64
    }
65
66
    /**
67
     * @return \FieldList
68
     */
69
    public function getCMSFields() {
70
        $fields = parent::getCMSFields();
71
        $fields->removeByName(['Content', 'SlideSpeed', 'EnableMouseEvents', 'Infinite']);
72
        $fields->findOrMakeTab('Root.Sliders', _t('SliderItem.PLURALNAME', 'Sliders'));
73
74
        if ($this->ID) {
75
            $sliderField = $this->createSliderGridField();
76
        } else {
77
            $label = _t('SliderBlock.PLEASE_SAVE_BEFORE_ADD_SLIDERS', 'Please save block before add sliders');
78
            $sliderField = LiteralField::create("PleaseSaveBeforeAddSliders", "<p class=\"message notice\">{$label}</p>");
79
        }
80
81
        $fields->addFieldsToTab('Root.Main', [
82
            TextField::create('SlideSpeed', _t('SliderBlock.SLIDE_SPEED', 'Slide speed (ms)')),
83
            DropdownField::create('EnableMouseEvents', _t('SliderBlock.ENABLE_MOUSE_EVENTS', 'Enable mouse events?'), BlocksUtility::localized_answers()),
84
            DropdownField::create('Infinite', _t('SliderBlock.INFINITE', 'Infinite?'), BlocksUtility::localized_answers()),
85
        ]);
86
87
        $fields->addFieldToTab('Root.Sliders', $sliderField);
88
89
        return $fields;
90
    }
91
92
    /**
93
     * @return GridField
94
     */
95
    protected function createSliderGridField() {
96
        $config = GridFieldConfig::create()->addComponents(
97
            new GridFieldToolbarHeader(),
98
            new GridFieldSortableHeader(),
99
            new GridFieldDataColumns(),
100
            new GridFieldPaginator(20),
101
            new GridFieldDetailForm()
102
        );
103
104
        if ($this->ID) {
105
            $config->addComponent(new GridFieldOrderableRows('SortOrder'));
106
        }
107
108
        if ($this->canEdit()) {
109
            $multiClass = new GridFieldAddNewMultiClass();
110
            $multiClass->setClasses($this->getSliderClasses());
111
112
            $config->addComponents(
113
                $multiClass,
114
                new GridFieldEditButton()
115
            );
116
        }
117
118
        if ($this->canDelete()) {
119
            $config->addComponent(new GridFieldDeleteAction());
120
        }
121
122
        $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...
123
124
        return $sliders;
125
    }
126
127
    /**
128
     * Get slider types which are sub classes for BaseSliderItem
129
     *
130
     * @return array
131
     */
132
    public function getSliderClasses() {
133
        $classes = ArrayLib::valuekey(ClassInfo::subclassesFor('BaseSliderItem'));
134
        array_shift($classes);
135
        foreach ($classes as $k => $v) {
136
            $classes[$k] = singleton($k)->singular_name();
137
        }
138
139
        return $classes;
140
    }
141
142
    /**
143
     * @return array|string
144
     */
145
    public function getSliderOptions() {
146
        return \Convert::raw2att(\Convert::array2json([
147
            'slideSpeed'        => ! empty($this->SlideSpeed) ? (int) $this->SlideSpeed : 300,
148
            'enableMouseEvents' => (bool) $this->EnableMouseEvents,
149
            'infinite'          => (int) $this->Infinite,
150
        ]));
151
    }
152
}
153
154
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...
155
156
    public function init() {
157
        if (SliderBlock::config()->load_slider_script) {
158
            Requirements::javascript("//cdnjs.cloudflare.com/ajax/libs/lory.js/2.2.0/lory.min.js");
159
            Requirements::javascript(CONTENT_BLOCKS_DIR."/assets/javascript/universal-slider.js");
160
        }
161
162
        parent::init();
163
    }
164
165
}