silverstripe-framework/Forms/TabSet.php

233 lines
5.5 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\Forms;
use SilverStripe\Core\Convert;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\View\Requirements;
use InvalidArgumentException;
/**
* 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.
* By default, the HTML is generated using FieldHolder.
2014-08-15 08:53:05 +02:00
*
* <b>Usage</b>
2014-08-15 08:53:05 +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>
*/
class TabSet extends CompositeField {
2014-08-15 08:53:05 +02:00
/**
* @var TabSet
*/
protected $tabSet;
/**
* @var string
*/
protected $id;
2014-08-15 08:53:05 +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
// Get following arguments
$tabs = func_get_args();
array_shift($tabs);
2014-08-15 08:53:05 +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);
}
2014-08-15 08:53:05 +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
// 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");
}
}
}
2014-08-15 08:53:05 +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;
}
}
2014-08-15 08:53:05 +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;
}
2014-08-15 08:53:05 +02:00
/**
* Returns a tab-strip and the associated tabs.
* The HTML is a standardised format, containing a &lt;ul;
*
* @param array $properties
* @return DBHTMLText|string
*/
public function FieldHolder($properties = array()) {
Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery/jquery.js');
Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery-ui/jquery-ui.js');
Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery-cookie/jquery.cookie.js');
2014-08-15 08:53:05 +02:00
2012-07-12 19:56:19 +02:00
Requirements::css(FRAMEWORK_DIR . '/thirdparty/jquery-ui-themes/smoothness/jquery-ui.css');
2014-08-15 08:53:05 +02:00
Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery-entwine/dist/jquery.entwine-dist.js');
2014-08-15 08:53:05 +02:00
Requirements::javascript(FRAMEWORK_DIR . '/client/dist/js/TabSet.js');
2014-08-15 08:53:05 +02:00
$obj = $properties ? $this->customise($properties) : $this;
2014-08-15 08:53:05 +02:00
return $obj->renderWith($this->getTemplates());
}
2014-08-15 08:53:05 +02:00
/**
* Return a set of all this classes tabs
*
* @return FieldList
*/
public function Tabs() {
return $this->children;
}
2014-08-15 08:53:05 +02:00
/**
* @param FieldList $children Assign list of tabs
*/
public function setTabs($children){
$this->children = $children;
}
/**
* Assign to a TabSet instance
*
* @param TabSet $val
* @return $this
*/
public function setTabSet($val) {
$this->tabSet = $val;
return $this;
}
2014-08-15 08:53:05 +02:00
/**
* Get parent tabset
*
* @return TabSet
*/
public function getTabSet() {
return $this->tabSet;
}
public function getAttributes() {
return array_merge(
$this->attributes,
array(
'id' => $this->ID(),
'class' => $this->extraClass()
)
);
}
2014-08-15 08:53:05 +02:00
/**
* Add a new child field to the end of the set.
*
* @param FormField $field
*/
public function push(FormField $field) {
if ($field instanceof Tab || $field instanceof TabSet) {
$field->setTabSet($this);
}
parent::push($field);
}
2014-08-15 08:53:05 +02:00
/**
* Add a new child field to the beginning of the set.
*
* @param FormField $field
*/
public function unshift(FormField $field) {
if ($field instanceof Tab || $field instanceof TabSet) {
$field->setTabSet($this);
}
parent::unshift($field);
}
/**
* Inserts a field before a particular field in a FieldList.
*
* @param string $insertBefore Name of the field to insert before
* @param FormField $field The form field to insert
* @return FormField|null
*/
public function insertBefore($insertBefore, $field) {
if ($field instanceof Tab || $field instanceof TabSet) {
$field->setTabSet($this);
}
return parent::insertBefore($insertBefore, $field);
}
2014-08-15 08:53:05 +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
*/
public function insertAfter($insertAfter, $field) {
if ($field instanceof Tab || $field instanceof TabSet) {
$field->setTabSet($this);
}
return parent::insertAfter($insertAfter, $field);
}
}