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
|
|
|
$ownerPage = $this->Parent()->getOwnerPage(); |
|
|
|
|
118
|
|
|
$tab->push(new LiteralField('DisplaysOnPage', sprintf( |
119
|
|
|
"<p>The original content block appears on <a href='%s'>%s</a></p>", |
120
|
|
|
($ownerPage->hasMethod('CMSEditLink') && $ownerPage->canEdit()) ? $ownerPage->CMSEditLink() : $ownerPage->Link(), |
121
|
|
|
$ownerPage->MenuTitle |
122
|
|
|
))); |
123
|
|
|
|
124
|
|
|
$virtual->setConfig(new GridFieldConfig_Base()); |
|
|
|
|
125
|
|
|
$virtual |
|
|
|
|
126
|
|
|
->setTitle(_t('BaseElement.OTHERPAGES', 'Other pages')) |
127
|
|
|
->getConfig() |
128
|
|
|
->removeComponentsByType('GridFieldAddExistingAutocompleter') |
129
|
|
|
->removeComponentsByType('GridFieldAddNewButton') |
130
|
|
|
->removeComponentsByType('GridFieldDeleteAction') |
131
|
|
|
->removeComponentsByType('GridFieldDetailForm') |
132
|
|
|
->addComponent(new ElementalGridFieldDeleteAction()); |
133
|
|
|
|
134
|
|
|
$virtual->getConfig() |
|
|
|
|
135
|
|
|
->getComponentByType('GridFieldDataColumns') |
136
|
|
|
->setDisplayFields(array( |
137
|
|
|
'getPage.Title' => 'Title', |
138
|
|
|
'getPage.Link' => 'Link' |
139
|
|
|
)); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$this->extend('updateCMSFields', $fields); |
144
|
|
|
|
145
|
|
|
if ($this->IsInDB()) { |
146
|
|
|
if ($this->isEndofLine('BaseElement') && $this->hasExtension('VersionViewerDataObject')) { |
147
|
|
|
$fields = $this->addVersionViewer($fields, $this); |
|
|
|
|
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $fields; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Version viewer must only be added at if this is the final getCMSFields for a class. |
156
|
|
|
* in order to avoid having to rename all fields from eg Root.Main to Root.Current.Main |
157
|
|
|
* To do this we test if getCMSFields is from the current class |
158
|
|
|
*/ |
159
|
|
|
public function isEndofLine($className) |
160
|
|
|
{ |
161
|
|
|
$methodFromClass = ClassInfo::has_method_from( |
162
|
|
|
$this->ClassName, 'getCMSFields', $className |
163
|
|
|
); |
164
|
|
|
|
165
|
|
|
if($methodFromClass) { |
166
|
|
|
return true; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
|
171
|
|
|
public function onBeforeWrite() |
172
|
|
|
{ |
173
|
|
|
parent::onBeforeWrite(); |
174
|
|
|
|
175
|
|
|
if (!$this->Sort) { |
|
|
|
|
176
|
|
|
$parentID = ($this->ParentID) ? $this->ParentID : 0; |
|
|
|
|
177
|
|
|
|
178
|
|
|
$this->Sort = DB::query("SELECT MAX(\"Sort\") + 1 FROM \"Widget\" WHERE \"ParentID\" = $parentID")->value(); |
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if ($this->MoveToListID) { |
|
|
|
|
182
|
|
|
$this->ListID = $this->MoveToListID; |
|
|
|
|
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Ensure that if there are elements that are virtualised from this element |
188
|
|
|
* that we move the original element to replace one of the virtual elements |
189
|
|
|
* But only if it's a delete not an unpublish |
190
|
|
|
*/ |
191
|
|
|
public function onBeforeDelete() { |
192
|
|
|
parent::onBeforeDelete(); |
193
|
|
|
|
194
|
|
|
if(Versioned::get_reading_mode() == 'Stage.Stage') { |
195
|
|
|
$firstVirtual = false; |
196
|
|
|
$allVirtual = $this->getVirtualLinkedElements(); |
197
|
|
|
if ($this->getPublishedVirtualLinkedElements()->Count() > 0) { |
198
|
|
|
// choose the first one |
199
|
|
|
$firstVirtual = $this->getPublishedVirtualLinkedElements()->First(); |
200
|
|
|
$wasPublished = true; |
201
|
|
|
} else if ($allVirtual->Count() > 0) { |
202
|
|
|
// choose the first one |
203
|
|
|
$firstVirtual = $this->getVirtualLinkedElements()->First(); |
204
|
|
|
$wasPublished = false; |
205
|
|
|
} |
206
|
|
|
if ($firstVirtual) { |
207
|
|
|
$origParentID = $this->ParentID; |
|
|
|
|
208
|
|
|
$origSort = $this->Sort; |
|
|
|
|
209
|
|
|
|
210
|
|
|
$clone = $this->duplicate(false); |
211
|
|
|
|
212
|
|
|
// set clones values to first virtual's values |
213
|
|
|
$clone->ParentID = $firstVirtual->ParentID; |
214
|
|
|
$clone->Sort = $firstVirtual->Sort; |
215
|
|
|
|
216
|
|
|
$clone->write(); |
217
|
|
|
if ($wasPublished) { |
|
|
|
|
218
|
|
|
$clone->doPublish(); |
219
|
|
|
$firstVirtual->doUnpublish(); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
// clone has a new ID, so need to repoint |
223
|
|
|
// all the other virtual elements |
224
|
|
|
foreach($allVirtual as $virtual) { |
225
|
|
|
if ($virtual->ID == $firstVirtual->ID) { |
226
|
|
|
continue; |
227
|
|
|
} |
228
|
|
|
$pub = false; |
229
|
|
|
if ($virtual->isPublished()) { |
230
|
|
|
$pub = true; |
231
|
|
|
} |
232
|
|
|
$virtual->LinkedElementID = $clone->ID; |
233
|
|
|
$virtual->write(); |
234
|
|
|
if ($pub) { |
235
|
|
|
$virtual->doPublish(); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
$firstVirtual->delete(); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @return string |
246
|
|
|
*/ |
247
|
|
|
public function i18n_singular_name() |
248
|
|
|
{ |
249
|
|
|
return _t(__CLASS__, $this->config()->title); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @return string |
254
|
|
|
*/ |
255
|
|
|
public function getElementType() |
256
|
|
|
{ |
257
|
|
|
return $this->i18n_singular_name(); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @return string |
262
|
|
|
*/ |
263
|
|
View Code Duplication |
public function getTitle() |
|
|
|
|
264
|
|
|
{ |
265
|
|
|
if ($title = $this->getField('Title')) { |
266
|
|
|
return $title; |
267
|
|
|
} else { |
268
|
|
|
if (!$this->isInDb()) { |
269
|
|
|
return; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
return $this->config()->title; |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @return string |
278
|
|
|
*/ |
279
|
|
View Code Duplication |
public function getCMSTitle() |
|
|
|
|
280
|
|
|
{ |
281
|
|
|
if ($title = $this->getField('Title')) { |
282
|
|
|
return $this->config()->title . ': ' . $title; |
283
|
|
|
} else { |
284
|
|
|
if (!$this->isInDb()) { |
285
|
|
|
return; |
286
|
|
|
} |
287
|
|
|
return $this->config()->title; |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
public function ControllerTop() |
292
|
|
|
{ |
293
|
|
|
return Controller::curr(); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
public function getPage() |
|
|
|
|
297
|
|
|
{ |
298
|
|
|
if ($this->virtualOwner) { |
299
|
|
|
return $this->virtualOwner->getPage(); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
$area = $this->Parent(); |
|
|
|
|
303
|
|
|
|
304
|
|
|
if ($area instanceof ElementalArea) { |
305
|
|
|
return $area->getOwnerPage(); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
return null; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Override the {@link Widget::forTemplate()} method so that holders are not rendered twice. The controller should |
313
|
|
|
* render with widget inside the |
314
|
|
|
* |
315
|
|
|
* @return HTML |
|
|
|
|
316
|
|
|
*/ |
317
|
|
|
public function forTemplate($holder = true) |
318
|
|
|
{ |
319
|
|
|
return $this->renderWith($this->class); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @return string |
324
|
|
|
*/ |
325
|
|
|
public function getEditLink() { |
326
|
|
|
return Controller::join_links( |
327
|
|
|
Director::absoluteBaseURL(), |
328
|
|
|
'admin/elemental/BaseElement/EditForm/field/BaseElement/item', |
329
|
|
|
$this->ID, |
330
|
|
|
'edit' |
331
|
|
|
); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
public function onBeforeVersionedPublish() |
335
|
|
|
{ |
336
|
|
|
|
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
public static function all_allowed_elements() { |
340
|
|
|
$classes = array(); |
341
|
|
|
|
342
|
|
|
// get all dataobject with the elemental extension |
343
|
|
|
foreach(ClassInfo::subclassesFor('DataObject') as $className) { |
|
|
|
|
344
|
|
|
if(Object::has_extension($className, 'ElementPageExtension')) { |
345
|
|
|
$classes[] = $className; |
346
|
|
|
} |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
// get all allowd_elements for these classes |
350
|
|
|
$allowed = array(); |
351
|
|
|
foreach($classes as $className) { |
352
|
|
|
$allowed_elements = Config::inst()->get($className, 'allowed_elements'); |
353
|
|
|
if ($allowed_elements) { |
354
|
|
|
$allowed = array_merge($allowed, $allowed_elements); |
355
|
|
|
} |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
// $allowed[] = 'ElementVirtualLinked'; |
|
|
|
|
359
|
|
|
$allowed = array_unique($allowed); |
360
|
|
|
|
361
|
|
|
$elements = array(); |
362
|
|
|
foreach($allowed as $className) { |
363
|
|
|
$elements[$className] = _t($className, Config::inst()->get($className, 'title')); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
asort($elements); |
367
|
|
|
|
368
|
|
|
return $elements; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
public function getDefaultSearchContext() |
372
|
|
|
{ |
373
|
|
|
$fields = $this->scaffoldSearchFields(); |
374
|
|
|
|
375
|
|
|
$elements = BaseElement::all_allowed_elements(); |
376
|
|
|
|
377
|
|
|
$fields->push(DropdownField::create('ClassName', 'Element Type', $elements) |
378
|
|
|
->setEmptyString('All types')); |
379
|
|
|
$filters = $this->owner->defaultSearchFilters(); |
|
|
|
|
380
|
|
|
|
381
|
|
|
return new SearchContext( |
382
|
|
|
$this->owner->class, |
|
|
|
|
383
|
|
|
$fields, |
384
|
|
|
$filters |
385
|
|
|
); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
public function setVirtualOwner(ElementVirtualLinked $virtualOwner) { |
389
|
|
|
$this->virtualOwner = $virtualOwner; |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* Finds and returns elements |
394
|
|
|
* that are virtual elements which link to this element |
395
|
|
|
*/ |
396
|
|
|
public function getVirtualLinkedElements() { |
397
|
|
|
return ElementVirtualLinked::get()->filter('LinkedElementID', $this->ID); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Finds and returns published elements |
402
|
|
|
* that are virtual elements which link to this element |
403
|
|
|
*/ |
404
|
|
|
public function getPublishedVirtualLinkedElements() { |
405
|
|
|
$current = Versioned::get_reading_mode(); |
406
|
|
|
Versioned::set_reading_mode('Stage.Live'); |
407
|
|
|
$v = $this->getVirtualLinkedElements(); |
408
|
|
|
Versioned::set_reading_mode($current); |
409
|
|
|
return $v; |
410
|
|
|
} |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
|
414
|
|
|
|
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.