HtmlNavzone::addElements()   C
last analyzed

Complexity

Conditions 8
Paths 2

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 26
rs 5.3846
cc 8
eloc 20
nc 2
nop 1
1
<?php
2
3
namespace Ajax\bootstrap\html\content;
4
5
use Ajax\JsUtils;
6
use Ajax\bootstrap\html\base\CssRef;
7
use Ajax\bootstrap\html\content\HtmlDropdownItem;
8
use Ajax\bootstrap\html\HtmlDropdown;
9
use Ajax\bootstrap\html\HtmlLink;
10
use Ajax\common\html\BaseHtml;
11
use Ajax\bootstrap\html\base\HtmlBsDoubleElement;
12
13
/**
14
 * Inner element for Twitter Bootstrap HTML Navbar component
15
 * @author jc
16
 * @version 1.001
17
 */
18
class HtmlNavzone extends BaseHtml {
19
	protected $class="navbar-nav";
20
	protected $elements;
21
22
	/**
23
	 *
24
	 * @param string $identifier the id
25
	 */
26
	public function __construct($identifier) {
27
		parent::__construct($identifier);
28
		$this->tagName="ul";
29
		$this->_template='<%tagName% id="%identifier%" class="nav navbar-nav %class%">%elements%</%tagName%>';
30
		$this->elements=array ();
31
	}
32
33
	public function setClass($value) {
34
		$this->setMemberCtrl($this->class, $value, CssRef::navbarZoneClasses());
35
	}
36
37
	public function asForm() {
38
		$this->addToMember($this->class, "navbar-form");
39
	}
40
41
	public function addElement($element) {
42
		if($element instanceof HtmlLink){
43
			$this->addLink($element);
44
		} else if (is_object($element)) {
45
			$this->elements []=$element;
46
		} else if (is_array($element)) {
47
			$this->addLink(array_pop($element), array_pop($element));
48
		} else {
49
			$this->addLink($element);
50
		}
51
	}
52
53
	public function setValues($class, $tagName, $elements=array()) {
54
		$this->class=$class;
55
		$this->tagName=$tagName;
56
		$this->elements=$elements;
57
		return $this;
58
	}
59
60
	public function addElements($elements) {
61
		if (is_array($elements)) {
62
			foreach ( $elements as $key => $element ) {
63
				$iid=$this->getElementsCount()+1;
64
				if ($element instanceof HtmlDropdownItem)
65
					$this->elements []=$element;
66
				else if (is_array($element)) {
67
					if (is_string($key)===true) {
68
						$dropdown=new HtmlDropdown($this->identifier."-dropdown-".$iid);
69
						$dropdown->addItems($element);
70
						$dropdown->setBtnCaption($key);
71
						$dropdown->setMTagName("li");
72
						$this->addElement($dropdown);
73
					} else {
74
						$this->addLink(array_pop($element), array_pop($element));
75
					}
76
				} else if (is_object($element)) {
77
					$this->addElement($element);
78
				} else if (is_string($element)) {
79
					$this->addLink($element);
80
				}
81
				// TODO A vérifier
82
			}
83
		}
84
		return $this;
85
	}
86
87
	public function addLink($caption, $href="#") {
88
		$iid=$this->getElementsCount()+1;
89
		$li=new HtmlBsDoubleElement($this->identifier."-li-".$iid, "li");
90
		if($caption instanceof HtmlLink){
91
			$link=$caption;
92
		}else{
93
			$link=new HtmlLink($this->identifier."-link-".$iid, $href, $caption);
94
		}
95
		$li->setContent($link);
96
		$this->addElement($li);
97
	}
98
99
	public static function form($identifier, $elements=array()) {
100
		$result=new HtmlNavzone($identifier);
101
		return $result->setValues("navbar-form navbar-left", "form", $elements);
102
	}
103
104
	public static function left($identifier, $elements=array()) {
105
		$result=new HtmlNavzone($identifier);
106
		return $result->setValues("navbar-left", "ul", $elements);
107
	}
108
109
	public static function right($identifier, $elements=array()) {
110
		$result=new HtmlNavzone($identifier);
111
		return $result->setValues("navbar-right", "ul", $elements);
112
	}
113
114
	public static function formRight($identifier, $elements=array()) {
115
		$result=new HtmlNavzone($identifier);
116
		return $result->setValues("navbar-right navbar-form", "ul", $elements);
117
	}
118
119
	public static function nav($identifier, $elements=array()) {
120
		$result=new HtmlNavzone($identifier);
121
		return $result->setValues("navbar-nav", "ul", $elements);
122
	}
123
124
	public function run(JsUtils $js) {
125
		foreach ( $this->elements as $element ) {
126
			$element->run($js);
127
		}
128
	}
129
130
	public function getElementsCount() {
131
		return sizeof($this->elements);
132
	}
133
134
	public function fromArray($array) {
135
		return $this->addElements($array);
136
	}
137
138
	public function __toString() {
139
		return $this->compile();
140
	}
141
142
	/**
143
	 * @param int $index
144
	 * @return BaseHtml
145
	 */
146
	public function getElement($index){
147
		return $this->elements[$index];
148
	}
149
}