1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package elemental |
5
|
|
|
*/ |
6
|
|
|
class BaseElement extends Widget |
|
|
|
|
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* @var array $db |
10
|
|
|
*/ |
11
|
|
|
private static $db = array( |
|
|
|
|
12
|
|
|
'ExtraClass' => 'Varchar(255)', |
13
|
|
|
'HideTitle' => 'Boolean' |
14
|
|
|
); |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array $has_one |
18
|
|
|
*/ |
19
|
|
|
private static $has_one = array( |
|
|
|
|
20
|
|
|
'List' => 'ElementList' // optional. |
21
|
|
|
); |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array $has_many |
25
|
|
|
*/ |
26
|
|
|
private static $has_many = array( |
|
|
|
|
27
|
|
|
'VirtualClones' => 'ElementVirtualLinked' |
28
|
|
|
); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private static $title = "Content Block"; |
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private static $singular_name = 'Content Block'; |
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
private static $summary_fields = array( |
|
|
|
|
44
|
|
|
'ID' => 'ID', |
45
|
|
|
'Title' => 'Title', |
46
|
|
|
'ElementType' => 'Type' |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
private static $searchable_fields = array( |
|
|
|
|
53
|
|
|
'ID' => array( |
54
|
|
|
'field' => 'NumericField' |
55
|
|
|
), |
56
|
|
|
'Title', |
57
|
|
|
'LastEdited', |
58
|
|
|
'ClassName' |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var boolean |
63
|
|
|
*/ |
64
|
|
|
private static $enable_title_in_template = false; |
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var Object |
68
|
|
|
* The virtual owner VirtualLinkedElement |
69
|
|
|
*/ |
70
|
|
|
public $virtualOwner; |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
public function getCMSFields() |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$fields = $this->scaffoldFormFields(array( |
76
|
|
|
'includeRelations' => ($this->ID > 0), |
77
|
|
|
'tabbed' => true, |
78
|
|
|
'ajaxSafe' => true |
79
|
|
|
)); |
80
|
|
|
|
81
|
|
|
$fields->insertAfter(new ReadonlyField('ClassNameTranslated', _t('BaseElement.TYPE', 'Type'), $this->i18n_singular_name()), 'Title'); |
|
|
|
|
82
|
|
|
$fields->removeByName('ListID'); |
83
|
|
|
$fields->removeByName('ParentID'); |
84
|
|
|
$fields->removeByName('Sort'); |
85
|
|
|
$fields->removeByName('ExtraClass'); |
86
|
|
|
|
87
|
|
|
if (!$this->config()->enable_title_in_template) { |
88
|
|
|
$fields->removeByName('HideTitle'); |
89
|
|
|
$title = $fields->fieldByName('Root.Main.Title'); |
90
|
|
|
|
91
|
|
|
if ($title) { |
92
|
|
|
$title->setRightTitle('For reference only. Does not appear in the template.'); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$fields->addFieldToTab('Root.Settings', new TextField('ExtraClass', 'Extra CSS Classes to add')); |
97
|
|
|
|
98
|
|
|
if (!is_a($this, 'ElementList')) { |
99
|
|
|
$lists = ElementList::get()->filter('ParentID', $this->ParentID); |
|
|
|
|
100
|
|
|
|
101
|
|
|
if ($lists->exists()) { |
102
|
|
|
$fields->addFieldToTab('Root.Settings', |
103
|
|
|
$move = new DropdownField('MoveToListID', 'Move this to another list', $lists->map('ID', 'CMSTitle'), '') |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
$move->setEmptyString('Select a list..'); |
107
|
|
|
$move->setHasEmptyDefault(true); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
if($virtual = $fields->dataFieldByName('VirtualClones')) { |
113
|
|
|
if($this->Parent() && $this->Parent()->exists() && $this->Parent()->getOwnerPage() && $this->Parent()->getOwnerPage()->exists()) { |
|
|
|
|
114
|
|
|
$tab = $fields->findOrMakeTab('Root.VirtualClones'); |
115
|
|
|
$tab->setTitle(_t('BaseElement.VIRTUALTABTITLE', 'Linked To')); |
116
|
|
|
|
117
|
|
|
$tab->push(new LiteralField('DisplaysOnPage', sprintf( |
118
|
|
|
"<p>The original content block appears on <a href='%s'>%s</a></p>", |
119
|
|
|
$this->Parent()->getOwnerPage()->Link(), |
|
|
|
|
120
|
|
|
$this->Parent()->getOwnerPage()->MenuTitle |
|
|
|
|
121
|
|
|
))); |
122
|
|
|
|
123
|
|
|
$virtual->setConfig(new GridFieldConfig_Base()); |
|
|
|
|
124
|
|
|
$virtual |
|
|
|
|
125
|
|
|
->setTitle(_t('BaseElement.OTHERPAGES', 'Other pages')) |
126
|
|
|
->getConfig() |
127
|
|
|
->removeComponentsByType('GridFieldAddExistingAutocompleter') |
128
|
|
|
->removeComponentsByType('GridFieldAddNewButton') |
129
|
|
|
->removeComponentsByType('GridFieldDeleteAction') |
130
|
|
|
->removeComponentsByType('GridFieldDetailForm') |
131
|
|
|
->addComponent(new ElementalGridFieldDeleteAction()); |
132
|
|
|
|
133
|
|
|
$virtual->getConfig() |
|
|
|
|
134
|
|
|
->getComponentByType('GridFieldDataColumns') |
135
|
|
|
->setDisplayFields(array( |
136
|
|
|
'getPage.Title' => 'Title', |
137
|
|
|
'getPage.Link' => 'Link' |
138
|
|
|
)); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$this->extend('updateCMSFields', $fields); |
143
|
|
|
|
144
|
|
|
if ($this->IsInDB()) { |
145
|
|
|
if ($this->isEndofLine('BaseElement') && $this->hasExtension('VersionViewerDataObject')) { |
146
|
|
|
$fields = $this->addVersionViewer($fields, $this); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $fields; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Version viewer must only be added at if this is the final getCMSFields for a class. |
155
|
|
|
* in order to avoid having to rename all fields from eg Root.Main to Root.Current.Main |
156
|
|
|
* To do this we test if getCMSFields is from the current class |
157
|
|
|
*/ |
158
|
|
|
public function isEndofLine($className) |
159
|
|
|
{ |
160
|
|
|
$methodFromClass = ClassInfo::has_method_from( |
161
|
|
|
$this->ClassName, 'getCMSFields', $className |
162
|
|
|
); |
163
|
|
|
|
164
|
|
|
if($methodFromClass) { |
165
|
|
|
return true; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
|
170
|
|
|
public function onBeforeWrite() |
171
|
|
|
{ |
172
|
|
|
parent::onBeforeWrite(); |
173
|
|
|
|
174
|
|
|
if (!$this->Sort) { |
|
|
|
|
175
|
|
|
$parentID = ($this->ParentID) ? $this->ParentID : 0; |
|
|
|
|
176
|
|
|
|
177
|
|
|
$this->Sort = DB::query("SELECT MAX(\"Sort\") + 1 FROM \"Widget\" WHERE \"ParentID\" = $parentID")->value(); |
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
if ($this->MoveToListID) { |
|
|
|
|
181
|
|
|
$this->ListID = $this->MoveToListID; |
|
|
|
|
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Ensure that if there are elements that are virtualised from this element |
187
|
|
|
* that we move the original element to replace one of the virtual elements |
188
|
|
|
* But only if it's a delete not an unpublish |
189
|
|
|
*/ |
190
|
|
|
public function onBeforeDelete() { |
191
|
|
|
parent::onBeforeDelete(); |
192
|
|
|
|
193
|
|
|
if(Versioned::get_reading_mode() == 'Stage.Stage') { |
194
|
|
|
$firstVirtual = false; |
195
|
|
|
$allVirtual = $this->getVirtualLinkedElements(); |
196
|
|
|
if ($this->getPublishedVirtualLinkedElements()->Count() > 0) { |
197
|
|
|
// choose the first one |
198
|
|
|
$firstVirtual = $this->getPublishedVirtualLinkedElements()->First(); |
199
|
|
|
$wasPublished = true; |
200
|
|
|
} else if ($allVirtual->Count() > 0) { |
201
|
|
|
// choose the first one |
202
|
|
|
$firstVirtual = $this->getVirtualLinkedElements()->First(); |
203
|
|
|
$wasPublished = false; |
204
|
|
|
} |
205
|
|
|
if ($firstVirtual) { |
206
|
|
|
$origParentID = $this->ParentID; |
|
|
|
|
207
|
|
|
$origSort = $this->Sort; |
|
|
|
|
208
|
|
|
|
209
|
|
|
$clone = $this->duplicate(false); |
210
|
|
|
|
211
|
|
|
// set clones values to first virtual's values |
212
|
|
|
$clone->ParentID = $firstVirtual->ParentID; |
213
|
|
|
$clone->Sort = $firstVirtual->Sort; |
214
|
|
|
|
215
|
|
|
$clone->write(); |
216
|
|
|
if ($wasPublished) { |
|
|
|
|
217
|
|
|
$clone->doPublish(); |
218
|
|
|
$firstVirtual->doUnpublish(); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
// clone has a new ID, so need to repoint |
222
|
|
|
// all the other virtual elements |
223
|
|
|
foreach($allVirtual as $virtual) { |
224
|
|
|
if ($virtual->ID == $firstVirtual->ID) { |
225
|
|
|
continue; |
226
|
|
|
} |
227
|
|
|
$pub = false; |
228
|
|
|
if ($virtual->isPublished()) { |
229
|
|
|
$pub = true; |
230
|
|
|
} |
231
|
|
|
$virtual->LinkedElementID = $clone->ID; |
232
|
|
|
$virtual->write(); |
233
|
|
|
if ($pub) { |
234
|
|
|
$virtual->doPublish(); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
$firstVirtual->delete(); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @return string |
245
|
|
|
*/ |
246
|
|
|
public function i18n_singular_name() |
247
|
|
|
{ |
248
|
|
|
return _t(__CLASS__, $this->config()->title); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @return string |
253
|
|
|
*/ |
254
|
|
|
public function getElementType() |
255
|
|
|
{ |
256
|
|
|
return $this->i18n_singular_name(); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @return string |
261
|
|
|
*/ |
262
|
|
View Code Duplication |
public function getTitle() |
|
|
|
|
263
|
|
|
{ |
264
|
|
|
if ($title = $this->getField('Title')) { |
265
|
|
|
return $title; |
266
|
|
|
} else { |
267
|
|
|
if (!$this->isInDb()) { |
268
|
|
|
return; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
return $this->config()->title; |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @return string |
277
|
|
|
*/ |
278
|
|
View Code Duplication |
public function getCMSTitle() |
|
|
|
|
279
|
|
|
{ |
280
|
|
|
if ($title = $this->getField('Title')) { |
281
|
|
|
return $this->config()->title . ': ' . $title; |
282
|
|
|
} else { |
283
|
|
|
if (!$this->isInDb()) { |
284
|
|
|
return; |
285
|
|
|
} |
286
|
|
|
return $this->config()->title; |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
public function ControllerTop() |
291
|
|
|
{ |
292
|
|
|
return Controller::curr(); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
public function getPage() |
|
|
|
|
296
|
|
|
{ |
297
|
|
|
if ($this->virtualOwner) { |
298
|
|
|
return $this->virtualOwner->getPage(); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
$area = $this->Parent(); |
|
|
|
|
302
|
|
|
|
303
|
|
|
if ($area instanceof ElementalArea) { |
304
|
|
|
return $area->getOwnerPage(); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
return null; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Override the {@link Widget::forTemplate()} method so that holders are not rendered twice. The controller should |
312
|
|
|
* render with widget inside the |
313
|
|
|
* |
314
|
|
|
* @return HTML |
|
|
|
|
315
|
|
|
*/ |
316
|
|
|
public function forTemplate($holder = true) |
317
|
|
|
{ |
318
|
|
|
return $this->renderWith($this->class); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* @return string |
323
|
|
|
*/ |
324
|
|
|
public function getEditLink() { |
325
|
|
|
return Controller::join_links( |
326
|
|
|
Director::absoluteBaseURL(), |
327
|
|
|
'admin/elemental/BaseElement/EditForm/field/BaseElement/item', |
328
|
|
|
$this->ID, |
329
|
|
|
'edit' |
330
|
|
|
); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
public function onBeforeVersionedPublish() |
334
|
|
|
{ |
335
|
|
|
|
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
public static function all_allowed_elements() { |
339
|
|
|
$classes = array(); |
340
|
|
|
|
341
|
|
|
// get all dataobject with the elemental extension |
342
|
|
|
foreach(ClassInfo::subclassesFor('DataObject') as $className) { |
|
|
|
|
343
|
|
|
if(Object::has_extension($className, 'ElementPageExtension')) { |
344
|
|
|
$classes[] = $className; |
345
|
|
|
} |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
// get all allowd_elements for these classes |
349
|
|
|
$allowed = array(); |
350
|
|
|
foreach($classes as $className) { |
351
|
|
|
$allowed = array_merge($allowed, Config::inst()->get($className, 'allowed_elements')); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
// $allowed[] = 'ElementVirtualLinked'; |
|
|
|
|
355
|
|
|
$allowed = array_unique($allowed); |
356
|
|
|
|
357
|
|
|
$elements = array(); |
358
|
|
|
foreach($allowed as $className) { |
359
|
|
|
$elements[$className] = _t($className, Config::inst()->get($className, 'title')); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
asort($elements); |
363
|
|
|
|
364
|
|
|
return $elements; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
public function getDefaultSearchContext() |
368
|
|
|
{ |
369
|
|
|
$fields = $this->scaffoldSearchFields(); |
370
|
|
|
|
371
|
|
|
$elements = BaseElement::all_allowed_elements(); |
372
|
|
|
|
373
|
|
|
$fields->push(DropdownField::create('ClassName', 'Element Type', $elements) |
374
|
|
|
->setEmptyString('All types')); |
375
|
|
|
$filters = $this->owner->defaultSearchFilters(); |
|
|
|
|
376
|
|
|
|
377
|
|
|
return new SearchContext( |
378
|
|
|
$this->owner->class, |
|
|
|
|
379
|
|
|
$fields, |
380
|
|
|
$filters |
381
|
|
|
); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
public function setVirtualOwner(ElementVirtualLinked $virtualOwner) { |
385
|
|
|
$this->virtualOwner = $virtualOwner; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* Finds and returns elements |
390
|
|
|
* that are virtual elements which link to this element |
391
|
|
|
*/ |
392
|
|
|
public function getVirtualLinkedElements() { |
393
|
|
|
return ElementVirtualLinked::get()->filter('LinkedElementID', $this->ID); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Finds and returns published elements |
398
|
|
|
* that are virtual elements which link to this element |
399
|
|
|
*/ |
400
|
|
|
public function getPublishedVirtualLinkedElements() { |
401
|
|
|
$current = Versioned::get_reading_mode(); |
402
|
|
|
Versioned::set_reading_mode('Stage.Live'); |
403
|
|
|
$v = $this->getVirtualLinkedElements(); |
404
|
|
|
Versioned::set_reading_mode($current); |
405
|
|
|
return $v; |
406
|
|
|
} |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
|
410
|
|
|
|
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.