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'; |
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private static $table_name = 'ElementAccordion'; |
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private static $db = [ |
|
|
|
|
39
|
|
|
'Content' => 'HTMLText', |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
private static $has_many = array( |
|
|
|
|
46
|
|
|
'Panels' => AccordionPanel::class, |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
private static $owns = [ |
|
|
|
|
53
|
|
|
'Panels', |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var bool |
58
|
|
|
*/ |
59
|
|
|
private static $inline_editable = false; |
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
1 |
|
* @param bool $includerelations |
63
|
|
|
* @return array |
64
|
1 |
|
*/ |
65
|
|
|
public function fieldLabels($includerelations = true) |
66
|
1 |
|
{ |
67
|
1 |
|
$labels = parent::fieldLabels($includerelations); |
68
|
|
|
|
69
|
1 |
|
$labels['Content'] = _t(__CLASS__.'.ContentLabel', 'Intro'); |
70
|
|
|
$labels['Panels'] = _t(__CLASS__ . '.PanelsLabel', 'Panels'); |
71
|
|
|
|
72
|
|
|
return $labels; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return FieldList |
77
|
1 |
|
*/ |
78
|
|
|
public function getCMSFields() |
79
|
1 |
|
{ |
80
|
1 |
|
$this->beforeUpdateCMSFields(function ($fields) { |
81
|
|
|
/* @var FieldList $fields */ |
82
|
|
|
$fields->removeByName(array( |
83
|
1 |
|
'Sort', |
84
|
1 |
|
)); |
85
|
1 |
|
|
86
|
1 |
|
$fields->dataFieldByName('Content') |
87
|
|
|
->setDescription(_t( |
88
|
1 |
|
__CLASS__.'.ContentDescription', |
89
|
|
|
'optional. Add introductory copy to your accordion.' |
90
|
1 |
|
)) |
91
|
|
|
->setRows(5); |
92
|
1 |
|
|
93
|
1 |
|
if ($this->ID) { |
94
|
|
|
/** @var GridField $panels */ |
95
|
1 |
|
$panels = $fields->dataFieldByName('Panels'); |
96
|
|
|
$panels->setTitle($this->fieldLabel('Panels')); |
97
|
1 |
|
|
98
|
1 |
|
$fields->removeByName('Panels'); |
99
|
1 |
|
|
100
|
1 |
|
$config = $panels->getConfig(); |
101
|
|
|
$config->addComponent(new GridFieldOrderableRows('Sort')); |
102
|
1 |
|
$config->removeComponentsByType(GridFieldAddExistingAutocompleter::class); |
103
|
|
|
$config->removeComponentsByType(GridFieldDeleteAction::class); |
104
|
1 |
|
|
105
|
|
|
$fields->addFieldToTab('Root.Main', $panels); |
106
|
1 |
|
} |
107
|
|
|
}); |
108
|
|
|
|
109
|
|
|
return parent::getCMSFields(); |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
/** |
113
|
|
|
* @return DBHTMLText |
114
|
1 |
|
*/ |
115
|
1 |
|
public function getSummary() |
116
|
1 |
|
{ |
117
|
1 |
|
$count = $this->Panels()->count(); |
118
|
1 |
|
$label = _t( |
119
|
|
|
AccordionPanel::class . '.PLURALS', |
120
|
1 |
|
'{count} Accordion Panel|{count} Accordion Panels', |
121
|
|
|
[ 'count' => $count ] |
122
|
|
|
); |
123
|
|
|
return DBField::create_field('HTMLText', $label)->Summary(20); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
protected function provideBlockSchema() |
130
|
|
|
{ |
131
|
|
|
$blockSchema = parent::provideBlockSchema(); |
132
|
|
|
$blockSchema['content'] = $this->getSummary(); |
133
|
|
|
return $blockSchema; |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
/** |
137
|
|
|
* @return string |
138
|
1 |
|
*/ |
139
|
|
|
public function getType() |
140
|
|
|
{ |
141
|
|
|
return _t(__CLASS__.'.BlockType', 'Accordion'); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|