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.
|
|
|
|
*
|
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.
|
2010-10-15 05:55:22 +02:00
|
|
|
*
|
|
|
|
* <b>Usage</b>
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* new FieldGroup(
|
|
|
|
* new FieldGroup(
|
|
|
|
* new HeaderField('FieldGroup 1'),
|
|
|
|
* new TextField('Firstname')
|
|
|
|
* ),
|
|
|
|
* new FieldGroup(
|
|
|
|
* new HeaderField('FieldGroup 2'),
|
|
|
|
* new TextField('Surname')
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* <b>Adding to existing FieldGroup instances</b>
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* function getCMSFields() {
|
|
|
|
* $fields = parent::getCMSFields();
|
|
|
|
*
|
|
|
|
* $fields->addFieldToTab(
|
2011-04-15 06:36:22 +02:00
|
|
|
* 'Root.Content',
|
2010-10-15 05:55:22 +02:00
|
|
|
* new FieldGroup(
|
|
|
|
* new TimeField("StartTime","What's the start time?"),
|
|
|
|
* new TimeField("EndTime","What's the end time?")
|
|
|
|
* ),
|
|
|
|
* 'Content'
|
|
|
|
* );
|
|
|
|
*
|
|
|
|
* return $fields;
|
|
|
|
*
|
|
|
|
* }
|
|
|
|
* </code>
|
|
|
|
*
|
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 {
|
|
|
|
protected $zebra;
|
2007-11-19 02:51:32 +01:00
|
|
|
public $subfieldParam = "SmallFieldHolder";
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
function __construct($arg1 = null, $arg2 = null) {
|
|
|
|
if(is_array($arg1) || is_a($arg1, 'FieldSet')) {
|
|
|
|
$fields = $arg1;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$fields = func_get_args();
|
|
|
|
if(!is_object(reset($fields))) $this->title = array_shift($fields);
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::__construct($fields);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the name (ID) for the element.
|
|
|
|
* In some cases the FieldGroup doesn't have a title, but we still want
|
|
|
|
* the ID / name to be set. This code, generates the ID from the nested children
|
|
|
|
*/
|
|
|
|
function Name(){
|
|
|
|
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
|
|
|
}
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Returns a set of <span class="subfield"> tags, each containing a sub-field.
|
|
|
|
* You can also use <% control FieldSet %>, if you'd like more control over the generated HTML
|
|
|
|
*
|
2008-01-11 02:49:50 +01:00
|
|
|
* @todo Shouldn't use SmallFieldHolder() (very difficult to style),
|
2007-07-19 12:40:28 +02:00
|
|
|
* it is easier to overwrite the <div class="field"> behaviour in a more specific class
|
|
|
|
*/
|
|
|
|
function Field() {
|
2011-10-29 06:09:12 +02:00
|
|
|
$fs = $this->FieldList();
|
2011-07-29 05:10:14 +02:00
|
|
|
$spaceZebra = isset($this->zebra) ? " fieldgroup-$this->zebra" : '';
|
2007-07-19 12:40:28 +02:00
|
|
|
$idAtt = isset($this->id) ? " id=\"{$this->id}\"" : '';
|
2008-08-11 05:10:59 +02:00
|
|
|
$content = "<div class=\"fieldgroup$spaceZebra\"$idAtt>";
|
2012-02-23 22:43:37 +01:00
|
|
|
|
|
|
|
$count = 1;
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($fs as $subfield) {
|
2008-08-11 05:10:59 +02:00
|
|
|
$childZebra = (!isset($childZebra) || $childZebra == "odd") ? "even" : "odd";
|
2011-07-29 05:10:14 +02:00
|
|
|
if($subfield->hasMethod('setZebra')) {
|
|
|
|
$subfield->setZebra($childZebra);
|
|
|
|
}
|
2012-02-23 22:43:37 +01:00
|
|
|
|
|
|
|
//label the first and last fields of each surrounding div
|
|
|
|
if ($count == 1) $firstLast = "first";
|
|
|
|
elseif ($count == count($fs)) $firstLast = "last";
|
|
|
|
else $firstLast = '';
|
|
|
|
|
|
|
|
$content .= "<div class=\"fieldgroup-field $firstLast\">" . $subfield->{$this->subfieldParam}() . "</div>";
|
|
|
|
$count++;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-08-11 05:10:59 +02:00
|
|
|
$content .= "</div>";
|
2011-07-29 05:10:14 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setID($id) {
|
|
|
|
$this->id = Convert::raw2att($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set an odd/even class
|
|
|
|
*/
|
|
|
|
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);
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function FieldHolder() {
|
2009-02-02 00:49:53 +01:00
|
|
|
$Title = $this->XML_val('Title');
|
|
|
|
$Message = $this->XML_val('Message');
|
|
|
|
$MessageType = $this->XML_val('MessageType');
|
|
|
|
$RightTitle = $this->XML_val('RightTitle');
|
|
|
|
$Type = $this->XML_val('Type');
|
|
|
|
$extraClass = $this->XML_val('extraClass');
|
|
|
|
$Name = $this->XML_val('Name');
|
|
|
|
$Field = $this->XML_val('Field');
|
|
|
|
|
|
|
|
$titleBlock = (!empty($Title)) ? "<label class=\"left\">$Title</label>" : "";
|
|
|
|
$messageBlock = (!empty($Message)) ? "<span class=\"message $MessageType\">$Message</span>" : "";
|
|
|
|
$rightTitleBlock = (!empty($RightTitle)) ? "<label class=\"right\">$RightTitle</label>" : "";
|
2011-12-18 16:57:26 +01:00
|
|
|
$id = $Name ? ' id="$Name"' : '';
|
2009-02-02 00:49:53 +01:00
|
|
|
|
|
|
|
return <<<HTML
|
2011-12-18 16:57:26 +01:00
|
|
|
<div$id class="field $Type $extraClass">$titleBlock<div class="middleColumn">$Field</div>$rightTitleBlock$messageBlock</div>
|
2009-02-02 00:49:53 +01:00
|
|
|
HTML;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function Message() {
|
2011-10-29 06:09:12 +02:00
|
|
|
$fs = $this->FieldList();
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($fs as $subfield) {
|
|
|
|
if($m = $subfield->Message()) $message[] = $m;
|
|
|
|
}
|
|
|
|
if(isset($message)) return implode(", ", $message) . ". ";
|
|
|
|
}
|
|
|
|
|
|
|
|
function MessageType(){
|
2011-10-29 06:09:12 +02:00
|
|
|
$fs = $this->FieldList();
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($fs as $subfield) {
|
|
|
|
if($m = $subfield->MessageType()) $MessageType[] = $m;
|
|
|
|
}
|
|
|
|
if(isset($MessageType)) {
|
|
|
|
return implode(". ", $MessageType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This allows fields within this fieldgroup to still allow them to get valuated.
|
|
|
|
*/
|
|
|
|
function jsValidation(){
|
2011-10-29 06:09:12 +02:00
|
|
|
$fs = $this->FieldList();
|
2007-07-19 12:40:28 +02:00
|
|
|
$validationCode = '';
|
|
|
|
|
|
|
|
foreach($fs as $subfield) {
|
|
|
|
if($value = $subfield->jsValidation()) {
|
|
|
|
$validationCode .= $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $validationCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
function php($data){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|