BUGFIX: Ensure that menu items of the same priority show the first added item closes to the left

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/branches/2.3@65863 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2008-11-13 21:51:24 +00:00
parent b13dc40ac1
commit 2738afa74e

View File

@ -60,7 +60,7 @@ class CMSMenu extends Object implements Iterator
* @param string $code A unique identifier (used to create a CSS ID and as it's key in {@link $menu_items} * @param string $code A unique identifier (used to create a CSS ID and as it's key in {@link $menu_items}
* @param string $menuTitle The link's title in the CMS menu * @param string $menuTitle The link's title in the CMS menu
* @param string $url The url of the link * @param string $url The url of the link
* @param integer $priority The menu priority (sorting order) of the menu item * @param integer $priority The menu priority (sorting order) of the menu item. Higher priorities will be further left.
* @return boolean The result of the operation. * @return boolean The result of the operation.
*/ */
public static function add_link($code, $menuTitle, $url, $priority = -1) { public static function add_link($code, $menuTitle, $url, $priority = -1) {
@ -145,8 +145,13 @@ class CMSMenu extends Object implements Iterator
public static function replace_menu_item($code, $menuTitle, $url, $controllerClass = null, $priority = -1) { public static function replace_menu_item($code, $menuTitle, $url, $controllerClass = null, $priority = -1) {
$menuItems = self::$menu_items; $menuItems = self::$menu_items;
$menuItems[$code] = new CMSMenuItem($menuTitle, $url, $controllerClass, $priority); $menuItems[$code] = new CMSMenuItem($menuTitle, $url, $controllerClass, $priority);
$menuPriority = array();
$i = 0;
foreach($menuItems as $key => $menuItem) { foreach($menuItems as $key => $menuItem) {
$menuPriority[$key] = $menuItem->priority; $i++;
// This funny litle formula ensures that the first item added with the same priority will be left-most.
$menuPriority[$key] = $menuItem->priority*100 - $i;
} }
array_multisort($menuPriority, SORT_DESC, $menuItems); array_multisort($menuPriority, SORT_DESC, $menuItems);