|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_View |
|
17 * @subpackage Helper |
|
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 * @version $Id: Breadcrumbs.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
21 */ |
|
22 |
|
23 /** |
|
24 * @see Zend_View_Helper_Navigation_HelperAbstract |
|
25 */ |
|
26 require_once 'Zend/View/Helper/Navigation/HelperAbstract.php'; |
|
27 |
|
28 /** |
|
29 * Helper for printing breadcrumbs |
|
30 * |
|
31 * @category Zend |
|
32 * @package Zend_View |
|
33 * @subpackage Helper |
|
34 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
35 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
36 */ |
|
37 class Zend_View_Helper_Navigation_Breadcrumbs |
|
38 extends Zend_View_Helper_Navigation_HelperAbstract |
|
39 { |
|
40 /** |
|
41 * Breadcrumbs separator string |
|
42 * |
|
43 * @var string |
|
44 */ |
|
45 protected $_separator = ' > '; |
|
46 |
|
47 /** |
|
48 * The minimum depth a page must have to be included when rendering |
|
49 * |
|
50 * @var int |
|
51 */ |
|
52 protected $_minDepth = 1; |
|
53 |
|
54 /** |
|
55 * Whether last page in breadcrumb should be hyperlinked |
|
56 * |
|
57 * @var bool |
|
58 */ |
|
59 protected $_linkLast = false; |
|
60 |
|
61 /** |
|
62 * Partial view script to use for rendering menu |
|
63 * |
|
64 * @var string|array |
|
65 */ |
|
66 protected $_partial; |
|
67 |
|
68 /** |
|
69 * View helper entry point: |
|
70 * Retrieves helper and optionally sets container to operate on |
|
71 * |
|
72 * @param Zend_Navigation_Container $container [optional] container to |
|
73 * operate on |
|
74 * @return Zend_View_Helper_Navigation_Breadcrumbs fluent interface, |
|
75 * returns self |
|
76 */ |
|
77 public function breadcrumbs(Zend_Navigation_Container $container = null) |
|
78 { |
|
79 if (null !== $container) { |
|
80 $this->setContainer($container); |
|
81 } |
|
82 |
|
83 return $this; |
|
84 } |
|
85 |
|
86 // Accessors: |
|
87 |
|
88 /** |
|
89 * Sets breadcrumb separator |
|
90 * |
|
91 * @param string $separator separator string |
|
92 * @return Zend_View_Helper_Navigation_Breadcrumbs fluent interface, |
|
93 * returns self |
|
94 */ |
|
95 public function setSeparator($separator) |
|
96 { |
|
97 if (is_string($separator)) { |
|
98 $this->_separator = $separator; |
|
99 } |
|
100 |
|
101 return $this; |
|
102 } |
|
103 |
|
104 /** |
|
105 * Returns breadcrumb separator |
|
106 * |
|
107 * @return string breadcrumb separator |
|
108 */ |
|
109 public function getSeparator() |
|
110 { |
|
111 return $this->_separator; |
|
112 } |
|
113 |
|
114 /** |
|
115 * Sets whether last page in breadcrumbs should be hyperlinked |
|
116 * |
|
117 * @param bool $linkLast whether last page should |
|
118 * be hyperlinked |
|
119 * @return Zend_View_Helper_Navigation_Breadcrumbs fluent interface, |
|
120 * returns self |
|
121 */ |
|
122 public function setLinkLast($linkLast) |
|
123 { |
|
124 $this->_linkLast = (bool) $linkLast; |
|
125 return $this; |
|
126 } |
|
127 |
|
128 /** |
|
129 * Returns whether last page in breadcrumbs should be hyperlinked |
|
130 * |
|
131 * @return bool whether last page in breadcrumbs should be hyperlinked |
|
132 */ |
|
133 public function getLinkLast() |
|
134 { |
|
135 return $this->_linkLast; |
|
136 } |
|
137 |
|
138 /** |
|
139 * Sets which partial view script to use for rendering menu |
|
140 * |
|
141 * @param string|array $partial partial view script or |
|
142 * null. If an array is |
|
143 * given, it is expected to |
|
144 * contain two values; |
|
145 * the partial view script |
|
146 * to use, and the module |
|
147 * where the script can be |
|
148 * found. |
|
149 * @return Zend_View_Helper_Navigation_Breadcrumbs fluent interface, |
|
150 * returns self |
|
151 */ |
|
152 public function setPartial($partial) |
|
153 { |
|
154 if (null === $partial || is_string($partial) || is_array($partial)) { |
|
155 $this->_partial = $partial; |
|
156 } |
|
157 |
|
158 return $this; |
|
159 } |
|
160 |
|
161 /** |
|
162 * Returns partial view script to use for rendering menu |
|
163 * |
|
164 * @return string|array|null |
|
165 */ |
|
166 public function getPartial() |
|
167 { |
|
168 return $this->_partial; |
|
169 } |
|
170 |
|
171 // Render methods: |
|
172 |
|
173 /** |
|
174 * Renders breadcrumbs by chaining 'a' elements with the separator |
|
175 * registered in the helper |
|
176 * |
|
177 * @param Zend_Navigation_Container $container [optional] container to |
|
178 * render. Default is to |
|
179 * render the container |
|
180 * registered in the helper. |
|
181 * @return string helper output |
|
182 */ |
|
183 public function renderStraight(Zend_Navigation_Container $container = null) |
|
184 { |
|
185 if (null === $container) { |
|
186 $container = $this->getContainer(); |
|
187 } |
|
188 |
|
189 // find deepest active |
|
190 if (!$active = $this->findActive($container)) { |
|
191 return ''; |
|
192 } |
|
193 |
|
194 $active = $active['page']; |
|
195 |
|
196 // put the deepest active page last in breadcrumbs |
|
197 if ($this->getLinkLast()) { |
|
198 $html = $this->htmlify($active); |
|
199 } else { |
|
200 $html = $active->getLabel(); |
|
201 if ($this->getUseTranslator() && $t = $this->getTranslator()) { |
|
202 $html = $t->translate($html); |
|
203 } |
|
204 $html = $this->view->escape($html); |
|
205 } |
|
206 |
|
207 // walk back to root |
|
208 while ($parent = $active->getParent()) { |
|
209 if ($parent instanceof Zend_Navigation_Page) { |
|
210 // prepend crumb to html |
|
211 $html = $this->htmlify($parent) |
|
212 . $this->getSeparator() |
|
213 . $html; |
|
214 } |
|
215 |
|
216 if ($parent === $container) { |
|
217 // at the root of the given container |
|
218 break; |
|
219 } |
|
220 |
|
221 $active = $parent; |
|
222 } |
|
223 |
|
224 return strlen($html) ? $this->getIndent() . $html : ''; |
|
225 } |
|
226 |
|
227 /** |
|
228 * Renders the given $container by invoking the partial view helper |
|
229 * |
|
230 * The container will simply be passed on as a model to the view script, |
|
231 * so in the script it will be available in <code>$this->container</code>. |
|
232 * |
|
233 * @param Zend_Navigation_Container $container [optional] container to |
|
234 * pass to view script. |
|
235 * Default is to use the |
|
236 * container registered in the |
|
237 * helper. |
|
238 * @param string|array $partial [optional] partial view |
|
239 * script to use. Default is |
|
240 * to use the partial |
|
241 * registered in the helper. |
|
242 * If an array is given, it is |
|
243 * expected to contain two |
|
244 * values; the partial view |
|
245 * script to use, and the |
|
246 * module where the script can |
|
247 * be found. |
|
248 * @return string helper output |
|
249 */ |
|
250 public function renderPartial(Zend_Navigation_Container $container = null, |
|
251 $partial = null) |
|
252 { |
|
253 if (null === $container) { |
|
254 $container = $this->getContainer(); |
|
255 } |
|
256 |
|
257 if (null === $partial) { |
|
258 $partial = $this->getPartial(); |
|
259 } |
|
260 |
|
261 if (empty($partial)) { |
|
262 require_once 'Zend/View/Exception.php'; |
|
263 $e = new Zend_View_Exception( |
|
264 'Unable to render menu: No partial view script provided' |
|
265 ); |
|
266 $e->setView($this->view); |
|
267 throw $e; |
|
268 } |
|
269 |
|
270 // put breadcrumb pages in model |
|
271 $model = array('pages' => array()); |
|
272 if ($active = $this->findActive($container)) { |
|
273 $active = $active['page']; |
|
274 $model['pages'][] = $active; |
|
275 while ($parent = $active->getParent()) { |
|
276 if ($parent instanceof Zend_Navigation_Page) { |
|
277 $model['pages'][] = $parent; |
|
278 } else { |
|
279 break; |
|
280 } |
|
281 |
|
282 if ($parent === $container) { |
|
283 // break if at the root of the given container |
|
284 break; |
|
285 } |
|
286 |
|
287 $active = $parent; |
|
288 } |
|
289 $model['pages'] = array_reverse($model['pages']); |
|
290 } |
|
291 |
|
292 if (is_array($partial)) { |
|
293 if (count($partial) != 2) { |
|
294 require_once 'Zend/View/Exception.php'; |
|
295 $e = new Zend_View_Exception( |
|
296 'Unable to render menu: A view partial supplied as ' |
|
297 . 'an array must contain two values: partial view ' |
|
298 . 'script and module where script can be found' |
|
299 ); |
|
300 $e->setView($this->view); |
|
301 throw $e; |
|
302 } |
|
303 |
|
304 return $this->view->partial($partial[0], $partial[1], $model); |
|
305 } |
|
306 |
|
307 return $this->view->partial($partial, null, $model); |
|
308 } |
|
309 |
|
310 // Zend_View_Helper_Navigation_Helper: |
|
311 |
|
312 /** |
|
313 * Renders helper |
|
314 * |
|
315 * Implements {@link Zend_View_Helper_Navigation_Helper::render()}. |
|
316 * |
|
317 * @param Zend_Navigation_Container $container [optional] container to |
|
318 * render. Default is to |
|
319 * render the container |
|
320 * registered in the helper. |
|
321 * @return string helper output |
|
322 */ |
|
323 public function render(Zend_Navigation_Container $container = null) |
|
324 { |
|
325 if ($partial = $this->getPartial()) { |
|
326 return $this->renderPartial($container, $partial); |
|
327 } else { |
|
328 return $this->renderStraight($container); |
|
329 } |
|
330 } |
|
331 } |