2013-04-16 16:07:41 +02:00
|
|
|
<?php
|
2017-01-17 23:43:11 +01:00
|
|
|
|
|
|
|
namespace SilverStripe\Widgets\Extensions;
|
|
|
|
|
|
|
|
use SilverStripe\Forms\CheckboxField;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\ORM\DataExtension;
|
|
|
|
use SilverStripe\Widgets\Forms\WidgetAreaEditor;
|
|
|
|
use SilverStripe\Widgets\Model\WidgetArea;
|
|
|
|
|
2013-04-16 16:07:41 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2015-11-18 05:08:21 +01:00
|
|
|
class WidgetPageExtension extends DataExtension
|
|
|
|
{
|
|
|
|
private static $db = array(
|
|
|
|
'InheritSideBar' => 'Boolean',
|
|
|
|
);
|
2013-04-16 16:07:41 +02:00
|
|
|
|
2015-11-18 05:08:21 +01:00
|
|
|
private static $defaults = array(
|
|
|
|
'InheritSideBar' => true
|
|
|
|
);
|
2013-04-16 16:07:41 +02:00
|
|
|
|
2015-11-18 05:08:21 +01:00
|
|
|
private static $has_one = array(
|
2017-01-17 23:43:11 +01:00
|
|
|
'SideBar' => WidgetArea::class
|
2015-11-18 05:08:21 +01:00
|
|
|
);
|
2013-04-16 16:07:41 +02:00
|
|
|
|
2015-11-18 05:08:21 +01:00
|
|
|
public function updateCMSFields(FieldList $fields)
|
|
|
|
{
|
|
|
|
$fields->addFieldToTab(
|
|
|
|
"Root.Widgets",
|
|
|
|
new CheckboxField("InheritSideBar", 'Inherit Sidebar From Parent')
|
|
|
|
);
|
|
|
|
$fields->addFieldToTab(
|
|
|
|
"Root.Widgets",
|
|
|
|
new WidgetAreaEditor("SideBar")
|
|
|
|
);
|
|
|
|
}
|
2013-04-16 16:07:41 +02:00
|
|
|
|
2015-11-18 05:08:21 +01:00
|
|
|
/**
|
|
|
|
* @return WidgetArea
|
|
|
|
*/
|
|
|
|
public function SideBarView()
|
|
|
|
{
|
2017-01-17 23:43:11 +01:00
|
|
|
if ($this->owner->InheritSideBar
|
2015-11-18 05:08:21 +01:00
|
|
|
&& ($parent = $this->owner->getParent())
|
|
|
|
&& $parent->hasMethod('SideBarView')
|
|
|
|
) {
|
|
|
|
return $parent->SideBarView();
|
|
|
|
} elseif ($this->owner->SideBar()->exists()) {
|
|
|
|
return $this->owner->SideBar();
|
|
|
|
}
|
|
|
|
}
|
2017-01-17 23:43:11 +01:00
|
|
|
|
2015-11-18 05:08:21 +01:00
|
|
|
public function onBeforeDuplicate($duplicatePage)
|
|
|
|
{
|
|
|
|
if ($this->owner->hasField('SideBarID')) {
|
|
|
|
$sideBar = $this->owner->getComponent('SideBar');
|
|
|
|
$duplicateWidgetArea = $sideBar->duplicate();
|
2013-04-16 16:07:41 +02:00
|
|
|
|
2015-11-18 05:08:21 +01:00
|
|
|
foreach ($sideBar->Items() as $originalWidget) {
|
|
|
|
$widget = $originalWidget->duplicate(false);
|
|
|
|
$widget->ParentID = $duplicateWidgetArea->ID;
|
|
|
|
$widget->write();
|
|
|
|
}
|
2013-07-08 14:42:54 +02:00
|
|
|
|
2015-11-18 05:08:21 +01:00
|
|
|
$duplicatePage->SideBarID = $duplicateWidgetArea->ID;
|
|
|
|
}
|
2013-07-08 14:42:54 +02:00
|
|
|
|
2015-11-18 05:08:21 +01:00
|
|
|
return $duplicatePage;
|
|
|
|
}
|
2013-05-21 14:30:51 +02:00
|
|
|
}
|