2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Lets you include a nested group of fields inside a template.
|
|
|
|
* This control gives you more flexibility over form layout.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-06-27 09:00:01 +02:00
|
|
|
* Note: the child fields within a field group aren't rendered using FieldHolder(). Instead,
|
2007-07-19 12:40:28 +02:00
|
|
|
* SmallFieldHolder() is called, which just prefixes $Field with a <label> tag, if the Title is set.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* <b>Usage</b>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* <code>
|
2013-07-26 16:01:24 +02:00
|
|
|
* FieldGroup::create(
|
|
|
|
* FieldGroup::create(
|
|
|
|
* HeaderField::create('FieldGroup 1'),
|
|
|
|
* TextField::create('Firstname')
|
2010-10-15 05:55:22 +02:00
|
|
|
* ),
|
2013-07-26 16:01:24 +02:00
|
|
|
* FieldGroup::create(
|
|
|
|
* HeaderField::create('FieldGroup 2'),
|
|
|
|
* TextField::create('Surname')
|
2010-10-15 05:55:22 +02:00
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* <b>Adding to existing FieldGroup instances</b>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* <code>
|
|
|
|
* function getCMSFields() {
|
|
|
|
* $fields = parent::getCMSFields();
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* $fields->addFieldToTab(
|
2014-08-15 08:53:05 +02:00
|
|
|
* 'Root.Main',
|
2013-07-26 16:01:24 +02:00
|
|
|
* FieldGroup::create(
|
|
|
|
* TimeField::create("StartTime","What's the start time?"),
|
|
|
|
* TimeField::create("EndTime","What's the end time?")
|
2010-10-15 05:55:22 +02:00
|
|
|
* ),
|
|
|
|
* 'Content'
|
2014-08-15 08:53:05 +02:00
|
|
|
* );
|
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* return $fields;
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* }
|
|
|
|
* </code>
|
2013-07-26 16:01:24 +02:00
|
|
|
*
|
|
|
|
* <b>Setting a title to a FieldGroup</b>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-07-26 16:01:24 +02:00
|
|
|
* <code>
|
|
|
|
* $fields->addFieldToTab("Root.Main",
|
|
|
|
* FieldGroup::create(
|
|
|
|
* TimeField::create('StartTime','What's the start time?'),
|
|
|
|
* TimeField::create('EndTime', 'What's the end time?')
|
|
|
|
* )->setTitle('Time')
|
|
|
|
* );
|
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-structural
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class FieldGroup extends CompositeField {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $zebra;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-06 06:34:09 +02:00
|
|
|
/**
|
|
|
|
* Create a new field group.
|
|
|
|
*
|
|
|
|
* Accepts any number of arguments.
|
|
|
|
*
|
|
|
|
* @param mixed $titleOrField Either the field title, list of fields, or first field
|
|
|
|
* @param mixed ...$otherFields Subsequent fields or field list (if passing in title to $titleOrField)
|
|
|
|
*/
|
|
|
|
public function __construct($titleOrField = null, $otherFields = null) {
|
|
|
|
$title = null;
|
|
|
|
if(is_array($titleOrField) || $titleOrField instanceof FieldList) {
|
|
|
|
$fields = $titleOrField;
|
|
|
|
|
|
|
|
// This would be discarded otherwise
|
|
|
|
if($otherFields) {
|
|
|
|
throw new InvalidArgumentException(
|
|
|
|
'$otherFields is not accepted if passing in field list to $titleOrField'
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-06 06:34:09 +02:00
|
|
|
} else if(is_array($otherFields) || $otherFields instanceof FieldList) {
|
|
|
|
$title = $titleOrField;
|
|
|
|
$fields = $otherFields;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
|
|
|
$fields = func_get_args();
|
2016-07-06 06:34:09 +02:00
|
|
|
if(!is_object(reset($fields))) {
|
|
|
|
$title = array_shift($fields);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
parent::__construct($fields);
|
2016-07-06 06:34:09 +02:00
|
|
|
|
|
|
|
if($title) {
|
|
|
|
$this->setTitle($title);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Returns the name (ID) for the element.
|
2014-08-15 08:53:05 +02:00
|
|
|
* In some cases the FieldGroup doesn't have a title, but we still want
|
2007-07-19 12:40:28 +02:00
|
|
|
* the ID / name to be set. This code, generates the ID from the nested children
|
|
|
|
*/
|
2016-07-06 06:34:09 +02:00
|
|
|
public function getName(){
|
2016-07-25 07:24:26 +02:00
|
|
|
if($this->name) {
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!$this->title) {
|
2011-10-29 06:09:12 +02:00
|
|
|
$fs = $this->FieldList();
|
2007-07-19 12:40:28 +02:00
|
|
|
$compositeTitle = '';
|
|
|
|
$count = 0;
|
|
|
|
foreach($fs as $subfield){
|
2016-07-06 06:34:09 +02:00
|
|
|
/** @var FormField $subfield */
|
2011-10-29 13:07:40 +02:00
|
|
|
$compositeTitle .= $subfield->getName();
|
|
|
|
if($subfield->getName()) $count++;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-06-23 01:37:22 +02:00
|
|
|
/** @skipUpgrade */
|
2007-07-19 12:40:28 +02:00
|
|
|
if($count == 1) $compositeTitle .= 'Group';
|
2012-02-27 22:14:02 +01:00
|
|
|
return preg_replace("/[^a-zA-Z0-9]+/", "", $compositeTitle);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-02-27 22:14:02 +01:00
|
|
|
return preg_replace("/[^a-zA-Z0-9]+/", "", $this->title);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-02-27 22:14:02 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Set an odd/even class
|
2012-04-13 23:47:17 +02:00
|
|
|
*
|
|
|
|
* @param string $zebra one of odd or even.
|
2016-07-06 06:34:09 +02:00
|
|
|
* @return $this
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-12-08 12:20:20 +01:00
|
|
|
public function setZebra($zebra) {
|
|
|
|
if($zebra == 'odd' || $zebra == 'even') $this->zebra = $zebra;
|
|
|
|
else user_error("setZebra passed '$zebra'. It should be passed 'odd' or 'even'", E_USER_WARNING);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-04-13 23:47:17 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getZebra() {
|
2012-04-13 23:47:17 +02:00
|
|
|
return $this->zebra;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-13 23:47:17 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Message() {
|
2015-11-17 17:34:17 +01:00
|
|
|
$fs = array();
|
|
|
|
$this->collateDataFields($fs);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($fs as $subfield) {
|
2013-02-27 18:01:48 +01:00
|
|
|
if($m = $subfield->Message()) $message[] = rtrim($m, ".");
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-02-27 18:01:48 +01:00
|
|
|
return (isset($message)) ? implode(", ", $message) . "." : "";
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
|
|
|
|
2012-04-13 23:47:17 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function MessageType() {
|
2015-11-17 17:34:17 +01:00
|
|
|
$fs = array();
|
|
|
|
$this->collateDataFields($fs);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($fs as $subfield) {
|
|
|
|
if($m = $subfield->MessageType()) $MessageType[] = $m;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-13 23:47:17 +02:00
|
|
|
return (isset($MessageType)) ? implode(". ", $MessageType) : "";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-12-08 12:20:20 +01:00
|
|
|
}
|