Completed
Push — master ( b535d8...118a49 )
by Jean-Christophe
03:00
created

HtmlBreadcrumbs::setStartIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 1
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
	/**
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 function 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
	public function __construct($identifier,$elements=array(),$autoActive=true,$startIndex=0,$hrefFunction=NULL){
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();};
0 ignored issues
show
Documentation Bug introduced by
It seems like function ($e) { return $e->getContent(); } of type object<Closure> is incompatible with the declared type object<Ajax\bootstrap\html\function> of property $_hrefFunction.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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 HtmlDoubleElement("","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
	public function getHref($index=null,$separator="/"){
112
		if(!isset($index)){
113
			$index=sizeof($this->content);
114
		}
115
		if($this->absolutePaths===true){
116
			return $this->_hrefFunction($this->content[$index]);
0 ignored issues
show
Documentation Bug introduced by
The method _hrefFunction does not exist on object<Ajax\bootstrap\html\HtmlBreadcrumbs>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
117
		}else{
118
			return $this->root.implode($separator, array_slice(array_map(function($e){return $this->_hrefFunction($e);}, $this->content),$this->startIndex,$index+1));
0 ignored issues
show
Documentation Bug introduced by
The method _hrefFunction does not exist on object<Ajax\bootstrap\html\HtmlBreadcrumbs>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
119
		}
120
	}
121
122
	/*
123
	 * (non-PHPdoc)
124
	 * @see \Ajax\bootstrap\html\BaseHtml::compile()
125
	 */
126
	public function compile(JsUtils $js=NULL, View $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
	 * @param string $attr the html attribute used to build the elements url
0 ignored issues
show
Bug introduced by
There is no parameter named $attr. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
168
	 * @return HtmlBreadcrumbs
169
	 */
170
	public function autoGetOnClick($targetSelector){
171
		return $this->getOnClick($this->root, $targetSelector,array("attr"=>$this->attr));
172
	}
173
174
	public function contentAsString(){
175
		if($this->autoActive){
176
			$this->setActive();
177
		}
178
		return parent::contentAsString();
179
	}
180
181
	public function getElement($index){
182
		return $this->content[$index];
183
	}
184
185
	/**
186
	 * Add a glyphicon to the element at index $index
187
	 * @param mixed $glyph
188
	 * @param int $index
189
	 */
190
	public function addGlyph($glyph,$index=0){
191
		$elm=$this->getElement($index);
192
		return $elm->wrapContentWithGlyph($glyph);
193
	}
194
195
	/**
196
	 * Add new elements in breadcrumbs corresponding to request dispatcher : controllerName, actionName, parameters
197
	 * @param Dispatcher $dispatcher the request dispatcher
198
	 * @return \Ajax\bootstrap\html\HtmlBreadcrumbs
199
	 */
200
	public function fromDispatcher($dispatcher){
201
		$params=$dispatcher->getParams();
202
		$action=$dispatcher->getActionName();
203
		$items=array($dispatcher->getControllerName());
204
		if(\sizeof($params)>0 || \strtolower($action)!="index" ){
205
			$items[]=$action;
206
			$items=array_merge($items,$params);
207
		}
208
		return $this->addElements($items);
209
	}
210
211
212
	/**
213
	 * sets the function who generates the href elements. default : function($element){return $element->getContent()}
214
	 * @param function $_hrefFunction
215
	 * @return \Ajax\bootstrap\html\HtmlBreadcrumbs
216
	 */
217
	public function setHrefFunction($_hrefFunction) {
218
		$this->_hrefFunction = $_hrefFunction;
219
		return $this;
220
	}
221
222
	public function setStartIndex($startIndex) {
223
		$this->startIndex=$startIndex;
224
		return $this;
225
	}
226
227
228
}