Passed
Push — master ( 5d914a...ea5826 )
by Jason
04:12
created

ElementAccordion::getCMSFields()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 1
nop 0
dl 0
loc 24
rs 9.8333
c 0
b 0
f 0
ccs 14
cts 14
cp 1
crap 2
1
<?php
2
3
namespace Dynamic\Elements\Accordion\Elements;
4
5
use DNADesign\Elemental\Models\BaseElement;
6
use Dynamic\Elements\Accordion\Model\AccordionPanel;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\GridField\GridField;
9
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
10
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
11
use SilverStripe\ORM\FieldType\DBField;
12
use SilverStripe\ORM\FieldType\DBHTMLText;
13
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
14
15
/**
16
 * Class ElementAccordion
17
 * @package Dynamic\Elements\Accordion\Elements
18
 *
19
 * @property string $Content
20
 *
21
 * @method \SilverStripe\ORM\HasManyList Panels()
22
 */
23
class ElementAccordion extends BaseElement
24
{
25
    /**
26
     * @var string
27
     */
28
    private static $icon = 'font-icon-block-content';
0 ignored issues
show
introduced by
The private property $icon is not used, and could be removed.
Loading history...
29
30
    /**
31
     * @var string
32
     */
33
    private static $singular_name = 'Accordion';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
34
35
    /**
36
     * @var string
37
     */
38
    private static $plural_name = 'Accordions';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
39
40
    /**
41
     * @var string
42
     */
43
    private static $table_name = 'ElementAccordion';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
44
45
    /**
46
     * @var string
47
     */
48
    private static $description = 'A collapsing list of content';
0 ignored issues
show
introduced by
The private property $description is not used, and could be removed.
Loading history...
49
50
    /**
51
     * @var array
52
     */
53
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
54
        'Content' => 'HTMLText',
55
    ];
56
57
    /**
58
     * @var array
59
     */
60
    private static $has_many = array(
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
61
        'Panels' => AccordionPanel::class,
62
    );
63
64
    /**
65
     * Set to false to prevent an in-line edit form from showing in an elemental area. Instead the element will be
66
     * clickable and a GridFieldDetailForm will be used.
67
     *
68
     * @config
69
     * @var bool
70
     */
71
    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...
72
73
    /**
74
     * @return FieldList
75
     */
76
    public function getCMSFields()
77
    {
78 1
        $this->beforeUpdateCMSFields(function ($fields) {
79
            /* @var FieldList $fields */
80 1
            $fields->removeByName(array(
81 1
                'Sort',
82
            ));
83
84 1
            $fields->dataFieldByName('Content')
85 1
                ->setRows(8);
86
87 1
            if ($this->ID) {
88
                /** @var GridField $panels */
89 1
                $panels = $fields->dataFieldByName('Panels');
90 1
                $panels->setTitle(_t(__CLASS__.'.Panels', 'Panels'));
91
92 1
                $config = $panels->getConfig();
93 1
                $config->addComponent(new GridFieldOrderableRows('Sort'));
94 1
                $config->removeComponentsByType(GridFieldAddExistingAutocompleter::class);
95 1
                $config->removeComponentsByType(GridFieldDeleteAction::class);
96
            }
97 1
        });
98
99 1
        return parent::getCMSFields();
100
    }
101
102
    /**
103
     * @return DBHTMLText
104
     */
105 1
    public function getSummary()
106
    {
107 1
        if ($this->Panels()->count() == 1) {
108
            $label = ' panel';
109
        } else {
110 1
            $label = ' panels';
111
        }
112 1
        return DBField::create_field('HTMLText', $this->Panels()->count() . $label)->Summary(20);
113
    }
114
115
    /**
116
     * @return array
117
     */
118
    protected function provideBlockSchema()
119
    {
120
        $blockSchema = parent::provideBlockSchema();
121
        $blockSchema['content'] = $this->getSummary();
122
        return $blockSchema;
123
    }
124
125
    /**
126
     * @return string
127
     */
128 1
    public function getType()
129
    {
130 1
        return _t(__CLASS__.'.BlockType', 'Accordion');
131
    }
132
}
133