HtmlNavzone::addElements()   B
last analyzed

Complexity

Conditions 8
Paths 2

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 19
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 24
rs 8.4444
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
			}
82
		}
83
		return $this;
84
	}
85
86
	public function addLink($caption, $href="#") {
87
		$iid=$this->getElementsCount()+1;
88
		$li=new HtmlBsDoubleElement($this->identifier."-li-".$iid, "li");
89
		if($caption instanceof HtmlLink){
90
			$link=$caption;
91
		}else{
92
			$link=new HtmlLink($this->identifier."-link-".$iid, $href, $caption);
93
		}
94
		$li->setContent($link);
95
		$this->addElement($li);
96
	}
97
98
	public static function form($identifier, $elements=array()) {
99
		$result=new HtmlNavzone($identifier);
100
		return $result->setValues("navbar-form navbar-left", "form", $elements);
101
	}
102
103
	public static function left($identifier, $elements=array()) {
104
		$result=new HtmlNavzone($identifier);
105
		return $result->setValues("navbar-left", "ul", $elements);
106
	}
107
108
	public static function right($identifier, $elements=array()) {
109
		$result=new HtmlNavzone($identifier);
110
		return $result->setValues("navbar-right", "ul", $elements);
111
	}
112
113
	public static function formRight($identifier, $elements=array()) {
114
		$result=new HtmlNavzone($identifier);
115
		return $result->setValues("navbar-right navbar-form", "ul", $elements);
116
	}
117
118
	public static function nav($identifier, $elements=array()) {
119
		$result=new HtmlNavzone($identifier);
120
		return $result->setValues("navbar-nav", "ul", $elements);
121
	}
122
123
	public function run(JsUtils $js) {
124
		foreach ( $this->elements as $element ) {
125
			$element->run($js);
126
		}
127
	}
128
129
	public function getElementsCount() {
130
		return sizeof($this->elements);
131
	}
132
133
	public function fromArray($array) {
134
		return $this->addElements($array);
135
	}
136
137
	public function __toString() {
138
		return $this->compile();
139
	}
140
141
	/**
142
	 * @param int $index
143
	 * @return BaseHtml
144
	 */
145
	public function getElement($index){
146
		return $this->elements[$index];
147
	}
148
}
149