/
FormScaffolder.php
194 lines (175 loc) · 5.83 KB
/
FormScaffolder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
*
* @package sapphire
* @subpackage forms
*
* @uses DBField::scaffoldFormField()
* @uses DataObject::fieldLabels()
*/
class FormScaffolder extends Object {
/**
* @var DataObject $obj The object defining the fields to be scaffolded
* through its metadata like $db, $searchable_fields, etc.
*/
protected $obj;
/**
* @var boolean $tabbed Return fields in a tabset, with all main fields in the path "Root.Main",
* relation fields in "Root.<relationname>" (if {@link $includeRelations} is enabled).
*/
public $tabbed = false;
/**
* @var boolean $ajaxSafe
*/
public $ajaxSafe = false;
/**
* @var array $restrictFields Numeric array of a field name whitelist.
* If left blank, all fields from {@link DataObject->db()} will be included.
*
* @todo Implement restrictions for has_many and many_many relations.
*/
public $restrictFields;
/**
* @var array $fieldClasses Optional mapping of fieldnames to subclasses of {@link FormField}.
* By default the scaffolder will determine the field instance by {@link DBField::scaffoldFormField()}.
*
* @todo Implement fieldClasses for has_many and many_many relations
*/
public $fieldClasses;
/**
* @var boolean $includeRelations Include has_one, has_many and many_many relations
*/
public $includeRelations = false;
/**
* @param DataObject $obj
* @param array $params
*/
function __construct($obj) {
$this->obj = $obj;
parent::__construct();
}
/**
* Gets the form fields as defined through the metadata
* on {@link $obj} and the custom parameters passed to FormScaffolder.
* Depending on those parameters, the fields can be used in ajax-context,
* contain {@link TabSet}s etc.
*
* @return FieldList
*/
public function getFieldSet() {
$fields = new FieldList();
// tabbed or untabbed
if($this->tabbed) {
$fields->push(new TabSet("Root", $mainTab = new Tab("Main")));
$mainTab->setTitle(_t('SiteTree.TABMAIN', "Main"));
}
// add database fields
foreach($this->obj->db() as $fieldName => $fieldType) {
if($this->restrictFields && !in_array($fieldName, $this->restrictFields)) continue;
// @todo Pass localized title
if($this->fieldClasses && isset($this->fieldClasses[$fieldName])) {
$fieldClass = $this->fieldClasses[$fieldName];
$fieldObject = new $fieldClass($fieldName);
} else {
$fieldObject = $this->obj->dbObject($fieldName)->scaffoldFormField(null, $this->getParamsArray());
}
$fieldObject->setTitle($this->obj->fieldLabel($fieldName));
if($this->tabbed) {
$fields->addFieldToTab("Root.Main", $fieldObject);
} else {
$fields->push($fieldObject);
}
}
// add has_one relation fields
if($this->obj->has_one()) {
foreach($this->obj->has_one() as $relationship => $component) {
if($this->restrictFields && !in_array($relationship, $this->restrictFields)) continue;
$fieldName = "{$relationship}ID";
if($this->fieldClasses && isset($this->fieldClasses[$fieldName])) {
$fieldClass = $this->fieldClasses[$fieldName];
$hasOneField = new $fieldClass($fieldName);
} else {
$hasOneField = $this->obj->dbObject($fieldName)->scaffoldFormField(null, $this->getParamsArray());
}
$hasOneField->setTitle($this->obj->fieldLabel($relationship));
if($this->tabbed) {
$fields->addFieldToTab("Root.Main", $hasOneField);
} else {
$fields->push($hasOneField);
}
}
}
// only add relational fields if an ID is present
if($this->obj->ID) {
// add has_many relation fields
if($this->obj->has_many() && ($this->includeRelations === true || isset($this->includeRelations['has_many']))) {
foreach($this->obj->has_many() as $relationship => $component) {
if($this->tabbed) {
$relationTab = $fields->findOrMakeTab(
"Root.$relationship",
$this->obj->fieldLabel($relationship)
);
}
$relationshipFields = singleton($component)->summaryFields();
$foreignKey = $this->obj->getRemoteJoinField($relationship);
$fieldClass = (isset($this->fieldClasses[$relationship])) ? $this->fieldClasses[$relationship] : 'ComplexTableField';
$ctf = new $fieldClass(
$this,
$relationship,
null,
$relationshipFields,
"getCMSFields"
);
$ctf->setPermissions(TableListField::permissions_for_object($component));
if($this->tabbed) {
$fields->addFieldToTab("Root.$relationship", $ctf);
} else {
$fields->push($ctf);
}
}
}
if($this->obj->many_many() && ($this->includeRelations === true || isset($this->includeRelations['many_many']))) {
foreach($this->obj->many_many() as $relationship => $component) {
if($this->tabbed) {
$relationTab = $fields->findOrMakeTab(
"Root.$relationship",
$this->obj->fieldLabel($relationship)
);
}
$relationshipFields = singleton($component)->summaryFields();
$fieldClass = (isset($this->fieldClasses[$relationship])) ? $this->fieldClasses[$relationship] : 'ComplexTableField';
$ctf = new $fieldClass(
$this,
$relationship,
$this->obj->$relationship(),
$relationshipFields,
"getCMSFields"
);
$ctf->setPermissions(TableListField::permissions_for_object($component));
$ctf->popupClass = "ScaffoldingComplexTableField_Popup";
if($this->tabbed) {
$fields->addFieldToTab("Root.$relationship", $ctf);
} else {
$fields->push($ctf);
}
}
}
}
return $fields;
}
/**
* Return an array suitable for passing on to {@link DBField->scaffoldFormField()}
* without tying this call to a FormScaffolder interface.
*
* @return array
*/
protected function getParamsArray() {
return array(
'tabbed' => $this->tabbed,
'includeRelations' => $this->includeRelations,
'restrictFields' => $this->restrictFields,
'fieldClasses' => $this->fieldClasses,
'ajaxSafe' => $this->ajaxSafe
);
}
}