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

BaseSliderItem::forTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
/**
4
 * @author    Donatas Navidonskis <[email protected]>
5
 * @since     2017
6
 * @class     BaseSliderItem
7
 *
8
 * @property int    BlockID
9
 * @property int    SortOrder
10
 * @property string Title
11
 * @property string Content
12
 *
13
 * @method SliderBlock Block
14
 */
15
class BaseSliderItem extends \DataObject {
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...
16
17
    /**
18
     * @var array
19
     * @config
20
     */
21
    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...
22
        'Title'     => 'Varchar',
23
        'Content'   => 'HTMLText',
24
        'SortOrder' => 'Int',
25
    ];
26
27
    /**
28
     * @var array
29
     * @config
30
     */
31
    private static $has_one = [
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 $has_one 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...
32
        'Block' => 'SliderBlock',
33
    ];
34
35
    /**
36
     * The default sort expression. This will be inserted in the ORDER BY
37
     * clause of a SQL query if no other sort expression is provided.
38
     *
39
     * @var string
40
     * @config
41
     */
42
    private static $default_sort = 'SortOrder ASC';
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 $default_sort 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...
43
44
    /**
45
     * @return string
46
     */
47
    public function singular_name() {
48
        return _t('SliderItem.SINGULARNAME', 'Slider');
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function plural_name() {
55
        return _t('SliderItem.PLURALNAME', 'Sliders');
56
    }
57
58
    /**
59
     * Default summary fields within localized label title's.
60
     *
61
     * @return array
62
     */
63
    public function summaryFields() {
64
        return [
65
            'Title'      => $this->fieldLabel('Title'),
66
            'SliderType' => $this->fieldLabel('SliderType'),
67
        ];
68
    }
69
70
    /**
71
     * @return string
72
     */
73
    public function getSliderType() {
74
        return $this->singular_name();
75
    }
76
77
    /**
78
     * @return \FieldList
79
     */
80
    public function getCMSFields() {
81
        $fields = parent::getCMSFields();
82
        $fields->removeByName(['SortOrder', 'Title', 'Content']);
83
        $fields->findOrMakeTab('Root.Media', $this->fieldLabel('Media'));
84
85
        $fields->addFieldsToTab('Root.Main', [
86
            \TextField::create('Title', $this->fieldLabel('Title')),
87
            $contentField = \HtmlEditorField::create('Content', $this->fieldLabel('Content')),
88
        ]);
89
90
        $contentField
91
            ->setRows(15);
92
93
        $this->extend('updateCMSFields', $fields);
94
95
        return $fields;
96
    }
97
98
    /**
99
     * @param bool $includeRelations
100
     *
101
     * @return array
102
     */
103
    public function fieldLabels($includeRelations = true) {
104
        return array_merge(parent::fieldLabels($includeRelations), [
105
            'Title'      => _t('SliderItem.TITLE', 'Title'),
106
            'Content'    => _t('SliderItem.CONTENT', 'Content'),
107
            'Picture'    => _t('SliderItem.PICTURE', 'Picture'),
108
            'Media'      => _t('SliderItem.MEDIA', 'Media'),
109
            'Video'      => _t('SliderItem.VIDEO', 'Video'),
110
            'SliderType' => _t('SliderItem.SLIDER_TYPE', 'Slider type'),
111
        ]);
112
    }
113
114
    /**
115
     * @return \HTMLText
116
     */
117
    public function forTemplate() {
118
        return $this->renderWith(
119
            sprintf("%s_%s", $this->Block()->ClassName, $this->ClassName)
0 ignored issues
show
Documentation Bug introduced by
The method Block does not exist on object<BaseSliderItem>? 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...
120
        );
121
    }
122
}