Adds comparison function based on items' publicity and uses it to order profile item...
authorStéphane Jacob <sj@m4x.org>
Fri, 11 Feb 2011 13:19:12 +0000 (14:19 +0100)
committerStéphane Jacob <sj@m4x.org>
Fri, 11 Feb 2011 15:07:35 +0000 (16:07 +0100)
Signed-off-by: Stéphane Jacob <sj@m4x.org>
classes/address.php
classes/phone.php
classes/profilevisibility.php
modules/profile/jobs.inc.php

index 3be8b91..d338680 100644 (file)
@@ -826,7 +826,9 @@ class Address
             }
         }
 
-        return self::formArrayWalk($data, 'toFormArray', $success, true);
+        $addresses = self::formArrayWalk($data, 'toFormArray', $success, true);
+        usort($addresses, 'ProfileVisibility::comparePublicity');
+        return $addresses;
     }
 
     static public function formArrayToString(array $data)
index 9a9f1b0..3d602be 100644 (file)
@@ -375,7 +375,9 @@ class Phone
     // Formats an array of form phones into an array of form formatted phones.
     static public function formatFormArray(array $data, &$success = true, $maxPublicity = null)
     {
-        return self::formArrayWalk($data, 'toFormArray', $success, true, $maxPublicity);
+        $phones = self::formArrayWalk($data, 'toFormArray', $success, true, $maxPublicity);
+        usort($phones, 'ProfileVisibility::comparePublicity');
+        return $phones;
     }
 
     static public function formArrayToString(array $data)
index e7dbbd4..cf5687d 100644 (file)
@@ -31,9 +31,13 @@ class ProfileVisibility
 
     private $level;
 
-    public function __construct($level = null)
+    public function __construct($level = null, $force = false)
     {
-        $this->setLevel($level);
+        if ($force) {
+            $this->forceLevel($level);
+        } else {
+            $this->setLevel($level);
+        }
     }
 
     public function setLevel($level = self::VIS_PUBLIC)
@@ -62,6 +66,15 @@ class ProfileVisibility
         }
     }
 
+    public function forceLevel($level)
+    {
+        if ($level != self::VIS_PRIVATE && $level != self::VIS_AX && $level != self::VIS_PUBLIC) {
+            Platal::page()->kill('Invalid visibility: ' . $level);
+        }
+
+        $this->level = $level;
+    }
+
     public function level()
     {
         if ($this->level == null) {
@@ -80,6 +93,14 @@ class ProfileVisibility
     {
         return in_array($visibility, $this->levels());
     }
+
+    static public function comparePublicity($a, $b)
+    {
+        $a_pub = new ProfileVisibility($a['pub'], true);
+        $b_pub = new ProfileVisibility($b['pub'], true);
+
+        return !$a_pub->isVisible($b_pub->level());
+    }
 }
 
 
index 0420c84..d258517 100644 (file)
@@ -248,6 +248,7 @@ class ProfileSettingJob implements ProfileSetting
                 $success = ($success && $s);
             }
         }
+        usort($value, 'ProfileVisibility::comparePublicity');
         return $value;
     }