Passed
Push — master ( b9b96d...e3435c )
by Jason
02:55
created

ElementFeatures::ElementSummary()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
namespace Dynamic\Elements\Features\Elements;
4
5
use DNADesign\Elemental\Models\BaseElement;
6
use Dynamic\Elements\Features\Model\FeatureObject;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
9
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
10
use SilverStripe\ORM\FieldType\DBField;
11
use SilverStripe\ORM\FieldType\DBHTMLText;
12
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
13
14
/**
15
 * Class PageSectionBlock.
16
 *
17
 * @method HasManyList $Features
18
 */
19
class ElementFeatures extends BaseElement
20
{
21
    /**
22
     * @var string
23
     */
24
    private static $icon = 'font-icon-block-banner';
0 ignored issues
show
introduced by
The private property $icon is not used, and could be removed.
Loading history...
25
26
    /**
27
     * @return string
28
     */
29
    private static $singular_name = 'Features Element';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
30
31
    /**
32
     * @return string
33
     */
34
    private static $plural_name = 'Features Elements';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
35
36
    /**
37
     * @var string
38
     */
39
    private static $table_name = 'ElementFeatures';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
40
41
    /**
42
     * @var array
43
     */
44
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
45
        'Content' => 'HTMLText',
46
        'Alternate' => 'Boolean',
47
    ];
48
49
    /**
50
     * @var array
51
     */
52
    private static $has_many = [
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
53
        'Features' => FeatureObject::class,
54
    ];
55
56
    /**
57
     * Set to false to prevent an in-line edit form from showing in an elemental area. Instead the element will be
58
     * clickable and a GridFieldDetailForm will be used.
59
     *
60
     * @config
61
     * @var bool
62
     */
63
    private static $inline_editable = false;
0 ignored issues
show
introduced by
The private property $inline_editable is not used, and could be removed.
Loading history...
64
65
    /**
66
     * @return \SilverStripe\Forms\FieldList
67
     */
68
    public function getCMSFields()
69
    {
70 1
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
71 1
            $fields->dataFieldByName('Content')
72 1
                ->setRows(8);
73 1
            $fields->dataFieldByName('Alternate')->setTitle('Alternate images and text');
74
75 1
            if ($this->ID) {
76
                // Features
77 1
                $features = $fields->dataFieldByName('Features');
78 1
                $config = $features->getConfig();
79 1
                $config->addComponent(new GridFieldOrderableRows());
80 1
                $config->removeComponentsByType(GridFieldAddExistingAutocompleter::class);
81 1
                $config->removeComponentsByType(GridFieldDeleteAction::class);
82
            }
83 1
        });
84
85 1
        return parent::getCMSFields();
86
    }
87
88
    /**
89
     * @return mixed
90
     */
91 1
    public function getFeaturesList()
92
    {
93 1
        return $this->Features()->sort('Sort');
0 ignored issues
show
Bug introduced by
The method Features() does not exist on Dynamic\Elements\Features\Elements\ElementFeatures. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
        return $this->/** @scrutinizer ignore-call */ Features()->sort('Sort');
Loading history...
94
    }
95
96
    /**
97
     * @return DBHTMLText
98
     */
99 1
    public function getSummary()
100
    {
101 1
        if ($this->Features()->count() == 1) {
102 1
            $feature = 'feature';
103
        } else {
104
            $feature = 'features';
105
        }
106 1
        return DBField::create_field('HTMLText', $this->Features()->count() . ' ' . $feature)->Summary(20);
107
    }
108
109
    /**
110
     * @return array
111
     */
112
    protected function provideBlockSchema()
113
    {
114
        $blockSchema = parent::provideBlockSchema();
115
        $blockSchema['content'] = $this->getSummary();
116
        return $blockSchema;
117
    }
118
119
    /**
120
     * @return string
121
     */
122 1
    public function getType()
123
    {
124 1
        return _t(__CLASS__.'.BlockType', 'Features');
125
    }
126
}
127