Breadcrumbs.php 11 KB

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