Completed
Push — master ( 73e21a...085edf )
by Jean-Christophe
04:37
created

HtmlBreadcrumbs::fromDispatcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 3
1
<?php
2
namespace Ajax\bootstrap\html;
3
4
use Ajax\bootstrap\html\base\HtmlBsDoubleElement;
5
use Ajax\bootstrap\html\HtmlLink;
6
use Ajax\JsUtils;
7
8
use Ajax\bootstrap\html\base\HtmlNavElement;
9
/**
10
 * Twitter Bootstrap Breadcrumbs component
11
 * @see http://getbootstrap.com/components/#breadcrumbs
12
 * @author jc
13
 * @version 1.001
14
 * @method _hrefFunction($e)
15
 */
16
class HtmlBreadcrumbs extends HtmlNavElement {
17
18
	/**
19
	 * @var integer the start index for href generation
20
	 */
21
	protected $startIndex=0;
22
	/**
23
	 * @var boolean $autoActive sets the last element's class to <b>active</b> if true
24
	 */
25
	protected $autoActive;
26
27
	/**
28
	 * @var boolean if set to true, the path of the elements is absolute
29
	 */
30
	protected $absolutePaths;
31
32
	/**
33
	 * @var object<Closure> the function who generates the href elements. default : function($e){return $e->getContent()}
34
	 */
35
	protected $_hrefFunction;
36
37
38
	/**
39
	 * @param string $identifier
40
	 * @param array $elements
41
	 * @param boolean $autoActive sets the last element's class to <b>active</b> if true
42
	 * @param function $hrefFunction the function who generates the href elements. default : function($e){return $e->getContent()}
43
	 */
44 View Code Duplication
	public function __construct($identifier,$elements=array(),$autoActive=true,$startIndex=0,$hrefFunction=NULL){
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...
45
		parent::__construct($identifier,"ol");
46
		$this->startIndex=$startIndex;
47
		$this->setProperty("class", "breadcrumb");
48
		$this->content=array();
49
		$this->autoActive=$autoActive;
50
		$this->absolutePaths;
51
		$this->_hrefFunction=function ($e){return $e->getContent();};
52
		if(isset($hrefFunction)){
53
			$this->_hrefFunction=$hrefFunction;
54
		}
55
		$this->addElements($elements);
56
	}
57
58
	/**
59
	 * @param mixed $element
60
	 * @param string $href
61
	 * @return \Ajax\bootstrap\html\HtmlLink
62
	 */
63
	public function addElement($element,$href="",$glyph=NULL){
64
		$size=sizeof($this->content);
65
		if(is_array($element)){
66
			$elm=new HtmlLink("lnk-".$this->identifier."-".$size);
67
			$elm->fromArray($element);
68
		}else if($element instanceof HtmlLink){
69
			$elm=$element;
70
		}else{
71
			$elm=new HtmlLink("lnk-".$this->identifier."-".$size,$href,$element);
72
			if(isset($glyph)){
73
				$elm->wrapContentWithGlyph($glyph);
74
			}
75
		}
76
		$elm->wrap("<li>","</li>");
77
		$this->content[]=$elm;
78
		$elm->setProperty($this->attr, $this->getHref($size));
79
		return $elm;
80
	}
81
82
	public function setActive($index=null){
83
		if(!isset($index)){
84
			$index=sizeof($this->content)-1;
85
		}
86
		$li=new HtmlBsDoubleElement("","li");
87
		$li->setClass("active");
88
		$li->setContent($this->content[$index]->getContent());
89
		$this->content[$index]=$li;
90
	}
91
92
	public function addElements($elements){
93
		foreach ( $elements as $element ) {
94
			$this->addElement($element);
95
		}
96
		return $this;
97
	}
98
99
	public function fromArray($array){
100
		$array=parent::fromArray($array);
101
		$this->addElements($array);
102
		return $array;
103
	}
104
105
	/**
106
	 * Return the url of the element at $index or the breadcrumbs url if $index is ommited
107
	 * @param int $index
108
	 * @param string $separator
109
	 * @return string
110
	 */
111 View Code Duplication
	public function getHref($index=null,$separator="/"){
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...
112
		if(!isset($index)){
113
			$index=sizeof($this->content);
114
		}
115
		if($this->absolutePaths===true){
116
			return $this->_hrefFunction($this->content[$index]);
117
		}else{
118
			return $this->root.implode($separator, array_slice(array_map(function($e){return $this->_hrefFunction($e);}, $this->content),$this->startIndex,$index+1));
119
		}
120
	}
121
122
	/*
123
	 * (non-PHPdoc)
124
	 * @see \Ajax\bootstrap\html\BaseHtml::compile()
125
	 */
126
	public function compile(JsUtils $js=NULL, $view=NULL) {
127
		if($this->autoActive){
128
			$this->setActive();
129
		}
130
		return parent::compile($js, $view);
131
	}
132
133
	/* (non-PHPdoc)
134
	 * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
135
	 */
136
	public function fromDatabaseObject($object, $function) {
137
		return $this->addElement($function($object));
138
	}
139
140
		/*
141
	 * (non-PHPdoc)
142
	 * @see \Ajax\bootstrap\html\base\BaseHtml::on()
143
	 */
144
	public function on($event, $jsCode, $stopPropagation=false, $preventDefault=false) {
145
		foreach ($this->content as $element){
146
			$element->on($event,$jsCode,$stopPropagation,$preventDefault);
147
		}
148
		return $this;
149
	}
150
151
	public function setAutoActive($autoActive) {
152
		$this->autoActive = $autoActive;
153
		return $this;
154
	}
155
156
	public function _ajaxOn($operation, $event, $url, $responseElement="", $parameters=array()) {
157
		foreach ($this->content as $element){
158
			$element->_ajaxOn($operation, $event, $url, $responseElement, $parameters);
159
		}
160
		return $this;
161
	}
162
163
	/**
164
	 * Associate an ajax get to the breadcrumbs elements, displayed in $targetSelector
165
	 * $attr member is used to build each element url
166
	 * @param string $targetSelector the target of the get
167
	 * @return HtmlBreadcrumbs
168
	 */
169
	public function autoGetOnClick($targetSelector){
170
		return $this->getOnClick($this->root, $targetSelector,array("attr"=>$this->attr));
171
	}
172
173
	public function contentAsString(){
174
		if($this->autoActive){
175
			$this->setActive();
176
		}
177
		return parent::contentAsString();
178
	}
179
180
	public function getElement($index){
181
		return $this->content[$index];
182
	}
183
184
	/**
185
	 * Add a glyphicon to the element at index $index
186
	 * @param mixed $glyph
187
	 * @param int $index
188
	 */
189
	public function addGlyph($glyph,$index=0){
190
		$elm=$this->getElement($index);
191
		return $elm->wrapContentWithGlyph($glyph);
192
	}
193
194
	/**
195
	 * Add new elements in breadcrumbs corresponding to request dispatcher : controllerName, actionName, parameters
196
	 * @param JsUtils $js
197
	 * @param Dispatcher $dispatcher the request dispatcher
198
	 * @return \Ajax\bootstrap\html\HtmlBreadcrumbs
199
	 */
200
	public function fromDispatcher(JsUtils $js,$dispatcher,$startIndex=0){
201
		$this->startIndex=$startIndex;
202
		return $this->addElements($js->fromDispatcher($dispatcher));
203
	}
204
205
206
	/**
207
	 * sets the function who generates the href elements. default : function($element){return $element->getContent()}
208
	 * @param function $_hrefFunction
209
	 * @return \Ajax\bootstrap\html\HtmlBreadcrumbs
210
	 */
211
	public function setHrefFunction($_hrefFunction) {
212
		$this->_hrefFunction = $_hrefFunction;
213
		return $this;
214
	}
215
216
	public function setStartIndex($startIndex) {
217
		$this->startIndex=$startIndex;
218
		return $this;
219
	}
220
221
222
}