silverstripe-framework/forms/FieldSet.php

384 lines
11 KiB
PHP
Raw Normal View History

<?php
/**
* DataObjectSet designed for form fields.
* It extends the DataObjectSet with the ability to get a sequential set of fields.
* @package forms
* @subpackage fields-structural
*/
class FieldSet extends DataObjectSet {
/**
* Cached flat representation of all fields in this set,
* including fields nested in {@link CompositeFields}.
*
* @uses self::collateDataFields()
* @var array
*/
protected $sequentialSet;
/**
* Return a sequential set of all fields that have data. This excludes wrapper composite fields
* as well as heading / help text fields.
*/
public function dataFields() {
if(!$this->sequentialSet) $this->collateDataFields($this->sequentialSet);
return $this->sequentialSet;
}
protected function collateDataFields(&$list) {
foreach($this as $field) {
switch externals to trunk. Inform-merge: from the changeset: r32477: Merge 2.0-inform from trunk previously r32478: Merge 2.0-inform from trunk previously r32481: merge 2.0infom with lastes chunk r32483: merge 2.0infom with lastes chunk r33526: Final styling of all forms and combined communication form add/alter javascript for height adjustment of First / Second block r33580: styling for combined form communication in myinfom pages r33706: styling of combined form (communication) in Email r33881: made compatible to $extraClass r33885: added defaultVal r33887: fixed typo r34728: modified SmallFieldHolder?() r34729: added "validationError"-class r34914: WIP3866: Factfinder: Hide "self emplyed" block r34964: Change current plan upto TraumaInsurance? r35038: disabled friggin field focus r35230: #1032 Fixed hash-link insertion r35887: conditionally setting parameters in sourceID() - to avoid empty overrides r35892: Saving value in SQL-compatible format (YYYY-MM-DD instead of DD/MM/YYYY), with fallback for non-sql values (just passed through without conversion) r35928: Removed "create a" from PageType?-dropdown, sorting alphabetically, falling back to $singular_name r35990: branched off for membertablefield r35994: fix for membertablefield r36024: added array-condition needed for DMYDateField r36083: fix bug for compositeField -> dropDatalessField r36394: removed debug code r36826: change wrong indent format r36828: WIP 4262: Logging out of My Inform goes to blank page r36858: Fixed error caused in r12472 while merging to Session-class r37132: Merged partial changesets from branches/2.0-nzct, only adding childID to detailform when not in add-mode r40815: add an unsubscribe record when a member subscribe a newslettertype r41113: fix the bug described in http://support.silverstripe.com/info/ticket/31: CRM not showing search results r43226: fixed search (partial merge from trunk) r43268: merged createNewPassword() from trunk, was referencing a non-existinent global function randomString() git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@45473 467b73ca-7a2a-4603-9d3b-597d59a354a9
2007-11-23 02:10:19 +01:00
if($field->isComposite()) $field->collateDataFields($list);
if($field->hasData()) {
$name = $field->Name();
if(isset($list[$name])) {
$errSuffix = "";
if($this->form) $errSuffix = " in your '{$this->form->class}' form called '" . $this->form->Name() . "'";
else $errSuffix = '';
user_error("collateDataFields() I noticed that a field called '$name' appears twice$errSuffix.", E_USER_ERROR);
}
$list[$name] = $field;
}
}
}
/**
* Add an extra field to a tab within this fieldset.
* This is most commonly used when overloading getCMSFields()
* @param tabName The name of the tab or tabset. Subtabs can be referred to as TabSet.Tab or TabSet.Tab.Subtab.
* This function will create any missing tabs.
* @param field The {@link FormField} object to add to the end of that tab.
* @param insertBefore The name of the field to insert before. Optional.
*/
public function addFieldToTab($tabName, $field, $insertBefore = null) {
// This is a cache that must be flushed
$this->sequentialSet = null;
// Find the tab
$tab = $this->findOrMakeTab($tabName);
// Add the field to the end of this set
if($insertBefore) $tab->insertBefore($field, $insertBefore);
else $tab->push($field);
}
/**
* Remove the given field from the given tab in the field.
*/
public function removeFieldFromTab($tabName, $fieldName) {
// This is a cache that must be flushed
$this->sequentialSet = null;
// Find the tab
$tab = $this->findOrMakeTab($tabName);
$tab->removeByName($fieldName);
}
/**
* Remove a field from this fieldset by name.
* It musn't be buried in a composite field.--- changed
* It could be buried in a composite field now. --- 5/09/2006
*/
public function removeByName($fieldName) {
foreach($this->items as $i => $child) {
if(is_object($child) && ($child->Name() == $fieldName || $child->Title() == $fieldName)) {
// unset($this->items[$i]);
array_splice( $this->items, $i, 1 );
break;
} else if($child->isComposite()) $child->removeByName($fieldName);
}
}
/**
* Add a number of extra fields to a tab within this fieldset.
* This is most commonly used when overloading getCMSFields()
* @param tabName The name of the tab or tabset. Subtabs can be referred to as TabSet.Tab or TabSet.Tab.Subtab.
* This function will create any missing tabs.
* @param fields An array of {@link FormField} objects.
*/
public function addFieldsToTab($tabName, $fields) {
$this->sequentialSet = null;
// Find the tab
$tab = $this->findOrMakeTab($tabName);
// Add the fields to the end of this set
foreach($fields as $field) $tab->push($field);
}
public function replaceField($fieldName, $newField) {
if($this->sequentialSet) $this->sequentialSet = null;
foreach($this->items as $i => $field) {
if(is_object($field)) {
if($field->Name() == $fieldName) {
$this->items[$i] = $newField;
return true;
} else if($field->isComposite()) {
if($field->replaceField($fieldName, $newField)) return true;
}
}
}
return false;
}
/**
* Returns the specified tab object, creating it if necessary.
* @param tabName The tab to return, in the form "Tab.Subtab.Subsubtab"
*/
protected function findOrMakeTab($tabName) {
$parts = explode('.',$tabName);
// We could have made this recursive, but I've chosen to keep all the logic code within FieldSet rather than add it to TabSet and Tab too.
$currentPointer = $this;
foreach($parts as $part) {
$parentPointer = $currentPointer;
$currentPointer = $currentPointer->fieldByName($part);
// Create any missing tabs
if(!$currentPointer) {
if(is_a($parentPointer,'TabSet')) {
$currentPointer = new Tab($part);
$parentPointer->push($currentPointer);
} else {
user_error("FieldSet::addFieldToTab() Tried to add a tab to a " . $parentPointer->class . " object - '$part' didn't exist.", E_USER_ERROR);
}
}
}
return $currentPointer;
}
/**
* Returns a named field.
*
* @todo Implement similiarly to dataFieldByName() to support nested sets - or merge with dataFields()
*/
public function fieldByName($name) {
foreach($this->items as $child) {
if($name == $child->Name() || $name == $child->id) return $child;
}
}
/**
* Returns a named field in a sequential set.
* Use this if you're using nested FormFields.
*
* @param string $name The name of the field to return
* @return FormField instance
*/
public function dataFieldByName($name) {
if($dataFields = $this->dataFields()) {
foreach($dataFields as $child) {
if($name == $child->Name() || $name == $child->id) return $child;
}
}
}
/**
* Inserts a field before a particular field in a FieldSet.
*
* @param FormField $item The form field to insert
* @param string $name Name of the field to insert before
*/
public function insertBefore($item, $name) {
if($this->sequentialSet) $this->sequentialSet = null;
$i = 0;
foreach($this->items as $child) {
if($name == $child->Name() || $name == $child->id) {
array_splice($this->items, $i, 0, array($item));
return;
}
$i++;
}
$this->items[] = $item;
}
/**
* Inserts an item before the item with name $name
* It can be buried in a composite field
* If no item with name $name is found, $item is inserted at the end of the FieldSet
*
* @param FormField $item The item to be inserted
* @param string $name The name of the item that $item should be before
* @param int $level For internal use only, should not be passed
*/
public function insertBeforeRecursive($item, $name, $level = 0) {
if($this->sequentialSet) $this->sequentialSet = null;
$i = 0;
foreach($this->items as $child) {
if($name == $child->Name() || $name == $child->id) {
array_splice($this->items, $i, 0, array($item));
return $level;
} else if($child->isComposite()) {
if($level = $child->insertBeforeRecursive($item,$name,$level+1)) return $level;
}
$i++;
}
if ($level === 0) {
$this->items[] = $item;
return 0;
}
return false;
}
/**
* Inserts a field after a particular field in a FieldSet.
*
* @param FormField $item The form field to insert
* @param string $name Name of the field to insert after
*/
public function insertAfter($item, $name) {
if($this->sequentialSet) $this->sequentialSet = null;
$i = 0;
foreach($this->items as $child) {
if($name == $child->Name() || $name == $child->id) {
array_splice($this->items, $i + 1, 0, array($item));
return;
}
$i++;
}
$this->items[] = $item;
}
/**
* Push a single field into this FieldSet instance.
*
* @param FormField $item The FormField to add
* @param string $key An option array key (field name)
*/
public function push($item, $key = null) {
if($this->sequentialSet) $this->sequentialSet = null;
return parent::push($item, $key = null);
}
/**
* Set the Form instance for this FieldSet.
*
* @param Form $form
*/
public function setForm($form) {
foreach($this as $field) $field->setForm($form);
}
/**
* Load the given data into this form.
* @param data An map of data to load into the FieldSet.
*/
public function setValues($data) {
foreach($this->dataFields() as $field) {
$fieldName = $field->Name();
if(isset($data[$fieldName])) $field->setValue($data[$fieldName]);
}
}
/**
* Return all <input type="hidden"> fields
* in a form - including fields nested in {@link CompositeFields}.
* Useful when doing custom field layouts.
*
* @return FieldSet
*/
function HiddenFields() {
$hiddenFields = new FieldSet();
$dataFields = $this->dataFields();
if($dataFields) foreach($dataFields as $field) {
if($field instanceof HiddenField) $hiddenFields->push($field);
}
return $hiddenFields;
}
/**
* Transform this FieldSet with a given tranform method,
* e.g. $this->transform(new ReadonlyTransformation())
*
* @return FieldSet
*/
function transform($trans) {
$this->sequentialSet = null;
$newFields = new FieldSet();
foreach($this as $field) {
$newFields->push($field->transform($trans));
}
return $newFields;
}
/**
* Transforms this FieldSet instance to readonly.
*
* @return FieldSet
*/
function makeReadonly() {
return $this->transform(new ReadonlyTransformation());
}
/**
* Transform the named field into a readonly feld.
*/
function makeFieldReadonly($fieldName) {
// Iterate on items, looking for the applicable field
foreach($this->items as $i => $field) {
// Once it's found, use FormField::transform to turn the field into a readonly version of itself.
if($field->Name() == $fieldName) {
$this->items[$i] = $field->transform(new ReadonlyTransformation());
// Clear an internal cache
$this->sequentialSet = null;
// A true results indicates that the field was foudn
return true;
}
}
return false;
}
/**
* Change the order of fields in this FieldSet by specifying an ordered list of field names.
* This works well in conjunction with SilverStripe's scaffolding functions: take the scaffold, and
* shuffle the fields around to the order that you want.
*
* Please note that any tabs or other dataless fields will be clobbered by this operation.
*
* Field names can be given as an array, or just as a list of arguments.
*/
function changeFieldOrder($fieldNames) {
// Field names can be given as an array, or just as a list of arguments.
if(!is_array($fieldNames)) $fieldNames = func_get_args();
// Build a map of fields indexed by their name. This will make the 2nd step much easier.
$fieldMap = array();
foreach($this->dataFields() as $field) $fieldMap[$field->Name()] = $field;
// Iterate through the ordered list of names, building a new array to be put into $this->items.
// While we're doing this, empty out $fieldMap so that we can keep track of leftovers.
// Unrecognised field names are okay; just ignore them
$fields = array();
foreach($fieldNames as $fieldName) {
if(isset($fieldMap[$fieldName])) {
$fields[] = $fieldMap[$fieldName];
unset($fieldMap[$fieldName]);
}
}
// Add the leftover fields to the end of the list.
$fields = $fields + array_values($fieldMap);
// Update our internal $this->items parameter.
$this->items = $fields;
// Re-set an internal cache
$this->sequentialSet = null;
}
}
?>