WidgetPageExtension   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 32
dl 0
loc 65
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A updateCMSFields() 0 9 1
A onBeforeDuplicate() 0 16 3
A SideBarView() 0 9 5
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 = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
23
        'InheritSideBar' => 'Boolean',
24
    ];
25
26
    private static $defaults = [
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
27
        'InheritSideBar' => true
28
    ];
29
30
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
31
        'SideBar' => WidgetArea::class,
32
    ];
33
34
    private static $owns = [
0 ignored issues
show
introduced by
The private property $owns is not used, and could be removed.
Loading history...
35
        'SideBar',
36
    ];
37
38
    private static $cascade_deletes = [
0 ignored issues
show
introduced by
The private property $cascade_deletes is not used, and could be removed.
Loading history...
39
        'SideBar',
40
    ];
41
42
    public function updateCMSFields(FieldList $fields)
43
    {
44
        $fields->addFieldToTab(
45
            "Root.Widgets",
46
            new CheckboxField("InheritSideBar", _t(__CLASS__ . '.INHERITSIDEBAR', 'Inherit Sidebar From Parent'))
47
        );
48
        $fields->addFieldToTab(
49
            "Root.Widgets",
50
            new WidgetAreaEditor("SideBar")
51
        );
52
    }
53
54
    /**
55
     * @return WidgetArea
56
     */
57
    public function SideBarView()
58
    {
59
        if ($this->owner->InheritSideBar
60
            && ($parent = $this->owner->getParent())
61
            && $parent->hasMethod('SideBarView')
62
        ) {
63
            return $parent->SideBarView();
64
        } elseif ($this->owner->SideBar()->exists()) {
65
            return $this->owner->SideBar();
66
        }
67
    }
68
69
    public function onBeforeDuplicate($duplicatePage)
70
    {
71
        if ($this->owner->hasField('SideBarID')) {
72
            $sideBar = $this->owner->getComponent('SideBar');
73
            $duplicateWidgetArea = $sideBar->duplicate();
74
75
            foreach ($sideBar->Items() as $originalWidget) {
76
                $widget = $originalWidget->duplicate(false);
77
                $widget->ParentID = $duplicateWidgetArea->ID;
78
                $widget->write();
79
            }
80
81
            $duplicatePage->SideBarID = $duplicateWidgetArea->ID;
82
        }
83
84
        return $duplicatePage;
85
    }
86
}
87