Completed
Push — master ( 94aea7...5914b8 )
by Jean-Christophe
04:29
created

HtmlBreadcrumb::on()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 4
nc 2
nop 4
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
 * @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 JsUtils $js
93
	 * @param Dispatcher $dispatcher the request dispatcher
94
	 * @return \Ajax\bootstrap\html\HtmlBreadcrumbs
95
	 */
96
	public function fromDispatcher(JsUtils $js,$dispatcher, $startIndex=0) {
97
		return $this->addItems($js->fromDispatcher($dispatcher));
98
	}
99
100
	/**
101
	 * Return the url of the element at $index or the breadcrumbs url if $index is ommited
102
	 * @param int $index
103
	 * @param string $separator
104
	 * @return string
105
	 */
106 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...
107
		if (!isset($index)) {
108
			$index=sizeof($this->content);
109
		}
110
		if ($this->absolutePaths === true) {
111
			return $this->_hrefFunction($this->content[$index]);
112
		} else {
113
			return $this->root . implode($separator, array_slice(array_map(function ($e) {
114
				return $this->_hrefFunction($e);
115
			}, $this->content), $this->startIndex, $index + 1));
116
		}
117
	}
118
119
	/**
120
	 * sets the function who generates the href elements.
121
	 * default : function($element){return $element->getContent()}
122
	 * @param function $_hrefFunction
123
	 * @return \Ajax\bootstrap\html\HtmlBreadcrumbs
124
	 */
125
	public function setHrefFunction($_hrefFunction) {
126
		$this->_hrefFunction=$_hrefFunction;
127
		return $this;
128
	}
129
130
	public function setStartIndex($startIndex) {
131
		$this->startIndex=$startIndex;
132
		return $this;
133
	}
134
135
	public function setAutoActive($autoActive) {
136
		$this->autoActive=$autoActive;
137
		return $this;
138
	}
139
140
	/*
141
	 * (non-PHPdoc)
142
	 * @see \Ajax\bootstrap\html\BaseHtml::compile()
143
	 */
144
	public function compile(JsUtils $js=NULL, $view=NULL) {
145
		if ($this->autoActive) {
146
			$this->setActive();
147
		}
148
		$count=$this->count();
149
		for($i=1; $i < $count; $i++) {
150
			$this->content[$i]->wrap($this->getContentDivider($i - 1));
151
		}
152
		return parent::compile($js, $view);
153
	}
154
155
	/*
156
	 * (non-PHPdoc)
157
	 * @see \Ajax\bootstrap\html\base\BaseHtml::on()
158
	 */
159
	public function on($event, $jsCode, $stopPropagation=false, $preventDefault=false) {
160
		foreach ( $this->content as $element ) {
161
			$element->on($event, $jsCode, $stopPropagation, $preventDefault);
162
		}
163
		return $this;
164
	}
165
166
	public function _ajaxOn($operation, $event, $url, $responseElement="", $parameters=array()) {
167
		foreach ( $this->content as $element ) {
168
			if ($element->getProperty($this->attr) != NULL)
169
				$element->_ajaxOn($operation, $event, $url, $responseElement, $parameters);
170
		}
171
		return $this;
172
	}
173
174
	/**
175
	 *
176
	 * {@inheritDoc}
177
	 *
178
	 * @see \Ajax\common\html\HtmlCollection::createItem()
179
	 */
180
	protected function createItem($value) {
181
		$count=$this->count();
182
		$itemO=new HtmlSemDoubleElement("item-" . $this->identifier . "-" . $count, "a", "section");
183
		if (\is_array($value))
184
			$itemO->fromArray($value);
185
		else {
186
			$itemO->setContent($value);
187
		}
188
		return $itemO;
189
	}
190
191
	public function addIconAt($icon, $index) {
192
		$item=$this->getItem($index);
193
		if (isset($item)) {
194
			$icon=new HtmlIcon("icon-" . $this->identifier, $icon);
195
			$item->wrapContent($icon);
196
		}
197
	}
198
199
	public function addItem($item) {
200
		$count=$this->count();
201
		$itemO=parent::addItem($item);
202
		$this->addToPropertyCtrl("class", "section", array ("section" ));
203
		$itemO->setProperty($this->attr, $this->getHref($count));
204
		return $itemO;
205
	}
206
207
	public function asLinks() {
208
		$this->contentAs("a");
209
	}
210
211
	public function asTexts() {
212
		$this->contentAs("div");
213
	}
214
}