Passed
Push — master ( 99e98a...333494 )
by Jean-Christophe
02:12
created

HtmlTab::setInverted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Ajax\semantic\html\modules;
3
4
use Ajax\semantic\html\base\HtmlSemCollection;
5
use Ajax\semantic\html\elements\HtmlSegment;
6
use Ajax\semantic\html\collections\menus\HtmlMenu;
7
use Ajax\semantic\html\base\constants\Side;
8
use Ajax\JsUtils;
9
use Ajax\semantic\html\base\constants\Direction;
10
use Ajax\semantic\html\content\HtmlMenuItem;
11
12
/**
13
 * Semantic Tab component
14
 *
15
 * @see http://semantic-ui.com/collections/tab.html
16
 * @author jc
17
 * @version 1.0.2
18
 */
19
class HtmlTab extends HtmlSemCollection {
20
21
	protected $params = [];
22
23
	protected $_activated = false;
24
25
	public function __construct($identifier, $tabs = array()) {
26
		parent::__construct($identifier, "div", "");
27
		$menu = new HtmlMenu("menu" . $this->identifier);
28
		$menu->asTab(false)->setAttachment(NULL, Side::TOP);
29
		$this->content["menu"] = $menu;
30
		$this->addItems($tabs);
31
	}
32
	
33
	public function getMenu(){
34
		return $this->content['menu'];
35
	}
36
37
	/**
38
	 *
39
	 * {@inheritdoc}
40
	 * @see \Ajax\common\html\HtmlCollection::createItem()
41
	 * @return HtmlSegment
42
	 */
43
	protected function createItem($value) {
44
		$count = $this->count();
45
		$title = $value;
46
		$content = NULL;
47
		if (\is_array($value)) {
48
			$title = @$value[0];
49
			$content = @$value[1];
50
		}
51
		$menuItem = $this->content["menu"]->addItem($title);
52
		$menuItem->addToProperty("data-tab", $menuItem->getIdentifier());
53
		$menuItem->removeProperty("href");
54
		$result = $this->createSegment($count, $content, $menuItem->getIdentifier());
55
		$result->menuTab = $menuItem;
56
		return $result;
57
	}
58
59
	/**
60
	 *
61
	 * @param int $count
62
	 * @param string $content
63
	 * @param string $datatab
64
	 * @return \Ajax\semantic\html\elements\HtmlSegment
65
	 */
66
	private function createSegment($count, $content, $datatab) {
67
		$segment = new HtmlSegment("item-" . $this->identifier . "-" . $count, $content);
68
		$segment->setAttachment(NULL, Side::BOTTOM)
69
			->addToProperty("class", "tab")
70
			->addToProperty("data-tab", $datatab);
71
		return $segment;
72
	}
73
74
	/**
75
	 * Sets the content of the tab at position $index
76
	 *
77
	 * @param int $index
78
	 *        	index of the tab
79
	 * @param String $content
80
	 *        	new content
81
	 * @return \Ajax\semantic\html\modules\HtmlTab
82
	 */
83
	public function setTabContent($index, $content) {
84
		$menu = $this->content["menu"];
85
		if ($index < $menu->count()) {
86
			if (isset($this->content[$index]) === false) {
87
				$this->content[$index] = $this->createSegment($index, $content, $menu->getItem($index)
88
					->getIdentifier());
89
			} else
90
				$this->content[$index]->setContent($content);
91
		}
92
		return $this;
93
	}
94
95
	/**
96
	 * Sets all contents of tabs
97
	 *
98
	 * @param array $contents
99
	 * @return \Ajax\semantic\html\modules\HtmlTab
100
	 */
101
	public function setTabsContent($contents) {
102
		$size = \sizeof($contents);
103
		for ($i = 0; $i < $size; $i ++) {
104
			$this->setTabContent($i, $contents[$i]);
105
		}
106
		return $this;
107
	}
108
109
	/**
110
	 * Activates the tab element at $index
111
	 *
112
	 * @param int $index
113
	 * @return \Ajax\semantic\html\modules\HtmlTab
114
	 */
115
	public function activate($index) {
116
		$item = $this->content["menu"]->getItem($index);
117
		if ($item != null) {
118
			$item->setActive(true);
119
			$this->content[$index]->setActive(true);
120
			$this->_activated = true;
121
		}
122
		return $this;
123
	}
124
125
	/**
126
	 * Adds a new tab
127
	 *
128
	 * @param string $title
129
	 * @param string $content
130
	 * @return \Ajax\semantic\html\elements\HtmlSegment
131
	 */
132
	public function addTab($title, $content) {
133
		return $this->addItem([
134
			$title,
135
			$content
136
		]);
137
	}
138
139
	/**
140
	 * Renders the content of $controller::$action and sets the response to the tab at $index position
141
	 *
142
	 * @param int $index
143
	 * @param JsUtils $js
144
	 * @param string $title
145
	 *        	The panel title
146
	 * @param object $initialController
147
	 * @param string $controller
148
	 *        	a controller
149
	 * @param string $action
150
	 *        	an action
151
	 * @param array $params
152
	 * @return \Ajax\semantic\html\elements\HtmlSegment
153
	 */
154
	public function forwardTab($index, JsUtils $js, $title, $initialController, $controller, $action, $params = array()) {
155
		if (\array_key_exists($index, $this->content)) {
156
			$this->content[$index] = $js->forward($initialController, $controller, $action, $params);
157
			return $this->content[$index];
158
		}
159
160
		return $this->addAndForwardTab($js, $title, $initialController, $controller, $action, $params);
161
	}
162
163
	/**
164
	 * Renders the content of an existing view : $controller/$action and sets the response to the tab at $index position
165
	 *
166
	 * @param
167
	 *        	$index
168
	 * @param JsUtils $js
169
	 * @param string $title
170
	 *        	The panel title
171
	 * @param object $initialController
172
	 * @param string $viewName
173
	 * @param array $params
174
	 *        	The parameters to pass to the view
175
	 * @return \Ajax\semantic\html\elements\HtmlSegment
176
	 */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $index at position 0 could not be parsed: Unknown type name '$index' at position 0 in $index.
Loading history...
177
	public function renderViewTab($index, JsUtils $js, $title, $initialController, $viewName, $params = array()) {
178
		if (\array_key_exists($index, $this->content)) {
179
			$this->content[$index] = $js->renderContent($initialController, $viewName, $params);
180
			return $this->content[$index];
181
		}
182
		return $this->addAndRenderViewTab($js, $title, $initialController, $viewName, $params);
183
	}
184
185
	/**
186
	 * render the content of $controller::$action and set the response to a new tab
187
	 *
188
	 * @param JsUtils $js
189
	 * @param string $title
190
	 *        	The panel title
191
	 * @param object $initialController
192
	 * @param string $controller
193
	 *        	a controller
194
	 * @param string $action
195
	 *        	an action
196
	 * @param array $params
197
	 * @return \Ajax\semantic\html\elements\HtmlSegment
198
	 */
199
	public function addAndForwardTab(JsUtils $js, $title, $initialController, $controller, $action, $params = array()) {
200
		\ob_start();
201
		$js->forward($initialController, $controller, $action, $params);
202
		$content = \ob_get_clean();
203
		return $this->addTab($title, $content);
204
	}
205
206
	/**
207
	 * render the content of an existing view : $controller/$action and set the response to a new tab
208
	 *
209
	 * @param JsUtils $js
210
	 * @param string $title
211
	 *        	The panel title
212
	 * @param object $initialController
213
	 * @param string $viewName
214
	 * @param array $params
215
	 *        	The parameters to pass to the view
216
	 * @return \Ajax\semantic\html\elements\HtmlSegment
217
	 */
218
	public function addAndRenderViewTab(JsUtils $js, $title, $initialController, $viewName, $params = array()) {
219
		return $this->addTab($title, $js->renderContent($initialController, $viewName, $params));
220
	}
221
222
	public function setPointing($value = Direction::NONE) {
223
		return $this->content["menu"]->setPointing($value);
224
	}
225
226
	public function setSecondary() {
227
		return $this->content["menu"]->setSecondary();
228
	}
229
230
	/**
231
	 * Returns the menu item at position $index
232
	 *
233
	 * @param int $index
234
	 * @return HtmlMenuItem
235
	 */
236
	public function getMenuTab($index) {
237
		return $this->content["menu"]->getItem($index);
238
	}
239
240
	/**
241
	 * Returns the tab at position $index
242
	 *
243
	 * @param int $index
244
	 * @return HtmlSegment
245
	 */
246
	public function getTab($index) {
247
		return $this->content[$index];
248
	}
249
250
	/**
251
	 * Sets the menu of tabs
252
	 *
253
	 * @param HtmlMenu $menu
254
	 * @return \Ajax\semantic\html\modules\HtmlTab
255
	 */
256
	public function setMenu($menu) {
257
		$contentSize = \sizeof($this->content);
258
		for ($i = 0; $i < $contentSize; $i ++) {
259
			if ($menu->getItem($i) !== NULL) {
260
				if (isset($this->content[$i])) {
261
					$menu->getItem($i)->addToProperty("data-tab", $this->content[$i]->getProperty("data-tab"));
262
				}
263
			}
264
		}
265
		$menuSize = $menu->count();
266
		for ($i = 0; $i < $menuSize; $i ++) {
267
			$menu->getItem($i)->removeProperty("href");
268
			if (isset($this->content[$i]) === false) {
269
				$this->content[$i] = $this->createSegment($i, "New content", $menu->getItem($i)
270
					->getIdentifier());
271
			}
272
			$menu->getItem($i)->addToProperty("data-tab", $this->content[$i]->getProperty("data-tab"));
273
		}
274
275
		$this->content["menu"] = $menu;
276
		return $this;
277
	}
278
279
	/*
280
	 * (non-PHPdoc)
281
	 * @see BaseHtml::run()
282
	 */
283
	public function run(JsUtils $js) {
284
		if (isset($this->_bsComponent) === false)
285
			$this->_bsComponent = $js->semantic()->tab("#" . $this->identifier . " .item", $this->params);
286
		$this->addEventsOnRun($js);
287
		return $this->_bsComponent;
288
	}
289
290
	public function compile(JsUtils $js = NULL, &$view = NULL) {
291
		if (! $this->_activated && $this->content["menu"]->count() > 0 && \sizeof($this->content) > 1)
292
			$this->activate(0);
293
		return parent::compile($js, $view);
294
	}
295
	
296
	public function setInverted($recursive=true){
297
		parent::setInverted($recursive);
298
		$this->content['menu']->addClass('inverted');
299
	}
300
}
301