Fix networking with new network_type issues introduced by previous commit
[platal.git] / include / profilefields.inc.php
index 26d73a1..d2de132 100644 (file)
  */
 abstract class ProfileField
 {
+    public static $fields = array(
+        Profile::FETCH_ADDRESSES      => 'ProfileAddresses',
+        Profile::FETCH_CORPS          => 'ProfileCorps',
+        Profile::FETCH_EDU            => 'ProfileEducation',
+        Profile::FETCH_JOBS           => 'ProfileJobs',
+        Profile::FETCH_MEDALS         => 'ProfileMedals',
+        Profile::FETCH_NETWORKING     => 'ProfileNetworking',
+        Profile::FETCH_PHONES         => 'ProfilePhones',
+        Profile::FETCH_MENTOR_SECTOR  => 'ProfileMentoringSectors',
+        Profile::FETCH_MENTOR_COUNTRY => 'ProfileMentoringCountries',
+    );
+
     /** The profile to which this field belongs
      */
     public $pid;
@@ -139,6 +151,42 @@ class Phone
             $this->$key = $val;
         }
     }
+
+    public function hasFlags($flags) {
+        return $this->hasType($flags) && $this->hasLink($flags);
+    }
+
+    /** Returns true if this phone's type matches the flags
+     */
+    public function hasType($flags) {
+        $flags = $flags & Profile::PHONE_TYPE_ANY;
+        return (
+            ($flags == Profile::PHONE_TYPE_ANY)
+            ||
+            (($flags & Profile::PHONE_TYPE_FAX) && $this->type == self::TYPE_FAX)
+            ||
+            (($flags & Profile::PHONE_TYPE_FIXED) && $this->type == self::TYPE_FIXED)
+            ||
+            (($flags & Profile::PHONE_TYPE_MOBILE) && $this->type == self::TYPE_MOBILE)
+        );
+    }
+
+    /** Returns true if this phone's link matches the flags
+     */
+    public function hasLink($flags) {
+        $flags = $flags & Profile::PHONE_LINK_ANY;
+        return (
+            ($flags == Profile::PHONE_LINK_ANY)
+            ||
+            (($flags & Profile::PHONE_LINK_COMPANY) && $this->link_type == self::LINK_COMPANY)
+            ||
+            (($flags & Profile::PHONE_LINK_JOB) && $this->link_type == self::LINK_JOB)
+            ||
+            (($flags & Profile::PHONE_LINK_ADDRESS) && $this->link_type == self::LINK_ADDRESS)
+            ||
+            (($flags & Profile::PHONE_LINK_PROFILE) && $this->link_type == self::LINK_PROFILE)
+        );
+    }
 }
 // }}}
 // {{{ class Company
@@ -283,6 +331,9 @@ class Address
 
     public function hasFlag($flag)
     {
+        if (!$this->flags instanceof PlFlagSet) {
+            $this->flags = new PlFlagSet($this->flags);
+        }
         return $this->flags->hasFlag($flag);
     }
 }
@@ -397,10 +448,11 @@ class ProfileMedals extends ProfileField
 
     public static function fetchData(array $pids, ProfileVisibility $visibility)
     {
-        $data = XDB::iterator('SELECT  pm.pid, pm.mid, pm.gid, pme.text, pme.img
+        $data = XDB::iterator('SELECT  pm.pid, pm.mid, pm.gid, pme.text, pme.img, pmge.text AS grade
                                  FROM  profile_medals AS pm
                             LEFT JOIN  profiles AS p ON (pm.pid = p.pid)
                             LEFT JOIN  profile_medal_enum AS pme ON (pme.id = pm.mid)
+                            LEFT JOIN  profile_medal_grade_enum AS pmge ON (pmge.mid = pm.mid AND pmge.gid = pm.gid)
                                 WHERE  pm.pid IN {?} AND p.medals_pub IN {?}
                              ORDER BY  ' . XDB::formatCustomOrder('pm.pid', $pids),
                                 $pids, $visibility->levels());
@@ -414,20 +466,23 @@ class ProfileNetworking extends ProfileField
 {
     private $networks = array();
 
-    public function __construct(PlIterator $it)
+    public function __construct(PlInnerSubIterator $it)
     {
+        $this->pid = $it->value();
         while ($network = $it->next()) {
-            $this->networks[$network['nwid']] = $network['address'];
+            $network['network_type'] = new PlFlagSet($network['network_type']);
+            $this->networks[$network['id']] = $network;
         }
     }
 
     public static function fetchData(array $pids, ProfileVisibility $visibility)
     {
-        $data = XDB::iterator('SELECT  pid, nwid, address, network_type
-                                 FROM  profile_networking
+        $data = XDB::iterator('SELECT  pid, id, address, pne.nwid, pne.network_type, pne.link, pne.name
+                                 FROM  profile_networking AS pn
+                            LEFT JOIN  profile_networking_enum AS pne USING(nwid)
                                 WHERE  pid IN {?} AND pub IN {?}
                              ORDER BY  ' . XDB::formatCustomOrder('pid', $pids) . ',
-                                       network_type, nwid',
+                                       pn.nwid, id',
                                $pids, $visibility->levels());
 
         return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
@@ -435,20 +490,21 @@ class ProfileNetworking extends ProfileField
 
     public function get($flags, $limit = null)
     {
+        if (!$flags) {
+            $flags = Profile::NETWORKING_ALL;
+        }
         $nws = array();
         $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))
-            ) {
+            if (($flags & Profile::NETWORKING_WEB) && $nw['network_type']->hasFlag('web') ||
+                ($flags & Profile::NETWORKING_IM) && $nw['network_type']->hasFlag('im') ||
+                ($flags & Profile::NETWORKING_SOCIAL) && $nw['network_type']->hasFlag('social') ||
+                ($flags & Profile::NETWORKING_ALL)) {
                 $nws[$id] = $nw;
                 ++$nb;
-            }
-            if ($nb >= $limit) {
-                break;
+                if (isset($limit) && $nb >= $limit) {
+                    break;
+                }
             }
         }
         return $nws;
@@ -491,7 +547,7 @@ class ProfileCorps extends ProfileField
                             LEFT JOIN  profile_corps_enum AS pceo ON (pceo.id = pc.original_corpsid)
                             LEFT JOIN  profile_corps_enum AS pcec ON (pcec.id = pc.current_corpsid)
                             LEFT JOIN  profile_corps_rank_enum AS pcrec ON (pcrec.id = pc.rankid)
-                                WHERE  pc.pid IN {?} AND pc.corps_pub IN {?}
+                                WHERE  pc.pid IN {?} AND pc.corps_pub IN {?} AND pceo.id != 1
                              ORDER BY  ' . XDB::formatCustomOrder('pid', $pids),
                                 $pids, $visibility->levels());
 
@@ -499,6 +555,59 @@ class ProfileCorps extends ProfileField
     }
 }
 // }}}
+// {{{ class ProfileMentoringSectors                  [ Field ]
+class ProfileMentoringSectors extends ProfileField
+{
+    public $sectors = array();
+
+    public function __construct(PlInnerSubIterator $it)
+    {
+        $this->pid = $it->value();
+        while ($sector = $it->next()) {
+            $this->sectors[] = $sector;
+        }
+    }
+
+    public static function fetchData(array $pids, ProfileVisibility $visibility)
+    {
+        $data = XDB::iterator('SELECT  pms.pid, pjse.name AS sector, pjsse.name AS subsector
+                                 FROM  profile_mentor_sector AS pms
+                            LEFT JOIN  profile_job_sector_enum AS pjse ON (pjse.id = pms.sectorid)
+                            LEFT JOIN  profile_job_subsector_enum AS pjsse ON (pjsse.id = pms.subsectorid)
+                                WHERE  pms.pid IN {?}
+                             ORDER BY  ' . XDB::formatCustomOrder('pms.pid', $pids),
+                                $pids);
+
+        return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
+    }
+}
+// }}}
+// {{{ class ProfileMentoringCountries                [ Field ]
+class ProfileMentoringCountries extends ProfileField
+{
+    public $countries = array();
+
+    public function __construct(PlInnerSubIterator $it)
+    {
+        $this->pid = $it->value();
+        while ($country = $it->next()) {
+            $this->countries[$country['id']] = $country['name'];
+        }
+    }
+
+    public static function fetchData(array $pids, ProfileVisibility $visibility)
+    {
+        $data = XDB::iterator('SELECT  pmc.pid, pmc.country AS id, gc.countryFR AS name
+                                 FROM  profile_mentor_country AS pmc
+                            LEFT JOIN  geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pmc.country)
+                                WHERE  pmc.pid IN {?}
+                             ORDER BY  ' . XDB::formatCustomOrder('pmc.pid', $pids),
+                                $pids);
+
+        return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
+    }
+}
+// }}}
 
 /** Loading of data for a Profile :
  * 1) load jobs, addresses, phones
@@ -596,10 +705,12 @@ class ProfilePhones extends ProfileField
         $phones = array();
         $nb = 0;
         foreach ($this->phones as $id => $phone) {
-            $phones[$id] = $phone;
-            ++$nb;
-            if ($limit != null && $nb == $limit) {
-                break;
+            if ($phone->hasFlags($flags)) {
+                $phones[$id] = $phone;
+                ++$nb;
+                if ($limit != null && $nb == $limit) {
+                    break;
+                }
             }
         }
         return $phones;
@@ -607,7 +718,7 @@ class ProfilePhones extends ProfileField
 
     public static function fetchData(array $pids, ProfileVisibility $visibility)
     {
-        $data = XDB::iterator('SELECT  tel_type AS type, search_tel AS search, display_tel AS display, link_type, comment
+        $data = XDB::iterator('SELECT  tel_type AS type, search_tel AS search, display_tel AS display, link_type, comment, pid
                                  FROM  profile_phones
                                 WHERE  pid IN {?} AND pub IN {?}
                              ORDER BY  ' . XDB::formatCustomOrder('pid', $pids),
@@ -671,7 +782,7 @@ class ProfileJobs extends ProfileField
         }
     }
 
-    public static function addAddresses(ProfileAddresses $addresses)
+    public function addAddresses(ProfileAddresses $addresses)
     {
         $a = $addresses->get(Profile::ADDRESS_PRO);
         foreach ($a as $address) {
@@ -681,11 +792,10 @@ class ProfileJobs extends ProfileField
         }
     }
 
-    public static function addCompanies(array $companies)
+    public function addCompanies(array $companies)
     {
-        foreach ($this->jobs as $job)
-        {
-            $job->company = $companies[$job->jobid];
+        foreach ($this->jobs as $job) {
+            $this->company = $companies[$job->jobid];
         }
     }
 }