Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 371 lines (318 sloc) 9.345 kb
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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
<?php
/**
* 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 {

/**
* @var FieldList
*/
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;

/**
* @var String custom HTML tag to render with, e.g. to produce a <fieldset>.
*/
protected $tag = 'div';

/**
* @var String Optional description for this set of fields.
* If the {@link $tag} property is set to use a 'fieldset', this will be
* rendered as a <legend> tag, otherwise its a 'title' attribute.
*/
protected $legend;

public function __construct($children = null) {
if($children instanceof FieldList) {
$this->children = $children;
} elseif(is_array($children)) {
$this->children = new FieldList($children);
} else {
$children = is_array(func_get_args()) ? func_get_args() : array();
$this->children = new FieldList($children);
}
$this->children->setContainerField($this);

// Skipping FormField::__construct(), but we have to make sure this
// doesn't count as a broken constructor
$this->brokenOnConstruct = false;
Object::__construct();
}

/**
* Returns all the sub-fields, suitable for <% control FieldList %>
*
* @return FieldList
*/
public function FieldList() {
return $this->children;
}

public function setID($id) {
$this->id = $id;
return $this;
}

/**
* Accessor method for $this->children
*
* @return FieldList
*/
public function getChildren() {
return $this->children;
}

/**
* @param FieldList $children
*/
public function setChildren($children) {
$this->children = $children;
return $this;
}

/**
* @param string
*/
public function setTag($tag) {
$this->tag = $tag;

return $this;
}

/**
* @return string
*/
public function getTag() {
return $this->tag;
}

/**
* @param string
*/
public function setLegend($legend) {
$this->legend = $legend;
return $this;
}

/**
* @return string
*/
public function getLegend() {
return $this->legend;
}

public function extraClasses() {
$classes = array('field', 'CompositeField', parent::extraClasses());
if($this->columnCount) $classes[] = 'multicolumn';

return implode(' ', $classes);
}

public function getAttributes() {
return array_merge(
parent::getAttributes(),
array(
'tabindex' => null,
'type' => null,
'value' => null,
'type' => null,
'title' => ($this->tag == 'fieldset') ? null : $this->legend
)
);
}

/**
* 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, $saveableOnly = false) {
foreach($this->children as $field) {
if(is_object($field)) {
if($field->isComposite()) $field->collateDataFields($list, $saveableOnly);
if($saveableOnly) {
$isIncluded = ($field->hasData() && !$field->isReadonly() && !$field->isDisabled());
} else {
$isIncluded = ($field->hasData());
}
if($isIncluded) {
$name = $field->getName();
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;
}
}
}
}
}

public function setForm($form) {
foreach($this->children as $f)
if(is_object($f)) $f->setForm($form);

parent::setForm($form);

return $this;
}

public function setColumnCount($columnCount) {
$this->columnCount = $columnCount;
return $this;
}

public function getColumnCount() {
return $this->columnCount;
}

public function isComposite() {
return true;
}

public function hasData() {
return false;
}

public function fieldByName($name) {
return $this->children->fieldByName($name);
}

/**
* Add a new child field to the end of the set.
*
* @param FormField
*/
public function push(FormField $field) {
$this->children->push($field);
}

/**
* @uses FieldList->insertBefore()
*/
public function insertBefore($field, $insertBefore) {
$ret = $this->children->insertBefore($field, $insertBefore);
$this->sequentialSet = null;
return $ret;
}

public function insertAfter($field, $insertAfter) {
$ret = $this->children->insertAfter($field, $insertAfter);
$this->sequentialSet = null;
return $ret;
}

/**
* Remove a field from this CompositeField by Name.
* The field could also be inside a CompositeField.
*
* @param string $fieldName The name of the field
* @param boolean $dataFieldOnly If this is true, then a field will only
* be removed if it's a data field. Dataless fields, such as tabs, will
* be left as-is.
*/
public function removeByName($fieldName, $dataFieldOnly = false) {
$this->children->removeByName($fieldName, $dataFieldOnly);
}

public function replaceField($fieldName, $newField) {
return $this->children->replaceField($fieldName, $newField);
}

public function rootFieldList() {
if(is_object($this->containerFieldList)) return $this->containerFieldList->rootFieldList();
else return $this->children;
}

/**
* Return a readonly version of this field. Keeps the composition but returns readonly
* versions of all the child {@link FormField} objects.
*
* @return CompositeField
*/
public function performReadonlyTransformation() {
$newChildren = new FieldList();
$clone = clone $this;
if($clone->getChildren()) foreach($clone->getChildren() as $idx => $child) {
if(is_object($child)) $child = $child->transform(new ReadonlyTransformation());
$newChildren->push($child, $idx);
}

$clone->children = $newChildren;
$clone->readonly = true;
$clone->addExtraClass($this->extraClass());
$clone->setDescription($this->getDescription());

return $clone;
}

/**
* Return a disabled version of this field. Keeps the composition but returns disabled
* versions of all the child {@link FormField} objects.
*
* @return CompositeField
*/
public function performDisabledTransformation() {
$newChildren = new FieldList();
$clone = clone $this;
if($clone->getChildren()) foreach($clone->getChildren() as $idx => $child) {
if(is_object($child)) $child = $child->transform(new DisabledTransformation());
$newChildren->push($child, $idx);
}

$clone->children = $newChildren;
$clone->readonly = true;
$clone->addExtraClass($this->extraClass());
$clone->setDescription($this->getDescription());
foreach($this->attributes as $k => $v) {
$clone->setAttribute($k, $v);
}

return $clone;
}

public function IsReadonly() {
return $this->readonly;
}

/**
* Find the numerical position of a field within
* the children collection. Doesn't work recursively.
*
* @param string|FormField
* @return int Position in children collection (first position starts with 0). Returns FALSE if the field can't
* be found.
*/
public function fieldPosition($field) {
if(is_string($field)) $field = $this->fieldByName($field);
if(!$field) return false;

$i = 0;
foreach($this->children as $child) {
if($child->getName() == $field->getName()) return $i;
$i++;
}

return false;
}

/**
* Transform the named field into a readonly feld.
*
* @param string|FormField
*/
public function makeFieldReadonly($field) {
$fieldName = ($field instanceof FormField) ? $field->getName() : $field;

// Iterate on items, looking for the applicable field
foreach($this->children as $i => $item) {
if($item->isComposite()) {
$item->makeFieldReadonly($fieldName);
} else {
// Once it's found, use FormField::transform to turn the field into a readonly version of itself.
if($item->getName() == $fieldName) {
$this->children->replaceField($fieldName, $item->transform(new ReadonlyTransformation()));

// Clear an internal cache
$this->sequentialSet = null;

// A true results indicates that the field was foudn
return true;
}
}
}
return false;
}

public function debug() {
$result = "$this->class ($this->name) <ul>";
foreach($this->children as $child) {
$result .= "<li>" . Debug::text($child) . "&nbsp;</li>";
}
$result .= "</ul>";
return $result;
}

public function validate($validator) {
$valid = true;
foreach($this->children as $idx => $child){
$valid = ($child && $child->validate($validator) && $valid);
}
return $valid;
}

}

Something went wrong with that request. Please try again.