1 <?php
2 3 4 5 6 7 8 9 10 11 12
13 class Widget extends DataObject {
14 static $db = array(
15 "Sort" => "Int",
16 "Enabled" => "Boolean"
17 );
18
19 static $defaults = array(
20 'Enabled' => true
21 );
22
23 static $has_one = array(
24 "Parent" => "WidgetArea",
25 );
26
27 static $has_many = array();
28 static $many_many = array();
29 static $belongs_many_many = array();
30
31 static $default_sort = "\"Sort\"";
32
33 static $title = "Widget Title";
34 static $cmsTitle = "Name of this widget";
35 static $description = "Description of what this widget does.";
36
37 function getCMSFields() {
38 $fields = parent::getCMSFields();
39 $fields->replaceField('Sort', new HiddenField('Sort'));
40 $this->extend('updateCMSFields', $fields);
41 return $fields;
42 }
43
44 45 46 47 48
49 function WidgetHolder() {
50 return $this->renderWith("WidgetHolder");
51 }
52
53 function Title() {
54 return Object::get_static($this->class, 'title');
55 }
56
57 function CMSTitle() {
58 return Object::get_static($this->class, 'cmsTitle');
59 }
60
61 function Description() {
62 return Object::get_static($this->class, 'description');
63 }
64
65 function DescriptionSegment() {
66 return $this->renderWith('WidgetDescription');
67 }
68
69 70 71
72 function EditableSegment() {
73 return $this->renderWith('WidgetEditor');
74 }
75
76 function CMSEditor() {
77 $output = '';
78 $fields = $this->getCMSFields();
79 foreach($fields as $field) {
80 $name = $field->Name();
81 $field->setValue($this->getField($name));
82 $renderedField = $field->FieldHolder();
83 $renderedField = ereg_replace("name=\"([A-Za-z0-9\-_]+)\"", "name=\"Widget[" . $this->ID . "][\\1]\"", $renderedField);
84 $renderedField = ereg_replace("id=\"([A-Za-z0-9\-_]+)\"", "id=\"Widget[" . $this->ID . "][\\1]\"", $renderedField);
85 $output .= $renderedField;
86 }
87 return $output;
88 }
89
90 function ClassName() {
91 return $this->class;
92 }
93
94 function Name() {
95 return "Widget[".$this->ID."]";
96 }
97
98 function EnabledTitle() {
99 return ($this->Enabled) ? _t('Boolean.YES','YES') : _t('Boolean.NO','NO');
100 }
101
102 function populateFromPostData($data) {
103 foreach($data as $name => $value) {
104 if($name != "Type") {
105 $this->setField($name, $value);
106 }
107 }
108
109 $this->write();
110
111
112 $this->Name = $this->class.$this->ID;
113 $this->write();
114 }
115
116 }
117
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
136 class Widget_Controller extends Controller {
137
138 139 140
141 protected $widget;
142
143 function __construct($widget = null) {
144
145 if($widget) {
146 $this->widget = $widget;
147 $this->failover = $widget;
148 }
149
150 parent::__construct();
151 }
152
153 public function Link($action = null) {
154 return Controller::curr()->Link (
155 Controller::join_links('widget', ($this->widget ? $this->widget->ID : null), $action)
156 );
157 }
158
159 160 161
162 function getWidget() {
163 return $this->widget;
164 }
165
166 167 168 169 170 171
172 function WidgetHolder() {
173 return $this->renderWith("WidgetHolder");
174 }
175
176 177 178 179 180 181 182 183
184 function editablesegment() {
185 $className = $this->urlParams['ID'];
186 if(class_exists($className) && is_subclass_of($className, 'Widget')) {
187 $obj = new $className();
188 return $obj->EditableSegment();
189 } else {
190 user_error("Bad widget class: $className", E_USER_WARNING);
191 return "Bad widget class name given";
192 }
193 }
194 }
195
196 197 198 199
200 class Widget_TreeDropdownField extends TreeDropdownField {
201 function FieldHolder() {}
202 function Field() {}
203 }
204
205 ?>
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-