Skip to content

Loading…

Fixes #3035. Reversing menu before sorting to display menu items in the ... #212

Closed
wants to merge 1 commit into from

3 participants

@sembrestels
Elgg member

...registered order in a last instance.

@ewinslow
Elgg member

I'm not convinced this is going to work correctly for all cases. We might need some tests here to prove correctness.

@sembrestels
Elgg member

I undersand your worry. It doesn't seem to go working at all, but if you review the code a little more:

  1. array_reverse() works with any kind of array, and $section is always an array.
  2. After the reverse we have a sort function. If items must be sorted, a previous reverse doesn't matter.
  3. The reverse only corrects what usort do. Usort only reverses the array if it find all elements equivalent, and we need a reverse before to undo this.

I didn't find any flaw in the code.

@brettp brettp closed this
@brettp
Elgg member

Let's hold off on this until we have some unit tests. I'm going to close this request because it's on master. @sembrestels Please open PRs against the branch the ticket is filed under (1.8, in this case). More discussion at the ticket: http://trac.elgg.org/ticket/3035

@sembrestels
Elgg member

@brettp Default version in trac is 1.7, and lots of tickets are under this version. Anyway, if I'm doing a 1.8.x ticket, I should send it to 1.8 branch, do you?

@brettp
Elgg member

I meant make the pull request against the branch that matches the milestone of the ticket. If a ticket is on milestone 1.8.4 the PR goes against the 1.8 branch, if it's 1.7.15 it's against the 1.7 branch. Does that make sense?

@sembrestels
Elgg member

Understood. Of course it make sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 5, 2012
  1. @sembrestels

    Fixes #3035. Reversing menu before sorting to display menu items in t…

    sembrestels committed
    …he registered order in a last instance.
Showing with 9 additions and 3 deletions.
  1. +6 −2 engine/classes/ElggMenuBuilder.php
  2. +3 −1 engine/classes/ElggMenuItem.php
View
8 engine/classes/ElggMenuBuilder.php
@@ -204,6 +204,7 @@ protected function sort($sort_by) {
// sort each section
foreach ($this->menu as $index => $section) {
+ $section = array_reverse($section); // Refs #3035.
usort($section, $sort_callback);
$this->menu[$index] = $section;
@@ -262,7 +263,10 @@ public static function compareByName($a, $b) {
public static function compareByWeight($a, $b) {
$a = $a->getWeight();
$b = $b->getWeight();
-
- return $a > $b;
+
+ if ($a == $b) {
+ return 0;
+ }
+ return ($a < $b) ? -1 : 1;
}
}
View
4 engine/classes/ElggMenuItem.php
@@ -542,7 +542,9 @@ public function getChildren() {
* @return void
*/
public function sortChildren($sortFunction) {
- usort($this->data['children'], $sortFunction);
+ $children = array_reverse($this->data['children']); // Refs #3035.
+ usort($children, $sortFunction);
+ $this->data['children'] = $children;
}
/**
Something went wrong with that request. Please try again.