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