2013-04-16 16:07:41 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Adds a single {@link WidgetArea} called "SideBar" to {@link Page} classes.
|
|
|
|
* Adjust your templates to render the resulting
|
|
|
|
* {@link WidgetArea} as required, through the $SideBarView placeholder.
|
|
|
|
*
|
|
|
|
* This extension is just an example on how to use the widgets functionality,
|
|
|
|
* feel free to create your own relationships, naming conventions, etc.
|
|
|
|
* without using this class.
|
|
|
|
*/
|
|
|
|
class WidgetPageExtension extends DataExtension {
|
|
|
|
|
2013-05-21 14:30:51 +02:00
|
|
|
private static $db = array(
|
2013-04-16 16:07:41 +02:00
|
|
|
'InheritSideBar' => 'Boolean',
|
|
|
|
);
|
|
|
|
|
2013-05-21 14:30:51 +02:00
|
|
|
private static $defaults = array(
|
2013-04-16 16:07:41 +02:00
|
|
|
'InheritSideBar' => true
|
|
|
|
);
|
|
|
|
|
2013-05-21 14:30:51 +02:00
|
|
|
private static $has_one = array(
|
2013-04-16 16:07:41 +02:00
|
|
|
'SideBar' => 'WidgetArea'
|
|
|
|
);
|
|
|
|
|
2013-05-21 14:30:51 +02:00
|
|
|
public function updateCMSFields(FieldList $fields) {
|
2013-04-16 16:07:41 +02:00
|
|
|
$fields->addFieldToTab(
|
|
|
|
"Root.Widgets",
|
|
|
|
new CheckboxField("InheritSideBar", 'Inherit Sidebar From Parent')
|
|
|
|
);
|
|
|
|
$fields->addFieldToTab(
|
|
|
|
"Root.Widgets",
|
|
|
|
new WidgetAreaEditor("SideBar")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return WidgetArea
|
|
|
|
*/
|
2013-05-21 14:30:51 +02:00
|
|
|
public function SideBarView() {
|
2013-04-16 16:07:41 +02:00
|
|
|
if(
|
2013-08-02 03:25:10 +02:00
|
|
|
$this->owner->InheritSideBar
|
|
|
|
&& ($parent = $this->owner->getParent())
|
|
|
|
&& $parent->hasMethod('SideBarView')
|
2013-04-16 16:07:41 +02:00
|
|
|
) {
|
2013-08-02 03:25:10 +02:00
|
|
|
return $parent->SideBarView();
|
2013-04-16 16:07:41 +02:00
|
|
|
} elseif($this->owner->SideBar()->exists()){
|
|
|
|
return $this->owner->SideBar();
|
|
|
|
}
|
|
|
|
}
|
2013-07-08 14:42:54 +02:00
|
|
|
|
|
|
|
public function onBeforeDuplicate($duplicatePage) {
|
|
|
|
if($this->owner->hasField('SideBarID')) {
|
|
|
|
$sideBar = $this->owner->getComponent('SideBar');
|
|
|
|
$duplicateWidgetArea = $sideBar->duplicate();
|
|
|
|
|
|
|
|
foreach($sideBar->Items() as $originalWidget) {
|
|
|
|
$widget = $originalWidget->duplicate(false);
|
|
|
|
$widget->ParentID = $duplicateWidgetArea->ID;
|
|
|
|
$widget->write();
|
|
|
|
}
|
|
|
|
|
|
|
|
$duplicatePage->SideBarID = $duplicateWidgetArea->ID;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return $duplicatePage;
|
|
|
|
}
|
2013-04-16 16:07:41 +02:00
|
|
|
|
2013-08-08 15:28:11 +02:00
|
|
|
/**
|
|
|
|
* Support Translatable so that we don't link WidgetAreas across translations
|
|
|
|
*/
|
|
|
|
public function onTranslatableCreate() {
|
|
|
|
//reset the sidebar ID
|
|
|
|
$this->owner->SideBarID = 0;
|
|
|
|
}
|
|
|
|
|
2013-05-21 14:30:51 +02:00
|
|
|
}
|