2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\Forms;
|
|
|
|
|
|
|
|
use SilverStripe\Core\Convert;
|
2016-07-06 06:34:09 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLText;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\View\Requirements;
|
|
|
|
use InvalidArgumentException;
|
2016-07-06 06:34:09 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Defines a set of tabs in a form.
|
2014-08-15 08:53:05 +02:00
|
|
|
* The tabs are build with our standard tabstrip javascript library.
|
2010-10-15 05:55:22 +02:00
|
|
|
* By default, the HTML is generated using FieldHolder.
|
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>
|
|
|
|
* new TabSet(
|
|
|
|
* $name = "TheTabSetName",
|
|
|
|
* new Tab(
|
|
|
|
* $title='Tab one',
|
|
|
|
* new HeaderField("A header"),
|
|
|
|
* new LiteralField("Lipsum","Lorem ipsum dolor sit amet enim.")
|
|
|
|
* ),
|
|
|
|
* new Tab(
|
|
|
|
* $title='Tab two',
|
|
|
|
* new HeaderField("A second header"),
|
|
|
|
* new LiteralField("Lipsum","Ipsum dolor sit amet enim.")
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* </code>
|
2016-09-15 04:48:38 +02:00
|
|
|
*
|
|
|
|
* Caution: The form field does not include any JavaScript or CSS when used outside of the CMS context,
|
|
|
|
* since the required frontend dependencies are included through CMS bundling.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class TabSet extends CompositeField {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-09-07 05:35:47 +02:00
|
|
|
/**
|
|
|
|
* Use custom react component
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $schemaComponent = 'Tabs';
|
|
|
|
|
2008-10-10 18:00:50 +02:00
|
|
|
/**
|
2016-07-06 06:34:09 +02:00
|
|
|
* @var TabSet
|
|
|
|
*/
|
|
|
|
protected $tabSet;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
2008-10-10 18:00:50 +02:00
|
|
|
*/
|
2016-07-06 06:34:09 +02:00
|
|
|
protected $id;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-06 06:34:09 +02:00
|
|
|
/**
|
|
|
|
* @param string $name Identifier
|
|
|
|
* @param string|Tab|TabSet $titleOrTab 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 Tab|TabSet ...$tabs All further parameters are inserted as children into the TabSet
|
|
|
|
*/
|
|
|
|
public function __construct($name, $titleOrTab = null, $tabs = 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
|
|
|
|
$tabs = func_get_args();
|
|
|
|
array_shift($tabs);
|
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($titleOrTab && is_string($titleOrTab)) {
|
|
|
|
$title = $titleOrTab;
|
|
|
|
array_shift($tabs);
|
|
|
|
} 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
|
|
|
// Normalise children list
|
|
|
|
if(count($tabs) === 1 && (is_array($tabs[0]) || $tabs[0] instanceof FieldList)) {
|
|
|
|
$tabs = $tabs[0];
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-06 06:34:09 +02:00
|
|
|
// Ensure tabs are assigned to this tabset
|
|
|
|
if($tabs) {
|
|
|
|
foreach($tabs as $tab) {
|
|
|
|
if ($tab instanceof Tab || $tab instanceof TabSet) {
|
|
|
|
$tab->setTabSet($this);
|
|
|
|
} else {
|
|
|
|
throw new InvalidArgumentException("TabSet can only contain instances of other Tab or Tabsets");
|
|
|
|
}
|
|
|
|
}
|
2008-10-10 18:00:50 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-06 06:34:09 +02:00
|
|
|
parent::__construct($tabs);
|
|
|
|
|
|
|
|
// 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 . '_set';
|
|
|
|
} 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
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Returns a tab-strip and the associated tabs.
|
|
|
|
* The HTML is a standardised format, containing a <ul;
|
2016-07-06 06:34:09 +02:00
|
|
|
*
|
|
|
|
* @param array $properties
|
|
|
|
* @return DBHTMLText|string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-04-11 08:07:55 +02:00
|
|
|
public function FieldHolder($properties = array()) {
|
|
|
|
$obj = $properties ? $this->customise($properties) : $this;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-14 12:08:00 +02:00
|
|
|
return $obj->renderWith($this->getTemplates());
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2016-07-06 06:34:09 +02:00
|
|
|
* Return a set of all this classes tabs
|
|
|
|
*
|
|
|
|
* @return FieldList
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public function Tabs() {
|
|
|
|
return $this->children;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-06 06:34:09 +02:00
|
|
|
/**
|
|
|
|
* @param FieldList $children Assign list of tabs
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
public function setTabs($children){
|
|
|
|
$this->children = $children;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-06 06:34:09 +02:00
|
|
|
/**
|
|
|
|
* Get parent tabset
|
2016-08-19 00:51:35 +02:00
|
|
|
*
|
2016-07-06 06:34:09 +02:00
|
|
|
* @return TabSet
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
public function getTabSet() {
|
2016-07-06 06:34:09 +02:00
|
|
|
return $this->tabSet;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-05-08 10:18:08 +02:00
|
|
|
|
|
|
|
public function getAttributes() {
|
|
|
|
return array_merge(
|
|
|
|
$this->attributes,
|
|
|
|
array(
|
2016-07-06 06:34:09 +02:00
|
|
|
'id' => $this->ID(),
|
2012-10-31 15:57:48 +01:00
|
|
|
'class' => $this->extraClass()
|
2012-05-08 10:18:08 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
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.
|
2016-07-06 06:34:09 +02:00
|
|
|
*
|
|
|
|
* @param FormField $field
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-04-12 02:11:53 +02:00
|
|
|
public function push(FormField $field) {
|
2016-07-06 06:34:09 +02:00
|
|
|
if ($field instanceof Tab || $field instanceof TabSet) {
|
|
|
|
$field->setTabSet($this);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
parent::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.
|
2016-07-06 06:34:09 +02:00
|
|
|
*
|
|
|
|
* @param FormField $field
|
2015-12-14 17:18:57 +01:00
|
|
|
*/
|
|
|
|
public function unshift(FormField $field) {
|
2016-07-06 06:34:09 +02:00
|
|
|
if ($field instanceof Tab || $field instanceof TabSet) {
|
|
|
|
$field->setTabSet($this);
|
|
|
|
}
|
2015-12-14 17:18:57 +01:00
|
|
|
parent::unshift($field);
|
|
|
|
}
|
|
|
|
|
2008-11-11 03:35:54 +01:00
|
|
|
/**
|
2011-10-28 03:37:27 +02:00
|
|
|
* Inserts a field before a particular field in a FieldList.
|
2008-11-11 03:35:54 +01:00
|
|
|
*
|
2015-10-07 01:37:48 +02:00
|
|
|
* @param string $insertBefore Name of the field to insert before
|
|
|
|
* @param FormField $field The form field to insert
|
2016-07-06 06:34:09 +02:00
|
|
|
* @return FormField|null
|
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
|
|
|
if ($field instanceof Tab || $field instanceof TabSet) {
|
|
|
|
$field->setTabSet($this);
|
|
|
|
}
|
2015-10-07 01:37:48 +02:00
|
|
|
return parent::insertBefore($insertBefore, $field);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-10-07 01:37:48 +02:00
|
|
|
/**
|
|
|
|
* Inserts a field after a particular field in a FieldList.
|
|
|
|
*
|
|
|
|
* @param string $insertAfter Name of the field to insert after
|
|
|
|
* @param FormField $field The form field to insert
|
|
|
|
* @return FormField|null
|
|
|
|
*/
|
2014-01-07 17:02:43 +01:00
|
|
|
public function insertAfter($insertAfter, $field) {
|
2016-07-06 06:34:09 +02:00
|
|
|
if ($field instanceof Tab || $field instanceof TabSet) {
|
|
|
|
$field->setTabSet($this);
|
|
|
|
}
|
2015-10-07 01:37:48 +02:00
|
|
|
return parent::insertAfter($insertAfter, $field);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-04-12 02:11:53 +02:00
|
|
|
}
|