1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* A list contains nested {@link BaseElement} such as a list of related files. |
5
|
|
|
* |
6
|
|
|
* @package elemental |
7
|
|
|
*/ |
8
|
|
|
class ElementList extends BaseElement |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
private static $db = array( |
|
|
|
|
12
|
|
|
'ListDescription' => 'HTMLText' |
13
|
|
|
); |
14
|
|
|
|
15
|
|
|
private static $has_many = array( |
|
|
|
|
16
|
|
|
'Elements' => 'BaseElement' |
17
|
|
|
); |
18
|
|
|
|
19
|
|
|
private static $duplicate_relations = array( |
|
|
|
|
20
|
|
|
'Elements' |
21
|
|
|
); |
22
|
|
|
|
23
|
|
|
private static $title = "Element List Element"; |
|
|
|
|
24
|
|
|
|
25
|
|
|
private static $description = "Orderable list of elements"; |
|
|
|
|
26
|
|
|
|
27
|
|
|
private static $enable_title_in_template = true; |
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return FieldList |
31
|
|
|
*/ |
32
|
|
|
public function getCMSFields() |
33
|
|
|
{ |
34
|
|
|
$elements = $this->Elements(); |
|
|
|
|
35
|
|
|
$isInDb = $this->isInDB(); |
36
|
|
|
|
37
|
|
|
$this->beforeUpdateCMSFields(function ($fields) use ($elements, $isInDb) { |
38
|
|
|
$fields->removeByName('Root.Elements'); |
39
|
|
|
$fields->removeByName('Elements'); |
40
|
|
|
|
41
|
|
|
$desc = HTMLEditorField::create('ListDescription', 'List Description'); |
42
|
|
|
$desc->setRightTitle('Optional'); |
43
|
|
|
$fields->addFieldToTab('Root.Main', $desc); |
44
|
|
|
|
45
|
|
|
if ($isInDb) { |
46
|
|
|
$adder = new ElementalGridFieldAddNewMultiClass('buttons-before-left'); |
47
|
|
|
|
48
|
|
|
$list = $this->getAvailableTypes(); |
49
|
|
|
|
50
|
|
|
if($list) { |
|
|
|
|
51
|
|
|
$adder->setClasses($list); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$config = GridFieldConfig_RecordEditor::create(100); |
55
|
|
|
$config->addComponent(new GridFieldSortableRows('Sort')); |
56
|
|
|
$config->removeComponentsByType('GridFieldAddNewButton'); |
57
|
|
|
$config->removeComponentsByType('GridFieldSortableHeader'); |
58
|
|
|
$config->removeComponentsByType('GridFieldDeleteAction'); |
59
|
|
|
$config->removeComponentsByType('GridFieldAddExistingAutocompleter'); |
60
|
|
|
$config->addComponent(new GridFieldTitleHeader()); |
61
|
|
|
$config->addComponent($adder); |
62
|
|
|
$config->addComponent($autocompleter = new ElementalGridFieldAddExistingAutocompleter('buttons-before-right')); |
63
|
|
|
|
64
|
|
|
if ($this->owner->canDelete()) { |
|
|
|
|
65
|
|
|
$config->addComponent(new ElementalGridFieldDeleteAction()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if($list) { |
|
|
|
|
69
|
|
|
$autocompleter->setSearchList( |
70
|
|
|
BaseElement::get()->filter('ClassName', array_keys($list)) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$widgetArea = new GridField( |
75
|
|
|
'Elements', |
76
|
|
|
Config::inst()->get("ElementPageExtension", 'elements_title'), |
77
|
|
|
$elements, |
78
|
|
|
$config |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
$fields->addFieldToTab('Root.Main', $widgetArea); |
82
|
|
|
} else { |
83
|
|
|
$fields->addFieldToTab('Root.Main', LiteralField::create('warn', '<p class="message notice">Once you save this object you will be able to add items</p>')); |
84
|
|
|
} |
85
|
|
|
}); |
86
|
|
|
|
87
|
|
|
return parent::getCMSFields(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
public function getAvailableTypes() { |
94
|
|
|
if (is_array($this->config()->get('allowed_elements'))) { |
95
|
|
|
$list = $this->config()->get('allowed_elements'); |
96
|
|
|
|
97
|
|
View Code Duplication |
if($this->config()->get('sort_types_alphabetically') !== false) { |
|
|
|
|
98
|
|
|
$sorted = array(); |
99
|
|
|
|
100
|
|
|
foreach ($list as $class) { |
101
|
|
|
$inst = singleton($class); |
102
|
|
|
|
103
|
|
|
if ($inst->canCreate()) { |
104
|
|
|
$sorted[$class] = singleton($class)->i18n_singular_name(); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$list = $sorted; |
109
|
|
|
asort($list); |
110
|
|
|
} |
111
|
|
View Code Duplication |
} else { |
|
|
|
|
112
|
|
|
$classes = ClassInfo::subclassesFor('BaseElement'); |
113
|
|
|
$list = array(); |
114
|
|
|
unset($classes['BaseElement']); |
115
|
|
|
|
116
|
|
|
$disallowedElements = (array) $this->config()->get('disallowed_elements'); |
117
|
|
|
|
118
|
|
|
if (!in_array('ElementVirtualLinked', $disallowedElements)) { |
119
|
|
|
array_push($disallowedElements, 'ElementVirtualLinked'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
foreach ($classes as $class) { |
|
|
|
|
123
|
|
|
$inst = singleton($class); |
124
|
|
|
|
125
|
|
|
if (!in_array($class, $disallowedElements) && $inst->canCreate()) { |
126
|
|
|
$list[$class] = singleton($class)->i18n_singular_name(); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
asort($list); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if (method_exists($this, 'sortElementalOptions')) { |
134
|
|
|
$this->sortElementalOptions($list); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $list; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Used in template instead of {@link Widgets()} to wrap each widget in its |
142
|
|
|
* controller, making it easier to access and process form logic and |
143
|
|
|
* actions stored in {@link WidgetController}. |
144
|
|
|
* |
145
|
|
|
* @return SS_List - Collection of {@link WidgetController} instances. |
146
|
|
|
*/ |
147
|
|
|
public function WidgetControllers() { |
148
|
|
|
$controllers = new ArrayList(); |
149
|
|
|
|
150
|
|
|
foreach($this->Elements()->filter('Enabled', 1) as $widget) { |
|
|
|
|
151
|
|
|
$controller = $widget->getController(); |
152
|
|
|
|
153
|
|
|
$controller->init(); |
154
|
|
|
$controllers->push($controller); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $controllers; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.