element * * @var bool */ protected $addClassToListItem = false; /** * Whether labels should be escaped * * @var bool */ protected $escapeLabels = true; /** * Whether only active branch should be rendered * * @var bool */ protected $onlyActiveBranch = false; /** * Partial view script to use for rendering menu * * @var string|array */ protected $partial = null; /** * Whether parents should be rendered when only rendering active branch * * @var bool */ protected $renderParents = true; /** * CSS class to use for the ul element * * @var string */ protected $ulClass = 'navigation'; /** * View helper entry point: * Retrieves helper and optionally sets container to operate on * * @param AbstractContainer $container [optional] container to operate on * @return Menu */ public function __invoke($container = null) { if (null !== $container) { $this->setContainer($container); } return $this; } /** * Renders menu * * Implements {@link HelperInterface::render()}. * * If a partial view is registered in the helper, the menu will be rendered * using the given partial script. If no partial is registered, the menu * will be rendered as an 'ul' element by the helper's internal method. * * @see renderPartial() * @see renderMenu() * * @param AbstractContainer $container [optional] container to render. Default is * to render the container registered in the helper. * @return string */ public function render($container = null) { $partial = $this->getPartial(); if ($partial) { return $this->renderPartial($container, $partial); } return $this->renderMenu($container); } /** * Renders the deepest active menu within [$minDepth, $maxDepth], (called * from {@link renderMenu()}) * * @param AbstractContainer $container container to render * @param string $ulClass CSS class for first UL * @param string $indent initial indentation * @param int|null $minDepth minimum depth * @param int|null $maxDepth maximum depth * @param bool $escapeLabels Whether or not to escape the labels * @param bool $addClassToListItem Whether or not page class applied to
echo 'Number of pages: ', count($this->container);
.
*
* @param AbstractContainer $container [optional] container to pass to view
* script. Default is to use the container
* registered in the helper.
* @param string|array $partial [optional] partial view script to use.
* Default is to use the partial
* registered in the helper. If an array
* is given, it is expected to contain two
* values; the partial view script to use,
* and the module where the script can be
* found.
* @return string
* @throws Exception\RuntimeException if no partial provided
* @throws Exception\InvalidArgumentException if partial is invalid array
*/
public function renderPartial($container = null, $partial = null)
{
$this->parseContainer($container);
if (null === $container) {
$container = $this->getContainer();
}
if (null === $partial) {
$partial = $this->getPartial();
}
if (empty($partial)) {
throw new Exception\RuntimeException(
'Unable to render menu: No partial view script provided'
);
}
$model = array(
'container' => $container
);
if (is_array($partial)) {
if (count($partial) != 2) {
throw new Exception\InvalidArgumentException(
'Unable to render menu: A view partial supplied as '
. 'an array must contain two values: partial view '
. 'script and module where script can be found'
);
}
$partialHelper = $this->view->plugin('partial');
return $partialHelper($partial[0], /*$partial[1], */$model);
}
$partialHelper = $this->view->plugin('partial');
return $partialHelper($partial, $model);
}
/**
* Renders the inner-most sub menu for the active page in the $container
*
* This is a convenience method which is equivalent to the following call:
*
* renderMenu($container, array(
* 'indent' => $indent,
* 'ulClass' => $ulClass,
* 'minDepth' => null,
* 'maxDepth' => null,
* 'onlyActiveBranch' => true,
* 'renderParents' => false
* ));
*
*
* @param AbstractContainer $container [optional] container to
* render. Default is to render
* the container registered in
* the helper.
* @param string $ulClass [optional] CSS class to
* use for UL element. Default
* is to use the value from
* {@link getUlClass()}.
* @param string|int $indent [optional] indentation as
* a string or number of
* spaces. Default is to use
* the value retrieved from
* {@link getIndent()}.
* @return string
*/
public function renderSubMenu(
AbstractContainer $container = null,
$ulClass = null,
$indent = null
) {
return $this->renderMenu($container, array(
'indent' => $indent,
'ulClass' => $ulClass,
'minDepth' => null,
'maxDepth' => null,
'onlyActiveBranch' => true,
'renderParents' => false,
'escapeLabels' => true,
'addClassToListItem' => false,
));
}
/**
* Returns an HTML string containing an 'a' element for the given page if
* the page's href is not empty, and a 'span' element if it is empty
*
* Overrides {@link AbstractHelper::htmlify()}.
*
* @param AbstractPage $page page to generate HTML for
* @param bool $escapeLabel Whether or not to escape the label
* @param bool $addClassToListItem Whether or not to add the page class to the list item
* @return string
*/
public function htmlify(AbstractPage $page, $escapeLabel = true, $addClassToListItem = false)
{
// get label and title for translating
$label = $page->getLabel();
$title = $page->getTitle();
// translate label and title?
if (null !== ($translator = $this->getTranslator())) {
$textDomain = $this->getTranslatorTextDomain();
if (is_string($label) && !empty($label)) {
$label = $translator->translate($label, $textDomain);
}
if (is_string($title) && !empty($title)) {
$title = $translator->translate($title, $textDomain);
}
}
// get attribs for element
$attribs = array(
'id' => $page->getId(),
'title' => $title,
);
if ($addClassToListItem === false) {
$attribs['class'] = $page->getClass();
}
// does page have a href?
$href = $page->getHref();
if ($href) {
$element = 'a';
$attribs['href'] = $href;
$attribs['target'] = $page->getTarget();
} else {
$element = 'span';
}
$html = '<' . $element . $this->htmlAttribs($attribs) . '>';
if ($escapeLabel === true) {
$escaper = $this->view->plugin('escapeHtml');
$html .= $escaper($label);
} else {
$html .= $label;
}
$html .= '' . $element . '>';
return $html;
}
/**
* Normalizes given render options
*
* @param array $options [optional] options to normalize
* @return array
*/
protected function normalizeOptions(array $options = array())
{
if (isset($options['indent'])) {
$options['indent'] = $this->getWhitespace($options['indent']);
} else {
$options['indent'] = $this->getIndent();
}
if (isset($options['ulClass']) && $options['ulClass'] !== null) {
$options['ulClass'] = (string) $options['ulClass'];
} else {
$options['ulClass'] = $this->getUlClass();
}
if (array_key_exists('minDepth', $options)) {
if (null !== $options['minDepth']) {
$options['minDepth'] = (int) $options['minDepth'];
}
} else {
$options['minDepth'] = $this->getMinDepth();
}
if ($options['minDepth'] < 0 || $options['minDepth'] === null) {
$options['minDepth'] = 0;
}
if (array_key_exists('maxDepth', $options)) {
if (null !== $options['maxDepth']) {
$options['maxDepth'] = (int) $options['maxDepth'];
}
} else {
$options['maxDepth'] = $this->getMaxDepth();
}
if (!isset($options['onlyActiveBranch'])) {
$options['onlyActiveBranch'] = $this->getOnlyActiveBranch();
}
if (!isset($options['escapeLabels'])) {
$options['escapeLabels'] = $this->escapeLabels;
}
if (!isset($options['renderParents'])) {
$options['renderParents'] = $this->getRenderParents();
}
if (!isset($options['addClassToListItem'])) {
$options['addClassToListItem'] = $this->getAddClassToListItem();
}
return $options;
}
/**
* Sets a flag indicating whether labels should be escaped
*
* @param bool $flag [optional] escape labels
* @return Menu
*/
public function escapeLabels($flag = true)
{
$this->escapeLabels = (bool) $flag;
return $this;
}
/**
* Enables/disables page class applied to