1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package elemental |
5
|
|
|
*/ |
6
|
|
|
class ElementPageExtension extends DataExtension |
|
|
|
|
7
|
|
|
{ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @config |
11
|
|
|
* |
12
|
|
|
* @var string $elements_title Title of the element in the CMS. |
13
|
|
|
*/ |
14
|
|
|
private static $elements_title = 'Content Blocks'; |
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @config |
18
|
|
|
* |
19
|
|
|
* @var array $ignored_classes Classes to ignore adding elements too. |
20
|
|
|
*/ |
21
|
|
|
private static $ignored_classes = array(); |
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array $db |
25
|
|
|
*/ |
26
|
|
|
private static $db = array(); |
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array $has_one |
30
|
|
|
*/ |
31
|
|
|
private static $has_one = array( |
|
|
|
|
32
|
|
|
'ElementArea' => 'ElementalArea' |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Setup the CMS Fields |
37
|
|
|
* |
38
|
|
|
* @param FieldList |
39
|
|
|
*/ |
40
|
|
|
public function updateCMSFields(FieldList $fields) |
41
|
|
|
{ |
42
|
|
|
if(!$this->supportsElemental()) { |
43
|
|
|
return false; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// add an empty holder for content as some module explicitly use insert |
47
|
|
|
// after content. |
48
|
|
|
$fields->replaceField('Content', new LiteralField('Content', '')); |
49
|
|
|
|
50
|
|
|
$adder = new ElementalGridFieldAddNewMultiClass(); |
51
|
|
|
|
52
|
|
|
$list = $this->getAvailableTypes(); |
53
|
|
|
if($list) { |
|
|
|
|
54
|
|
|
$adder->setClasses($list); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$area = $this->owner->ElementArea(); |
58
|
|
|
|
59
|
|
|
if ($this->owner->exists() && (!$area->exists() || !$area->isInDB())) { |
60
|
|
|
$area->write(); |
61
|
|
|
|
62
|
|
|
$this->owner->ElementAreaID = $area->ID; |
63
|
|
|
$this->owner->write(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$elements = $this->owner->ElementArea()->Elements(); |
67
|
|
|
if (!$elements || $elements instanceof ArrayList || $elements instanceof UnsavedRelationList) { |
68
|
|
|
// Allow gridfield to render on an unsaved DataObject |
69
|
|
|
$elements = new UnsavedRelationList('ElementalArea', 'Widgets', 'BaseElement'); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$gridField = GridField::create('ElementArea', |
73
|
|
|
Config::inst()->get("ElementPageExtension", 'elements_title'), |
74
|
|
|
$elements, |
75
|
|
|
GridFieldConfig_RelationEditor::create() |
76
|
|
|
->removeComponentsByType('GridFieldAddNewButton') |
77
|
|
|
->removeComponentsByType('GridFieldDeleteAction') |
78
|
|
|
->removeComponentsByType('GridFieldAddExistingAutocompleter') |
79
|
|
|
->addComponent(new ElementalGridFieldAddExistingAutocompleter()) |
80
|
|
|
->addComponent(new ElementalGridFieldDeleteAction()) |
81
|
|
|
->addComponent($adder) |
82
|
|
|
->addComponent(new GridFieldSortableRows('Sort')) |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
$config = $gridField->getConfig(); |
86
|
|
|
$paginator = $config->getComponentByType('GridFieldPaginator'); |
87
|
|
|
$paginator->setItemsPerPage(100); |
88
|
|
|
|
89
|
|
|
$config->removeComponentsByType('GridFieldDetailForm'); |
90
|
|
|
$config->addComponent(new VersionedDataObjectDetailsForm()); |
91
|
|
|
|
92
|
|
|
$fields->addFieldToTab('Root.Main', $gridField); |
93
|
|
|
|
94
|
|
|
return $fields; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
public function getAvailableTypes() { |
101
|
|
|
if (is_array($this->owner->config()->get('allowed_elements'))) { |
102
|
|
|
$list = $this->owner->config()->get('allowed_elements'); |
103
|
|
|
|
104
|
|
|
if($this->owner->config()->get('sort_types_alphabetically') !== false) { |
105
|
|
|
$sorted = array(); |
106
|
|
|
|
107
|
|
|
foreach ($list as $class) { |
108
|
|
|
$inst = singleton($class); |
109
|
|
|
|
110
|
|
|
if ($inst->canCreate()) { |
111
|
|
|
$sorted[$class] = singleton($class)->i18n_singular_name(); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$list = $sorted; |
116
|
|
|
asort($list); |
117
|
|
|
} |
118
|
|
|
} else { |
119
|
|
|
$classes = ClassInfo::subclassesFor('BaseElement'); |
120
|
|
|
$list = array(); |
121
|
|
|
unset($classes['BaseElement']); |
122
|
|
|
|
123
|
|
|
foreach ($classes as $class) { |
|
|
|
|
124
|
|
|
$inst = singleton($class); |
125
|
|
|
|
126
|
|
|
if ($inst->canCreate()) { |
127
|
|
|
$list[$class] = singleton($class)->i18n_singular_name(); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
asort($list); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if (method_exists($this->owner, 'sortElementalOptions')) { |
135
|
|
|
$this->owner->sortElementalOptions($list); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $list; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Make sure there is always a WidgetArea sidebar for adding widgets |
143
|
|
|
* |
144
|
|
|
*/ |
145
|
|
|
public function onBeforeWrite() |
146
|
|
|
{ |
147
|
|
|
// enable theme in case elements are being rendered with templates stored in theme folder |
148
|
|
|
$originalThemeEnabled = Config::inst()->get('SSViewer', 'theme_enabled'); |
149
|
|
|
Config::inst()->update('SSViewer', 'theme_enabled', true); |
150
|
|
|
|
151
|
|
|
if(!$this->supportsElemental()) { |
152
|
|
|
return; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if ($this->owner->hasMethod('ElementArea')) { |
156
|
|
|
$elements = $this->owner->ElementArea(); |
157
|
|
|
|
158
|
|
|
if (!$elements->isInDB()) { |
159
|
|
|
$elements->write(); |
160
|
|
|
$this->owner->ElementAreaID = $elements->ID; |
161
|
|
|
} else { |
162
|
|
|
// Copy widgets content to Content to enable search |
163
|
|
|
$searchableContent = array(); |
164
|
|
|
|
165
|
|
|
Requirements::clear(); |
166
|
|
|
foreach ($elements->Elements() as $element) { |
167
|
|
|
if ($element->config()->exclude_from_content) { |
168
|
|
|
continue; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$controller = $element->getController(); |
172
|
|
|
|
173
|
|
|
foreach ($elements->Items() as $element) { |
174
|
|
|
$controller->init(); |
175
|
|
|
|
176
|
|
|
array_push($searchableContent, $controller->WidgetHolder()); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
Requirements::restore(); |
180
|
|
|
|
181
|
|
|
$this->owner->Content = trim(implode(' ', $searchableContent)); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
|
186
|
|
|
// set theme_enabled back to what it was |
187
|
|
|
Config::inst()->update('SSViewer', 'theme_enabled', $originalThemeEnabled); |
188
|
|
|
|
189
|
|
|
parent::onBeforeWrite(); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @return boolean |
194
|
|
|
*/ |
195
|
|
|
public function supportsElemental() { |
196
|
|
|
if (method_exists($this->owner, 'includeElemental')) { |
197
|
|
|
return $this->owner->includeElemental(); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
if (is_a($this->owner, 'RedirectorPage')) { |
201
|
|
|
return false; |
202
|
|
|
} else if ($ignored = Config::inst()->get('ElementPageExtension', 'ignored_classes')) { |
203
|
|
|
foreach ($ignored as $check) { |
|
|
|
|
204
|
|
|
if (is_a($this->owner, $check)) { |
205
|
|
|
return false; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return true; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* If the page is duplicated, copy the widgets across too. |
215
|
|
|
* |
216
|
|
|
* Gets called twice from either direction, due to bad DataObject and SiteTree code, hence the weird if statement |
217
|
|
|
* |
218
|
|
|
* @return Page The duplicated page |
|
|
|
|
219
|
|
|
*/ |
220
|
|
|
public function onAfterDuplicate($duplicatePage) |
221
|
|
|
{ |
222
|
|
|
if ($this->owner->ID != 0 && $this->owner->ID < $duplicatePage->ID) { |
223
|
|
|
$originalWidgetArea = $this->owner->getComponent('ElementArea'); |
224
|
|
|
$duplicateWidgetArea = $originalWidgetArea->duplicate(false); |
225
|
|
|
$duplicateWidgetArea->write(); |
226
|
|
|
$duplicatePage->ElementAreaID = $duplicateWidgetArea->ID; |
227
|
|
|
$duplicatePage->write(); |
228
|
|
|
|
229
|
|
View Code Duplication |
foreach ($originalWidgetArea->Items() as $originalWidget) { |
|
|
|
|
230
|
|
|
$duplicateWidget = $originalWidget->duplicate(true); |
231
|
|
|
|
232
|
|
|
// manually set the ParentID of each widget, so we don't get versioning issues |
233
|
|
|
DB::query(sprintf("UPDATE Widget SET ParentID = %d WHERE ID = %d", $duplicateWidgetArea->ID, $duplicateWidget->ID)); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* If the page is duplicated across subsites, copy the widgets across too. |
240
|
|
|
* |
241
|
|
|
* @return Page The duplicated page |
|
|
|
|
242
|
|
|
*/ |
243
|
|
|
public function onAfterDuplicateToSubsite($originalPage) |
244
|
|
|
{ |
245
|
|
|
$originalWidgetArea = $originalPage->getComponent('ElementArea'); |
246
|
|
|
$duplicateWidgetArea = $originalWidgetArea->duplicate(false); |
247
|
|
|
$duplicateWidgetArea->write(); |
248
|
|
|
$this->owner->ElementAreaID = $duplicateWidgetArea->ID; |
249
|
|
|
$this->owner->write(); |
250
|
|
|
|
251
|
|
View Code Duplication |
foreach ($originalWidgetArea->Items() as $originalWidget) { |
|
|
|
|
252
|
|
|
$duplicateWidget = $originalWidget->duplicate(true); |
253
|
|
|
|
254
|
|
|
// manually set the ParentID of each widget, so we don't get versioning issues |
255
|
|
|
DB::query(sprintf("UPDATE Widget SET ParentID = %d WHERE ID = %d", $duplicateWidgetArea->ID, $duplicateWidget->ID)); |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Publish |
261
|
|
|
*/ |
262
|
|
|
public function onAfterPublish() |
263
|
|
|
{ |
264
|
|
|
if ($id = $this->owner->ElementAreaID) { |
265
|
|
|
$widgets = Versioned::get_by_stage('BaseElement', 'Stage', "ParentID = '$id'"); |
266
|
|
|
$staged = array(); |
267
|
|
|
|
268
|
|
|
foreach ($widgets as $widget) { |
269
|
|
|
$staged[] = $widget->ID; |
270
|
|
|
|
271
|
|
|
$widget->publish('Stage', 'Live'); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
// remove any elements that are on live but not in draft. |
275
|
|
|
$widgets = Versioned::get_by_stage('BaseElement', 'Live', "ParentID = '$id'"); |
276
|
|
|
|
277
|
|
|
foreach ($widgets as $widget) { |
278
|
|
|
if (!in_array($widget->ID, $staged)) { |
279
|
|
|
$widget->deleteFromStage('Live'); |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Roll back all changes if the parent page has a rollback event |
287
|
|
|
* |
288
|
|
|
* Only do rollback if it's the 'cancel draft changes' rollback, not a specific version |
289
|
|
|
* rollback. |
290
|
|
|
* |
291
|
|
|
* @param string $version |
292
|
|
|
* @return null |
293
|
|
|
*/ |
294
|
|
|
public function onBeforeRollback($version) |
295
|
|
|
{ |
296
|
|
|
if ($version !== 'Live') { |
297
|
|
|
// we don't yet have a smart way of rolling back to a specific version |
298
|
|
|
return; |
299
|
|
|
} |
300
|
|
|
if ($id = $this->owner->ElementAreaID) { |
301
|
|
|
$widgets = Versioned::get_by_stage('BaseElement', 'Live', "ParentID = '$id'"); |
302
|
|
|
$staged = array(); |
303
|
|
|
|
304
|
|
|
foreach ($widgets as $widget) { |
305
|
|
|
$staged[] = $widget->ID; |
306
|
|
|
|
307
|
|
|
$widget->invokeWithExtensions('onBeforeRollback', $widget); |
308
|
|
|
|
309
|
|
|
$widget->publish("Live", "Stage", false); |
310
|
|
|
|
311
|
|
|
$widget->invokeWithExtensions('onAfterRollback', $widget); |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|
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.