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
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($arg1 = null, $arg2 = null) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(is_array($arg1) || is_a($arg1, 'FieldSet')) {
|
|
|
|
$fields = $arg1;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-10-28 03:37:27 +02:00
|
|
|
} else if(is_array($arg2) || is_a($arg2, 'FieldList')) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->title = $arg1;
|
|
|
|
$fields = $arg2;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
|
|
|
$fields = func_get_args();
|
|
|
|
if(!is_object(reset($fields))) $this->title = array_shift($fields);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
parent::__construct($fields);
|
|
|
|
}
|
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
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function 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){
|
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.
|
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
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function php($data) {
|
2007-07-19 12:40:28 +02:00
|
|
|
return;
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
2012-12-08 12:20:20 +01:00
|
|
|
}
|