Add ProfileNetworking
authorRaphaël Barrois <raphael.barrois@polytechnique.org>
Tue, 16 Mar 2010 18:11:46 +0000 (19:11 +0100)
committerRaphaël Barrois <raphael.barrois@polytechnique.org>
Mon, 22 Mar 2010 12:49:28 +0000 (13:49 +0100)
Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
classes/profile.php
include/profilefields.inc.php

index f3fb591..4cfc845 100644 (file)
@@ -354,7 +354,7 @@ class Profile
 
     /* Educations
      */
-    private $educations;
+    private $educations = null;
     public function setEducations(ProfileEducation $edu)
     {
         $this->educations = $edu;
@@ -362,6 +362,9 @@ class Profile
 
     public function getEducations($flags, $limit = null)
     {
+        if ($this->educations == null) {
+            return PlIteratorUtils::fromArray(array());
+        }
         return $this->educations->get($flags, $limit);
     }
 
@@ -373,25 +376,18 @@ class Profile
 
     /** Networking
      */
+    private $networks = null;
+    public function setNetworking(ProfileNetworking $nw)
+    {
+        $this->networks = $nw;
+    }
 
     public function getNetworking($flags, $limit = null)
     {
-        $where = XDB::format('pn.pid = {?}', $this->id());
-        if ($flags & self::NETWORKING_WEB) {
-            $where .= ' AND pn.network_type = 0'; // XXX hardcoded reference to web site index
-        }
-        if ($this->visibility) {
-            $where .= ' AND pn.pub IN ' . XDB::formatArray($this->visibility);
+        if ($this->networks == null) {
+            return PlIteratorUtils::fromArray(array());
         }
-        $limit = is_null($limit) ? '' : XDB::format('LIMIT {?}', (int)$limit);
-        return XDB::iterator('SELECT  pne.name, pne.icon,
-                                      IF (LENGTH(pne.link) > 0, REPLACE(pne.link, \'%s\', pn.address),
-                                                                pn.address) AS address
-                                FROM  profile_networking AS pn
-                          INNER JOIN  profile_networking_enum AS pne ON (pn.network_type = pne.network_type)
-                               WHERE  ' . $where . '
-                            ORDER BY  pn.network_type, pn.nwid
-                                      ' . $limit);
+        return $this->networks->get($flags, $limit);
     }
 
     public function getWebSite()
index ec64e92..44aa6e2 100644 (file)
@@ -379,38 +379,45 @@ class ProfileMedals extends ProfileField
 class ProfileNetworking extends ProfileField
 {
     private $networks = array();
-    private $visibilities = array();
 
     private function __construct(PlIterator $it)
     {
         while ($network = $it->next()) {
             $this->networks[$network['nwid']] = $network['address'];
-            $this->visibilities[$network['nwid']] = $network['pub'];
         }
     }
 
     public static function fetchData(array $pids, $visibility)
     {
-        $data = XDB::iterator('SELECT  pid, nwid, address, pub
+        $data = XDB::iterator('SELECT  pid, nwid, address, network_type
                                  FROM  profile_networking
                                 WHERE  pid IN {?} AND pub IN {?}
-                             ORDER BY  ' . XDB::formatCustomOrder('pid', $pids),
-                                XDB::formatArray($pids),
-                                XDB::formatArray($visibility)
-                            );
+                             ORDER BY  ' . XDB::formatCustomOrder('pid', $pids) . ',
+                                       network_type, nwid',
+                               $pids, $visibility);
 
         return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
     }
 
-    public function networks()
+    public function get($flags, $limit = null)
     {
         $nws = array();
-        foreach ($this->visibilities as $id => $vis) {
-            if ($this->profile->isVisible($vis)) {
-                $nws[$id] = $this->networks[$id];
+        $nb = 0;
+        foreach ($this->networks as $id => $nw) {
+            // XXX hardcoded reference to web site index
+            if (
+                (($flags & self::NETWORKING_WEB) && $nw['network_type'] == 0)
+                ||
+                (! ($flags & self::NETWORKING_WEB))
+            ) {
+                $nws[$id] = $nw;
+                ++$nb;
+            }
+            if ($nb >= $limit) {
+                break;
             }
         }
-        return $nws;
+        return PlIteratorUtils::fromArray($nws);
     }
 }
 // }}}