Completed
Push — master ( f3b14d...1fe4c3 )
by Damian
10s
created

WidgetPageExtension::updateCMSFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
3
namespace SilverStripe\Widgets\Extensions;
4
5
use SilverStripe\Forms\CheckboxField;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\ORM\DataExtension;
8
use SilverStripe\Widgets\Forms\WidgetAreaEditor;
9
use SilverStripe\Widgets\Model\WidgetArea;
10
11
/**
12
 * Adds a single {@link WidgetArea} called "SideBar" to {@link Page} classes.
13
 * Adjust your templates to render the resulting
14
 * {@link WidgetArea} as required, through the $SideBarView placeholder.
15
 *
16
 * This extension is just an example on how to use the widgets functionality,
17
 * feel free to create your own relationships, naming conventions, etc.
18
 * without using this class.
19
 */
20
class WidgetPageExtension extends DataExtension
21
{
22
    private static $db = array(
0 ignored issues
show
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
23
        'InheritSideBar' => 'Boolean',
24
    );
25
26
    private static $defaults = array(
0 ignored issues
show
Unused Code introduced by
The property $defaults is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
27
        'InheritSideBar' => true
28
    );
29
30
    private static $has_one = array(
0 ignored issues
show
Unused Code introduced by
The property $has_one is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
31
        'SideBar' => WidgetArea::class
32
    );
33
34
    public function updateCMSFields(FieldList $fields)
35
    {
36
        $fields->addFieldToTab(
37
            "Root.Widgets",
38
            new CheckboxField("InheritSideBar", 'Inherit Sidebar From Parent')
39
        );
40
        $fields->addFieldToTab(
41
            "Root.Widgets",
42
            new WidgetAreaEditor("SideBar")
43
        );
44
    }
45
46
    /**
47
     * @return WidgetArea
48
     */
49
    public function SideBarView()
50
    {
51
        if ($this->owner->InheritSideBar
52
            && ($parent = $this->owner->getParent())
53
            && $parent->hasMethod('SideBarView')
54
        ) {
55
            return $parent->SideBarView();
56
        } elseif ($this->owner->SideBar()->exists()) {
57
            return $this->owner->SideBar();
58
        }
59
    }
60
61
    public function onBeforeDuplicate($duplicatePage)
62
    {
63
        if ($this->owner->hasField('SideBarID')) {
64
            $sideBar = $this->owner->getComponent('SideBar');
65
            $duplicateWidgetArea = $sideBar->duplicate();
66
67
            foreach ($sideBar->Items() as $originalWidget) {
68
                $widget = $originalWidget->duplicate(false);
69
                $widget->ParentID = $duplicateWidgetArea->ID;
70
                $widget->write();
71
            }
72
73
            $duplicatePage->SideBarID = $duplicateWidgetArea->ID;
74
        }
75
76
        return $duplicatePage;
77
    }
78
79
    /**
80
     * Support Translatable so that we don't link WidgetAreas across translations
81
     */
82
    public function onTranslatableCreate()
83
    {
84
        //reset the sidebar ID
85
        $this->owner->SideBarID = 0;
86
    }
87
}
88