2011-05-06 00:51:02 +02:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Forms;
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
/**
|
|
|
|
* A list designed to hold form field instances.
|
|
|
|
*
|
2016-08-19 00:51:35 +02:00
|
|
|
* @method FormField[] getIterator()
|
2011-05-06 00:51:02 +02:00
|
|
|
*/
|
|
|
|
class FieldList extends ArrayList {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
/**
|
|
|
|
* Cached flat representation of all fields in this set,
|
|
|
|
* including fields nested in {@link CompositeFields}.
|
|
|
|
*
|
|
|
|
* @uses self::collateDataFields()
|
2016-08-19 00:51:35 +02:00
|
|
|
* @var FormField[]
|
2011-05-06 00:51:02 +02:00
|
|
|
*/
|
|
|
|
protected $sequentialSet;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
/**
|
2016-08-19 00:51:35 +02:00
|
|
|
* @var FormField[]
|
2011-05-06 00:51:02 +02:00
|
|
|
*/
|
|
|
|
protected $sequentialSaveableSet;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
/**
|
2016-08-19 00:51:35 +02:00
|
|
|
* If this fieldlist is owned by a parent field (e.g. CompositeField)
|
|
|
|
* this is the parent field.
|
|
|
|
*
|
|
|
|
* @var FieldList|FormField
|
2011-05-06 00:51:02 +02:00
|
|
|
*/
|
|
|
|
protected $containerField;
|
2012-05-09 17:18:16 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
public function __construct($items = array()) {
|
|
|
|
if (!is_array($items) || func_num_args() > 1) {
|
|
|
|
$items = func_get_args();
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::__construct($items);
|
|
|
|
|
|
|
|
foreach ($items as $item) {
|
2016-08-19 00:51:35 +02:00
|
|
|
if ($item instanceof FormField) {
|
|
|
|
$item->setContainerFieldList($this);
|
|
|
|
}
|
2011-05-06 00:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-25 07:24:26 +02:00
|
|
|
public function __clone() {
|
|
|
|
// Clone all fields in this list
|
|
|
|
foreach($this->items as $key => $field) {
|
|
|
|
$this->items[$key] = clone $field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
/**
|
|
|
|
* Return a sequential set of all fields that have data. This excludes wrapper composite fields
|
|
|
|
* as well as heading / help text fields.
|
2016-08-19 00:51:35 +02:00
|
|
|
*
|
|
|
|
* @return FormField[]
|
2011-05-06 00:51:02 +02:00
|
|
|
*/
|
|
|
|
public function dataFields() {
|
2016-08-19 00:51:35 +02:00
|
|
|
if(!$this->sequentialSet) {
|
|
|
|
$this->collateDataFields($this->sequentialSet);
|
|
|
|
}
|
2011-05-06 00:51:02 +02:00
|
|
|
return $this->sequentialSet;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
public function saveableFields() {
|
2016-08-19 00:51:35 +02:00
|
|
|
if(!$this->sequentialSaveableSet) {
|
|
|
|
$this->collateDataFields($this->sequentialSaveableSet, true);
|
|
|
|
}
|
2011-05-06 00:51:02 +02:00
|
|
|
return $this->sequentialSaveableSet;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
protected function flushFieldsCache() {
|
|
|
|
$this->sequentialSet = null;
|
|
|
|
$this->sequentialSaveableSet = null;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
protected function collateDataFields(&$list, $saveableOnly = false) {
|
|
|
|
foreach($this as $field) {
|
2016-08-19 00:51:35 +02:00
|
|
|
if($field instanceof CompositeField) {
|
|
|
|
$field->collateDataFields($list, $saveableOnly);
|
|
|
|
}
|
2011-05-06 00:51:02 +02:00
|
|
|
|
|
|
|
if($saveableOnly) {
|
|
|
|
$isIncluded = ($field->hasData() && !$field->isReadonly() && !$field->isDisabled());
|
|
|
|
} else {
|
|
|
|
$isIncluded = ($field->hasData());
|
|
|
|
}
|
|
|
|
if($isIncluded) {
|
2011-10-29 06:01:52 +02:00
|
|
|
$name = $field->getName();
|
2011-05-06 00:51:02 +02:00
|
|
|
if(isset($list[$name])) {
|
2012-09-26 23:34:00 +02:00
|
|
|
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);
|
2011-05-06 00:51:02 +02:00
|
|
|
}
|
|
|
|
$list[$name] = $field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
/**
|
2011-10-28 03:37:27 +02:00
|
|
|
* Add an extra field to a tab within this FieldList.
|
2011-05-06 00:51:02 +02:00
|
|
|
* This is most commonly used when overloading getCMSFields()
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* @param string $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.
|
2011-05-06 00:51:02 +02:00
|
|
|
* @param FormField $field The {@link FormField} object to add to the end of that tab.
|
|
|
|
* @param string $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->flushFieldsCache();
|
|
|
|
|
|
|
|
// Find the tab
|
|
|
|
$tab = $this->findOrMakeTab($tabName);
|
|
|
|
|
|
|
|
// Add the field to the end of this set
|
2014-01-07 17:02:43 +01:00
|
|
|
if($insertBefore) $tab->insertBefore($insertBefore, $field);
|
2011-05-06 00:51:02 +02:00
|
|
|
else $tab->push($field);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
/**
|
2011-10-28 03:37:27 +02:00
|
|
|
* Add a number of extra fields to a tab within this FieldList.
|
2011-05-06 00:51:02 +02:00
|
|
|
* This is most commonly used when overloading getCMSFields()
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* @param string $tabName The name of the tab or tabset. Subtabs can be referred to as TabSet.Tab
|
|
|
|
* or TabSet.Tab.Subtab.
|
2011-05-06 00:51:02 +02:00
|
|
|
* This function will create any missing tabs.
|
|
|
|
* @param array $fields An array of {@link FormField} objects.
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param string $insertBefore Name of field to insert before
|
2011-05-06 00:51:02 +02:00
|
|
|
*/
|
|
|
|
public function addFieldsToTab($tabName, $fields, $insertBefore = null) {
|
|
|
|
$this->flushFieldsCache();
|
|
|
|
|
|
|
|
// Find the tab
|
|
|
|
$tab = $this->findOrMakeTab($tabName);
|
|
|
|
|
|
|
|
// Add the fields to the end of this set
|
|
|
|
foreach($fields as $field) {
|
|
|
|
// Check if a field by the same name exists in this tab
|
|
|
|
if($insertBefore) {
|
2014-01-07 17:02:43 +01:00
|
|
|
$tab->insertBefore($insertBefore, $field);
|
2013-10-17 04:38:22 +02:00
|
|
|
} elseif(($name = $field->getName()) && $tab->fieldByName($name)) {
|
2011-05-06 00:51:02 +02:00
|
|
|
// It exists, so we need to replace the old one
|
2011-10-29 06:01:52 +02:00
|
|
|
$this->replaceField($field->getName(), $field);
|
2011-05-06 00:51:02 +02:00
|
|
|
} else {
|
|
|
|
$tab->push($field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the given field from the given tab in the field.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-05-06 00:51:02 +02:00
|
|
|
* @param string $tabName The name of the tab
|
|
|
|
* @param string $fieldName The name of the field
|
|
|
|
*/
|
|
|
|
public function removeFieldFromTab($tabName, $fieldName) {
|
|
|
|
$this->flushFieldsCache();
|
|
|
|
|
|
|
|
// Find the tab
|
|
|
|
$tab = $this->findOrMakeTab($tabName);
|
|
|
|
$tab->removeByName($fieldName);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
/**
|
2011-10-28 03:37:27 +02:00
|
|
|
* Removes a number of fields from a Tab/TabSet within this FieldList.
|
2011-05-06 00:51:02 +02:00
|
|
|
*
|
|
|
|
* @param string $tabName The name of the Tab or TabSet field
|
|
|
|
* @param array $fields A list of fields, e.g. array('Name', 'Email')
|
|
|
|
*/
|
|
|
|
public function removeFieldsFromTab($tabName, $fields) {
|
|
|
|
$this->flushFieldsCache();
|
|
|
|
|
|
|
|
// Find the tab
|
|
|
|
$tab = $this->findOrMakeTab($tabName);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
// Add the fields to the end of this set
|
|
|
|
foreach($fields as $field) $tab->removeByName($field);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
/**
|
2013-05-08 18:14:21 +02:00
|
|
|
* Remove a field or fields from this FieldList by Name.
|
2011-05-06 00:51:02 +02:00
|
|
|
* The field could also be inside a CompositeField.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-08 18:14:21 +02:00
|
|
|
* @param string|array $fieldName The name of, or an array with the field(s) or tab(s)
|
2011-05-06 00:51:02 +02:00
|
|
|
* @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) {
|
|
|
|
if(!$fieldName) {
|
2011-10-28 03:37:27 +02:00
|
|
|
user_error('FieldList::removeByName() was called with a blank field name.', E_USER_WARNING);
|
2011-05-06 00:51:02 +02:00
|
|
|
}
|
2013-05-08 18:14:21 +02:00
|
|
|
|
|
|
|
// Handle array syntax
|
|
|
|
if(is_array($fieldName)) {
|
|
|
|
foreach($fieldName as $field){
|
|
|
|
$this->removeByName($field, $dataFieldOnly);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
$this->flushFieldsCache();
|
2016-08-19 00:51:35 +02:00
|
|
|
foreach($this as $i => $child) {
|
|
|
|
$childName = $child->getName();
|
|
|
|
if (!$childName) {
|
|
|
|
$childName = $child->Title();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(($childName == $fieldName) && (!$dataFieldOnly || $child->hasData())) {
|
|
|
|
array_splice( $this->items, $i, 1 );
|
|
|
|
break;
|
|
|
|
} else if($child instanceof CompositeField) {
|
|
|
|
$child->removeByName($fieldName, $dataFieldOnly);
|
2011-05-06 00:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
/**
|
|
|
|
* Replace a single field with another. Ignores dataless fields such as Tabs and TabSets
|
|
|
|
*
|
|
|
|
* @param string $fieldName The name of the field to replace
|
|
|
|
* @param FormField $newField The field object to replace with
|
|
|
|
* @return boolean TRUE field was successfully replaced
|
|
|
|
* FALSE field wasn't found, nothing changed
|
|
|
|
*/
|
|
|
|
public function replaceField($fieldName, $newField) {
|
|
|
|
$this->flushFieldsCache();
|
2016-08-19 00:51:35 +02:00
|
|
|
foreach($this as $i => $field) {
|
|
|
|
if($field->getName() == $fieldName && $field->hasData()) {
|
|
|
|
$this->items[$i] = $newField;
|
|
|
|
return true;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
} elseif($field instanceof CompositeField) {
|
|
|
|
if($field->replaceField($fieldName, $newField)) {
|
|
|
|
return true;
|
2011-05-06 00:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
/**
|
|
|
|
* Rename the title of a particular field name in this set.
|
|
|
|
*
|
|
|
|
* @param string $fieldName Name of field to rename title of
|
|
|
|
* @param string $newFieldTitle New title of field
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function renameField($fieldName, $newFieldTitle) {
|
2011-05-06 00:51:02 +02:00
|
|
|
$field = $this->dataFieldByName($fieldName);
|
|
|
|
if(!$field) return false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
$field->setTitle($newFieldTitle);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
return $field->Title() == $newFieldTitle;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function hasTabSet() {
|
|
|
|
foreach($this->items as $i => $field) {
|
|
|
|
if(is_object($field) && $field instanceof TabSet) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
/**
|
|
|
|
* Returns the specified tab object, creating it if necessary.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-05-06 00:51:02 +02:00
|
|
|
* @todo Support recursive creation of TabSets
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-05-06 00:51:02 +02:00
|
|
|
* @param string $tabName The tab to return, in the form "Tab.Subtab.Subsubtab".
|
|
|
|
* Caution: Does not recursively create TabSet instances, you need to make sure everything
|
|
|
|
* up until the last tab in the chain exists.
|
|
|
|
* @param string $title Natural language title of the tab. If {@link $tabName} is passed in dot notation,
|
|
|
|
* the title parameter will only apply to the innermost referenced tab.
|
|
|
|
* The title is only changed if the tab doesn't exist already.
|
|
|
|
* @return Tab The found or newly created Tab instance
|
|
|
|
*/
|
|
|
|
public function findOrMakeTab($tabName, $title = null) {
|
|
|
|
$parts = explode('.',$tabName);
|
2012-03-09 02:33:18 +01:00
|
|
|
$last_idx = count($parts) - 1;
|
2012-09-26 23:34:00 +02:00
|
|
|
// We could have made this recursive, but I've chosen to keep all the logic code within FieldList rather than
|
|
|
|
// add it to TabSet and Tab too.
|
2011-05-06 00:51:02 +02:00
|
|
|
$currentPointer = $this;
|
|
|
|
foreach($parts as $k => $part) {
|
|
|
|
$parentPointer = $currentPointer;
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @var FormField $currentPointer */
|
2011-05-06 00:51:02 +02:00
|
|
|
$currentPointer = $currentPointer->fieldByName($part);
|
|
|
|
// Create any missing tabs
|
|
|
|
if(!$currentPointer) {
|
2016-08-19 00:51:35 +02:00
|
|
|
if ($parentPointer instanceof TabSet) {
|
2011-05-06 00:51:02 +02:00
|
|
|
// use $title on the innermost tab only
|
2012-03-09 02:33:18 +01:00
|
|
|
if ($k == $last_idx) {
|
2012-03-09 20:14:06 +01:00
|
|
|
$currentPointer = isset($title) ? new Tab($part, $title) : new Tab($part);
|
2012-03-09 02:33:18 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$currentPointer = new TabSet($part);
|
2011-05-06 00:51:02 +02:00
|
|
|
}
|
|
|
|
$parentPointer->push($currentPointer);
|
2012-03-09 02:33:18 +01:00
|
|
|
}
|
|
|
|
else {
|
2016-08-19 00:51:35 +02:00
|
|
|
$withName = $parentPointer instanceof FormField
|
|
|
|
? " named '{$parentPointer->getName()}'"
|
|
|
|
: null;
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error("FieldList::addFieldToTab() Tried to add a tab to object"
|
|
|
|
. " '{$parentPointer->class}'{$withName} - '$part' didn't exist.", E_USER_ERROR);
|
2011-05-06 00:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
return $currentPointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a named field.
|
|
|
|
* You can use dot syntax to get fields from child composite fields
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-10-07 01:37:48 +02:00
|
|
|
* @todo Implement similarly to dataFieldByName() to support nested sets - or merge with dataFields()
|
2016-01-26 04:56:07 +01:00
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return FormField
|
2011-05-06 00:51:02 +02:00
|
|
|
*/
|
|
|
|
public function fieldByName($name) {
|
2016-08-19 00:51:35 +02:00
|
|
|
if(strpos($name,'.') !== false) {
|
|
|
|
list($name, $remainder) = explode('.',$name,2);
|
|
|
|
} else {
|
|
|
|
$remainder = null;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
foreach($this as $child) {
|
2011-10-29 06:01:52 +02:00
|
|
|
if(trim($name) == trim($child->getName()) || $name == $child->id) {
|
2011-05-06 00:51:02 +02:00
|
|
|
if($remainder) {
|
2016-08-19 00:51:35 +02:00
|
|
|
if($child instanceof CompositeField) {
|
2011-05-06 00:51:02 +02:00
|
|
|
return $child->fieldByName($remainder);
|
|
|
|
} else {
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error("Trying to get field '$remainder' from non-composite field $child->class.$name",
|
|
|
|
E_USER_WARNING);
|
2011-05-06 00:51:02 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return $child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
return null;
|
2011-05-06 00:51:02 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
/**
|
|
|
|
* Returns a named field in a sequential set.
|
|
|
|
* Use this if you're using nested FormFields.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-05-06 00:51:02 +02:00
|
|
|
* @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) {
|
2016-08-19 00:51:35 +02:00
|
|
|
if(trim($name) == trim($child->getName()) || $name == $child->id) {
|
|
|
|
return $child;
|
|
|
|
}
|
2011-05-06 00:51:02 +02:00
|
|
|
}
|
2012-05-14 04:42:54 +02:00
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
return null;
|
2011-05-06 00:51:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-28 03:37:27 +02:00
|
|
|
* Inserts a field before a particular field in a FieldList.
|
2011-05-06 00:51:02 +02:00
|
|
|
*
|
|
|
|
* @param string $name Name of the field to insert before
|
2014-01-07 17:02:43 +01:00
|
|
|
* @param FormField $item The form field to insert
|
2015-10-07 01:37:48 +02:00
|
|
|
* @return FormField|false
|
2011-05-06 00:51:02 +02:00
|
|
|
*/
|
2014-01-07 17:02:43 +01:00
|
|
|
public function insertBefore($name, $item) {
|
|
|
|
// Backwards compatibility for order of arguments
|
|
|
|
if($name instanceof FormField) {
|
|
|
|
list($item, $name) = array($name, $item);
|
|
|
|
}
|
2011-05-06 00:51:02 +02:00
|
|
|
$this->onBeforeInsert($item);
|
2012-05-14 04:42:54 +02:00
|
|
|
$item->setContainerFieldList($this);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
$i = 0;
|
2016-08-19 00:51:35 +02:00
|
|
|
foreach($this as $child) {
|
2011-10-29 06:01:52 +02:00
|
|
|
if($name == $child->getName() || $name == $child->id) {
|
2011-05-06 00:51:02 +02:00
|
|
|
array_splice($this->items, $i, 0, array($item));
|
|
|
|
return $item;
|
2016-08-19 00:51:35 +02:00
|
|
|
} elseif($child instanceof CompositeField) {
|
2014-01-07 17:02:43 +01:00
|
|
|
$ret = $child->insertBefore($name, $item);
|
2011-05-06 00:51:02 +02:00
|
|
|
if($ret) return $ret;
|
|
|
|
}
|
|
|
|
$i++;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-28 03:37:27 +02:00
|
|
|
* Inserts a field after a particular field in a FieldList.
|
2011-05-06 00:51:02 +02:00
|
|
|
*
|
|
|
|
* @param string $name Name of the field to insert after
|
2014-01-07 17:02:43 +01:00
|
|
|
* @param FormField $item The form field to insert
|
2015-10-07 01:37:48 +02:00
|
|
|
* @return FormField|false
|
2011-05-06 00:51:02 +02:00
|
|
|
*/
|
2014-01-07 17:02:43 +01:00
|
|
|
public function insertAfter($name, $item) {
|
|
|
|
// Backwards compatibility for order of arguments
|
|
|
|
if($name instanceof FormField) {
|
|
|
|
list($item, $name) = array($name, $item);
|
|
|
|
}
|
2011-05-06 00:51:02 +02:00
|
|
|
$this->onBeforeInsert($item);
|
2012-05-14 04:42:54 +02:00
|
|
|
$item->setContainerFieldList($this);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
$i = 0;
|
2016-08-19 00:51:35 +02:00
|
|
|
foreach($this as $child) {
|
2011-10-29 06:01:52 +02:00
|
|
|
if($name == $child->getName() || $name == $child->id) {
|
2011-05-06 00:51:02 +02:00
|
|
|
array_splice($this->items, $i+1, 0, array($item));
|
|
|
|
return $item;
|
2016-08-19 00:51:35 +02:00
|
|
|
} elseif($child instanceof CompositeField) {
|
2014-01-07 17:02:43 +01:00
|
|
|
$ret = $child->insertAfter($name, $item);
|
2011-05-06 00:51:02 +02:00
|
|
|
if($ret) return $ret;
|
|
|
|
}
|
|
|
|
$i++;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
/**
|
2015-12-14 17:18:57 +01:00
|
|
|
* Push a single field onto the end of this FieldList instance.
|
2011-05-06 00:51:02 +02:00
|
|
|
*
|
|
|
|
* @param FormField $item The FormField to add
|
|
|
|
*/
|
2015-12-14 17:18:57 +01:00
|
|
|
public function push($item) {
|
2011-05-06 00:51:02 +02:00
|
|
|
$this->onBeforeInsert($item);
|
2012-05-14 04:42:54 +02:00
|
|
|
$item->setContainerFieldList($this);
|
2013-04-06 08:16:59 +02:00
|
|
|
|
2015-12-14 17:18:57 +01:00
|
|
|
return parent::push($item);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Push a single field onto the beginning of this FieldList instance.
|
|
|
|
*
|
|
|
|
* @param FormField $item The FormField to add
|
|
|
|
*/
|
|
|
|
public function unshift($item) {
|
|
|
|
$this->onBeforeInsert($item);
|
|
|
|
$item->setContainerFieldList($this);
|
|
|
|
|
|
|
|
return parent::unshift($item);
|
2011-05-06 00:51:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-28 03:37:27 +02:00
|
|
|
* Handler method called before the FieldList is going to be manipulated.
|
2016-08-19 00:51:35 +02:00
|
|
|
*
|
|
|
|
* @param FormField $item
|
2011-05-06 00:51:02 +02:00
|
|
|
*/
|
|
|
|
protected function onBeforeInsert($item) {
|
|
|
|
$this->flushFieldsCache();
|
2013-04-06 08:16:59 +02:00
|
|
|
|
|
|
|
if($item->getName()) {
|
|
|
|
$this->rootFieldList()->removeByName($item->getName(), true);
|
|
|
|
}
|
2011-05-06 00:51:02 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
/**
|
2011-10-28 03:37:27 +02:00
|
|
|
* Set the Form instance for this FieldList.
|
2011-05-06 00:51:02 +02:00
|
|
|
*
|
2011-10-28 03:37:27 +02:00
|
|
|
* @param Form $form The form to set this FieldList to
|
2016-08-19 00:51:35 +02:00
|
|
|
* @return $this
|
2011-05-06 00:51:02 +02:00
|
|
|
*/
|
|
|
|
public function setForm($form) {
|
2013-04-06 08:16:59 +02:00
|
|
|
foreach($this as $field) {
|
|
|
|
$field->setForm($form);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2011-05-06 00:51:02 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
/**
|
|
|
|
* Load the given data into this form.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param array $data An map of data to load into the FieldList
|
|
|
|
* @return $this
|
2011-05-06 00:51:02 +02:00
|
|
|
*/
|
|
|
|
public function setValues($data) {
|
|
|
|
foreach($this->dataFields() as $field) {
|
2011-10-29 06:01:52 +02:00
|
|
|
$fieldName = $field->getName();
|
2016-08-19 00:51:35 +02:00
|
|
|
if(isset($data[$fieldName])) {
|
|
|
|
$field->setValue($data[$fieldName]);
|
|
|
|
}
|
2011-05-06 00:51:02 +02:00
|
|
|
}
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2011-05-06 00:51:02 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
/**
|
|
|
|
* Return all <input type="hidden"> fields
|
|
|
|
* in a form - including fields nested in {@link CompositeFields}.
|
|
|
|
* Useful when doing custom field layouts.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-10-28 03:37:27 +02:00
|
|
|
* @return FieldList
|
2011-05-06 00:51:02 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function HiddenFields() {
|
2011-11-30 16:51:09 +01:00
|
|
|
$hiddenFields = new FieldList();
|
2011-05-06 00:51:02 +02:00
|
|
|
$dataFields = $this->dataFields();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
if($dataFields) foreach($dataFields as $field) {
|
|
|
|
if($field instanceof HiddenField) $hiddenFields->push($field);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
return $hiddenFields;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-07 21:54:31 +01:00
|
|
|
/**
|
|
|
|
* Return all fields except for the hidden fields.
|
|
|
|
* Useful when making your own simplified form layouts.
|
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function VisibleFields() {
|
2012-03-07 21:54:31 +01:00
|
|
|
$visibleFields = new FieldList();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-07 21:54:31 +01:00
|
|
|
foreach($this as $field) {
|
|
|
|
if(!($field instanceof HiddenField)) $visibleFields->push($field);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-07 21:54:31 +01:00
|
|
|
return $visibleFields;
|
|
|
|
}
|
2011-05-06 00:51:02 +02:00
|
|
|
|
|
|
|
/**
|
2011-10-28 03:37:27 +02:00
|
|
|
* Transform this FieldList with a given tranform method,
|
2011-05-06 00:51:02 +02:00
|
|
|
* e.g. $this->transform(new ReadonlyTransformation())
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param FormTransformation $trans
|
2011-10-28 03:37:27 +02:00
|
|
|
* @return FieldList
|
2011-05-06 00:51:02 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function transform($trans) {
|
2011-05-06 00:51:02 +02:00
|
|
|
$this->flushFieldsCache();
|
2011-05-11 09:51:54 +02:00
|
|
|
$newFields = new FieldList();
|
2011-05-06 00:51:02 +02:00
|
|
|
foreach($this as $field) {
|
|
|
|
$newFields->push($field->transform($trans));
|
|
|
|
}
|
|
|
|
return $newFields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the root field set that this belongs to
|
2016-08-19 00:51:35 +02:00
|
|
|
*
|
|
|
|
* @return FieldList|FormField
|
2011-05-06 00:51:02 +02:00
|
|
|
*/
|
2012-05-14 04:42:54 +02:00
|
|
|
public function rootFieldList() {
|
2016-08-19 00:51:35 +02:00
|
|
|
if($this->containerField) {
|
|
|
|
return $this->containerField->rootFieldList();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
2011-05-06 00:51:02 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
/**
|
|
|
|
* @param $field
|
|
|
|
* @return $this
|
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function setContainerField($field) {
|
2011-05-06 00:51:02 +02:00
|
|
|
$this->containerField = $field;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2011-05-06 00:51:02 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
/**
|
2011-10-28 03:37:27 +02:00
|
|
|
* Transforms this FieldList instance to readonly.
|
2011-05-06 00:51:02 +02:00
|
|
|
*
|
2011-10-28 03:37:27 +02:00
|
|
|
* @return FieldList
|
2011-05-06 00:51:02 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function makeReadonly() {
|
2011-05-06 00:51:02 +02:00
|
|
|
return $this->transform(new ReadonlyTransformation());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transform the named field into a readonly feld.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-05-06 00:51:02 +02:00
|
|
|
* @param string|FormField
|
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function makeFieldReadonly($field) {
|
2011-10-29 06:01:52 +02:00
|
|
|
$fieldName = ($field instanceof FormField) ? $field->getName() : $field;
|
2011-05-06 00:51:02 +02:00
|
|
|
$srcField = $this->dataFieldByName($fieldName);
|
|
|
|
$this->replaceField($fieldName, $srcField->performReadonlyTransformation());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
/**
|
2011-10-28 03:37:27 +02:00
|
|
|
* Change the order of fields in this FieldList by specifying an ordered list of field names.
|
2011-05-06 00:51:02 +02:00
|
|
|
* This works well in conjunction with SilverStripe's scaffolding functions: take the scaffold, and
|
|
|
|
* shuffle the fields around to the order that you want.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-05-06 00:51:02 +02:00
|
|
|
* Please note that any tabs or other dataless fields will be clobbered by this operation.
|
|
|
|
*
|
|
|
|
* @param array $fieldNames Field names can be given as an array, or just as a list of arguments.
|
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function changeFieldOrder($fieldNames) {
|
2011-05-06 00:51:02 +02:00
|
|
|
// Field names can be given as an array, or just as a list of arguments.
|
|
|
|
if(!is_array($fieldNames)) $fieldNames = func_get_args();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
// Build a map of fields indexed by their name. This will make the 2nd step much easier.
|
|
|
|
$fieldMap = array();
|
2011-10-29 06:01:52 +02:00
|
|
|
foreach($this->dataFields() as $field) $fieldMap[$field->getName()] = $field;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
// 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]);
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
// Add the leftover fields to the end of the list.
|
2015-05-17 00:49:11 +02:00
|
|
|
$fields = array_values($fields + $fieldMap);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
// Update our internal $this->items parameter.
|
|
|
|
$this->items = $fields;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
$this->flushFieldsCache();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
/**
|
|
|
|
* Find the numerical position of a field within
|
|
|
|
* the children collection. Doesn't work recursively.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-05-06 00:51:02 +02:00
|
|
|
* @param string|FormField
|
2016-08-19 00:51:35 +02:00
|
|
|
* @return int Position in children collection (first position starts with 0).
|
|
|
|
* Returns FALSE if the field can't be found.
|
2011-05-06 00:51:02 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function fieldPosition($field) {
|
2016-08-19 00:51:35 +02:00
|
|
|
if($field instanceof FormField) {
|
|
|
|
$field = $field->getName();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
$i = 0;
|
|
|
|
foreach($this->dataFields() as $child) {
|
2016-08-19 00:51:35 +02:00
|
|
|
if($child->getName() == $field) {
|
|
|
|
return $i;
|
|
|
|
}
|
2011-05-06 00:51:02 +02:00
|
|
|
$i++;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-07 22:00:54 +01:00
|
|
|
/**
|
|
|
|
* Default template rendering of a FieldList will concatenate all FieldHolder values.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function forTemplate() {
|
2011-05-06 00:51:02 +02:00
|
|
|
$output = "";
|
|
|
|
foreach($this as $field) {
|
2012-03-07 22:00:54 +01:00
|
|
|
$output .= $field->FieldHolder();
|
2011-05-06 00:51:02 +02:00
|
|
|
}
|
|
|
|
return $output;
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|