2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* 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>
|
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 TabSet extends CompositeField {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-10 18:00:50 +02:00
|
|
|
/**
|
|
|
|
* @param string $name Identifier
|
|
|
|
* @param string $title (Optional) Natural language title of the tabset
|
|
|
|
* @param Tab|TabSet $unknown All further parameters are inserted as children into the TabSet
|
|
|
|
*/
|
|
|
|
public function __construct($name) {
|
|
|
|
$args = func_get_args();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-10 18:00:50 +02:00
|
|
|
$name = array_shift($args);
|
|
|
|
if(!is_string($name)) user_error('TabSet::__construct(): $name parameter to a valid string', E_USER_ERROR);
|
|
|
|
$this->name = $name;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-10 18:00:50 +02:00
|
|
|
$this->id = $name;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-10 18:00:50 +02:00
|
|
|
// Legacy handling: only assume second parameter as title if its a string,
|
|
|
|
// otherwise it might be a formfield instance
|
|
|
|
if(isset($args[0]) && is_string($args[0])) {
|
|
|
|
$title = array_shift($args);
|
|
|
|
}
|
|
|
|
$this->title = (isset($title)) ? $title : FormField::name_to_label($name);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-10 18:00:50 +02:00
|
|
|
if($args) foreach($args as $tab) {
|
|
|
|
$isValidArg = (is_object($tab) && (!($tab instanceof Tab) || !($tab instanceof TabSet)));
|
|
|
|
if(!$isValidArg) user_error('TabSet::__construct(): Parameter not a valid Tab instance', E_USER_ERROR);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-10 18:00:50 +02:00
|
|
|
$tab->setTabSet($this);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-10 18:00:50 +02:00
|
|
|
parent::__construct($args);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
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
|
|
|
|
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;
|
|
|
|
*/
|
2012-04-11 08:07:55 +02:00
|
|
|
public function FieldHolder($properties = array()) {
|
2012-03-24 04:38:57 +01:00
|
|
|
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
|
|
|
|
2012-03-24 04:38:57 +01:00
|
|
|
Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery-entwine/dist/jquery.entwine-dist.js');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-24 04:38:57 +01:00
|
|
|
Requirements::javascript(FRAMEWORK_DIR . '/javascript/TabSet.js');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-11 08:07:55 +02:00
|
|
|
$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
|
|
|
/**
|
|
|
|
* Return a dataobject set of all this classes tabs
|
|
|
|
*/
|
|
|
|
public function Tabs() {
|
|
|
|
return $this->children;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
public function setTabs($children){
|
|
|
|
$this->children = $children;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
public function getTabSet() {
|
2008-04-01 03:50:34 +02:00
|
|
|
if(isset($this->tabSet)) 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(
|
|
|
|
'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
|
|
|
/**
|
2015-10-07 01:37:48 +02:00
|
|
|
* Returns a named field.
|
|
|
|
*
|
|
|
|
* @param string $name Name of the field you want to find. Allows for dot notation.
|
|
|
|
* @return FormField|null
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public function fieldByName($name) {
|
2009-07-06 23:53:50 +02:00
|
|
|
if(strpos($name,'.') !== false) list($name, $remainder) = explode('.',$name,2);
|
|
|
|
else $remainder = null;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($this->children as $child) {
|
2009-07-06 23:53:50 +02:00
|
|
|
if(trim($name) == trim($child->Name) || $name == $child->id) {
|
|
|
|
if($remainder) {
|
|
|
|
if($child->isComposite()) {
|
|
|
|
return $child->fieldByName($remainder);
|
|
|
|
} else {
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error("Trying to get field '$remainder' from non-composite field $child->class.$name",
|
|
|
|
E_USER_WARNING);
|
2009-07-06 23:53:50 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return $child;
|
|
|
|
}
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2015-10-07 01:37:48 +02:00
|
|
|
|
|
|
|
return null;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a new child field to the end of the set.
|
|
|
|
*/
|
2012-04-12 02:11:53 +02:00
|
|
|
public function push(FormField $field) {
|
2007-07-19 12:40:28 +02:00
|
|
|
parent::push($field);
|
|
|
|
$field->setTabSet($this);
|
|
|
|
}
|
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.
|
|
|
|
*/
|
|
|
|
public function unshift(FormField $field) {
|
|
|
|
parent::unshift($field);
|
|
|
|
$field->setTabSet($this);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* @return FormField|null
|
2008-11-11 03:35:54 +01:00
|
|
|
*/
|
2014-01-07 17:02:43 +01:00
|
|
|
public function insertBefore($insertBefore, $field) {
|
2008-11-11 03:35:54 +01:00
|
|
|
if($field instanceof Tab) $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) {
|
2008-11-11 03:35:54 +01:00
|
|
|
if($field instanceof Tab) $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
|
|
|
}
|