Passed
Push — master ( 0bebef...727fe8 )
by Jean-Christophe
02:17
created

HtmlBreadcrumb   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 204
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 204
rs 10
c 0
b 0
f 0
wmc 30

17 Methods

Rating   Name   Duplication   Size   Complexity  
A fromDispatcher() 0 3 1
A __construct() 0 12 2
A autoGetOnClick() 0 2 1
A setActive() 0 7 2
A contentAsString() 0 5 2
A getHref() 0 10 3
A addItem() 0 6 1
A setHrefFunction() 0 3 1
A compile() 0 9 3
A setAutoActive() 0 3 1
A addIconAt() 0 5 2
A on() 0 5 2
A _ajaxOn() 0 7 3
A setStartIndex() 0 3 1
A createItem() 0 9 2
A asTexts() 0 2 1
A setAbsolutePaths() 0 7 2
1
<?php
2
3
namespace Ajax\semantic\html\collections;
4
5
use Ajax\semantic\html\base\HtmlSemNavElement;
6
use Ajax\semantic\html\base\HtmlSemDoubleElement;
7
8
use Ajax\semantic\html\elements\HtmlIcon;
9
use Ajax\JsUtils;
10
11
/**
12
 * Semantic UI Breadcrumb component
13
 *
14
 * @see http://semantic-ui.com/collections/breadcrumb.html
15
 * @method _hrefFunction($e)
16
 * @author jc
17
 * @version 1.001
18
 */
19
class HtmlBreadcrumb extends HtmlSemNavElement {
20
	/**
21
	 *
22
	 * @var integer the start index for href generation
23
	 */
24
	protected $startIndex=0;
25
	/**
26
	 *
27
	 * @var boolean $autoActive sets the last element's class to <b>active</b> if true
28
	 */
29
	protected $autoActive;
30
31
	/**
32
	 *
33
	 * @var boolean if set to true, the path of the elements is absolute
34
	 */
35
	protected $absolutePaths=false;
36
37
	/**
38
	 *
39
	 * @var callable the function who generates the href elements. default : function($e){return $e->getContent()}
40
	 */
41
	protected $_hrefFunction;
42
43
	/**
44
	 *
45
	 * @param string $identifier
46
	 * @param array $items
47
	 * @param boolean $autoActive sets the last element's class to <b>active</b> if true
48
	 * @param callable $hrefFunction the function who generates the href elements. default : function($e){return $e->getContent()}
49
	 */
50
	public function __construct($identifier, $items=array(), $autoActive=true, $startIndex=0, $hrefFunction=NULL) {
51
		parent::__construct($identifier, "div", "ui breadcrumb");
52
		$this->startIndex=$startIndex;
53
		$this->autoActive=$autoActive;
54
		$this->_contentSeparator="<div class='divider'> / </div>";
55
		$this->_hrefFunction=function (HtmlSemDoubleElement $e) {
56
			return $e->getContent();
57
		};
58
		if (isset($hrefFunction)) {
59
			$this->_hrefFunction=$hrefFunction;
60
		}
61
		$this->addItems($items);
62
	}
63
64
	/**
65
	 * Associate an ajax get to the breadcrumb elements, displayed in $targetSelector
66
	 * $this->attr member is used to build each element url
67
	 * @param string $targetSelector the target of the get
68
	 * @return HtmlBreadcrumb
69
	 */
70
	public function autoGetOnClick($targetSelector) {
71
		return $this->getOnClick($this->root, $targetSelector, array ("attr" => $this->attr ));
72
	}
73
74
	public function contentAsString() {
75
		if ($this->autoActive) {
76
			$this->setActive();
77
		}
78
		return parent::contentAsString();
79
	}
80
81
	/**
82
	 * @param int $index
83
	 */
84
	public function setActive($index=null) {
85
		if (!isset($index)) {
86
			$index=sizeof($this->content) - 1;
87
		}
88
		$activeItem=$this->content[$index];
89
		$activeItem->addToProperty("class", "active");
90
		$activeItem->setTagName("div");
91
	}
92
93
	/**
94
	 * Adds new elements in breadcrumbs corresponding to request dispatcher : controllerName, actionName, parameters
95
	 * @param JsUtils $js
96
	 * @param object $dispatcher the request dispatcher
97
	 * @return HtmlBreadcrumb
98
	 */
99
	public function fromDispatcher(JsUtils $js,$dispatcher, $startIndex=0) {
100
		$this->startIndex=$startIndex;
101
		return $this->addItems($js->fromDispatcher($dispatcher));
102
	}
103
104
	/**
105
	 * Returns the url of the element at $index or the breadcrumbs url if $index is ommited
106
	 * @param int $index
107
	 * @param string $separator
108
	 * @return string
109
	 */
110
	public function getHref($index=null, $separator="/") {
111
		if (!isset($index)) {
112
			$index=sizeof($this->content);
113
		}
114
		if ($this->absolutePaths === true) {
115
			return $this->_hrefFunction($this->content[$index]);
116
		} else {
117
			return $this->root . implode($separator, array_slice(array_map(function ($e) {
118
				return $this->_hrefFunction($e);
119
			}, $this->content), $this->startIndex, $index + 1));
120
		}
121
	}
122
123
	/**
124
	 * sets the function who generates the href elements.
125
	 * default : function($element){return $element->getContent()}
126
	 * @param callable $_hrefFunction
127
	 * @return HtmlBreadcrumb
128
	 */
129
	public function setHrefFunction($_hrefFunction) {
130
		$this->_hrefFunction=$_hrefFunction;
131
		return $this;
132
	}
133
134
	public function setStartIndex($startIndex) {
135
		$this->startIndex=$startIndex;
136
		return $this;
137
	}
138
139
	public function setAutoActive($autoActive) {
140
		$this->autoActive=$autoActive;
141
		return $this;
142
	}
143
144
	/*
145
	 * (non-PHPdoc)
146
	 * @see \Ajax\bootstrap\html\BaseHtml::compile()
147
	 */
148
	public function compile(JsUtils $js=NULL, &$view=NULL) {
149
		if ($this->autoActive) {
150
			$this->setActive();
151
		}
152
		$count=$this->count();
153
		for($i=1; $i < $count; $i++) {
154
			$this->content[$i]->wrap($this->getContentDivider($i - 1));
155
		}
156
		return parent::compile($js, $view);
157
	}
158
159
	/*
160
	 * (non-PHPdoc)
161
	 * @see \Ajax\bootstrap\html\base\BaseHtml::on()
162
	 */
163
	public function on($event, $jsCode, $stopPropagation=false, $preventDefault=false) {
164
		foreach ( $this->content as $element ) {
165
			$element->on($event, $jsCode, $stopPropagation, $preventDefault);
166
		}
167
		return $this;
168
	}
169
170
	public function _ajaxOn($operation, $event, $url, $responseElement="", $parameters=array()) {
171
		foreach ( $this->content as $element ) {
172
			if ($element->getProperty($this->attr) != NULL){
173
				$element->_ajaxOn($operation, $event, $url, $responseElement, $parameters);
174
			}
175
		}
176
		return $this;
177
	}
178
179
	/**
180
	 *
181
	 * {@inheritDoc}
182
	 *
183
	 * @see \Ajax\common\html\HtmlCollection::createItem()
184
	 */
185
	protected function createItem($value) {
186
		$count=$this->count();
187
		$itemO=new HtmlSemDoubleElement("item-" . $this->identifier . "-" . $count, "a", "section");
188
		if (\is_array($value))
189
			$itemO->fromArray($value);
190
		else {
191
			$itemO->setContent($value);
192
		}
193
		return $itemO;
194
	}
195
196
	public function addIconAt($icon, $index) {
197
		$item=$this->getItem($index);
198
		if (isset($item)) {
199
			$icon=new HtmlIcon("icon-" . $this->identifier, $icon);
200
			$item->wrapContent($icon);
201
		}
202
	}
203
204
	public function addItem($item) {
205
		$count=$this->count();
206
		$itemO=parent::addItem($item);
207
		$this->addToPropertyCtrl("class", "section", array ("section" ));
208
		$itemO->setProperty($this->attr, $this->getHref($count));
209
		return $itemO;
210
	}
211
212
	public function asTexts() {
213
		$this->contentAs("div");
214
	}
215
216
	public function setAbsolutePaths($absolutePaths) {
217
		$this->absolutePaths=$absolutePaths;
218
		$size=\sizeof($this->content);
219
		for($i=0;$i<$size;$i++){
220
			$this->content[$i]->setProperty($this->attr, $this->getHref($i));
221
		}
222
		return $this;
223
	}
224
225
}
226