Main addresses are displayed before secondary addresses (Closes #1401).
authorStéphane Jacob <sj@m4x.org>
Sun, 27 Feb 2011 22:15:28 +0000 (23:15 +0100)
committerStéphane Jacob <sj@m4x.org>
Sun, 27 Feb 2011 22:19:01 +0000 (23:19 +0100)
Signed-off-by: Stéphane Jacob <sj@m4x.org>
ChangeLog
classes/address.php

index 31bce03..4e44b04 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -35,6 +35,7 @@ Bug/Wish:
         - #1367: Improves postal address formatting                        -JAC
         - #1368: Allows admin profile edition even if birthdate is unknown -JAC
         - #1369: Properly displays moderated jobs                          -JAC
+        - #1401: Main addresses are displayed before secondary addresses   -JAC
 
     * Register:
         - #1374: Adapts registration for master and doctorate              -JAC
index 289cfb1..d92ffb9 100644 (file)
@@ -809,12 +809,27 @@ class Address
         return $addresses;
     }
 
+    // Compares two addresses. First sort by publicity, then place primary
+    // addresses before secondary addresses.
+    static private function compare(array $a, array $b)
+    {
+        $value = ProfileVisibility::comparePublicity($a, $b);
+        if ($value == 0) {
+            if ($a['secondary'] != $b['secondary']) {
+                $value = $a['secondary'] ? 1 : -1;
+            }
+        }
+        return $value;
+    }
+
     // Formats an array of form addresses into an array of form formatted addresses.
     static public function formatFormArray(array $data, &$success = true)
     {
+        $addresses = self::formArrayWalk($data, 'toFormArray', $success, true);
+
         // Only a single address can be the profile's current address and she must have one.
         $hasCurrent = false;
-        foreach ($data as $key => &$address) {
+        foreach ($addresses as $key => &$address) {
             if (isset($address['current']) && $address['current']) {
                 if ($hasCurrent) {
                     $address['current'] = false;
@@ -830,8 +845,7 @@ class Address
             }
         }
 
-        $addresses = self::formArrayWalk($data, 'toFormArray', $success, true);
-        usort($addresses, 'ProfileVisibility::comparePublicity');
+        usort($addresses, 'Address::compare');
         return $addresses;
     }