-
Notifications
You must be signed in to change notification settings - Fork 821
/
CompositeField.php
executable file
·220 lines (189 loc) · 6.18 KB
/
CompositeField.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
/**
* @package forms
* @subpackage fields-structural
*/
/**
* Base class for all fields that contain other fields.
* Implements sequentialisation - so that when we're saving / loading data, we can populate
* a tabbed form properly. All of the children are stored in $this->children
* @package forms
* @subpackage fields-structural
*/
class CompositeField extends FormField {
protected $children;
/**
* Set to true when this field is a readonly field
*/
protected $readonly;
/**
* @var $columnCount int Toggle different css-rendering for multiple columns
* ("onecolumn", "twocolumns", "threecolumns"). The content is determined
* by the $children-array, so wrap all items you want to have grouped in a
* column inside a CompositeField.
* Caution: Please make sure that this variable actually matches the
* count of your $children.
*/
protected $columnCount = null;
public function __construct($children = null) {
if(is_a($children, 'FieldSet')) {
$this->children = $children;
} elseif(is_array($children)) {
$this->children = new FieldSet($children);
} else {
$children = is_array(func_get_args()) ? func_get_args() : array();
$this->children = new FieldSet($children);
}
Object::__construct();
}
/**
* Returns all the sub-fields, suitable for <% control FieldSet %>
*/
public function FieldSet() {
return $this->children;
}
public function setID($id) {
$this->id = $id;
}
public function Field() {
return $this->FieldHolder();
}
/**
* Returns the fields nested inside another DIV
*/
function FieldHolder() {
$fs = $this->FieldSet();
$idAtt = isset($this->id) ? " id=\"{$this->id}\"" : '';
$className = ($this->columnCount) ? "field CompositeField {$this->extraClass()} multicolumn" : "field CompositeField {$this->extraClass()}";
$content = "<div class=\"$className\"$idAtt>\n";
foreach($fs as $subfield) {
if($this->columnCount) {
$className = "column{$this->columnCount}";
if(!next($fs)) $className .= " lastcolumn";
$content .= "\n<div class=\"{$className}\">\n" . $subfield->FieldHolder() . "\n</div>\n";
} else if($subfield){
$content .= "\n" . $subfield->FieldHolder() . "\n";
}
}
$content .= "</div>\n";
return $content;
}
/**
* Returns the fields in the restricted field holder inside a DIV.
*/
function SmallFieldHolder() {//return $this->FieldHolder();
$fs = $this->FieldSet();
$idAtt = isset($this->id) ? " id=\"{$this->id}\"" : '';
$className = ($this->columnCount) ? "field CompositeField {$this->extraClass()} multicolumn" : "field CompositeField {$this->extraClass()}";
$content = "<div class=\"$className\"$idAtt>";
foreach($fs as $subfield) {//echo ' subf'.$subfield->Name();
if($this->columnCount) {
$className = "column{$this->columnCount}";
if(!next($fs)) $className .= " lastcolumn";
$content .= "<div class=\"{$className}\">" . $subfield->FieldHolder() . "</div>";
} else if($subfield){
$content .= $subfield->SmallFieldHolder() . " ";
}
}
$content .= "</div>";
return $content;
}
/**
* Add all of the non-composite fields contained within this field to the list.
* Sequentialisation is used when connecting the form to its data source
*/
public function collateDataFields(&$list) {
foreach($this->children as $field) {
if(is_object($field)) {
if($field->isComposite()) $field->collateDataFields($list);
if($field->hasData()) {
$name = $field->Name();
if($name) {
$formName = (isset($this->form)) ? $this->form->FormName() : '(unknown form)';
if(isset($list[$name])) user_error("collateDataFields() I noticed that a field called '$name' appears twice in your form: '{$formName}'. One is a '{$field->class}' and the other is a '{$list[$name]->class}'", E_USER_ERROR);
$list[$name] = $field;
}
}
}
}
}
function setForm($form) {
foreach($this->children as $f) if(is_object($f)) $f->setForm($form);
parent::setForm($form);
}
function setColumnCount($columnCount) {
$this->columnCount = $columnCount;
}
function isComposite() { return true; }
function hasData() { return false; }
public function fieldByName($name) {
return $this->children->fieldByName($name);
}
/**
* Add a new child field to the end of the set.
*/
public function push(FormField $field) {
$this->children->push($field);
}
public function insertBefore($field, $insertBefore) {
return $this->children->insertBefore($field, $insertBefore);
}
public function insertBeforeRecursive($field, $insertBefore, $level = 0) {
return $this->children->insertBeforeRecursive($field, $insertBefore, $level+1);
}
public function removeByName($fieldName) {
$this->children->removeByName($fieldName);
}
public function replaceField($fieldName, $newField) {
return $this->children->replaceField($fieldName, $newField);
}
/**
* Return a readonly version of this field. Keeps the composition but returns readonly
* versions of all the children
*/
public function performReadonlyTransformation() {
$newChildren = new FieldSet();
foreach($this->children as $idx => $child) {
if(is_object($child)) $child = $child->transform(new ReadonlyTransformation());
$newChildren->push($child, $idx);
}
$this->children = $newChildren;
$this->readonly = true;
return $this;
}
/**
* Return a readonly version of this field. Keeps the composition but returns readonly
* versions of all the children
*/
public function performDisabledTransformation($trans) {
$newChildren = new FieldSet();
if($this->children) foreach($this->children as $idx => $child) {
if(is_object($child)) {
$child = $child->transform($trans);
}
$newChildren->push($child, $idx);
}
$this->children = $newChildren;
$this->readonly = true;
return $this;
}
function IsReadonly() {
return $this->readonly;
}
function debug() {
$result = "$this->class ($this->name) <ul>";
foreach($this->children as $child) {
$result .= "<li>" . Debug::text($child) . " </li>";
}
$result .= "</ul>";
return $result;
}
function validate($validator){
$valid = true;
foreach($this->children as $idx => $child){
$valid = ($child->validate($validator) && $valid);
}
return $valid;
}
}
?>