1 <?php
2 3 4 5 6 7
8
9 class FieldEditor extends FormField {
10
11 12 13 14 15
16 function FieldHolder() {
17 return $this->renderWith("FieldEditor");
18 }
19
20 21 22 23 24
25 public function canEdit() {
26 if($this->readonly) return false;
27
28 return $this->form->getRecord()->canEdit();
29 }
30
31 32 33 34 35 36 37
38 public function canDelete() {
39 if($this->readonly) return false;
40
41 return $this->form->getRecord()->canEdit();
42 }
43
44 45 46 47 48
49 function performReadonlyTransformation() {
50 $clone = clone $this;
51 $clone->readonly = true;
52 $fields = $clone->Fields();
53 if($fields) foreach($fields as $field) {
54 $field->setReadonly();
55 }
56
57 return $clone->customise(array('Fields' => $fields));
58 }
59
60 61 62 63 64
65 function Fields() {
66
67 if($this->form && $this->form->getRecord() && $this->name) {
68 $relationName = $this->name;
69 $fields = $this->form->getRecord()->getComponents($relationName);
70
71 if($fields) {
72 foreach($fields as $field) {
73 if(!$this->canEdit()) {
74 if(is_a($field, 'FormField')) {
75 $fields->remove($field);
76 $fields->push($field->performReadonlyTransformation());
77 }
78 }
79 }
80 }
81
82 return $fields;
83 }
84 }
85
86 87 88 89 90 91
92 function CreatableFields() {
93 $fields = ClassInfo::subclassesFor('EditableFormField');
94
95 if($fields) {
96 array_shift($fields);
97 asort($fields);
98 $output = new DataObjectSet();
99 foreach($fields as $field => $title) {
100
101 $niceTitle = singleton($title)->i18n_singular_name();
102 if($niceTitle && singleton($title)->canCreate()) {
103 $output->push(new ArrayData(array(
104 'ClassName' => $field,
105 'Title' => "$niceTitle"
106 )));
107 }
108 }
109 return $output;
110 }
111 return false;
112 }
113
114 115 116 117 118 119
120 function saveInto(DataObject $record) {
121 $name = $this->name;
122 $fieldSet = $record->$name();
123
124
125
126 $missingFields = array();
127
128 foreach($fieldSet as $existingField) {
129 $missingFields[$existingField->ID] = $existingField;
130 }
131
132 if(isset($_REQUEST[$name]) && is_array($_REQUEST[$name])) {
133 foreach($_REQUEST[$name] as $newEditableID => $newEditableData) {
134 if(!is_numeric($newEditableID)) continue;
135
136
137 $editable = DataObject::get_by_id('EditableFormField', $newEditableID);
138
139
140 if($editable) {
141
142
143 if(isset($missingFields[$editable->ID]) && isset($newEditableData) && is_array($newEditableData)) {
144 unset($missingFields[$editable->ID]);
145 }
146
147
148 if($editable->ParentID == 0) {
149 $editable->ParentID = $record->ID;
150 }
151
152
153 $editable->populateFromPostData($newEditableData);
154 }
155 }
156 }
157
158
159 if($this->canEdit()) {
160 foreach($missingFields as $removedField) {
161 if(is_numeric($removedField->ID)) {
162
163 $removedField->delete();
164 }
165 }
166 }
167 }
168
169 170 171 172 173 174
175 public function addfield() {
176
177 $parentID = $this->form->getRecord()->ID;
178
179 if($parentID) {
180 $parentID = Convert::raw2sql($parentID);
181
182 $highestSort = DB::query("SELECT MAX(\"Sort\") FROM \"EditableFormField\" WHERE \"ParentID\" = '$parentID'");
183
184 $sort = $highestSort->value() + 1;
185
186 $className = (isset($_REQUEST['Type'])) ? $_REQUEST['Type'] : '';
187 if(!$className) user_error('Please select a field type to created', E_USER_WARNING);
188
189 if(is_subclass_of($className, "EditableFormField")) {
190 $field = new $className();
191 $field->write();
192 $field->ParentID = $this->form->getRecord()->ID;
193 $field->Name = $field->class . $field->ID;
194 $field->Sort = $sort;
195 $field->write();
196 return $field->EditSegment();
197 }
198 }
199 return false;
200 }
201
202 203 204 205 206 207
208 public function addoptionfield() {
209
210 $parent = (isset($_REQUEST['Parent'])) ? $_REQUEST['Parent'] : false;
211
212
213 if($parent) {
214 $sql_parent = Convert::raw2sql($parent);
215
216 $highestSort = DB::query("SELECT MAX(\"Sort\") FROM \"EditableOption\" WHERE \"ParentID\" = '$sql_parent'");
217
218 $sort = $highestSort->value() + 1;
219
220 if($parent) {
221 $object = new EditableOption();
222 $object->write();
223 $object->ParentID = $parent;
224 $object->Sort = $sort;
225 $object->Name = 'option' . $object->ID;
226 $object->write();
227 return $object->EditSegment();
228 }
229 }
230 return false;
231 }
232 }
[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.
-