/
TabSet.php
168 lines (143 loc) · 4.54 KB
/
TabSet.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
* Defines a set of tabs in a form.
* The tabs are build with our standard tabstrip javascript library.
* By default, the HTML is generated using FieldHolder.
*
* <b>Usage</b>
*
* <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>
*
* @package forms
* @subpackage fields-structural
*/
class TabSet extends CompositeField {
/**
* @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();
$name = array_shift($args);
if(!is_string($name)) user_error('TabSet::__construct(): $name parameter to a valid string', E_USER_ERROR);
$this->name = $name;
$this->id = $name;
// 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);
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);
$tab->setTabSet($this);
}
parent::__construct($args);
}
public function id() {
if($this->tabSet) return $this->tabSet->id() . '_' . $this->id . '_set';
else return $this->id;
}
/**
* Returns a tab-strip and the associated tabs.
* The HTML is a standardised format, containing a <ul;
*/
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');
Requirements::css(FRAMEWORK_DIR . '/thirdparty/jquery-ui-themes/smoothness/jquery.ui.css');
Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery-entwine/dist/jquery.entwine-dist.js');
Requirements::javascript(FRAMEWORK_DIR . '/javascript/TabSet.js');
$obj = $properties ? $this->customise($properties) : $this;
return $obj->renderWith($this->getTemplates());
}
/**
* Return a dataobject set of all this classes tabs
*/
public function Tabs() {
return $this->children;
}
public function setTabs($children){
$this->children = $children;
}
public function setTabSet($val) {
$this->tabSet = $val;
return $this;
}
public function getTabSet() {
if(isset($this->tabSet)) return $this->tabSet;
}
public function getAttributes() {
return array_merge(
$this->attributes,
array(
'id' => $this->id(),
'class' => 'ss-tabset ' . $this->extraClass()
)
);
}
/**
* Returns the named tab
*/
public function fieldByName($name) {
if(strpos($name,'.') !== false) list($name, $remainder) = explode('.',$name,2);
else $remainder = null;
foreach($this->children as $child) {
if(trim($name) == trim($child->Name) || $name == $child->id) {
if($remainder) {
if($child->isComposite()) {
return $child->fieldByName($remainder);
} else {
user_error("Trying to get field '$remainder' from non-composite field $child->class.$name", E_USER_WARNING);
return null;
}
} else {
return $child;
}
}
}
}
/**
* Add a new child field to the end of the set.
*/
public function push(FormField $field) {
parent::push($field);
$field->setTabSet($this);
}
/**
* Inserts a field before a particular field in a FieldList.
*
* @param FormField $item The form field to insert
* @param string $name Name of the field to insert before
*/
public function insertBefore($field, $insertBefore) {
parent::insertBefore($field, $insertBefore);
if($field instanceof Tab) $field->setTabSet($this);
$this->sequentialSet = null;
}
public function insertAfter($field, $insertAfter) {
parent::insertAfter($field, $insertAfter);
if($field instanceof Tab) $field->setTabSet($this);
$this->sequentialSet = null;
}
public function removeByName( $tabName, $dataFieldOnly = false ) {
parent::removeByName( $tabName, $dataFieldOnly );
}
}