PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/View/Helper/Navigation/Breadcrumbs.php

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