Completed
Push — master ( b4ccca...781bd5 )
by Jean-Christophe
03:50
created

HtmlBreadcrumb::addIcon()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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