Completed
Push — master ( d19955...af891e )
by Sam
22s
created

TabSet::__construct()   C

Complexity

Conditions 11
Paths 13

Size

Total Lines 40
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 22
c 1
b 0
f 0
nc 13
nop 3
dl 0
loc 40
rs 5.2653

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
use SilverStripe\ORM\FieldType\DBHTMLText;
3
4
/**
5
 * Defines a set of tabs in a form.
6
 * The tabs are build with our standard tabstrip javascript library.
7
 * By default, the HTML is generated using FieldHolder.
8
 *
9
 * <b>Usage</b>
10
 *
11
 * <code>
12
 * new TabSet(
13
 * 	$name = "TheTabSetName",
14
 * 	new Tab(
15
 * 		$title='Tab one',
16
 * 		new HeaderField("A header"),
17
 * 		new LiteralField("Lipsum","Lorem ipsum dolor sit amet enim.")
18
 * 	),
19
 * 	new Tab(
20
 * 		$title='Tab two',
21
 * 		new HeaderField("A second header"),
22
 * 		new LiteralField("Lipsum","Ipsum dolor sit amet enim.")
23
 * 	)
24
 * )
25
 * </code>
26
 *
27
 * @package forms
28
 * @subpackage fields-structural
29
 */
30
class TabSet extends CompositeField {
31
32
	/**
33
	 * @var TabSet
34
	 */
35
	protected $tabSet;
36
37
	/**
38
	 * @var string
39
	 */
40
	protected $id;
41
42
	/**
43
	 * @param string $name Identifier
44
	 * @param string|Tab|TabSet $titleOrTab Natural language title of the tabset, or first tab.
45
	 * If its left out, the class uses {@link FormField::name_to_label()} to produce a title
46
	 * from the {@link $name} parameter.
47
	 * @param Tab|TabSet ...$tabs All further parameters are inserted as children into the TabSet
48
	 */
49
	public function __construct($name, $titleOrTab = null, $tabs = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $tabs is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
50
		if(!is_string($name)) {
51
			throw new InvalidArgumentException('Invalid string parameter for $name');
52
		}
53
54
		// Get following arguments
55
		$tabs = func_get_args();
56
		array_shift($tabs);
57
58
		// Detect title from second argument, if it is a string
59
		if($titleOrTab && is_string($titleOrTab)) {
60
			$title = $titleOrTab;
61
			array_shift($tabs);
62
		} else {
63
			$title = static::name_to_label($name);
64
		}
65
66
		// Normalise children list
67
		if(count($tabs) === 1 && (is_array($tabs[0]) || $tabs[0] instanceof FieldList)) {
68
			$tabs = $tabs[0];
69
		}
70
71
		// Ensure tabs are assigned to this tabset
72
		if($tabs) {
73
			foreach($tabs as $tab) {
74
				if ($tab instanceof Tab || $tab instanceof TabSet) {
75
					$tab->setTabSet($this);
76
				} else {
77
					throw new InvalidArgumentException("TabSet can only contain instances of other Tab or Tabsets");
78
				}
79
			}
80
		}
81
82
		parent::__construct($tabs);
83
84
		// Assign name and title (not assigned by parent constructor)
85
		$this->setName($name);
86
		$this->setTitle($title);
87
		$this->setID(Convert::raw2htmlid($name));
0 ignored issues
show
Bug introduced by
It seems like \Convert::raw2htmlid($name) targeting Convert::raw2htmlid() can also be of type array; however, TabSet::setID() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
88
	}
89
90
	public function ID() {
91
		if($this->tabSet) {
92
			return $this->tabSet->ID() . '_' . $this->id . '_set';
93
		} else {
94
			return $this->id;
95
		}
96
	}
97
98
	/**
99
	 * Set custom HTML ID to use for this tabset
100
	 *
101
	 * @param string $id
102
	 * @return $this
103
	 */
104
	public function setID($id) {
105
		$this->id = $id;
106
		return $this;
107
	}
108
109
	/**
110
	 * Returns a tab-strip and the associated tabs.
111
	 * The HTML is a standardised format, containing a &lt;ul;
112
	 *
113
	 * @param array $properties
114
	 * @return DBHTMLText|string
115
	 */
116
	public function FieldHolder($properties = array()) {
117
		Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery/jquery.js');
118
		Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery-ui/jquery-ui.js');
119
		Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery-cookie/jquery.cookie.js');
120
121
		Requirements::css(FRAMEWORK_DIR . '/thirdparty/jquery-ui-themes/smoothness/jquery-ui.css');
122
123
		Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery-entwine/dist/jquery.entwine-dist.js');
124
125
		Requirements::javascript(FRAMEWORK_DIR . '/client/dist/js/TabSet.js');
126
127
		$obj = $properties ? $this->customise($properties) : $this;
128
129
		return $obj->renderWith($this->getTemplates());
130
	}
131
132
	/**
133
	 * Return a set of all this classes tabs
134
	 *
135
	 * @return FieldList
136
	 */
137
	public function Tabs() {
138
		return $this->children;
139
	}
140
141
	/**
142
	 * @param FieldList $children Assign list of tabs
143
	 */
144
	public function setTabs($children){
145
		$this->children = $children;
146
	}
147
148
	/**
149
	 * Assign to a TabSet instance
150
	 *
151
	 * @param TabSet $val
152
	 * @return $this
153
	 */
154
	public function setTabSet($val) {
155
		$this->tabSet = $val;
156
		return $this;
157
	}
158
159
	/**
160
	 * Get parent tabset
161
	 * 
162
	 * @return TabSet
163
	 */
164
	public function getTabSet() {
165
		return $this->tabSet;
166
	}
167
168
	public function getAttributes() {
169
		return array_merge(
170
			$this->attributes,
171
			array(
172
				'id' => $this->ID(),
173
				'class' => $this->extraClass()
174
			)
175
		);
176
	}
177
178
	/**
179
	 * Add a new child field to the end of the set.
180
	 *
181
	 * @param FormField $field
182
	 */
183
	public function push(FormField $field) {
184
		if ($field instanceof Tab || $field instanceof TabSet) {
185
			$field->setTabSet($this);
186
		}
187
		parent::push($field);
188
	}
189
190
	/**
191
	 * Add a new child field to the beginning of the set.
192
	 *
193
	 * @param FormField $field
194
	 */
195
	public function unshift(FormField $field) {
196
		if ($field instanceof Tab || $field instanceof TabSet) {
197
			$field->setTabSet($this);
198
		}
199
		parent::unshift($field);
200
	}
201
202
	/**
203
	 * Inserts a field before a particular field in a FieldList.
204
	 *
205
	 * @param string $insertBefore Name of the field to insert before
206
	 * @param FormField $field The form field to insert
207
	 * @return FormField|null
208
	 */
209
	public function insertBefore($insertBefore, $field) {
210
		if ($field instanceof Tab || $field instanceof TabSet) {
211
			$field->setTabSet($this);
212
		}
213
		return parent::insertBefore($insertBefore, $field);
214
	}
215
216
	/**
217
	 * Inserts a field after a particular field in a FieldList.
218
	 *
219
	 * @param string $insertAfter Name of the field to insert after
220
	 * @param FormField $field The form field to insert
221
	 * @return FormField|null
222
	 */
223
	public function insertAfter($insertAfter, $field) {
224
		if ($field instanceof Tab || $field instanceof TabSet) {
225
			$field->setTabSet($this);
226
		}
227
		return parent::insertAfter($insertAfter, $field);
228
	}
229
}
230