Skip to content

Commit

Permalink
Merge pull request #293 from Srokap/ticket_3035
Browse files Browse the repository at this point in the history
Fixes #3035 - menu items appearing in reverse registration order
  • Loading branch information
mrclay committed Oct 25, 2012
2 parents 5dc4e27 + 86acb5c commit bdb6d69
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
32 changes: 23 additions & 9 deletions engine/classes/ElggMenuBuilder.php
Expand Up @@ -204,6 +204,9 @@ protected function sort($sort_by) {

// sort each section
foreach ($this->menu as $index => $section) {
foreach ($section as $key => $node) {
$section[$key]->original_order = $key;
}
usort($section, $sort_callback);
$this->menu[$index] = $section;

Expand Down Expand Up @@ -232,10 +235,14 @@ protected function sort($sort_by) {
* @return bool
*/
public static function compareByText($a, $b) {
$a = $a->getText();
$b = $b->getText();
$at = $a->getText();
$bt = $b->getText();

return strnatcmp($a, $b);
$result = strnatcmp($at, $bt);
if ($result === 0) {
return $a->original_order - $b->original_order;
}
return $result;
}

/**
Expand All @@ -246,10 +253,14 @@ public static function compareByText($a, $b) {
* @return bool
*/
public static function compareByName($a, $b) {
$a = $a->getName();
$b = $b->getName();
$an = $a->getName();
$bn = $b->getName();

return strcmp($a, $b);
$result = strcmp($an, $bn);
if ($result === 0) {
return $a->original_order - $b->original_order;
}
return $result;
}

/**
Expand All @@ -260,9 +271,12 @@ public static function compareByName($a, $b) {
* @return bool
*/
public static function compareByWeight($a, $b) {
$a = $a->getWeight();
$b = $b->getWeight();
$aw = $a->getWeight();
$bw = $b->getWeight();

return $a > $b;
if ($aw == $bw) {
return $a->original_order - $b->original_order;
}
return $aw - $bw;
}
}
3 changes: 3 additions & 0 deletions engine/classes/ElggMenuItem.php
Expand Up @@ -542,6 +542,9 @@ public function getChildren() {
* @return void
*/
public function sortChildren($sortFunction) {
foreach ($this->data['children'] as $key => $node) {
$this->data['children'][$key]->original_order = $key;
}
usort($this->data['children'], $sortFunction);
}

Expand Down

0 comments on commit bdb6d69

Please sign in to comment.