|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Widgets\Model; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Control\Controller; |
|
6
|
|
|
use SilverStripe\ORM\ArrayList; |
|
7
|
|
|
use SilverStripe\ORM\DataObject; |
|
8
|
|
|
use SilverStripe\ORM\HasManyList; |
|
9
|
|
|
use SilverStripe\ORM\SS_List; |
|
10
|
|
|
use SilverStripe\Versioned\Versioned; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Represents a set of widgets shown on a page. |
|
14
|
|
|
*/ |
|
15
|
|
|
class WidgetArea extends DataObject |
|
16
|
|
|
{ |
|
17
|
|
|
private static $has_many = [ |
|
|
|
|
|
|
18
|
|
|
"Widgets" => Widget::class |
|
19
|
|
|
]; |
|
20
|
|
|
|
|
21
|
|
|
private static $owns = [ |
|
|
|
|
|
|
22
|
|
|
'Widgets', |
|
23
|
|
|
]; |
|
24
|
|
|
|
|
25
|
|
|
private static $cascade_deletes = [ |
|
|
|
|
|
|
26
|
|
|
'Widgets', |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
private static $extensions = [ |
|
|
|
|
|
|
30
|
|
|
Versioned::class . '.versioned', |
|
31
|
|
|
]; |
|
32
|
|
|
|
|
33
|
|
|
private static $table_name = 'WidgetArea'; |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
public $template = __CLASS__; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Used in template instead of {@link Widgets()} to wrap each widget in its |
|
39
|
|
|
* controller, making it easier to access and process form logic and |
|
40
|
|
|
* actions stored in {@link WidgetController}. |
|
41
|
|
|
* |
|
42
|
|
|
* @return SS_List - Collection of {@link WidgetController} instances. |
|
43
|
|
|
*/ |
|
44
|
|
|
public function WidgetControllers() |
|
45
|
|
|
{ |
|
46
|
|
|
$controllers = new ArrayList(); |
|
47
|
|
|
$items = $this->ItemsToRender(); |
|
48
|
|
|
if (!is_null($items)) { |
|
49
|
|
|
foreach ($items as $widget) { |
|
50
|
|
|
/** @var Widget $widget */ |
|
51
|
|
|
|
|
52
|
|
|
/** @var Controller $controller */ |
|
53
|
|
|
$controller = $widget->getController(); |
|
54
|
|
|
|
|
55
|
|
|
$controller->doInit(); |
|
56
|
|
|
$controllers->push($controller); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
return $controllers; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return HasManyList |
|
64
|
|
|
*/ |
|
65
|
|
|
public function Items() |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->Widgets(); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return HasManyList |
|
72
|
|
|
*/ |
|
73
|
|
|
public function ItemsToRender() |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->Items()->filter('Enabled', 1); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return string - HTML |
|
80
|
|
|
*/ |
|
81
|
|
|
public function forTemplate() |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->renderWith($this->template); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $template |
|
89
|
|
|
*/ |
|
90
|
|
|
public function setTemplate($template) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->template = $template; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|