Completed
Push — master ( ea5826...32b527 )
by Jason
05:07 queued 02:24
created

ElementAccordion::fieldLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 $table_name = 'ElementAccordion';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
34
35
    /**
36
     * @var string
37
     */
38
    private static $description = 'Display content in collapsable panels.';
0 ignored issues
show
introduced by
The private property $description is not used, and could be removed.
Loading history...
39
40
    /**
41
     * @var array
42
     */
43
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
44
        'Content' => 'HTMLText',
45
    ];
46
47
    /**
48
     * @var array
49
     */
50
    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...
51
        'Panels' => AccordionPanel::class,
52
    );
53
54
    /**
55
     * Set to false to prevent an in-line edit form from showing in an elemental area. Instead the element will be
56
     * clickable and a GridFieldDetailForm will be used.
57
     *
58
     * @config
59
     * @var bool
60
     */
61
    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...
62
63
    /**
64
     * @param bool $includerelations
65
     * @return array
66
     */
67 1
    public function fieldLabels($includerelations = true)
68
    {
69 1
        $labels = parent::fieldLabels($includerelations);
70 1
        $labels['Panels'] = _t(__CLASS__ . '.PanelsLabel', 'Accordion panels');
71
72 1
        return $labels;
73
    }
74
75
    /**
76
     * @return FieldList
77
     */
78
    public function getCMSFields()
79
    {
80 1
        $this->beforeUpdateCMSFields(function ($fields) {
81
            /* @var FieldList $fields */
82 1
            $fields->removeByName(array(
83 1
                'Sort',
84
            ));
85
86 1
            $fields->dataFieldByName('Content')
87 1
                ->setRows(8);
88
89 1
            if ($this->ID) {
90
                /** @var GridField $panels */
91 1
                $panels = $fields->dataFieldByName('Panels');
92 1
                $panels->setTitle($this->fieldLabel('Panels'));
93
94 1
                $fields->removeByName('Panels');
95
96 1
                $config = $panels->getConfig();
97 1
                $config->addComponent(new GridFieldOrderableRows('Sort'));
98 1
                $config->removeComponentsByType(GridFieldAddExistingAutocompleter::class);
99 1
                $config->removeComponentsByType(GridFieldDeleteAction::class);
100
101 1
                $fields->addFieldToTab('Root.Main', $panels);
102
            }
103 1
        });
104
105 1
        return parent::getCMSFields();
106
    }
107
108
    /**
109
     * @return DBHTMLText
110
     */
111 1
    public function getSummary()
112
    {
113 1
        if ($this->Panels()->count() == 1) {
114
            $label = ' panel';
115
        } else {
116 1
            $label = ' panels';
117
        }
118 1
        return DBField::create_field('HTMLText', $this->Panels()->count() . $label)->Summary(20);
119
    }
120
121
    /**
122
     * @return array
123
     */
124
    protected function provideBlockSchema()
125
    {
126
        $blockSchema = parent::provideBlockSchema();
127
        $blockSchema['content'] = $this->getSummary();
128
        return $blockSchema;
129
    }
130
131
    /**
132
     * @return string
133
     */
134 1
    public function getType()
135
    {
136 1
        return _t(__CLASS__.'.BlockType', 'Accordion');
137
    }
138
}
139