2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\Forms;
|
|
|
|
|
|
|
|
use SilverStripe\Dev\Debug;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Base class for all fields that contain other fields.
|
2012-04-14 07:32:29 +02:00
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* Implements sequentialisation - so that when we're saving / loading data, we
|
|
|
|
* can populate a tabbed form properly. All of the children are stored in
|
2012-04-14 07:32:29 +02:00
|
|
|
* $this->children
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class CompositeField extends FormField {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-03 17:56:14 +02:00
|
|
|
/**
|
2011-05-11 09:51:54 +02:00
|
|
|
* @var FieldList
|
2008-10-03 17:56:14 +02:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $children;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Set to true when this field is a readonly field
|
|
|
|
*/
|
|
|
|
protected $readonly;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* @var $columnCount int Toggle different css-rendering for multiple columns
|
2007-07-19 12:40:28 +02:00
|
|
|
* ("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.
|
2014-08-15 08:53:05 +02:00
|
|
|
* Caution: Please make sure that this variable actually matches the
|
2007-07-19 12:40:28 +02:00
|
|
|
* count of your $children.
|
|
|
|
*/
|
|
|
|
protected $columnCount = null;
|
2012-02-16 15:44:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var String custom HTML tag to render with, e.g. to produce a <fieldset>.
|
|
|
|
*/
|
|
|
|
protected $tag = 'div';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-02-16 16:40:19 +01:00
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
|
2016-03-21 11:12:23 +01:00
|
|
|
protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_STRUCTURAL;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
public function __construct($children = null) {
|
2011-05-11 09:51:54 +02:00
|
|
|
if($children instanceof FieldList) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->children = $children;
|
|
|
|
} elseif(is_array($children)) {
|
2014-08-15 08:53:05 +02:00
|
|
|
$this->children = new FieldList($children);
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
2015-02-06 11:58:01 +01:00
|
|
|
//filter out null/empty items
|
|
|
|
$children = array_filter(func_get_args());
|
2014-08-15 08:53:05 +02:00
|
|
|
$this->children = new FieldList($children);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-09-11 02:02:49 +02:00
|
|
|
$this->children->setContainerField($this);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-06 06:34:09 +02:00
|
|
|
parent::__construct(null, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merge child field data into this form
|
|
|
|
*/
|
|
|
|
public function getSchemaDataDefaults() {
|
|
|
|
$defaults = parent::getSchemaDataDefaults();
|
|
|
|
$children = $this->getChildren();
|
|
|
|
if($children && $children->count()) {
|
|
|
|
$childSchema = [];
|
|
|
|
/** @var FormField $child */
|
|
|
|
foreach($children as $child) {
|
|
|
|
$childSchema[] = $child->getSchemaData();
|
|
|
|
}
|
|
|
|
$defaults['children'] = $childSchema;
|
|
|
|
}
|
|
|
|
return $defaults;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-03-19 11:43:23 +01:00
|
|
|
* Returns all the sub-fields, suitable for <% loop FieldList %>
|
2012-04-13 23:47:17 +02:00
|
|
|
*
|
|
|
|
* @return FieldList
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2011-05-11 09:51:54 +02:00
|
|
|
public function FieldList() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->children;
|
|
|
|
}
|
2011-05-11 09:51:54 +02:00
|
|
|
|
2008-08-20 06:41:55 +02:00
|
|
|
/**
|
|
|
|
* Accessor method for $this->children
|
2012-04-13 23:47:17 +02:00
|
|
|
*
|
2011-05-11 09:51:54 +02:00
|
|
|
* @return FieldList
|
2008-08-20 06:41:55 +02:00
|
|
|
*/
|
|
|
|
public function getChildren() {
|
|
|
|
return $this->children;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
/**
|
2011-05-11 09:51:54 +02:00
|
|
|
* @param FieldList $children
|
2016-07-06 06:34:09 +02:00
|
|
|
* @return $this
|
2008-12-04 23:38:32 +01:00
|
|
|
*/
|
|
|
|
public function setChildren($children) {
|
|
|
|
$this->children = $children;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2008-12-04 23:38:32 +01:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
/**
|
2016-07-06 06:34:09 +02:00
|
|
|
* @param string $tag
|
|
|
|
* @return $this
|
2012-04-13 23:47:17 +02:00
|
|
|
*/
|
2012-02-16 15:44:36 +01:00
|
|
|
public function setTag($tag) {
|
|
|
|
$this->tag = $tag;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-02-16 15:44:36 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-04-13 23:47:17 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* @return string
|
2012-04-13 23:47:17 +02:00
|
|
|
*/
|
2012-02-16 15:44:36 +01:00
|
|
|
public function getTag() {
|
|
|
|
return $this->tag;
|
|
|
|
}
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
/**
|
2016-07-06 06:34:09 +02:00
|
|
|
* @param string $legend
|
|
|
|
* @return $this
|
2012-04-13 23:47:17 +02:00
|
|
|
*/
|
2012-02-16 16:40:19 +01:00
|
|
|
public function setLegend($legend) {
|
|
|
|
$this->legend = $legend;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-04-13 23:47:17 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* @return string
|
2012-04-13 23:47:17 +02:00
|
|
|
*/
|
2012-02-16 16:40:19 +01:00
|
|
|
public function getLegend() {
|
|
|
|
return $this->legend;
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-08-04 13:03:17 +02:00
|
|
|
public function extraClass() {
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @skipUpgrade */
|
2015-08-04 13:03:17 +02:00
|
|
|
$classes = array('field', 'CompositeField', parent::extraClass());
|
2011-12-22 13:10:57 +01:00
|
|
|
if($this->columnCount) $classes[] = 'multicolumn';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-22 13:10:57 +01:00
|
|
|
return implode(' ', $classes);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getAttributes() {
|
2011-12-22 13:10:57 +01:00
|
|
|
return array_merge(
|
|
|
|
parent::getAttributes(),
|
2012-02-16 16:40:19 +01:00
|
|
|
array(
|
2014-08-15 08:53:05 +02:00
|
|
|
'tabindex' => null,
|
|
|
|
'type' => null,
|
|
|
|
'value' => null,
|
2012-02-16 16:40:19 +01:00
|
|
|
'title' => ($this->tag == 'fieldset') ? null : $this->legend
|
|
|
|
)
|
2011-12-22 13:10:57 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Add all of the non-composite fields contained within this field to the
|
2012-04-13 23:47:17 +02:00
|
|
|
* list.
|
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* Sequentialisation is used when connecting the form to its data source
|
2016-07-06 06:34:09 +02:00
|
|
|
*
|
|
|
|
* @param array $list
|
|
|
|
* @param bool $saveableOnly
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2008-08-12 04:58:48 +02:00
|
|
|
public function collateDataFields(&$list, $saveableOnly = false) {
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($this->children as $field) {
|
2016-07-06 06:34:09 +02:00
|
|
|
if(! $field instanceof FormField) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if($field instanceof CompositeField) {
|
|
|
|
$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);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-07-06 06:34:09 +02:00
|
|
|
$list[$name] = $field;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setForm($form) {
|
2016-07-25 07:24:26 +02:00
|
|
|
foreach($this->getChildren() as $field) {
|
2016-07-06 06:34:09 +02:00
|
|
|
if ($field instanceof FormField) {
|
|
|
|
$field->setForm($form);
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
parent::setForm($form);
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-25 07:24:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
public function setDisabled($disabled) {
|
|
|
|
parent::setDisabled($disabled);
|
|
|
|
foreach($this->getChildren() as $child) {
|
|
|
|
$child->setDisabled($disabled);
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setReadonly($readonly)
|
|
|
|
{
|
|
|
|
parent::setReadonly($readonly);
|
|
|
|
foreach($this->getChildren() as $child) {
|
|
|
|
$child->setReadonly($readonly);
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setColumnCount($columnCount) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->columnCount = $columnCount;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getColumnCount() {
|
2012-04-13 23:47:17 +02:00
|
|
|
return $this->columnCount;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function isComposite() {
|
2014-08-15 08:53:05 +02:00
|
|
|
return true;
|
2012-02-17 13:35:26 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function hasData() {
|
2014-08-15 08:53:05 +02:00
|
|
|
return false;
|
2012-02-17 13:35:26 +01:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2008-01-06 22:55:27 +01:00
|
|
|
public function fieldByName($name) {
|
|
|
|
return $this->children->fieldByName($name);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Add a new child field to the end of the set.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-04-13 23:47:17 +02:00
|
|
|
* @param FormField
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public function push(FormField $field) {
|
|
|
|
$this->children->push($field);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-12-14 17:18:57 +01:00
|
|
|
/**
|
|
|
|
* Add a new child field to the beginning of the set.
|
|
|
|
*
|
|
|
|
* @param FormField
|
|
|
|
*/
|
|
|
|
public function unshift(FormField $field) {
|
|
|
|
$this->children->unshift($field);
|
|
|
|
}
|
|
|
|
|
2008-11-11 03:35:54 +01:00
|
|
|
/**
|
2011-05-11 09:51:54 +02:00
|
|
|
* @uses FieldList->insertBefore()
|
2016-07-06 06:34:09 +02:00
|
|
|
*
|
|
|
|
* @param string $insertBefore
|
|
|
|
* @param FormField $field
|
|
|
|
* @return false|FormField
|
2008-11-11 03:35:54 +01:00
|
|
|
*/
|
2014-01-07 17:02:43 +01:00
|
|
|
public function insertBefore($insertBefore, $field) {
|
2016-07-06 06:34:09 +02:00
|
|
|
return $this->children->insertBefore($insertBefore, $field);
|
2007-09-16 18:05:05 +02:00
|
|
|
}
|
|
|
|
|
2015-10-07 01:37:48 +02:00
|
|
|
/**
|
|
|
|
* @uses FieldList->insertAfter()
|
2016-07-06 06:34:09 +02:00
|
|
|
* @param string $insertAfter
|
|
|
|
* @param FormField $field
|
|
|
|
* @return false|FormField
|
2015-10-07 01:37:48 +02:00
|
|
|
*/
|
2014-01-07 17:02:43 +01:00
|
|
|
public function insertAfter($insertAfter, $field) {
|
2016-07-06 06:34:09 +02:00
|
|
|
return $this->children->insertAfter($insertAfter, $field);
|
2008-11-11 03:35:54 +01:00
|
|
|
}
|
2008-09-12 06:42:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a field from this CompositeField by Name.
|
|
|
|
* The field could also be inside a CompositeField.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-09-12 06:42:24 +02:00
|
|
|
* @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);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function replaceField($fieldName, $newField) {
|
|
|
|
return $this->children->replaceField($fieldName, $newField);
|
|
|
|
}
|
2008-09-11 08:24:40 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function rootFieldList() {
|
2012-05-14 04:42:54 +02:00
|
|
|
if(is_object($this->containerFieldList)) return $this->containerFieldList->rootFieldList();
|
2008-09-11 08:24:40 +02:00
|
|
|
else return $this->children;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-25 07:24:26 +02:00
|
|
|
public function __clone() {
|
|
|
|
/** {@see FieldList::__clone(}} */
|
|
|
|
$this->setChildren(clone $this->children);
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2012-04-12 02:24:06 +02:00
|
|
|
* Return a readonly version of this field. Keeps the composition but returns readonly
|
|
|
|
* versions of all the child {@link FormField} objects.
|
|
|
|
*
|
|
|
|
* @return CompositeField
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public function performReadonlyTransformation() {
|
2011-05-11 09:51:54 +02:00
|
|
|
$newChildren = new FieldList();
|
2008-12-04 23:38:32 +01:00
|
|
|
$clone = clone $this;
|
2016-07-06 06:34:09 +02:00
|
|
|
if($clone->getChildren()) foreach($clone->getChildren() as $child) {
|
|
|
|
/** @var FormField $child */
|
|
|
|
$child = $child->transform(new ReadonlyTransformation());
|
|
|
|
$newChildren->push($child);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2016-07-25 07:24:26 +02:00
|
|
|
$clone->setChildren($newChildren);
|
|
|
|
$clone->setReadonly(true);
|
2012-12-13 13:51:28 +01:00
|
|
|
$clone->addExtraClass($this->extraClass());
|
|
|
|
$clone->setDescription($this->getDescription());
|
2012-04-12 02:24:06 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
return $clone;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-04-12 02:24:06 +02:00
|
|
|
* Return a disabled version of this field. Keeps the composition but returns disabled
|
|
|
|
* versions of all the child {@link FormField} objects.
|
|
|
|
*
|
|
|
|
* @return CompositeField
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-04-12 02:24:06 +02:00
|
|
|
public function performDisabledTransformation() {
|
2011-05-11 09:51:54 +02:00
|
|
|
$newChildren = new FieldList();
|
2008-12-04 23:38:32 +01:00
|
|
|
$clone = clone $this;
|
2016-07-06 06:34:09 +02:00
|
|
|
if($clone->getChildren()) foreach($clone->getChildren() as $child) {
|
|
|
|
/** @var FormField $child */
|
|
|
|
$child = $child->transform(new DisabledTransformation());
|
|
|
|
$newChildren->push($child);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2016-07-25 07:24:26 +02:00
|
|
|
$clone->setChildren($newChildren);
|
|
|
|
$clone->setDisabled(true);
|
2012-12-13 13:51:28 +01:00
|
|
|
$clone->addExtraClass($this->extraClass());
|
|
|
|
$clone->setDescription($this->getDescription());
|
|
|
|
foreach($this->attributes as $k => $v) {
|
|
|
|
$clone->setAttribute($k, $v);
|
|
|
|
}
|
2012-04-12 02:24:06 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
return $clone;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-11-23 02:10:19 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function IsReadonly() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->readonly;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-11 03:35:54 +01:00
|
|
|
/**
|
|
|
|
* Find the numerical position of a field within
|
|
|
|
* the children collection. Doesn't work recursively.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-11-11 03:35:54 +01:00
|
|
|
* @param string|FormField
|
2012-09-26 23:34:00 +02:00
|
|
|
* @return int Position in children collection (first position starts with 0). Returns FALSE if the field can't
|
|
|
|
* be found.
|
2008-11-11 03:35:54 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function fieldPosition($field) {
|
2016-07-06 06:34:09 +02:00
|
|
|
if(is_string($field)) {
|
|
|
|
$field = $this->fieldByName($field);
|
|
|
|
}
|
|
|
|
if(!$field) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-11 03:35:54 +01:00
|
|
|
$i = 0;
|
|
|
|
foreach($this->children as $child) {
|
2016-07-06 06:34:09 +02:00
|
|
|
/** @var FormField $child */
|
|
|
|
if($child->getName() == $field->getName()) {
|
|
|
|
return $i;
|
|
|
|
}
|
2008-11-11 03:35:54 +01:00
|
|
|
$i++;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-11 03:35:54 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
/**
|
|
|
|
* Transform the named field into a readonly feld.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-12-04 23:38:32 +01:00
|
|
|
* @param string|FormField
|
2016-07-06 06:34:09 +02:00
|
|
|
* @return bool
|
2008-12-04 23:38:32 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function makeFieldReadonly($field) {
|
2011-10-29 06:01:52 +02:00
|
|
|
$fieldName = ($field instanceof FormField) ? $field->getName() : $field;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
// Iterate on items, looking for the applicable field
|
|
|
|
foreach($this->children as $i => $item) {
|
2016-07-06 06:34:09 +02:00
|
|
|
if($item instanceof CompositeField) {
|
|
|
|
if($item->makeFieldReadonly($fieldName)) {
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
} elseif($item instanceof FormField && $item->getName() == $fieldName) {
|
2008-12-04 23:38:32 +01:00
|
|
|
// Once it's found, use FormField::transform to turn the field into a readonly version of itself.
|
2016-07-06 06:34:09 +02:00
|
|
|
$this->children->replaceField($fieldName, $item->transform(new ReadonlyTransformation()));
|
2008-12-04 23:38:32 +01:00
|
|
|
|
2016-07-06 06:34:09 +02:00
|
|
|
// A true results indicates that the field was found
|
|
|
|
return true;
|
2008-12-04 23:38:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function debug() {
|
2007-09-14 20:05:21 +02:00
|
|
|
$result = "$this->class ($this->name) <ul>";
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($this->children as $child) {
|
|
|
|
$result .= "<li>" . Debug::text($child) . " </li>";
|
|
|
|
}
|
|
|
|
$result .= "</ul>";
|
|
|
|
return $result;
|
|
|
|
}
|
2012-04-12 02:24:06 +02:00
|
|
|
|
2014-11-12 03:19:12 +01:00
|
|
|
/**
|
|
|
|
* Validate this field
|
|
|
|
*
|
|
|
|
* @param Validator $validator
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-05-11 02:32:00 +02:00
|
|
|
public function validate($validator) {
|
2007-08-23 07:47:54 +02:00
|
|
|
$valid = true;
|
2016-07-06 06:34:09 +02:00
|
|
|
foreach($this->children as $child){
|
|
|
|
/** @var FormField $child */
|
2008-10-08 04:00:12 +02:00
|
|
|
$valid = ($child && $child->validate($validator) && $valid);
|
2007-08-23 07:47:54 +02:00
|
|
|
}
|
|
|
|
return $valid;
|
|
|
|
}
|
2012-04-12 02:24:06 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|