/** * 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)); } } }
/** * Moves child to specified position. Rearange other children accordingly. * * @param ItemInterface $item * @param ItemInterface $child Child to move. * @param integer $position Position to move child to. */ public function moveChildToPosition(ItemInterface $item, ItemInterface $child, $position) { $name = $child->getName(); $order = array_keys($item->getChildren()); $oldPosition = array_search($name, $order); unset($order[$oldPosition]); $order = array_values($order); array_splice($order, $position, 0, $name); $item->reorderChildren($order); }
/** * Reorder the menu * * @param \Knp\Menu\ItemInterface $menu * * @return MenuItem */ private function reorderMenu($menu) { $menuItems = $menu->getChildren(); usort($menuItems, array($this, 'menuSort')); $newMenuOrder = array(); foreach ($menuItems as $menuItem) { $newMenuOrder[] = $menuItem->getName(); } $menu->reorderChildren($newMenuOrder); return $menu; }
/** * Reorderd the items in the menu based on the extra data * * @param ItemInterface $menu */ protected function reorderMenuItems(ItemInterface $menu) { $menuOrderArray = array(); $addLast = array(); $alreadyTaken = array(); foreach ($menu->getChildren() as $menuItem) { if ($menuItem->hasChildren()) { $this->reorderMenuItems($menuItem); } $orderNumber = $menuItem->getExtra('orderNumber'); if ($orderNumber != null) { if (!isset($menuOrderArray[$orderNumber])) { $menuOrderArray[$orderNumber] = $menuItem->getName(); } else { $alreadyTaken[$orderNumber] = $menuItem->getName(); } } else { $addLast[] = $menuItem->getName(); } } ksort($menuOrderArray); if (!empty($alreadyTaken)) { foreach ($alreadyTaken as $key => $value) { $keysArray = array_keys($menuOrderArray); $position = array_search($key, $keysArray); if ($position === false) { continue; } $menuOrderArray = array_merge(array_slice($menuOrderArray, 0, $position), array($value), array_slice($menuOrderArray, $position)); } } ksort($menuOrderArray); if (!empty($addLast)) { foreach ($addLast as $value) { $menuOrderArray[] = $value; } } if (!empty($menuOrderArray)) { $menu->reorderChildren($menuOrderArray); } }
/** * Reorder Menus items * * @param ItemInterface $menu */ protected function reorderMenuItems(ItemInterface $menu) { $menuOrderArray = array(); $addLast = array(); $alreadyTaken = array(); foreach ($menu->getChildren() as $key => $menuItem) { if ($menuItem->hasChildren()) { $this->reorderMenuItems($menuItem); } $orderNumber = $menuItem->getExtra('orderNumber'); if ($orderNumber != null) { if (!isset($menuOrderArray[$orderNumber])) { $menuOrderArray[$orderNumber] = $menuItem->getName(); } else { $alreadyTaken[$orderNumber] = $menuItem->getName(); // $alreadyTaken[] = array('orderNumber' => $orderNumber, 'name' => $menuItem->getName()); } } else { $addLast[] = $menuItem->getName(); } } // sort them after first pass ksort($menuOrderArray); // handle position duplicates if (count($alreadyTaken)) { foreach ($alreadyTaken as $key => $value) { // the ever shifting target $keysArray = array_keys($menuOrderArray); $position = array_search($key, $keysArray); if ($position === false) { continue; } $menuOrderArray = array_merge(array_slice($menuOrderArray, 0, $position), array($value), array_slice($menuOrderArray, $position)); } } // sort them after second pass ksort($menuOrderArray); // add items without ordernumber to the end if (count($addLast)) { foreach ($addLast as $key => $value) { $menuOrderArray[] = $value; } } if (count($menuOrderArray)) { $menu->reorderChildren($menuOrderArray); } }