/**
  * Reorder menu based on position attribute
  *
  * @param ItemInterface $menu
  */
 protected function sort(ItemInterface $menu)
 {
     if ($menu->hasChildren() && $menu->getDisplayChildren()) {
         $orderedChildren = [];
         $unorderedChildren = [];
         $hasOrdering = false;
         $children = $menu->getChildren();
         foreach ($children as &$child) {
             if ($child->hasChildren() && $child->getDisplayChildren()) {
                 $this->sort($child);
             }
             $position = $child->getExtra('position');
             if ($position !== null) {
                 $orderedChildren[$child->getName()] = (int) $position;
                 $hasOrdering = true;
             } else {
                 $unorderedChildren[] = $child->getName();
             }
         }
         if ($hasOrdering) {
             asort($orderedChildren);
             $menu->reorderChildren(array_merge(array_keys($orderedChildren), $unorderedChildren));
         }
     }
 }
 public function render(ItemInterface $item, array $options = array())
 {
     $options = array_merge($this->defaultOptions, $options);
     $translator = $options['translator'];
     $itemIterator = new \Knp\Menu\Iterator\RecursiveItemIterator($item);
     $iterator = new \RecursiveIteratorIterator($itemIterator, \RecursiveIteratorIterator::SELF_FIRST);
     $items = [];
     foreach ($iterator as $item) {
         $translatedLabel = $translator->trans($item->getLabel());
         $id = $item->getName();
         $itemData = ['id' => strtolower($item->getName()), 'name' => $translatedLabel, 'uri' => $item->getUri()];
         $itemData['has_children'] = $item->hasChildren();
         $parentId = $item->getParent()->getName();
         if ($parentId !== $id) {
             $itemData['parent'] = strtolower($parentId);
             if (!isset($items[$parentId]['children'])) {
                 $items[$parentId]['children'] = [];
             }
             $items[$parentId]['children'][] = $itemData;
         }
         if (isset($items[$id])) {
             $items[$id] = array_merge($itemData, $items[$id]);
         } else {
             $items[$id] = $itemData;
         }
     }
     return $items;
 }
 protected function modifyItem(ItemInterface $item, array $options)
 {
     if (!$item->getExtra('remove_leaf', false)) {
         return;
     }
     if (!$item->hasChildren()) {
         $this->removeItem($item);
     }
 }
Example #4
0
 /**
  * @param \Knp\Menu\ItemInterface $item
  * @param $attributes
  * @param $options
  * @return \Nette\Utils\Html|null
  */
 protected function getList(ItemInterface $item, $attributes, $options)
 {
     if (!$item->hasChildren() || 0 === $options['depth'] || !$item->getDisplayChildren()) {
         return null;
     }
     $list = Html::el('ul', $attributes);
     foreach ($this->getChildren($item, $options) as $child) {
         $list->add($child);
     }
     return $list;
 }
 protected function renderList(ItemInterface $item, array $attributes, array $options)
 {
     /**
      * Return an empty string if any of the following are true:
      *   a) The menu has no children eligible to be displayed
      *   b) The depth is 0
      *   c) This menu item has been explicitly set to hide its children
      */
     if (!$item->hasChildren() || 0 === $options['depth'] || !$item->getDisplayChildren()) {
         return '';
     }
     $html = $this->format('<ul>', 'ul', $item->getLevel(), $options);
     $html .= $this->renderChildren($item, $options);
     $html .= $this->format('</ul>', 'ul', $item->getLevel(), $options);
     return $html;
 }
Example #6
0
 protected function renderLabel(ItemInterface $item, array $options)
 {
     $html = '<i class="' . $item->getIcon() . '"></i> ';
     $html .= '<span>' . $item->getLabel() . '</span>';
     if ($item->hasChildren()) {
         $drop = false;
         foreach ($item->getChildren() as $child) {
             if (\App::isGranted($child->getPermissions())) {
                 $drop = true;
             }
         }
         if ($drop) {
             $html .= '<i class="fa fa-angle-left pull-right"></i>';
         }
     }
     return $html;
 }
Example #7
0
    /**
     * @see RendererInterface::render
     */
    public function render(ItemInterface $item, array $options = array())
    {
        $options = array_merge($this->getDefaultOptions(), $options);

        /**
         * Return an empty string if any of the following are true:
         *   a) The menu has no children eligible to be displayed
         *   b) The depth is 0
         *   c) This menu item has been explicitly set to hide its children
         */
        if (!$item->hasChildren() || 0 === $options['depth'] || !$item->getDisplayChildren()) {
            return '';
        }

        $html = $this->format('<ul'.$this->renderHtmlAttributes($item->getAttributes()).'>', 'ul', $item->getLevel());
        $html .= $this->renderChildren($item, $options);
        $html .= $this->format('</ul>', 'ul', $item->getLevel());

        return $html;
    }
 /**
  * @param ItemInterface $menu
  * @param array         $data
  * @param array         $itemList
  * @param array         $options
  *
  * @return \Knp\Menu\ItemInterface
  */
 private function createFromArray(ItemInterface $menu, array $data, array &$itemList, array $options = array())
 {
     $isAllowed = false;
     foreach ($data as $itemCode => $itemData) {
         if (!empty($itemList[$itemCode])) {
             $itemOptions = $itemList[$itemCode];
             if (empty($itemOptions['name'])) {
                 $itemOptions['name'] = $itemCode;
             }
             if (empty($itemOptions['route']) && empty($itemOptions['uri'])) {
                 $itemOptions['route'] = $itemCode;
             }
             if (!empty($itemData['position'])) {
                 $itemOptions['extras']['position'] = $itemData['position'];
             }
             $this->moveToExtras($itemOptions, 'translateDomain');
             $this->moveToExtras($itemOptions, 'translateParameters');
             $newMenuItem = $menu->addChild($itemOptions['name'], array_merge($itemOptions, $options));
             if (!empty($itemData['children'])) {
                 $this->createFromArray($newMenuItem, $itemData['children'], $itemList, $options);
             }
             $isAllowed = $isAllowed || $newMenuItem->getExtra('isAllowed');
         }
     }
     $menu->setExtra('isAllowed', $isAllowed);
     if ($menu->getExtra('hideIfEmpty') && $menu->hasChildren()) {
         $willDisplaySomeChildren = false;
         foreach ($menu->getChildren() as $child) {
             if ($child->isDisplayed() && $child->getExtra('isAllowed')) {
                 $willDisplaySomeChildren = true;
                 break;
             }
         }
         if (!$willDisplaySomeChildren) {
             $menu->setDisplay(false);
         }
     }
 }
Example #9
0
 /**
  * Concats the appropriate classes for menu links
  *
  * @param ItemInterface    $item
  * @param MatcherInterface $matcher
  * @param array            $options
  */
 public function buildClasses(ItemInterface &$item, MatcherInterface &$matcher, $options)
 {
     $showChildren = $item->hasChildren() && $item->getDisplayChildren();
     $isAncestor = $matcher->isAncestor($item, $options["matchingDepth"]);
     $isCurrent = $matcher->isCurrent($item);
     $class = $item->getAttribute("class");
     $classes = $class ? " {$class}" : "";
     $classes .= $isCurrent ? " {$options["currentClass"]}" : "";
     $classes .= $isAncestor ? " {$options["ancestorClass"]}" : "";
     $classes .= $isAncestor && $this->invisibleChildSelected($item, $matcher) ? " {$options["currentClass"]}" : "";
     $classes .= $item->actsLikeFirst() ? " {$options["firstClass"]}" : "";
     $classes .= $item->actsLikeLast() ? " {$options["lastClass"]}" : "";
     $item->setAttribute("class", trim($classes));
 }
Example #10
0
 /**
  * @param ItemInterface $item
  * @param array         $options
  *
  * @return string
  */
 protected function renderFolding(ItemInterface $item, array $options = [])
 {
     if ($item->hasChildren()) {
         return $this->renderFoldingElement($item, $options);
     }
     return '';
 }
 /**
  * Gets guessed values for different menu/nav types.
  *
  * @param ItemInterface $item
  * @param array         $options
  *
  * @return array
  */
 protected function getChildOptions(ItemInterface $item, array $options)
 {
     $childOptions = array();
     $hasChildren = $item->hasChildren() && (!isset($options['depth']) || $options['depth'] > $item->getLevel());
     if (in_array($options['automenu'], array('navbar')) && $hasChildren) {
         $childOptions = array('dropdown' => !isset($options['dropdown']) || $options['dropdown'], 'caret' => !isset($options['caret']) || $options['caret']);
     }
     if (in_array($options['automenu'], array('list-group'))) {
         $childOptions = array('list-group-item' => true);
     }
     return array_merge($options, $childOptions);
 }
 /**
  * Setting guessed good values for different menu / nav types
  *
  * @param ItemInterface $item
  * @param array         $options
  *
  * @return array $options
  */
 protected function getChildOptions(ItemInterface $item, array $options)
 {
     $childOptions = array();
     if (in_array($options["automenu"], array("navbar")) && $item->hasChildren()) {
         $childOptions = array("dropdown" => !isset($options["dropdown"]) || $options["dropdown"], "caret" => !isset($options["caret"]) || $options["caret"]);
     }
     if (in_array($options["automenu"], array("list-group"))) {
         $childOptions = array("list-group-item" => true);
     }
     return array_merge($options, $childOptions);
 }