/library/Zend/View/Helper/Navigation/Breadcrumbs.php

https://github.com/knutBachmann/zf2 · PHP · 335 lines · 138 code · 37 blank · 160 comment · 27 complexity · a4705f602ae55254e1af2b38edd69ed2 MD5 · raw file

  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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\View\Helper\Navigation;
  25. use Zend\Navigation\Container,
  26. Zend\Navigation\AbstractPage,
  27. Zend\View;
  28. /**
  29. * Helper for printing breadcrumbs
  30. *
  31. * @uses \Zend\View\Exception
  32. * @uses \Zend\View\Helper\Navigation\AbstractHelper
  33. * @category Zend
  34. * @package Zend_View
  35. * @subpackage Helper
  36. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class Breadcrumbs extends AbstractHelper
  40. {
  41. /**
  42. * Breadcrumbs separator string
  43. *
  44. * @var string
  45. */
  46. protected $_separator = ' &gt; ';
  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. * Whether last page in breadcrumb should be hyperlinked
  55. *
  56. * @var bool
  57. */
  58. protected $_linkLast = false;
  59. /**
  60. * Partial view script to use for rendering menu
  61. *
  62. * @var string|array
  63. */
  64. protected $_partial;
  65. /**
  66. * View helper entry point:
  67. * Retrieves helper and optionally sets container to operate on
  68. *
  69. * @param \Zend\Navigation\Container $container [optional] container to
  70. * operate on
  71. * @return \Zend\View\Helper\Navigation\Breadcrumbs fluent interface,
  72. * returns self
  73. */
  74. public function __invoke(Container $container = null)
  75. {
  76. if (null !== $container) {
  77. $this->setContainer($container);
  78. }
  79. return $this;
  80. }
  81. // Accessors:
  82. /**
  83. * Sets breadcrumb separator
  84. *
  85. * @param string $separator separator string
  86. * @return \Zend\View\Helper\Navigation\Breadcrumbs fluent interface,
  87. * returns self
  88. */
  89. public function setSeparator($separator)
  90. {
  91. if (is_string($separator)) {
  92. $this->_separator = $separator;
  93. }
  94. return $this;
  95. }
  96. /**
  97. * Returns breadcrumb separator
  98. *
  99. * @return string breadcrumb separator
  100. */
  101. public function getSeparator()
  102. {
  103. return $this->_separator;
  104. }
  105. /**
  106. * Sets whether last page in breadcrumbs should be hyperlinked
  107. *
  108. * @param bool $linkLast whether last page should
  109. * be hyperlinked
  110. * @return \Zend\View\Helper\Navigation\Breadcrumbs fluent interface,
  111. * returns self
  112. */
  113. public function setLinkLast($linkLast)
  114. {
  115. $this->_linkLast = (bool) $linkLast;
  116. return $this;
  117. }
  118. /**
  119. * Returns whether last page in breadcrumbs should be hyperlinked
  120. *
  121. * @return bool whether last page in breadcrumbs should be hyperlinked
  122. */
  123. public function getLinkLast()
  124. {
  125. return $this->_linkLast;
  126. }
  127. /**
  128. * Sets which partial view script to use for rendering menu
  129. *
  130. * @param string|array $partial partial view script or
  131. * null. If an array is
  132. * given, it is expected to
  133. * contain two values;
  134. * the partial view script
  135. * to use, and the module
  136. * where the script can be
  137. * found.
  138. * @return \Zend\View\Helper\Navigation\Breadcrumbs fluent interface,
  139. * returns self
  140. */
  141. public function setPartial($partial)
  142. {
  143. if (null === $partial || is_string($partial) || is_array($partial)) {
  144. $this->_partial = $partial;
  145. }
  146. return $this;
  147. }
  148. /**
  149. * Returns partial view script to use for rendering menu
  150. *
  151. * @return string|array|null
  152. */
  153. public function getPartial()
  154. {
  155. return $this->_partial;
  156. }
  157. // Render methods:
  158. /**
  159. * Renders breadcrumbs by chaining 'a' elements with the separator
  160. * registered in the helper
  161. *
  162. * @param \Zend\Navigation\Container $container [optional] container to
  163. * render. Default is to
  164. * render the container
  165. * registered in the helper.
  166. * @return string helper output
  167. */
  168. public function renderStraight(Container $container = null)
  169. {
  170. if (null === $container) {
  171. $container = $this->getContainer();
  172. }
  173. // find deepest active
  174. if (!$active = $this->findActive($container)) {
  175. return '';
  176. }
  177. $active = $active['page'];
  178. // put the deepest active page last in breadcrumbs
  179. if ($this->getLinkLast()) {
  180. $html = $this->htmlify($active);
  181. } else {
  182. $html = $active->getLabel();
  183. if ($this->getUseTranslator() && $t = $this->getTranslator()) {
  184. $html = $t->translate($html);
  185. }
  186. $html = $this->view->vars()->escape($html);
  187. }
  188. // walk back to root
  189. while ($parent = $active->getParent()) {
  190. if ($parent instanceof AbstractPage) {
  191. // prepend crumb to html
  192. $html = $this->htmlify($parent)
  193. . $this->getSeparator()
  194. . $html;
  195. }
  196. if ($parent === $container) {
  197. // at the root of the given container
  198. break;
  199. }
  200. $active = $parent;
  201. }
  202. return strlen($html) ? $this->getIndent() . $html : '';
  203. }
  204. /**
  205. * Renders the given $container by invoking the partial view helper
  206. *
  207. * The container will simply be passed on as a model to the view script,
  208. * so in the script it will be available in <code>$this->container</code>.
  209. *
  210. * @param \Zend\Navigation\Container $container [optional] container to
  211. * pass to view script.
  212. * Default is to use the
  213. * container registered in the
  214. * helper.
  215. * @param string|array $partial [optional] partial view
  216. * script to use. Default is
  217. * to use the partial
  218. * registered in the helper.
  219. * If an array is given, it is
  220. * expected to contain two
  221. * values; the partial view
  222. * script to use, and the
  223. * module where the script can
  224. * be found.
  225. * @return string helper output
  226. */
  227. public function renderPartial(Container $container = null,
  228. $partial = null)
  229. {
  230. if (null === $container) {
  231. $container = $this->getContainer();
  232. }
  233. if (null === $partial) {
  234. $partial = $this->getPartial();
  235. }
  236. if (empty($partial)) {
  237. $e = new View\Exception(
  238. 'Unable to render menu: No partial view script provided'
  239. );
  240. $e->setView($this->view);
  241. throw $e;
  242. }
  243. // put breadcrumb pages in model
  244. $model = array('pages' => array());
  245. if ($active = $this->findActive($container)) {
  246. $active = $active['page'];
  247. $model['pages'][] = $active;
  248. while ($parent = $active->getParent()) {
  249. if ($parent instanceof AbstractPage) {
  250. $model['pages'][] = $parent;
  251. } else {
  252. break;
  253. }
  254. if ($parent === $container) {
  255. // break if at the root of the given container
  256. break;
  257. }
  258. $active = $parent;
  259. }
  260. $model['pages'] = array_reverse($model['pages']);
  261. }
  262. if (is_array($partial)) {
  263. if (count($partial) != 2) {
  264. $e = new View\Exception(
  265. 'Unable to render menu: A view partial supplied as '
  266. . 'an array must contain two values: partial view '
  267. . 'script and module where script can be found'
  268. );
  269. $e->setView($this->view);
  270. throw $e;
  271. }
  272. $partialHelper = $this->view->plugin('partial');
  273. return $partialHelper($partial[0], $partial[1], $model);
  274. }
  275. $partialHelper = $this->view->plugin('partial');
  276. return $partialHelper($partial, null, $model);
  277. }
  278. // Zend\View\Helper\Navigation\Helper:
  279. /**
  280. * Renders helper
  281. *
  282. * Implements {@link Zend\View\Helper\Navigation\Helper::render()}.
  283. *
  284. * @param \Zend\Navigation\Container $container [optional] container to
  285. * render. Default is to
  286. * render the container
  287. * registered in the helper.
  288. * @return string helper output
  289. */
  290. public function render(Container $container = null)
  291. {
  292. if ($partial = $this->getPartial()) {
  293. return $this->renderPartial($container, $partial);
  294. } else {
  295. return $this->renderStraight($container);
  296. }
  297. }
  298. }