2011-05-06 00:51:02 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* A list designed to hold form field instances.
|
|
|
|
*
|
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-structural
|
|
|
|
*/
|
|
|
|
class FieldList extends ArrayList {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cached flat representation of all fields in this set,
|
|
|
|
* including fields nested in {@link CompositeFields}.
|
|
|
|
*
|
|
|
|
* @uses self::collateDataFields()
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $sequentialSet;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $sequentialSaveableSet;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @todo Documentation
|
|
|
|
*/
|
|
|
|
protected $containerField;
|
2012-05-09 17:18:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array Ordered list of regular expressions,
|
|
|
|
* see {@link setTabPathRewrites()}.
|
|
|
|
*/
|
|
|
|
protected $tabPathRewrites = array();
|
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) {
|
2012-05-14 04:42:54 +02:00
|
|
|
if ($item instanceof FormField) $item->setContainerFieldList($this);
|
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.
|
|
|
|
*/
|
|
|
|
public function dataFields() {
|
|
|
|
if(!$this->sequentialSet) $this->collateDataFields($this->sequentialSet);
|
|
|
|
return $this->sequentialSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function saveableFields() {
|
|
|
|
if(!$this->sequentialSaveableSet) $this->collateDataFields($this->sequentialSaveableSet, true);
|
|
|
|
return $this->sequentialSaveableSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function flushFieldsCache() {
|
|
|
|
$this->sequentialSet = null;
|
|
|
|
$this->sequentialSaveableSet = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function collateDataFields(&$list, $saveableOnly = false) {
|
|
|
|
foreach($this as $field) {
|
|
|
|
if($field->isComposite()) $field->collateDataFields($list, $saveableOnly);
|
|
|
|
|
|
|
|
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])) {
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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()
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
* @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
|
|
|
|
if($insertBefore) $tab->insertBefore($field, $insertBefore);
|
|
|
|
else $tab->push($field);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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()
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
* @param array $fields An array of {@link FormField} objects.
|
|
|
|
*/
|
|
|
|
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) {
|
|
|
|
$tab->insertBefore($field, $insertBefore);
|
2011-10-29 06:01:52 +02:00
|
|
|
} elseif($tab->fieldByName($field->getName())) {
|
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.
|
|
|
|
*
|
|
|
|
* @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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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);
|
|
|
|
|
|
|
|
// Add the fields to the end of this set
|
|
|
|
foreach($fields as $field) $tab->removeByName($field);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-28 03:37:27 +02:00
|
|
|
* Remove a field from this FieldList by Name.
|
2011-05-06 00:51:02 +02:00
|
|
|
* The field could also be inside a CompositeField.
|
|
|
|
*
|
|
|
|
* @param string $fieldName The name of the field or tab
|
|
|
|
* @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
|
|
|
}
|
|
|
|
$this->flushFieldsCache();
|
|
|
|
|
|
|
|
foreach($this->items as $i => $child) {
|
|
|
|
if(is_object($child)){
|
2011-10-29 06:01:52 +02:00
|
|
|
$childName = $child->getName();
|
2011-05-06 00:51:02 +02:00
|
|
|
if(!$childName) $childName = $child->Title();
|
|
|
|
|
|
|
|
if(($childName == $fieldName) && (!$dataFieldOnly || $child->hasData())) {
|
|
|
|
array_splice( $this->items, $i, 1 );
|
|
|
|
break;
|
|
|
|
} else if($child->isComposite()) {
|
|
|
|
$child->removeByName($fieldName, $dataFieldOnly);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
foreach($this->items as $i => $field) {
|
|
|
|
if(is_object($field)) {
|
2011-10-29 06:01:52 +02:00
|
|
|
if($field->getName() == $fieldName && $field->hasData()) {
|
2011-05-06 00:51:02 +02:00
|
|
|
$this->items[$i] = $newField;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} else if($field->isComposite()) {
|
|
|
|
if($field->replaceField($fieldName, $newField)) return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
$field->setTitle($newFieldTitle);
|
|
|
|
|
|
|
|
return $field->Title() == $newFieldTitle;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function hasTabSet() {
|
|
|
|
foreach($this->items as $i => $field) {
|
|
|
|
if(is_object($field) && $field instanceof TabSet) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the specified tab object, creating it if necessary.
|
|
|
|
*
|
|
|
|
* @todo Support recursive creation of TabSets
|
|
|
|
*
|
|
|
|
* @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) {
|
2012-05-09 17:18:16 +02:00
|
|
|
// Backwards compatibility measure: Allow rewriting of outdated tab paths
|
|
|
|
$tabName = $this->rewriteTabPath($tabName);
|
|
|
|
|
2011-05-06 00:51:02 +02:00
|
|
|
$parts = explode('.',$tabName);
|
2012-03-09 02:33:18 +01:00
|
|
|
$last_idx = count($parts) - 1;
|
2011-10-28 03:37:27 +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;
|
|
|
|
$currentPointer = $currentPointer->fieldByName($part);
|
|
|
|
// Create any missing tabs
|
|
|
|
if(!$currentPointer) {
|
|
|
|
if(is_a($parentPointer, 'TabSet')) {
|
|
|
|
// 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 {
|
2011-10-29 06:01:52 +02:00
|
|
|
$withName = ($parentPointer->hasMethod('Name')) ? " named '{$parentPointer->getName()}'" : null;
|
2011-10-28 03:37:27 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $currentPointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a named field.
|
|
|
|
* You can use dot syntax to get fields from child composite fields
|
|
|
|
*
|
|
|
|
* @todo Implement similiarly to dataFieldByName() to support nested sets - or merge with dataFields()
|
|
|
|
*/
|
|
|
|
public function fieldByName($name) {
|
2012-05-09 17:18:16 +02:00
|
|
|
$name = $this->rewriteTabPath($name);
|
2011-05-06 00:51:02 +02:00
|
|
|
if(strpos($name,'.') !== false) list($name, $remainder) = explode('.',$name,2);
|
|
|
|
else $remainder = null;
|
|
|
|
|
|
|
|
foreach($this->items 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) {
|
|
|
|
if($child->isComposite()) {
|
|
|
|
return $child->fieldByName($remainder);
|
|
|
|
} else {
|
|
|
|
user_error("Trying to get field '$remainder' from non-composite field $child->class.$name", E_USER_WARNING);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
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) {
|
2011-10-29 06:01:52 +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
|
|
|
}
|
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 FormField $item The form field to insert
|
|
|
|
* @param string $name Name of the field to insert before
|
|
|
|
*/
|
|
|
|
public function insertBefore($item, $name) {
|
|
|
|
$this->onBeforeInsert($item);
|
2012-05-14 04:42:54 +02:00
|
|
|
$item->setContainerFieldList($this);
|
2011-05-06 00:51:02 +02:00
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
foreach($this->items 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;
|
|
|
|
} elseif($child->isComposite()) {
|
|
|
|
$ret = $child->insertBefore($item, $name);
|
|
|
|
if($ret) return $ret;
|
|
|
|
}
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 FormField $item The form field to insert
|
|
|
|
* @param string $name Name of the field to insert after
|
|
|
|
*/
|
|
|
|
public function insertAfter($item, $name) {
|
|
|
|
$this->onBeforeInsert($item);
|
2012-05-14 04:42:54 +02:00
|
|
|
$item->setContainerFieldList($this);
|
2011-05-06 00:51:02 +02:00
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
foreach($this->items 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;
|
|
|
|
} elseif($child->isComposite()) {
|
|
|
|
$ret = $child->insertAfter($item, $name);
|
|
|
|
if($ret) return $ret;
|
|
|
|
}
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-28 03:37:27 +02:00
|
|
|
* Push a single field into this FieldList instance.
|
2011-05-06 00:51:02 +02:00
|
|
|
*
|
|
|
|
* @param FormField $item The FormField to add
|
|
|
|
* @param string $key An option array key (field name)
|
|
|
|
*/
|
|
|
|
public function push($item, $key = null) {
|
|
|
|
$this->onBeforeInsert($item);
|
2012-05-14 04:42:54 +02:00
|
|
|
$item->setContainerFieldList($this);
|
2011-05-06 00:51:02 +02:00
|
|
|
return parent::push($item, $key = null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-28 03:37:27 +02:00
|
|
|
* Handler method called before the FieldList is going to be manipulated.
|
2011-05-06 00:51:02 +02:00
|
|
|
*/
|
|
|
|
protected function onBeforeInsert($item) {
|
|
|
|
$this->flushFieldsCache();
|
2012-05-14 04:42:54 +02:00
|
|
|
if($item->getName()) $this->rootFieldList()->removeByName($item->getName(), true);
|
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
|
2011-05-06 00:51:02 +02:00
|
|
|
*/
|
|
|
|
public function setForm($form) {
|
|
|
|
foreach($this as $field) $field->setForm($form);
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2011-05-06 00:51:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the given data into this form.
|
|
|
|
*
|
2011-10-28 03:37:27 +02:00
|
|
|
* @param data An map of data to load into the FieldList
|
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();
|
2011-05-06 00:51:02 +02:00
|
|
|
if(isset($data[$fieldName])) $field->setValue($data[$fieldName]);
|
|
|
|
}
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
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.
|
|
|
|
*
|
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();
|
|
|
|
|
|
|
|
if($dataFields) foreach($dataFields as $field) {
|
|
|
|
if($field instanceof HiddenField) $hiddenFields->push($field);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $hiddenFields;
|
|
|
|
}
|
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();
|
|
|
|
|
|
|
|
foreach($this as $field) {
|
|
|
|
if(!($field instanceof HiddenField)) $visibleFields->push($field);
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
*
|
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
|
|
|
|
*/
|
2012-05-14 04:42:54 +02:00
|
|
|
public function rootFieldList() {
|
|
|
|
if($this->containerField) return $this->containerField->rootFieldList();
|
2011-05-06 00:51:02 +02:00
|
|
|
else 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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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.
|
|
|
|
*
|
|
|
|
* @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());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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.
|
|
|
|
*
|
|
|
|
* 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();
|
|
|
|
|
|
|
|
// 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;
|
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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the leftover fields to the end of the list.
|
|
|
|
$fields = $fields + array_values($fieldMap);
|
|
|
|
|
|
|
|
// Update our internal $this->items parameter.
|
|
|
|
$this->items = $fields;
|
|
|
|
|
|
|
|
$this->flushFieldsCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the numerical position of a field within
|
|
|
|
* the children collection. Doesn't work recursively.
|
|
|
|
*
|
|
|
|
* @param string|FormField
|
|
|
|
* @return Position in children collection (first position starts with 0). Returns FALSE if the field can't be found.
|
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function fieldPosition($field) {
|
2011-10-29 06:01:52 +02:00
|
|
|
if(is_object($field)) $field = $field->getName();
|
2011-05-06 00:51:02 +02:00
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
foreach($this->dataFields() as $child) {
|
2011-10-29 06:01:52 +02:00
|
|
|
if($child->getName() == $field) return $i;
|
2011-05-06 00:51:02 +02:00
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-05-09 17:18:16 +02:00
|
|
|
/**
|
|
|
|
* Ordered list of regular expressions
|
|
|
|
* matching a tab path, to their rewrite rule (through preg_replace()).
|
|
|
|
* Mainly used for backwards compatibility.
|
|
|
|
* Ensure that more specific rules are placed earlier in the list,
|
|
|
|
* and that tabs with children are accounted for in the rule sets.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* $fields->setTabPathRewriting(array(
|
|
|
|
* // Rewrite specific innermost tab
|
|
|
|
* '/^Root\.Content\.Main$/' => 'Root.Content',
|
|
|
|
* // Rewrite all innermost tabs
|
|
|
|
* '/^Root\.Content\.([^.]+)$/' => 'Root.\\1',
|
|
|
|
* ));
|
|
|
|
*
|
|
|
|
* @param array $rewrites
|
|
|
|
*/
|
|
|
|
public function setTabPathRewrites($rewrites) {
|
|
|
|
$this->tabPathRewrites = $rewrites;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getTabPathRewrites() {
|
|
|
|
return $this->tabPathRewrites;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Support function for backwards compatibility purposes.
|
|
|
|
* Caution: Volatile API, might be removed in 3.1 or later.
|
|
|
|
*
|
|
|
|
* @param String $tabname Path to a tab, e.g. "Root.Content.Main"
|
|
|
|
* @return String Rewritten path, based on {@link tabPathRewrites}
|
|
|
|
*/
|
|
|
|
protected function rewriteTabPath($name) {
|
|
|
|
$isRunningTest = (class_exists('SapphireTest', false) && SapphireTest::is_running_test());
|
|
|
|
foreach($this->getTabPathRewrites() as $regex => $replace) {
|
|
|
|
if(preg_match($regex, $name)) {
|
|
|
|
$newName = preg_replace($regex, $replace, $name);
|
2012-07-13 11:37:35 +02:00
|
|
|
Deprecation::notice('3.0.0',
|
|
|
|
sprintf(
|
|
|
|
'Using outdated tab path "%s", please use the new location "%s" instead',
|
|
|
|
$name,
|
|
|
|
$newName
|
|
|
|
),
|
|
|
|
Deprecation::SCOPE_GLOBAL
|
|
|
|
);
|
2012-05-09 17:18:16 +02:00
|
|
|
return $newName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No match found, return original path
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|