Completed
Pull Request — master (#143)
by Robbie
01:50
created

WidgetArea::onBeforeDelete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace SilverStripe\Widgets\Model;
4
5
use SilverStripe\ORM\ArrayList;
6
use SilverStripe\ORM\DataObject;
7
use SilverStripe\Widgets\Model\Widget;
8
9
/**
10
 * Represents a set of widgets shown on a page.
11
 *
12
 * @package widgets
13
 */
14
class WidgetArea extends DataObject
15
{
16
    /**
17
     * @var array
18
     */
19
    private static $has_many = array(
0 ignored issues
show
Unused Code introduced by
The property $has_many 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...
20
        "Widgets" => Widget::class
21
    );
22
23
    /**
24
     * @var string
25
     */
26
    private static $table_name = 'WidgetArea';
0 ignored issues
show
Unused Code introduced by
The property $table_name 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
28
    /**
29
     *
30
     * @var string
31
     */
32
    public $template = __CLASS__;
33
34
    /**
35
     * Used in template instead of {@link Widgets()} to wrap each widget in its
36
     * controller, making it easier to access and process form logic and
37
     * actions stored in {@link WidgetController}.
38
     *
39
     * @return SS_List - Collection of {@link WidgetController} instances.
40
     */
41
    public function WidgetControllers()
42
    {
43
        $controllers = new ArrayList();
44
        $items = $this->ItemsToRender();
45
        if (!is_null($items)) {
46
            foreach ($items as $widget) {
47
                $controller = $widget->getController();
48
49
                $controller->doInit();
50
                $controllers->push($controller);
51
            }
52
        }
53
        return $controllers;
54
    }
55
56
    /**
57
     * @return HasManyList
58
     */
59
    public function Items()
60
    {
61
        return $this->getComponents('Widgets');
62
    }
63
64
    /**
65
     * @return HasManyList
66
     */
67
    public function ItemsToRender()
68
    {
69
        return $this->getComponents('Widgets')
70
            ->filter("Enabled", 1);
71
    }
72
73
    /**
74
     * @return string - HTML
75
     */
76
    public function forTemplate()
77
    {
78
        return $this->renderWith($this->template);
79
    }
80
81
    /**
82
     *
83
     * @param string $template
84
     */
85
    public function setTemplate($template)
86
    {
87
        $this->template = $template;
88
    }
89
90
    /**
91
     * Delete all connected Widgets when this WidgetArea gets deleted
92
     */
93
    public function onBeforeDelete()
94
    {
95
        parent::onBeforeDelete();
96
        foreach ($this->Widgets() as $widget) {
97
            $widget->delete();
98
        }
99
    }
100
}
101