2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2008-01-09 05:18:36 +01:00
|
|
|
* Implements a single tab in a {@link TabSet}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* Here is a simple implementation of a Tab. Obviously, you can include as much fields
|
2010-10-15 05:55:22 +02:00
|
|
|
* inside as you want. A tab can contain other tabs as well.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* <code>
|
|
|
|
* new Tab(
|
|
|
|
* $title='Tab one',
|
|
|
|
* new HeaderField("A header"),
|
|
|
|
* new LiteralField("Lipsum","Lorem ipsum dolor sit amet enim.")
|
|
|
|
* )
|
|
|
|
* </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 Tab extends CompositeField {
|
2016-07-06 06:34:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var TabSet
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $tabSet;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-06 06:34:09 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $id;
|
|
|
|
|
2008-10-10 18:00:50 +02:00
|
|
|
/**
|
|
|
|
* @uses FormField::name_to_label()
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-10-10 18:00:50 +02:00
|
|
|
* @param string $name Identifier of the tab, without characters like dots or spaces
|
2016-07-06 06:34:09 +02:00
|
|
|
* @param string|FormField $titleOrField Natural language title of the tabset, or first tab.
|
|
|
|
* If its left out, the class uses {@link FormField::name_to_label()} to produce a title
|
|
|
|
* from the {@link $name} parameter.
|
|
|
|
* @param FormField ...$fields All following parameters are inserted as children to this tab
|
2008-10-10 18:00:50 +02:00
|
|
|
*/
|
2016-07-06 06:34:09 +02:00
|
|
|
public function __construct($name, $titleOrField = null, $fields = null) {
|
|
|
|
if(!is_string($name)) {
|
|
|
|
throw new InvalidArgumentException('Invalid string parameter for $name');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-06 06:34:09 +02:00
|
|
|
// Get following arguments
|
|
|
|
$fields = func_get_args();
|
|
|
|
array_shift($fields);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-06 06:34:09 +02:00
|
|
|
// Detect title from second argument, if it is a string
|
|
|
|
if($titleOrField && is_string($titleOrField)) {
|
|
|
|
$title = $titleOrField;
|
|
|
|
array_shift($fields);
|
|
|
|
} else {
|
|
|
|
$title = static::name_to_label($name);
|
2008-10-10 18:00:50 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-06 06:34:09 +02:00
|
|
|
// Remaining arguments are child fields
|
|
|
|
parent::__construct($fields);
|
|
|
|
|
|
|
|
// Assign name and title (not assigned by parent constructor)
|
|
|
|
$this->setName($name);
|
|
|
|
$this->setTitle($title);
|
|
|
|
$this->setID(Convert::raw2htmlid($name));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function ID() {
|
|
|
|
if($this->tabSet) {
|
|
|
|
return $this->tabSet->ID() . '_' . $this->id;
|
|
|
|
} else {
|
|
|
|
return $this->id;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-06 06:34:09 +02:00
|
|
|
/**
|
|
|
|
* Set custom HTML ID to use for this tabset
|
|
|
|
*
|
|
|
|
* @param string $id
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setID($id) {
|
|
|
|
$this->id = $id;
|
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-06 06:34:09 +02:00
|
|
|
/**
|
|
|
|
* Get child fields
|
|
|
|
*
|
|
|
|
* @return FieldList
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
public function Fields() {
|
|
|
|
return $this->children;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-06 06:34:09 +02:00
|
|
|
/**
|
|
|
|
* Assign to a TabSet instance
|
|
|
|
*
|
|
|
|
* @param TabSet $val
|
|
|
|
* @return $this
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
public function setTabSet($val) {
|
|
|
|
$this->tabSet = $val;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-06 06:34:09 +02:00
|
|
|
* Get parent tabset
|
|
|
|
*
|
|
|
|
* @return TabSet
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2016-07-06 06:34:09 +02:00
|
|
|
public function getTabSet() {
|
|
|
|
return $this->tabSet;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-05-08 10:18:08 +02:00
|
|
|
|
|
|
|
public function extraClass() {
|
|
|
|
return implode(' ', (array)$this->extraClasses);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAttributes() {
|
|
|
|
return array_merge(
|
|
|
|
$this->attributes,
|
|
|
|
array(
|
2016-07-06 06:34:09 +02:00
|
|
|
'id' => $this->ID(),
|
2012-05-08 10:18:08 +02:00
|
|
|
'class' => 'tab ' . $this->extraClass()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|