This repository was archived by the owner on Jan 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathBreadcrumbs.php
316 lines (284 loc) · 9.76 KB
/
Breadcrumbs.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\View\Helper\Navigation;
use Zend\Navigation\AbstractContainer;
use Zend\Navigation\Page\AbstractPage;
use Zend\View;
use Zend\View\Exception;
/**
* Helper for printing breadcrumbs.
*/
class Breadcrumbs extends AbstractHelper
{
/**
* Whether last page in breadcrumb should be hyperlinked.
*
* @var bool
*/
protected $linkLast = false;
/**
* The minimum depth a page must have to be included when rendering.
*
* @var int
*/
protected $minDepth = 1;
/**
* Partial view script to use for rendering menu.
*
* @var string|array
*/
protected $partial;
/**
* Breadcrumbs separator string.
*
* @var string
*/
protected $separator = ' > ';
/**
* Helper entry point.
*
* @param string|AbstractContainer $container container to operate on
* @return Breadcrumbs
*/
public function __invoke($container = null)
{
if (null !== $container) {
$this->setContainer($container);
}
return $this;
}
/**
* Renders helper.
*
* Implements {@link HelperInterface::render()}.
*
* @param AbstractContainer $container [optional] container to render. Default is
* to render the container registered in the helper.
* @return string
*/
public function render($container = null)
{
$partial = $this->getPartial();
if ($partial) {
return $this->renderPartial($container, $partial);
}
return $this->renderStraight($container);
}
/**
* Renders breadcrumbs by chaining 'a' elements with the separator
* registered in the helper.
*
* @param AbstractContainer $container [optional] container to render. Default is
* to render the container registered in the helper.
* @return string
*/
public function renderStraight($container = null)
{
$this->parseContainer($container);
if (null === $container) {
$container = $this->getContainer();
}
// find deepest active
if (!$active = $this->findActive($container)) {
return '';
}
$active = $active['page'];
// put the deepest active page last in breadcrumbs
if ($this->getLinkLast()) {
$html = $this->htmlify($active);
} else {
/** @var \Zend\View\Helper\EscapeHtml $escaper */
$escaper = $this->view->plugin('escapeHtml');
$html = $escaper(
$this->translate($active->getLabel(), $active->getTextDomain())
);
}
// walk back to root
while ($parent = $active->getParent()) {
if ($parent instanceof AbstractPage) {
// prepend crumb to html
$html = $this->htmlify($parent)
. $this->getSeparator()
. $html;
}
if ($parent === $container) {
// at the root of the given container
break;
}
$active = $parent;
}
return strlen($html) ? $this->getIndent() . $html : '';
}
/**
* Renders the given $container by invoking the partial view helper.
*
* The container will simply be passed on as a model to the view script
* as-is, and will be available in the partial script as 'container', e.g.
* <code>echo 'Number of pages: ', count($this->container);</code>.
*
* @param null|AbstractContainer $container [optional] container to pass to view
* script. Default is to use the container registered in the helper.
* @param null|string|array $partial [optional] partial view script to use.
* Default is to use the partial registered in the helper. If an array
* is given, it is expected to contain two values; the partial view
* script to use, and the module where the script can be found.
* @return string
* @throws Exception\RuntimeException if no partial provided
* @throws Exception\InvalidArgumentException if partial is invalid array
*/
public function renderPartial($container = null, $partial = null)
{
return $this->renderPartialModel([], $container, $partial);
}
/**
* Renders the given $container by invoking the partial view helper with the given parameters as the model.
*
* The container will simply be passed on as a model to the view script
* as-is, and will be available in the partial script as 'container', e.g.
* <code>echo 'Number of pages: ', count($this->container);</code>.
*
* Any parameters provided will be passed to the partial via the view model.
*
* @param null|AbstractContainer $container [optional] container to pass to view
* script. Default is to use the container registered in the helper.
* @param null|string|array $partial [optional] partial view script to use.
* Default is to use the partial registered in the helper. If an array
* is given, it is expected to contain two values; the partial view
* script to use, and the module where the script can be found.
* @return string
* @throws Exception\RuntimeException if no partial provided
* @throws Exception\InvalidArgumentException if partial is invalid array
*/
public function renderPartialWithParams(array $params = [], $container = null, $partial = null)
{
return $this->renderPartialModel($params, $container, $partial);
}
/**
* Sets whether last page in breadcrumbs should be hyperlinked.
*
* @param bool $linkLast whether last page should be hyperlinked
* @return Breadcrumbs
*/
public function setLinkLast($linkLast)
{
$this->linkLast = (bool) $linkLast;
return $this;
}
/**
* Returns whether last page in breadcrumbs should be hyperlinked.
*
* @return bool
*/
public function getLinkLast()
{
return $this->linkLast;
}
/**
* Sets which partial view script to use for rendering menu.
*
* @param string|array $partial partial view script or null. If an array is
* given, it is expected to contain two values; the partial view script
* to use, and the module where the script can be found.
* @return Breadcrumbs
*/
public function setPartial($partial)
{
if (null === $partial || is_string($partial) || is_array($partial)) {
$this->partial = $partial;
}
return $this;
}
/**
* Returns partial view script to use for rendering menu.
*
* @return string|array|null
*/
public function getPartial()
{
return $this->partial;
}
/**
* Sets breadcrumb separator.
*
* @param string $separator separator string
* @return Breadcrumbs
*/
public function setSeparator($separator)
{
if (is_string($separator)) {
$this->separator = $separator;
}
return $this;
}
/**
* Returns breadcrumb separator.
*
* @return string breadcrumb separator
*/
public function getSeparator()
{
return $this->separator;
}
/**
* Render a partial with the given "model".
*
* @param array $params
* @param null|AbstractContainer $container
* @param null|string|array $partial
* @return string
* @throws Exception\RuntimeException if no partial provided
* @throws Exception\InvalidArgumentException if partial is invalid array
*/
protected function renderPartialModel(array $params, $container, $partial)
{
$this->parseContainer($container);
if (null === $container) {
$container = $this->getContainer();
}
if (null === $partial) {
$partial = $this->getPartial();
}
if (empty($partial)) {
throw new Exception\RuntimeException(
'Unable to render breadcrumbs: No partial view script provided'
);
}
$model = array_merge($params, ['pages' => []], ['separator' => $this->getSeparator()]);
$active = $this->findActive($container);
if ($active) {
$active = $active['page'];
$model['pages'][] = $active;
while ($parent = $active->getParent()) {
if (! $parent instanceof AbstractPage) {
break;
}
$model['pages'][] = $parent;
if ($parent === $container) {
// break if at the root of the given container
break;
}
$active = $parent;
}
$model['pages'] = array_reverse($model['pages']);
}
/** @var \Zend\View\Helper\Partial $partialHelper */
$partialHelper = $this->view->plugin('partial');
if (is_array($partial)) {
if (count($partial) != 2) {
throw new Exception\InvalidArgumentException(
'Unable to render breadcrumbs: A view partial supplied as '
. 'an array must contain two values: partial view '
. 'script and module where script can be found'
);
}
return $partialHelper($partial[0], $model);
}
return $partialHelper($partial, $model);
}
}