Completed
Push — master ( 4924e4...d789b0 )
by Jean-Christophe
03:03
created

HtmlTab::forwardTab()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 8
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 7
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
11
/**
12
 * Semantic Tab component
13
 * @see http://semantic-ui.com/collections/tab.html
14
 * @author jc
15
 * @version 1.001
16
 */
17
class HtmlTab extends HtmlSemCollection{
18
19
	protected $params=array("debug"=>true);
20
21
	public function __construct( $identifier, $tabs=array()){
22
		parent::__construct( $identifier, "div", "");
23
		$menu=new HtmlMenu("menu".$this->identifier);
24
		$menu->asTab(false)->setAttachment(NULL,Side::TOP);
0 ignored issues
show
Documentation introduced by
NULL is of type null, but the function expects a object<Ajax\semantic\htm...e\HtmlSemDoubleElement>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
25
		$this->content["menu"]=$menu;
26
		$this->addItems($tabs);
27
	}
28
29
	/**
30
	 * {@inheritDoc}
31
	 * @see \Ajax\common\html\HtmlCollection::createItem()
32
	 * @return HtmlSegment
33
	 */
34
	protected function createItem($value){
35
		$count=$this->count();
36
		$title=$value;
37
		$content=NULL;
38
		if(\is_array($value)){
39
			$title=@$value[0];$content=@$value[1];
40
		}
41
		$menuItem=$this->content["menu"]->addItem($title);
42
		$menuItem->addToProperty("data-tab", $menuItem->getIdentifier());
43
		$menuItem->removeProperty("href");
44
		return $this->createSegment($count, $content, $menuItem->getIdentifier());
45
	}
46
47
	/**
48
	 * @param int $count
49
	 * @param string $content
50
	 * @param string $datatab
51
	 * @return \Ajax\semantic\html\elements\HtmlSegment
52
	 */
53
	private function createSegment($count,$content,$datatab){
54
		$segment=new HtmlSegment("item-".$this->identifier."-".$count, $content);
55
		$segment->setAttachment(NULL,Side::BOTTOM)->addToProperty("class", "tab")->addToProperty("data-tab",$datatab);
0 ignored issues
show
Documentation introduced by
NULL is of type null, but the function expects a object<Ajax\semantic\htm...e\HtmlSemDoubleElement>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
56
		return $segment;
57
	}
58
59
	/**
60
	 * Sets the content of the tab at position $index
61
	 * @param int $index index of the tab
62
	 * @param String $content new content
63
	 * @return \Ajax\semantic\html\modules\HtmlTab
64
	 */
65
	public function setTabContent($index,$content){
66
		$menu=$this->content["menu"];
67
		if($index<$menu->count()){
68
			if(isset($this->content[$index])===false){
69
				$this->content[$index]=$this->createSegment($index, $content, $menu->getItem($index)->getIdentifier());
70
			}else
71
				$this->content[$index]->setContent($content);
72
		}
73
		return $this;
74
	}
75
76
	/**
77
	 * Sets all contents of tabs
78
	 * @param array $contents
79
	 * @return \Ajax\semantic\html\modules\HtmlTab
80
	 */
81
	public function setTabsContent($contents){
82
		for($i=0;$i<\sizeof($contents);$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...
83
			$this->setTabContent($i, $contents[$i]);
84
		}
85
		return $this;
86
	}
87
88
	/**
89
	 * Activates the tab element at $index
90
	 * @param int $index
91
	 * @return \Ajax\semantic\html\modules\HtmlTab
92
	 */
93
	public function activate($index){
94
		$this->content["menu"]->getItem($index)->setActive(true);
95
		$this->content[$index]->setActive(true);
96
		return $this;
97
	}
98
99
	/**
100
	 * Adds a new tab
101
	 * @param string $title
102
	 * @param string $content
103
	 * @return \Ajax\semantic\html\elements\HtmlSegment
104
	 */
105
	public function addTab($title,$content){
106
		return $this->addItem([$title,$content]);
0 ignored issues
show
Documentation introduced by
array($title, $content) is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a object<Ajax\common\html\HtmlDoubleElement>|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
107
	}
108
109
	/**
110
	 * Renders the content of $controller::$action and sets the response to the tab at $index position
111
	 * @param int $index
112
	 * @param JsUtils $js
113
	 * @param string $title The panel title
114
	 * @param Controller $initialController
115
	 * @param string $controller a controller
116
	 * @param string $action an action
117
	 * @param array $params
118
	 * @return \Ajax\semantic\html\elements\HtmlSegment
119
	 */
120 View Code Duplication
	public function forwardTab($index,JsUtils $js,$title,$initialController,$controller,$action,$params=array()){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
		if(\array_key_exists($index, $this->content)){
122
			$this->content[$index]=$js->forward($initialController, $controller, $action,$params);
123
			return $this->content[$index];
124
		}
125
126
		return $this->addAndForwardTab($js, $title, $initialController, $controller, $action,$params);
127
	}
128
129
	/**
130
	 * Renders the content of an existing view : $controller/$action and sets the response to the tab at $index position
131
	 * @param $index
132
	 * @param JsUtils $js
133
	 * @param string $title The panel title
134
	 * @param Controller $initialController
135
	 * @param string $viewName
136
	 * @param $params The parameters to pass to the view
137
	 * @return \Ajax\semantic\html\elements\HtmlSegment
138
	 */
139 View Code Duplication
	public function renderViewTab($index,JsUtils $js,$title,$initialController, $viewName, $params=array()) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
		if(\array_key_exists($index, $this->content)){
141
			$this->content[$index]=$js->renderContent($initialController, $viewName,$params);
142
			return $this->content[$index];
143
		}
144
		return $this->addAndRenderViewTab($js, $title, $initialController, $viewName,$params);
145
	}
146
147
148
	/**
149
	 * render the content of $controller::$action and set the response to a new tab
150
	 * @param JsUtils $js
151
	 * @param string $title The panel title
152
	 * @param Controller $initialController
153
	 * @param string $controller a controller
154
	 * @param string $action an action
155
	 * @param array $params
156
	 * @return \Ajax\semantic\html\elements\HtmlSegment
157
	 */
158
	public function addAndForwardTab(JsUtils $js,$title,$initialController,$controller,$action,$params=array()){
159
		return $this->addTab($title, $js->forward($initialController, $controller, $action,$params));
160
	}
161
162
	/**
163
	 * render the content of an existing view : $controller/$action and set the response to a new tab
164
	 * @param JsUtils $js
165
	 * @param string $title The panel title
166
	 * @param Controller $initialController
167
	 * @param string $viewName
168
	 * @param $params The parameters to pass to the view
169
	 * @return \Ajax\semantic\html\elements\HtmlSegment
170
	 */
171
	public function addAndRenderViewTab(JsUtils $js,$title,$initialController, $viewName, $params=array()) {
172
		return $this->addTab($title, $js->renderContent($initialController, $viewName,$params));
173
	}
174
175
	public function setPointing($value=Direction::NONE) {
176
		return $this->content["menu"]->setPointing($value);
177
	}
178
179
	public function setSecondary() {
180
		return $this->content["menu"]->setSecondary();
181
	}
182
183
	/**
184
	 * Returns the menu item at position $index
185
	 * @param int $index
186
	 * @return Ajax\semantic\html\content\HtmlMenuItem
187
	 */
188
	public function getMenuTab($index){
189
		return $this->content["menu"]->getItem($index);
190
	}
191
192
	/**
193
	 * Returns the tab at position $index
194
	 * @param int $index
195
	 * @return HtmlSegment
196
	 */
197
	public function getTab($index){
198
		return $this->content[$index];
199
	}
200
201
	/**
202
	 * Sets the menu of tabs
203
	 * @param HtmlMenu $menu
204
	 * @return \Ajax\semantic\html\modules\HtmlTab
205
	 */
206
	public function setMenu($menu){
207
		for($i=0;$i<\sizeof($this->content);$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...
208 View Code Duplication
			if($menu->getItem($i)!==NULL){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
209
				if(isset($this->content[$i])===true){
210
					$menu->getItem($i)->addToProperty("data-tab",$this->content[$i]->getProperty("data-tab"));
211
				}
212
			}
213
		}
214
		for($i=0;$i<$menu->count();$i++){
215
			$menu->getItem($i)->removeProperty("href");
216 View Code Duplication
			if(isset($this->content[$i])===false){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
217
				$this->content[$i]=$this->createSegment($i, "New content", $menu->getItem($i)->getIdentifier());
218
			}
219
			$menu->getItem($i)->addToProperty("data-tab",$this->content[$i]->getProperty("data-tab"));
220
		}
221
222
		$this->content["menu"]=$menu;
223
		return $this;
224
	}
225
226
	/*
227
	 * (non-PHPdoc)
228
	 * @see BaseHtml::run()
229
	 */
230
	public function run(JsUtils $js) {
231 View Code Duplication
		if(isset($this->_bsComponent)===false)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
232
			$this->_bsComponent=$js->semantic()->tab("#".$this->identifier." .item",$this->params);
233
			$this->addEventsOnRun($js);
234
			return $this->_bsComponent;
235
	}
236
237
	public function compile(JsUtils $js=NULL, &$view=NULL) {
238
		if($this->content["menu"]->count()>0 && \sizeof($this->content)>1)
239
			$this->activate(0);
240
		return parent::compile($js,$view);
241
	}
242
}