File indexing completed on 2024-12-29 05:28:12
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_View 0017 * @subpackage Helper 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 * @version $Id$ 0021 */ 0022 0023 /** 0024 * @see Zend_View_Helper_Navigation_HelperAbstract 0025 */ 0026 // require_once 'Zend/View/Helper/Navigation/HelperAbstract.php'; 0027 0028 /** 0029 * Helper for printing breadcrumbs 0030 * 0031 * @category Zend 0032 * @package Zend_View 0033 * @subpackage Helper 0034 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0035 * @license http://framework.zend.com/license/new-bsd New BSD License 0036 */ 0037 class Zend_View_Helper_Navigation_Breadcrumbs 0038 extends Zend_View_Helper_Navigation_HelperAbstract 0039 { 0040 /** 0041 * Breadcrumbs separator string 0042 * 0043 * @var string 0044 */ 0045 protected $_separator = ' > '; 0046 0047 /** 0048 * The minimum depth a page must have to be included when rendering 0049 * 0050 * @var int 0051 */ 0052 protected $_minDepth = 1; 0053 0054 /** 0055 * Whether last page in breadcrumb should be hyperlinked 0056 * 0057 * @var bool 0058 */ 0059 protected $_linkLast = false; 0060 0061 /** 0062 * Partial view script to use for rendering menu 0063 * 0064 * @var string|array 0065 */ 0066 protected $_partial; 0067 0068 /** 0069 * View helper entry point: 0070 * Retrieves helper and optionally sets container to operate on 0071 * 0072 * @param Zend_Navigation_Container $container [optional] container to 0073 * operate on 0074 * @return Zend_View_Helper_Navigation_Breadcrumbs fluent interface, 0075 * returns self 0076 */ 0077 public function breadcrumbs(Zend_Navigation_Container $container = null) 0078 { 0079 if (null !== $container) { 0080 $this->setContainer($container); 0081 } 0082 0083 return $this; 0084 } 0085 0086 // Accessors: 0087 0088 /** 0089 * Sets breadcrumb separator 0090 * 0091 * @param string $separator separator string 0092 * @return Zend_View_Helper_Navigation_Breadcrumbs fluent interface, 0093 * returns self 0094 */ 0095 public function setSeparator($separator) 0096 { 0097 if (is_string($separator)) { 0098 $this->_separator = $separator; 0099 } 0100 0101 return $this; 0102 } 0103 0104 /** 0105 * Returns breadcrumb separator 0106 * 0107 * @return string breadcrumb separator 0108 */ 0109 public function getSeparator() 0110 { 0111 return $this->_separator; 0112 } 0113 0114 /** 0115 * Sets whether last page in breadcrumbs should be hyperlinked 0116 * 0117 * @param bool $linkLast whether last page should 0118 * be hyperlinked 0119 * @return Zend_View_Helper_Navigation_Breadcrumbs fluent interface, 0120 * returns self 0121 */ 0122 public function setLinkLast($linkLast) 0123 { 0124 $this->_linkLast = (bool) $linkLast; 0125 return $this; 0126 } 0127 0128 /** 0129 * Returns whether last page in breadcrumbs should be hyperlinked 0130 * 0131 * @return bool whether last page in breadcrumbs should be hyperlinked 0132 */ 0133 public function getLinkLast() 0134 { 0135 return $this->_linkLast; 0136 } 0137 0138 /** 0139 * Sets which partial view script to use for rendering menu 0140 * 0141 * @param string|array $partial partial view script or 0142 * null. If an array is 0143 * given, it is expected to 0144 * contain two values; 0145 * the partial view script 0146 * to use, and the module 0147 * where the script can be 0148 * found. 0149 * @return Zend_View_Helper_Navigation_Breadcrumbs fluent interface, 0150 * returns self 0151 */ 0152 public function setPartial($partial) 0153 { 0154 if (null === $partial || is_string($partial) || is_array($partial)) { 0155 $this->_partial = $partial; 0156 } 0157 0158 return $this; 0159 } 0160 0161 /** 0162 * Returns partial view script to use for rendering menu 0163 * 0164 * @return string|array|null 0165 */ 0166 public function getPartial() 0167 { 0168 return $this->_partial; 0169 } 0170 0171 // Render methods: 0172 0173 /** 0174 * Renders breadcrumbs by chaining 'a' elements with the separator 0175 * registered in the helper 0176 * 0177 * @param Zend_Navigation_Container $container [optional] container to 0178 * render. Default is to 0179 * render the container 0180 * registered in the helper. 0181 * @return string helper output 0182 */ 0183 public function renderStraight(Zend_Navigation_Container $container = null) 0184 { 0185 if (null === $container) { 0186 $container = $this->getContainer(); 0187 } 0188 0189 // find deepest active 0190 if (!$active = $this->findActive($container)) { 0191 return ''; 0192 } 0193 0194 $active = $active['page']; 0195 0196 // put the deepest active page last in breadcrumbs 0197 if ($this->getLinkLast()) { 0198 $html = $this->htmlify($active); 0199 } else { 0200 $html = $active->getLabel(); 0201 if ($this->getUseTranslator() && $t = $this->getTranslator()) { 0202 $html = $t->translate($html); 0203 } 0204 $html = $this->view->escape($html); 0205 } 0206 0207 // walk back to root 0208 while ($parent = $active->getParent()) { 0209 if ($parent instanceof Zend_Navigation_Page) { 0210 // prepend crumb to html 0211 $html = $this->htmlify($parent) 0212 . $this->getSeparator() 0213 . $html; 0214 } 0215 0216 if ($parent === $container) { 0217 // at the root of the given container 0218 break; 0219 } 0220 0221 $active = $parent; 0222 } 0223 0224 return strlen($html) ? $this->getIndent() . $html : ''; 0225 } 0226 0227 /** 0228 * Renders the given $container by invoking the partial view helper 0229 * 0230 * The container will simply be passed on as a model to the view script, 0231 * so in the script it will be available in <code>$this->container</code>. 0232 * 0233 * @param Zend_Navigation_Container $container [optional] container to 0234 * pass to view script. 0235 * Default is to use the 0236 * container registered in the 0237 * helper. 0238 * @param string|array $partial [optional] partial view 0239 * script to use. Default is 0240 * to use the partial 0241 * registered in the helper. 0242 * If an array is given, it is 0243 * expected to contain two 0244 * values; the partial view 0245 * script to use, and the 0246 * module where the script can 0247 * be found. 0248 * @return string helper output 0249 */ 0250 public function renderPartial(Zend_Navigation_Container $container = null, 0251 $partial = null) 0252 { 0253 if (null === $container) { 0254 $container = $this->getContainer(); 0255 } 0256 0257 if (null === $partial) { 0258 $partial = $this->getPartial(); 0259 } 0260 0261 if (empty($partial)) { 0262 // require_once 'Zend/View/Exception.php'; 0263 $e = new Zend_View_Exception( 0264 'Unable to render menu: No partial view script provided' 0265 ); 0266 $e->setView($this->view); 0267 throw $e; 0268 } 0269 0270 // put breadcrumb pages in model 0271 $model = array('pages' => array()); 0272 if ($active = $this->findActive($container)) { 0273 $active = $active['page']; 0274 $model['pages'][] = $active; 0275 while ($parent = $active->getParent()) { 0276 if ($parent instanceof Zend_Navigation_Page) { 0277 $model['pages'][] = $parent; 0278 } else { 0279 break; 0280 } 0281 0282 if ($parent === $container) { 0283 // break if at the root of the given container 0284 break; 0285 } 0286 0287 $active = $parent; 0288 } 0289 $model['pages'] = array_reverse($model['pages']); 0290 } 0291 0292 if (is_array($partial)) { 0293 if (count($partial) != 2) { 0294 // require_once 'Zend/View/Exception.php'; 0295 $e = new Zend_View_Exception( 0296 'Unable to render menu: A view partial supplied as ' 0297 . 'an array must contain two values: partial view ' 0298 . 'script and module where script can be found' 0299 ); 0300 $e->setView($this->view); 0301 throw $e; 0302 } 0303 0304 return $this->view->partial($partial[0], $partial[1], $model); 0305 } 0306 0307 return $this->view->partial($partial, null, $model); 0308 } 0309 0310 // Zend_View_Helper_Navigation_Helper: 0311 0312 /** 0313 * Renders helper 0314 * 0315 * Implements {@link Zend_View_Helper_Navigation_Helper::render()}. 0316 * 0317 * @param Zend_Navigation_Container $container [optional] container to 0318 * render. Default is to 0319 * render the container 0320 * registered in the helper. 0321 * @return string helper output 0322 */ 0323 public function render(Zend_Navigation_Container $container = null) 0324 { 0325 if ($partial = $this->getPartial()) { 0326 return $this->renderPartial($container, $partial); 0327 } else { 0328 return $this->renderStraight($container); 0329 } 0330 } 0331 }