HtmlTabs   B
last analyzed

Complexity

Total Complexity 38

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 38
c 2
b 0
f 0
lcom 2
cbo 5
dl 0
loc 165
rs 8.3999

21 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addTab_() 0 12 3
A setActive() 0 5 2
A disable() 0 3 1
A addTab() 0 15 3
A fromArray() 0 5 1
A addTabs() 0 6 2
A getTabstype() 0 3 1
A setTabstype() 0 4 1
A compile() 0 4 1
A run() 0 8 2
B createTabContents() 0 13 5
A addTabContents() 0 3 1
A getTabContent() 0 3 1
A setContentToTab() 0 5 2
A countTabs() 0 3 1
A getTabItem() 0 4 2
A fadeEffect() 0 9 3
A on() 0 6 2
A setStacked() 0 5 2
A fromDatabaseObject() 0 3 1
1
<?php
2
3
namespace Ajax\bootstrap\html;
4
5
/**
6
 * Composant Twitter Bootstrap Tabs
7
 *
8
 * @author jc
9
 * @version 1.001
10
 */
11
use Ajax\bootstrap\html\content\HtmlTabItem;
12
use Ajax\JsUtils;
13
use Ajax\bootstrap\components\Tabs;
14
use Ajax\bootstrap\html\base\HtmlBsDoubleElement;
15
use Ajax\bootstrap\html\content\HtmlTabContent;
16
17
18
class HtmlTabs extends HtmlBsDoubleElement {
19
	protected $tabs=array ();
20
	protected $_tabsType="tabs";
21
	protected $stacked="";
22
23
	public function __construct($identifier, $tagName="ul") {
24
		parent::__construct($identifier, $tagName);
25
		$this->_template="<%tagName% %properties%>%tabs%</%tagName%>%content%";
26
		$this->setProperty("class", "nav nav-".$this->_tabsType);
27
	}
28
29
	protected function addTab_($tab, $index=null) {
30
		if($tab instanceof HtmlDropdown){
31
			$tab->setMTagName("li");
32
		}
33
		if (isset($index)) {
34
			$inserted=array (
35
					$tab
36
			);
37
			array_splice($this->tabs, $index, 0, $inserted);
38
		} else
39
			$this->tabs []=$tab;
40
	}
41
42
	public function setActive($index){
43
		for ($i=0;$i<sizeof($this->tabs);$i++){
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
44
			$this->tabs[$i]->setActive($i==$index);
45
		}
46
	}
47
48
	public function disable($index){
49
		$this->tabs[$index]->disable();
50
	}
51
52
	public function addTab($element, $index=null) {
53
		$iid=$this->countTabs()+1;
54
		$tab=$element;
55
		if (is_string($element)) {
56
			$tab=new HtmlTabItem("tab-".$this->identifier."-".$iid, $element);
57
			$this->addTab_($tab, $index);
58
		} elseif (is_array($element)) {
59
			$tab=new HtmlTabItem("tab-".$this->identifier."-".$iid);
60
			$tab->fromArray($element);
61
			$this->addTab_($tab, $index);
62
		} else {
63
			$this->addTab_($tab, $index);
64
		}
65
		return $tab;
66
	}
67
68
	/*
69
	 * (non-PHPdoc)
70
	 * @see \Ajax\bootstrap\html\HtmlSingleElement::fromArray()
71
	 */
72
	public function fromArray($array) {
73
		$array=parent::fromArray($array);
74
		$this->addTabs($array);
75
		return $array;
76
	}
77
78
	public function addTabs($tabs) {
79
		foreach ( $tabs as $tab ) {
80
			$this->addTab($tab);
81
		}
82
		return $this;
83
	}
84
85
	public function getTabstype() {
86
		return $this->_tabsType;
87
	}
88
89
	public function setTabstype($_tabsType="tabs") {
90
		$this->_tabsType=$_tabsType;
91
		return $this;
92
	}
93
94
	/*
95
	 * (non-PHPdoc)
96
	 * @see \Ajax\bootstrap\html\BaseHtml::compile()
97
	 */
98
	public function compile(JsUtils $js=NULL, &$view=NULL) {
99
		$this->setProperty("class", "nav nav-".$this->_tabsType." ".$this->stacked);
100
		return parent::compile($js, $view);
101
	}
102
103
	/*
104
	 * (non-PHPdoc)
105
	 * @see \Ajax\bootstrap\html\HtmlDoubleElement::run()
106
	 */
107
	public function run(JsUtils $js) {
108
		$this->_bsComponent=new Tabs($js);
109
		foreach ( $this->tabs as $tab ) {
110
			$this->_bsComponent->addTab($tab->run($js));
111
		}
112
		$this->addEventsOnRun($js);
113
		return $this->_bsComponent;
114
	}
115
116
	public function createTabContents() {
117
		$tabContent=new HtmlTabContent("tabcontent-".$this->identifier);
118
		foreach ( $this->tabs as $tab ) {
119
			if ($tab instanceof HtmlTabItem)
120
				$tabContent->addTabItem($tab->getHref());
121
			elseif ($tab instanceof HtmlDropdown) {
122
				foreach ( $tab->getItems() as $dropdownItem ) {
123
					$tabContent->addTabItem($dropdownItem->getHref());
124
				}
125
			}
126
		}
127
		return $tabContent;
128
	}
129
130
	public function addTabContents() {
131
		$this->content=$this->createTabContents();
132
	}
133
134
	public function getTabContent($index) {
135
		$this->content->getTabItem($index);
136
	}
137
138
	public function setContentToTab($index, $text) {
139
		$tabContentItem=$this->content->getTabItem($index);
140
		if (isset($tabContentItem))
141
			$tabContentItem->setContent($text);
142
	}
143
144
	public function countTabs() {
145
		return sizeof($this->tabs);
146
	}
147
148
	public function getTabItem($index) {
149
		if ($index<sizeof($this->content->get))
150
			return $this->content;
151
	}
152
153
	public function fadeEffect() {
154
		if (sizeof($this->content->getTabItems())>0) {
155
			$this->content->getTabItem(0)->addToProperty("class", "fade in");
156
			$size=sizeof($this->tabs);
157
			for($index=0; $index<$size; $index++) {
158
				$this->content->getTabItem($index)->addToProperty("class", "fade");
159
			}
160
		}
161
	}
162
	
163
	public function on($event, $jsCode,$stopPropagation=false,$preventDefault=false){
164
		foreach ($this->tabs as $tab){
165
			$tab->on($event,$jsCode,$stopPropagation,$preventDefault);
166
		}
167
		return $this;
168
	}
169
170
	public function setStacked($stacked=true){
171
		if($stacked)
172
			$this->stacked="nav-stacked";
173
		else $this->stacked="";
174
	}
175
176
	/* (non-PHPdoc)
177
	 * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
178
	 */
179
	public function fromDatabaseObject($object, $function) {
180
		$this->addTab($function($object));
181
	}
182
}