Change École polytechnique favicon.
[platal.git] / include / profilefields.inc.php
index f97bbc4..8a3ace0 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2010 Polytechnique.org                              *
+ *  Copyright (C) 2003-2014 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
  */
 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_COUNTRY => 'ProfileMentoringCountries',
+        Profile::FETCH_JOB_TERMS      => 'ProfileJobTerms',
+        Profile::FETCH_MENTOR_TERMS   => 'ProfileMentoringTerms',
+        Profile::FETCH_SKILL          => 'ProfileSkills',
+        Profile::FETCH_LANGUAGE       => 'ProfileLanguages',
+        Profile::FETCH_PARTNER        => 'ProfilePartnerSharing',
+    );
+
     /** The profile to which this field belongs
      */
     public $pid;
@@ -34,10 +50,15 @@ abstract class ProfileField
      * @param $pids An array of pids
      * @param $visibility The level of visibility fetched fields must have
      * @return a PlIterator yielding data suitable for a "new ProfileBlah($data)"
+     *
+     * MUST be reimplemented for each kind of ProfileField.
      */
-    abstract public static function fetchData(array $pids, $visibility);
+    public static function fetchData(array $pids, Visibility $visibility)
+    {
+        return PlIteratorUtils::emptyIterator();
+    }
 
-    public static function buildForPID($cls, $pid, $visibility)
+    public static function buildForPID($cls, $pid, Visibility $visibility)
     {
         $res = self::buildFromPIDs($cls, array($pid), $visibility);
         return array_pop($res);
@@ -49,7 +70,7 @@ abstract class ProfileField
      * @param $visibility An array of allowed visibility contexts
      * @return An array of $pid => ProfileField
      */
-    public static function buildFromPIDs($cls, array $pids, $visibility)
+    public static function buildFromPIDs($cls, array $pids, Visibility $visibility)
     {
         $it = new ProfileFieldIterator($cls, $pids, $visibility);
         $res = array();
@@ -58,6 +79,12 @@ abstract class ProfileField
         }
         return $res;
     }
+
+    public static function getForPID($cls, $pid, Visibility $visibility)
+    {
+        $it = new ProfileFieldIterator($cls, array($pid), $visibility);
+        return $it->next();
+    }
 }
 // }}}
 
@@ -67,8 +94,11 @@ class ProfileFieldIterator implements PlIterator
     private $data;
     private $cls;
 
-    public function __construct($cls, array $pids, $visibility)
+    public function __construct($cls, array $pids, Visibility $visibility)
     {
+        if (is_numeric($cls) && isset(ProfileField::$fields[$cls])) {
+            $cls = ProfileField::$fields[$cls];
+        }
         $this->data = call_user_func(array($cls, 'fetchData'), $pids, $visibility);
         $this->cls = $cls;
     }
@@ -101,36 +131,6 @@ class ProfileFieldIterator implements PlIterator
 }
 // }}}
 
-// {{{ class Phone
-class Phone
-{
-    const TYPE_FAX    = 'fax';
-    const TYPE_FIXED  = 'fixed';
-    const TYPE_MOBILE = 'mobile';
-    public $type;
-
-    public $search;
-    public $display;
-    public $comment = '';
-
-    const LINK_JOB     = 'job';
-    const LINK_ADDRESS = 'address';
-    const LINK_PROFILE = 'user';
-    const LINK_COMPANY = 'hq';
-    public $link_type;
-    public $link_id;
-
-    /** Fields are :
-     * $type, $search, $display, $link_type, $link_id, $comment, $pid, $id
-     */
-    public function __construct($data)
-    {
-        foreach ($data as $key => $val) {
-            $this->$key = $val;
-        }
-    }
-}
-// }}}
 // {{{ class Company
 class Company
 {
@@ -144,23 +144,23 @@ class Company
     /** Fields are:
      * $id, $name, $acronym, $url
      */
-    public function __construct($date)
+    public function __construct($data)
     {
         foreach ($data as $key => $val) {
             $this->$key = $val;
         }
     }
 
-    public function setPhone(Phone &$phone)
+    public function setPhone(Phone $phone)
     {
         if ($phone->link_type == Phone::LINK_COMPANY && $phone->link_id == $this->id) {
             $this->phone = $phone;
         }
     }
 
-    public function setAddress(Address &$address)
+    public function setAddress(Address $address)
     {
-        if ($address->link_type == Address::LINK_COMPANY && $address->link_id == $this->id) {
+        if ($address->type == Address::LINK_COMPANY && $address->jobid == $this->id) {
             $this->address = $address;
         }
     }
@@ -168,20 +168,35 @@ class Company
 }
 // }}}
 // {{{ class Job
+/** profile_job describes a Job, links to:
+ * - a Profile, through `pid`
+ * - a Company, through `jobid`
+ * The `id` field is the id of this job in the list of the jobs of its profile
+ *
+ * For the documentation of the phone table, please see classes/phone.php.
+ * For the documentation of the address table, please see classes/address.php.
+ *
+ * The possible relations are as follow:
+ * A Job is linked to a Company and a Profile
+ */
 class Job
 {
     public $pid;
     public $id;
 
-    private $company = null;
-    private $phones = array();
-    private $address = null;
+    public $company = null;
+    public $phones = array();
+    public $address = null;
+    public $terms = array();
 
-    public $company_id;
+    public $jobid;
 
     public $description;
-    public $url;
-    public $email;
+    public $user_site;
+    public $user_email;
+
+    public $pub;
+    public $email_pub;
 
     /** Fields are:
      * pid, id, company_id, description, url, email
@@ -191,6 +206,15 @@ class Job
         foreach ($data as $key => $val) {
             $this->$key = $val;
         }
+        $this->company = CompanyList::get($this->jobid);
+        if (is_null($this->company)) {
+            $entreprises = ProfileValidate::get_typed_requests($this->pid, 'entreprise');
+            foreach ($entreprises as $entreprise) {
+                if ($entreprise->id == $this->id) {
+                    $this->company = new Company(array('name' => $entreprise->name));
+                }
+            }
+        }
     }
 
     public function phones()
@@ -198,50 +222,41 @@ class Job
         return $this->phones;
     }
 
-    public function company()
+    public function address()
     {
-        return $this->company;
+        return $this->address;
     }
 
-    public function addPhone(Phone &$phone)
+    public function addPhone(Phone $phone)
     {
         if ($phone->link_type == Phone::LINK_JOB && $phone->link_id == $this->id && $phone->pid == $this->pid) {
-            $this->phones[] = $phone;
+            $this->phones[$phone->uniqueId()] = $phone;
         }
     }
 
     public function setAddress(Address $address)
     {
-        if ($address->link_id == Address::LINK_JOB && $address->link_id == $this->id && $address->pid == $this->pid) {
+        if ($address->type == Address::LINK_JOB && $address->id == $this->id && $address->pid == $this->pid) {
             $this->address = $address;
         }
     }
 
-    public function setCompany(Company $company)
+    public function addTerm(JobTerm $term)
     {
-        $this->company = $company;
+        $this->terms[$term->jtid] = $term;
     }
 }
 // }}}
-// {{{ class Address
-class Address
+// {{{ class JobTerm
+class JobTerm
 {
-    const LINK_JOB     = 'job';
-    const LINK_COMPANY = 'hq';
-    const LINK_PROFILE = 'home';
-
-    public $link_id;
-    public $link_type;
-
-    public $flags;
-    public $text;
-    public $postcode;
-    public $country;
-
-    private $phones = array();
+    public $jtid;
+    public $full_name;
+    public $pid;
+    public $jid;
 
     /** Fields are:
-     * pîd, id, link_id, link_type, flags, text, postcode, country
+     * pid, jid, jtid, full_name
      */
     public function __construct($data)
     {
@@ -249,22 +264,36 @@ class Address
             $this->$key = $val;
         }
     }
+}
+// }}}
+// {{{ class Education
+class Education
+{
+    public $id;
+    public $pid;
 
-    public function addPhone(Phone &$phone)
-    {
-        if ($phone->link_type == Phone::LINK_ADDRESS && $phone->link_id == $this->id && $phone->pid == $this->pid) {
-            $this->phones[] = $phone;
-        }
-    }
+    public $entry_year;
+    public $grad_year;
+    public $program;
+    public $flags;
 
-    public function phones()
-    {
-        return $this->phones;
-    }
+    public $school;
+    public $school_short;
+    public $school_url;
+    public $country;
+
+    public $degree;
+    public $degree_short;
+    public $degree_level;
 
-    public function hasFlags($flags)
+    public $field;
+
+    public function __construct(array $data)
     {
-        return $flags & $this->flags;
+        foreach ($data as $key => $val) {
+            $this->$key = $val;
+        }
+        $this->flags      = new PlFlagSet($this->flags);
     }
 }
 // }}}
@@ -272,23 +301,61 @@ class Address
 // {{{ class ProfileEducation                         [ Field ]
 class ProfileEducation extends ProfileField
 {
-    public $schools = array();
+    private $educations = array();
 
-    private function __construct(PlIterator $it)
+    public function __construct(PlInnerSubIterator $it)
     {
-        $this->visibility = Profile::VISIBILITY_PUBLIC;
+        $this->pid = $it->value();
         while ($edu = $it->next()) {
-            $this->schools[$edu['id']] = $edu;
+            $this->educations[$edu['id']] = new Education($edu);
         }
     }
 
-    public static function fetchData(array $pids, $visibility)
+    public function get($flags, $limit)
     {
-        $data = XDB::iterator('SELECT  *
-                                 FROM  profile_education
-                                WHERE  pid IN {?}
-                             ORDER BY  ' . XDB::formatCustomOrder('pid', $pids),
-                                 $pids);
+        $educations = array();
+        $year = getdate();
+        $year = $year['year'];
+        $nb = 0;
+        foreach ($this->educations as $id => $edu) {
+            if (
+                (($flags & Profile::EDUCATION_MAIN) && $edu->flags->hasFlag('primary'))
+                ||
+                (($flags & Profile::EDUCATION_EXTRA) && !$edu->flags->hasFlag('primary'))
+                ||
+                (($flags & Profile::EDUCATION_FINISHED) && $edu->grad_year <= $year)
+                ||
+                (($flags & Profile::EDUCATION_CURRENT) && $edu->grad_year > $year)
+                ||
+                ($flags & Profile::EDUCATION_ALL)
+            ) {
+                $educations[$id] = $edu;
+                ++$nb;
+            }
+            if ($limit != null && $nb >= $limit) {
+                break;
+            }
+        }
+        return $educations;
+    }
+
+    public static function fetchData(array $pids, Visibility $visibility)
+    {
+        $data = XDB::iterator('SELECT  pe.id, pe.pid,
+                                       pe.entry_year, pe.grad_year, pe.program, pe.flags,
+                                       pee.name AS school, pee.abbreviation AS school_short,
+                                       pee.url AS school_url, gc.country,
+                                       pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level,
+                                       pefe.field
+                                 FROM  profile_education AS pe
+                            LEFT JOIN  profile_education_enum AS pee ON (pee.id = pe.eduid)
+                            LEFT JOIN  geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country)
+                            LEFT JOIN  profile_education_degree_enum AS pede ON (pede.id = pe.degreeid)
+                            LEFT JOIN  profile_education_field_enum AS pefe ON (pefe.id = pe.fieldid)
+                                WHERE  pe.pid IN {?}
+                             ORDER BY  ' . XDB::formatCustomOrder('pid', $pids) . ',
+                                       NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id',
+                                $pids);
 
         return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
     }
@@ -299,23 +366,25 @@ class ProfileMedals extends ProfileField
 {
     public $medals = array();
 
-    private function __construct(PlIterator $it)
+    public function __construct(PlInnerSubIterator $it)
     {
+        $this->pid = $it->value();
         while ($medal = $it->next()) {
-            $this->medals[$medal['mid']] = $medal['gid'];
+            $this->medals[$medal['mid']] = $medal;
         }
     }
 
-    public static function fetchData(array $pids, $visibility)
+    public static function fetchData(array $pids, Visibility $visibility)
     {
-        $data = XDB::iterator('SELECT  pm.pid, pm.mid, pm.gid
+        $data = XDB::iterator('SELECT  pm.pid, pm.mid, pm.gid, pme.text, pme.img, pmge.text AS grade, pm.level
                                  FROM  profile_medals AS pm
                             LEFT JOIN  profiles AS p ON (pm.pid = p.pid)
-                                WHERE  pm.pid IN {?} AND p.medals_pub IN {?}
+                            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)
+                            LEFT JOIN  profile_visibility_enum AS pve ON (pve.access_level = {?})
+                                WHERE  pm.pid IN {?} AND pve.best_display_level + 0 <= p.medals_pub + 0
                              ORDER BY  ' . XDB::formatCustomOrder('pm.pid', $pids),
-                                XDB::formatArray($pids),
-                                XDB::formatArray($visibility)
-                            );
+                                $visibility->level(), $pids);
 
         return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
     }
@@ -325,160 +394,173 @@ class ProfileMedals extends ProfileField
 class ProfileNetworking extends ProfileField
 {
     private $networks = array();
-    private $visibilities = array();
 
-    private function __construct(PlIterator $it)
+    public function __construct(PlInnerSubIterator $it)
     {
+        $this->pid = $it->value();
         while ($network = $it->next()) {
-            $this->networks[$network['nwid']] = $network['address'];
-            $this->visibilities[$network['nwid']] = $network['pub'];
+            $network['network_type'] = new PlFlagSet($network['network_type']);
+            $this->networks[$network['id']] = $network;
         }
     }
 
-    public static function fetchData(array $pids, $visibility)
+    public static function fetchData(array $pids, Visibility $visibility)
     {
-        $data = XDB::iterator('SELECT  pid, nwid, address, pub
-                                 FROM  profile_networking
-                                WHERE  pid IN {?} AND pub IN {?}
-                             ORDER BY  ' . XDB::formatCustomOrder('pid', $pids),
-                                XDB::formatArray($pids),
-                                XDB::formatArray($visibility)
-                            );
+        $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)
+                            LEFT JOIN  profile_visibility_enum AS pve ON (pve.access_level = {?})
+                                WHERE  pn.pid IN {?} AND pve.best_display_level + 0 <= pn.pub + 0
+                             ORDER BY  ' . XDB::formatCustomOrder('pid', $pids) . ',
+                                       pn.nwid, id',
+                               $visibility->level(), $pids);
 
         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) {
+            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 (isset($limit) && $nb >= $limit) {
+                    break;
+                }
             }
         }
         return $nws;
     }
 }
 // }}}
-// {{{ class ProfilePhoto                             [ Field ]
-class ProfilePhoto extends ProfileField
+// {{{ class ProfileCorps                             [ Field ]
+class ProfileCorps extends ProfileField
 {
-    public $pic;
+    public $original;
+    public $current;
+
+    public $original_name;
+    public $original_abbrev;
+    public $original_still_exists;
+
+    public $current_name;
+    public $current_abbrev;
+    public $current_still_exists;
+    public $current_rank;
+    public $current_rank_abbrev;
 
     public function __construct(array $data)
     {
-        if ($data == null || count($data) == 0) {
-            $this->pic = null;
-        } else {
-            $this->pid = $data['pid'];
-            $this->pic = PlImage::fromDATA($data['attach'],
-                                           $data['attachmime'],
-                                           $data['x'],
-                                           $data['y']);
+        foreach ($data as $key => $val) {
+            $this->$key = $val;
         }
     }
 
-    public static function fetchData(array $pids, $visibility)
+    public static function fetchData(array $pids, Visibility $visibility)
     {
-        $data = XDB::iterator('SELECT  *
-                                 FROM  profile_photos
-                                WHERE  pid IN {?} AND attachmime IN (\'jpeg\', \'png\') AND pub IN {?}
+        $data = XDB::iterator('SELECT  pc.pid, pc.original_corpsid AS original, pc.current_corpsid AS current,
+                                       pceo.name AS original_name, pceo.abbreviation AS original_abbrev,
+                                       pceo.still_exists AS original_still_exists,
+                                       pcec.name AS current_name, pcec.abbreviation AS current_abbrev,
+                                       pcec.still_exists AS current_still_exists,
+                                       pcrec.name AS current_rank, pcrec.abbreviation AS current_rank_abbrev,
+                                       rankid
+                                 FROM  profile_corps AS pc
+                            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)
+                            LEFT JOIN  profile_visibility_enum AS pve ON (pve.access_level = {?})
+                                WHERE  pc.pid IN {?} AND pve.best_display_level + 0 <= pc.corps_pub + 0 AND pceo.id != 1
                              ORDER BY  ' . XDB::formatCustomOrder('pid', $pids),
-                               $pids, $visibility);
+                                $visibility->level(), $pids);
 
         return $data;
     }
 }
 // }}}
-// {{{ class ProfileCorps                             [ Field ]
-class ProfileCorps extends ProfileField
+// {{{ class ProfileMentoringCountries                [ Field ]
+class ProfileMentoringCountries extends ProfileField
 {
-    public $original;
-    public $current;
-    public $rank;
+    public $countries = array();
 
-    private function __construct(array $data)
+    public function __construct(PlInnerSubIterator $it)
     {
-        $this->original = $data['original_corpsid'];
-        $this->current  = $data['current_corpsid'];
-        $this->rank     = $data['rankid'];
-        $this->visibility = $data['corps_pub'];
+        $this->pid = $it->value();
+        while ($country = $it->next()) {
+            $this->countries[$country['id']] = $country['name'];
+        }
     }
 
-    public static function fetchData(array $pids, $visibility)
+    public static function fetchData(array $pids, Visibility $visibility)
     {
-        $data = XDB::iterator('SELECT  pid, original_corpsid, current_corpsid,
-                                       rankid
-                                 FROM  profile_corps
-                                WHERE  pid IN {?} AND corps_pub IN {?}
-                             ORDER BY  ' . XDB::formatCustomOrder('pid', $pids),
-                                XDB::formatArray($pids),
-                                XDB::formatArray($visibility)
-                            );
+        $data = XDB::iterator('SELECT  pmc.pid, pmc.country AS id, gc.country 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 $data;
+        return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
     }
 }
 // }}}
-
-/** Loading of data for a Profile :
- * 1) load jobs, addresses, phones
- * 2) attach phones to addresses, jobs and profiles
- * 3) attach addresses to jobs and profiles
- */
-
 // {{{ class ProfileAddresses                         [ Field ]
 class ProfileAddresses extends ProfileField
 {
     private $addresses = array();
 
-    public function __construct(PlIterator $it)
+    public function __construct(PlInnerSubIterator $it)
     {
-        if ($it instanceof PlInnerSubIterator) {
-            $this->pid = $it->value();
-        }
-
-        while ($addr = $it->next()) {
-            $this->addresses[] = new Address($addr);
+        $this->pid = $it->value();
+        while ($address = $it->next()) {
+            $this->addresses[] = new Address($address);
         }
     }
 
     public function get($flags, $limit = null)
     {
-        $res = array();
+        $addresses = array();
         $nb = 0;
-        foreach ($this->addresses as $addr) {
-            if ($addr->hasFlags($flags)) {
-                $res[] = $addr;
-                $nb++;
+        foreach ($this->addresses as $address) {
+            if ((($flags & Profile::ADDRESS_MAIN) && $address->hasFlag('current'))
+                || (($flags & Profile::ADDRESS_POSTAL) && $address->hasFlag('mail'))
+                || (($flags & Profile::ADDRESS_PERSO) && $address->type == Address::LINK_PROFILE)
+                || (($flags & Profile::ADDRESS_PRO) && $address->type == Address::LINK_JOB)
+            ) {
+                $addresses[] = $address;
+                ++$nb;
             }
             if ($limit != null && $nb == $limit) {
                 break;
             }
         }
-        return PlIteratorUtils::fromArray($res);
+        return $addresses;
     }
 
-    public static function fetchData(array $pids, $visibility)
+    public static function fetchData(array $pids, Visibility $visibility)
     {
-        $data = XDB::iterator('SELECT  pid, text, postalCode, type, latitude, longitude,
-                                       flags, type
-                                 FROM  profile_addresses
-                                WHERE  pid in {?} AND pub IN {?}
-                             ORDER BY  ' . XDB::formatCustomOrder('pid', $pids),
-                               $pids, $visibility);
-
-        return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
+        $it = Address::iterate($pids, array(), array(), $visibility);
+        return PlIteratorUtils::subIterator($it->value(), PlIteratorUtils::arrayValueCallback('pid'));
     }
 
-    public static function addPhones(array $addresses, $phones)
+    public function addPhones(ProfilePhones $phones)
     {
-        foreach ($phones as $phone) {
-            if ($phone->link_type == Phone::LINK_ADDRESS) {
-                $addresses[$phone->link_id]->addPhone($phone);
+        $p = $phones->get(Profile::PHONE_LINK_ADDRESS | Profile::PHONE_TYPE_ANY);
+        foreach ($p as $phone) {
+            /* We must iterate on the addresses because id is not uniq thus,
+             * $this->addresse[$phone->link_id] is invalid.
+             */
+            foreach ($this->addresses as $address) {
+                if ($address->type == Address::LINK_PROFILE && $address->id == $phone->link_id) {
+                    $address->addPhone($phone);
+                }
             }
         }
-        return $addresses;
     }
 }
 // }}}
@@ -487,23 +569,34 @@ class ProfilePhones extends ProfileField
 {
     private $phones = array();
 
-    private function __construct(PlIterator $phones)
+    public function __construct(PlInnerSubIterator $it)
     {
+        $this->pid = $it->value();
         while ($phone = $it->next()) {
-            $this->phones[] = Phone::buildFromData($phone);
+            $this->phones[] = new Phone($phone);
         }
     }
 
-    public static function fetchData(array $pids, $visibility)
+    public function get($flags, $limit = null)
     {
-        $data = XDB::iterator('SELECT  type, search, display, link_type, comment
-                                 FROM  profile_phones
-                                WHERE  pid IN {?} AND pub IN {?}
-                             ORDER BY  ' . XDB::formatCustomOrder('pid', $pids),
-                                 XDB::formatArray($pids),
-                                 XDB::formatArray($visibility)
-                             );
-        return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
+        $phones = array();
+        $nb = 0;
+        foreach ($this->phones as $id => $phone) {
+            if ($phone->hasFlags($flags)) {
+                $phones[$id] = $phone;
+                ++$nb;
+                if ($limit != null && $nb == $limit) {
+                    break;
+                }
+            }
+        }
+        return $phones;
+    }
+
+    public static function fetchData(array $pids, Visibility $visibility)
+    {
+        $it = Phone::iterate($pids, array(), array(), $visibility);
+        return PlIteratorUtils::subIterator($it->value(), PlIteratorUtils::arrayValueCallback('pid'));
     }
 }
 // }}}
@@ -512,59 +605,224 @@ class ProfileJobs extends ProfileField
 {
     private $jobs = array();
 
-    private function __construct(PlIterator $jobs)
+    public function __construct(PlInnerSubIterator $jobs)
     {
+        $this->pid = $jobs->value();
         while ($job = $jobs->next()) {
-            $this->jobs[] = Jobs::buildFromData($job);
+            $this->jobs[$job['id']] = new Job($job);
         }
     }
 
-    public static function fetchData(array $pids, $visibility)
+    public static function fetchData(array $pids, Visibility $visibility)
     {
-        $data = XDB::iterator('SELECT  description, url, jobid, IF(email_pub IN {?}, email, NULL) AS email
+        CompanyList::preload($pids);
+        $data = XDB::iterator('SELECT  id, pid, description, url as user_site, jobid, pub,                                       IF(pve.best_display_level + 0 <= email_pub + 0, email, NULL) AS user_email, email_pub
                                  FROM  profile_job
-                                WHERE  pid IN {?} AND pub IN {?}
-                             ORDER BY  ' . XDB::formatCustomOrder('pid', $pids),
-                                 XDB::formatArray($visibility),
-                                 XDB::formatArray($pids),
-                                 XDB::formatArray($visibility)
-                             );
+                            LEFT JOIN  profile_visibility_enum AS pve ON (pve.access_level = {?})
+                                WHERE  pid IN {?} AND pve.best_display_level + 0 <= pub + 0
+                             ORDER BY  ' . XDB::formatCustomOrder('pid', $pids) . ', id',
+                                 $visibility->level(), $pids);
         return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
     }
 
-    public static function addPhones(array $jobs, array $phones)
+    public function get($flags, $limit = null)
     {
-        foreach ($phones as $phone)
-        {
-            if ($phone->link_type == Phone::LINK_JOB) {
-                $jobs[$phone->link_id]->addPhones($phone);
+        $jobs = array();
+        $nb = 0;
+        foreach ($this->jobs as $id => $job) {
+            $jobs[$id] = $job;
+            ++$nb;
+            if ($limit != null && $nb >= $limit) {
+                break;
             }
         }
         return $jobs;
     }
 
-    public static function addAddresses(array $jobs, array $addresses)
+    public function addPhones(ProfilePhones $phones)
     {
-        foreach ($addresses as $address)
-        {
-            if ($address->link_type == Address::LINK_JOB) {
-                $jobs[$address->link_id]->setAddress($address);
+        $p = $phones->get(Profile::PHONE_LINK_JOB | Profile::PHONE_TYPE_ANY);
+        foreach ($p as $phone) {
+            if ($phone->link_type == Phone::LINK_JOB && array_key_exists($phone->link_id, $this->jobs)) {
+                $this->jobs[$phone->link_id]->addPhone($phone);
             }
         }
-        return $jobs;
     }
 
-    public static function addCompanies(array $jobs, array $companies)
+    public function addAddresses(ProfileAddresses $addresses)
     {
-        foreach ($jobs as $job)
-        {
-            $job->setCompany($companies[$job->company_id]);
+        $a = $addresses->get(Profile::ADDRESS_PRO);
+        foreach ($a as $address) {
+            if ($address->type == Address::LINK_JOB && array_key_exists($address->id, $this->jobs)) {
+                $this->jobs[$address->id]->setAddress($address);
+            }
+        }
+    }
+
+    public function addCompanies(array $companies)
+    {
+        foreach ($this->jobs as $job) {
+            $this->company = $companies[$job->jobid];
+        }
+    }
+
+    public function addJobTerms(ProfileJobTerms $jobterms)
+    {
+        $terms = $jobterms->get();
+        foreach ($terms as $term) {
+            if ($this->pid == $term->pid && array_key_exists($term->jid, $this->jobs)) {
+                $this->jobs[$term->jid]->addTerm($term);
+            }
         }
-        return $jobs;
     }
 }
 // }}}
+// {{{ class ProfileJobTerms                          [ Field ]
+class ProfileJobTerms extends ProfileField
+{
+    private $jobterms = array();
+
+    public function __construct(PlInnerSubIterator $it)
+    {
+        $this->pid = $it->value();
+        while ($term = $it->next()) {
+            $this->jobterms[] = new JobTerm($term);
+        }
+    }
+
+    public function get()
+    {
+        return $this->jobterms;
+    }
 
+    public static function fetchData(array $pids, Visibility $visibility)
+    {
+        $data = XDB::iterator('SELECT  jt.jtid, jte.full_name, jt.pid, jt.jid
+                                 FROM  profile_job_term AS jt
+                           INNER JOIN  profile_job AS j ON (jt.pid = j.pid AND jt.jid = j.id)
+                            LEFT JOIN  profile_job_term_enum AS jte USING(jtid)
+                            LEFT JOIN  profile_visibility_enum AS pve ON (pve.access_level = {?})
+                                WHERE  jt.pid IN {?} AND pve.best_display_level + 0 <= j.pub + 0
+                             ORDER BY  ' . XDB::formatCustomOrder('jt.pid', $pids),
+                                 $visibility->level(), $pids);
+        return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
+    }
+}
+// }}}
+// {{{ class ProfileMentoringTerms                    [ Field ]
+class ProfileMentoringTerms extends ProfileJobTerms
+{
+    public static function fetchData(array $pids, Visibility $visibility)
+    {
+        $data = XDB::iterator('SELECT  mt.jtid, jte.full_name, mt.pid
+                                 FROM  profile_mentor_term AS mt
+                            LEFT JOIN  profile_job_term_enum AS jte USING(jtid)
+                                WHERE  mt.pid IN {?}
+                             ORDER BY  ' . XDB::formatCustomOrder('mt.pid', $pids),
+                                $pids);
+        return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
+    }
+}
+// }}}
+// {{{ class ProfileSkills                                   [ Field ]
+class ProfileSkills extends ProfileField
+{
+    public $skills = array();
+
+    public function __construct(PlInnerSubIterator $it)
+    {
+        $this->pid = $it->value();
+        while ($skill = $it->next()) {
+            $this->skills[$skill['cid']] = $skill;
+        }
+    }
+
+    public static function fetchData(array $pids, Visibility $visibility)
+    {
+        $data = XDB::iterator('SELECT  ps.cid, pse.text_fr, ps.level, ps.pid
+                                 FROM  profile_skills          AS ps
+                           INNER JOIN  profile_skill_enum      AS pse ON (pse.id = ps.cid)
+                                WHERE  ps.pid IN {?}
+                             ORDER BY  ' . XDB::formatCustomOrder('ps.pid', $pids),
+                              $pids);
+        return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
+    }
+}
+// }}}
+// {{{ class ProfileLanguages                                [ Field ]
+class ProfileLanguages extends ProfileField
+{
+    public $languages = array();
+
+    public function __construct(PlInnerSubIterator $it)
+    {
+        static $levels = array(
+            1 => 'connaissance basique',
+            2 => 'maîtrise des bases',
+            3 => 'maîtrise limitée',
+            4 => 'maîtrise générale',
+            5 => 'bonne maîtrise',
+            6 => 'maîtrise complète'
+        );
+
+        $this->pid = $it->value();
+        while ($language = $it->next()) {
+            $this->languages[$language['lid']] = $language;
+            $this->languages[$language['lid']]['level'] = $levels[$language['level']];
+        }
+    }
+
+    public static function fetchData(array $pids, Visibility $visibility)
+    {
+        $data = XDB::iterator('SELECT  ps.lid, pse.language, ps.level, ps.pid
+                                 FROM  profile_langskills     AS ps
+                           INNER JOIN  profile_langskill_enum AS pse ON (pse.iso_639_2b = ps.lid)
+                                WHERE  ps.pid IN {?}
+                             ORDER BY  ' . XDB::formatCustomOrder('ps.pid', $pids),
+                              $pids);
+        return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
+    }
+}
+// }}}
+// {{{ class ProfilePartnerSharing                    [ Field ]
+class ProfilePartnerSharing extends ProfileField
+{
+    public function __construct(PlInnerSubIterator $it)
+    {
+        require_once 'partnersharing.inc.php';
+
+        $this->pid = $it->value();
+        while ($partner_settings = $it->next()) {
+            $settings = new PartnerSettings($partner_settings);
+            $this->partners_settings[$settings->partner->id] = $settings;
+        }
+    }
+
+    public static function fetchData(array $pids, Visibility $visibility)
+    {
+        $data = XDB::iterator('SELECT  ppss.pid, ppss.exposed_uid, ppss.sharing_level,
+                                       ppss.allow_email, ppss.partner_id,
+                                       ppse.shortname AS partner_shortname,
+                                       ppse.name AS partner_name,
+                                       ppse.url AS partner_url
+                                 FROM  profile_partnersharing_settings AS ppss
+                            LEFT JOIN  profile_partnersharing_enum AS ppse ON (ppss.partner_id = ppse.id)
+                                WHERE  ppss.pid IN {?}
+                             ORDER BY  ' . XDB::formatCustomOrder('ppss.pid', $pids),
+                                 $pids);
+        return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
+    }
+
+    public function get($partner_id)
+    {
+        if (isset($this->partners_settings[$partner_id])) {
+            return $this->partners_settings[$partner_id];
+        } else {
+            return PartnerSettings::getEmpty($partner_id);
+        }
+    }
+}
+// }}}
 // {{{ class CompanyList
 class CompanyList
 {
@@ -578,41 +836,55 @@ class CompanyList
         }
         // Load raw data
         if (count($pids)) {
-            $join = 'LEFT JOIN profile_jobs ON (profile_job.jobid = profile_job_enum.id)';
-            $where = 'profile_jobs.pid IN ' . XDB::formatArray($pids);
+            $join = 'LEFT JOIN profile_job ON (profile_job.jobid = pje.id)';
+            $where = 'WHERE profile_job.pid IN ' . XDB::formatArray($pids);
         } else {
             $join = '';
             $where = '';
         }
 
-        $it = XDB::iterator('SELECT  pje.id, pje.name, pje.acronmy, pje.url,
-                                     pa.flags, pa.text, pa.postcode, pa.country,
-                                     pa.link_type, pa.pub
+        $it = XDB::iterator('SELECT  pje.id, pje.name, pje.acronym, pje.url,
+                                     pa.flags, pa.text, pa.type, pa.pub
                                FROM  profile_job_enum AS pje
                           LEFT JOIN  profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\')
                                   ' . $join . '
                                   ' . $where);
+        $newcompanies = array();
         while ($row = $it->next()) {
-            $cp = Company::buildFromData($row);
-            $addr = Address::buildFromData($row);
+            $cp = new Company($row);
+            $addr = new Address($row);
             $cp->setAddress($addr);
+            if (!array_key_exists($row['id'], self::$companies)) {
+                $newcompanies[] = $row['id'];
+            }
             self::$companies[$row['id']] = $cp;
         }
 
-        // TODO: add phones to addresses
+        // Add phones to hq
+        if (count($newcompanies)) {
+            $it = Phone::iterate(array(), array(Phone::LINK_COMPANY), $newcompanies, Visibility::defaultForRead());
+            while ($phone = $it->next()) {
+                self::$companies[$phone->link_id]->setPhone($phone);
+            }
+        }
+
         if (count($pids) == 0) {
             self::$fullload = true;
         }
     }
 
-    static public function getCompany($id)
+    static public function get($id)
     {
-        if (!array_key_exists($id, self::$companies)) {
+        if (!is_null($id) && !array_key_exists($id, self::$companies)) {
             self::preload();
         }
-        return self::$companies[$id];
+        if (isset(self::$companies[$id])) {
+            return self::$companies[$id];
+        }
+        return null;
     }
 }
+// }}}
 
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
 ?>