Moving to GitHub.
[platal.git] / include / profilefields.inc.php
index 506b8ac..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   *
@@ -33,10 +33,12 @@ abstract class ProfileField
         Profile::FETCH_MEDALS         => 'ProfileMedals',
         Profile::FETCH_NETWORKING     => 'ProfileNetworking',
         Profile::FETCH_PHONES         => 'ProfilePhones',
-        Profile::FETCH_MENTOR_SECTOR  => 'ProfileMentoringSectors',
         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
@@ -51,12 +53,12 @@ abstract class ProfileField
      *
      * MUST be reimplemented for each kind of ProfileField.
      */
-    public static function fetchData(array $pids, ProfileVisibility $visibility)
+    public static function fetchData(array $pids, Visibility $visibility)
     {
         return PlIteratorUtils::emptyIterator();
     }
 
-    public static function buildForPID($cls, $pid, ProfileVisibility $visibility)
+    public static function buildForPID($cls, $pid, Visibility $visibility)
     {
         $res = self::buildFromPIDs($cls, array($pid), $visibility);
         return array_pop($res);
@@ -68,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, ProfileVisibility $visibility)
+    public static function buildFromPIDs($cls, array $pids, Visibility $visibility)
     {
         $it = new ProfileFieldIterator($cls, $pids, $visibility);
         $res = array();
@@ -78,7 +80,7 @@ abstract class ProfileField
         return $res;
     }
 
-    public static function getForPID($cls, $pid, ProfileVisibility $visibility)
+    public static function getForPID($cls, $pid, Visibility $visibility)
     {
         $it = new ProfileFieldIterator($cls, array($pid), $visibility);
         return $it->next();
@@ -92,7 +94,7 @@ class ProfileFieldIterator implements PlIterator
     private $data;
     private $cls;
 
-    public function __construct($cls, array $pids, ProfileVisibility $visibility)
+    public function __construct($cls, array $pids, Visibility $visibility)
     {
         if (is_numeric($cls) && isset(ProfileField::$fields[$cls])) {
             $cls = ProfileField::$fields[$cls];
@@ -149,16 +151,16 @@ class Company
         }
     }
 
-    public function setPhone(Phone &$phone)
+    public function setPhone(Phone $phone)
     {
-        if ($phone->linkType() == Phone::LINK_COMPANY && $phone->linkId() == $this->id) {
+        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;
         }
     }
@@ -166,6 +168,17 @@ 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;
@@ -182,9 +195,8 @@ class Job
     public $user_site;
     public $user_email;
 
-    public $sector;
-    public $subsector;
-    public $subsubsector;
+    public $pub;
+    public $email_pub;
 
     /** Fields are:
      * pid, id, company_id, description, url, email
@@ -196,9 +208,12 @@ class Job
         }
         $this->company = CompanyList::get($this->jobid);
         if (is_null($this->company)) {
-            require_once 'validations.inc.php';
-            $entreprise = ProfileValidate::get_typed_requests($this->pid, 'entreprise');
-            $this->company = new Company(array('name' =>  $entreprise[$this->id]->name));
+            $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));
+                }
+            }
         }
     }
 
@@ -212,21 +227,21 @@ class Job
         return $this->address;
     }
 
-    public function addPhone(Phone &$phone)
+    public function addPhone(Phone $phone)
     {
-        if ($phone->linkType() == Phone::LINK_JOB && $phone->linkId() == $this->id && $phone->pid() == $this->pid) {
+        if ($phone->link_type == Phone::LINK_JOB && $phone->link_id == $this->id && $phone->pid == $this->pid) {
             $this->phones[$phone->uniqueId()] = $phone;
         }
     }
 
     public function setAddress(Address $address)
     {
-        if ($address->link_type == 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 addTerm(JobTerm &$term)
+    public function addTerm(JobTerm $term)
     {
         $this->terms[$term->jtid] = $term;
     }
@@ -251,74 +266,6 @@ class JobTerm
     }
 }
 // }}}
-// {{{ class Address
-class Address
-{
-    const LINK_JOB     = 'job';
-    const LINK_COMPANY = 'hq';
-    const LINK_PROFILE = 'home';
-
-    public $flags;
-    public $id; // The ID of the address among those associated with its link
-    public $link_id; // The ID of the object to which the address is linked (profile, job, company)
-    public $link_type;
-
-    public $text;
-    public $postalCode;
-    public $latitude;
-    public $longitude;
-
-    public $locality;
-    public $subAdministrativeArea;
-    public $administrativeArea;
-    public $country;
-
-    public $comment;
-
-    private $phones = array();
-
-    /** Fields are:
-     * pîd, id, link_id, link_type, flags, text, postcode, country
-     */
-    public function __construct($data)
-    {
-        foreach ($data as $key => $val) {
-            $this->$key = $val;
-        }
-        $this->flags = new PlFlagSet($this->flags);
-    }
-
-    public function uid() {
-        $uid = $this->link_type . '_';
-        if ($this->link_type != self::LINK_COMPANY) {
-            $uid .= $this->pid . '_';
-        }
-        $uid .= $this->link_id . '_' . $this->id;
-    }
-
-    public function addPhone(Phone &$phone)
-    {
-        if (
-            $phone->linkType() == Phone::LINK_ADDRESS && $phone->linkId() == $this->id &&
-            ($this->link_type == self::LINK_COMPANY || $phone->pid() == $this->pid) ) {
-            $this->phones[$phone->uniqueId()] = $phone;
-        }
-    }
-
-    public function phones()
-    {
-        return $this->phones;
-    }
-
-    public function hasFlag($flag)
-    {
-        if (!$this->flags instanceof PlFlagSet) {
-            $this->flags = new PlFlagSet($this->flags);
-        }
-        return $this->flags->hasFlag($flag);
-    }
-}
-// }}}
 // {{{ class Education
 class Education
 {
@@ -392,12 +339,12 @@ class ProfileEducation extends ProfileField
         return $educations;
     }
 
-    public static function fetchData(array $pids, ProfileVisibility $visibility)
+    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.countryFR AS country,
+                                       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
@@ -427,16 +374,17 @@ class ProfileMedals extends ProfileField
         }
     }
 
-    public static function fetchData(array $pids, ProfileVisibility $visibility)
+    public static function fetchData(array $pids, Visibility $visibility)
     {
-        $data = XDB::iterator('SELECT  pm.pid, pm.mid, pm.gid, pme.text, pme.img, pmge.text AS grade
+        $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)
                             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 {?}
+                            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),
-                                $pids, $visibility->levels());
+                                $visibility->level(), $pids);
 
         return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
     }
@@ -456,15 +404,16 @@ class ProfileNetworking extends ProfileField
         }
     }
 
-    public static function fetchData(array $pids, ProfileVisibility $visibility)
+    public static function fetchData(array $pids, Visibility $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)
-                                WHERE  pid IN {?} AND pub IN {?}
+                            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',
-                               $pids, $visibility->levels());
+                               $visibility->level(), $pids);
 
         return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
     }
@@ -512,7 +461,7 @@ class ProfileCorps extends ProfileField
         }
     }
 
-    public static function fetchData(array $pids, ProfileVisibility $visibility)
+    public static function fetchData(array $pids, Visibility $visibility)
     {
         $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,
@@ -525,41 +474,15 @@ 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 {?} AND pceo.id != 1
+                            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->levels());
+                                $visibility->level(), $pids);
 
         return $data;
     }
 }
 // }}}
-// {{{ 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
 {
@@ -573,9 +496,9 @@ class ProfileMentoringCountries extends ProfileField
         }
     }
 
-    public static function fetchData(array $pids, ProfileVisibility $visibility)
+    public static function fetchData(array $pids, Visibility $visibility)
     {
-        $data = XDB::iterator('SELECT  pmc.pid, pmc.country AS id, gc.countryFR AS name
+        $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 {?}
@@ -586,114 +509,56 @@ class ProfileMentoringCountries extends ProfileField
     }
 }
 // }}}
-
-/** 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
- */
-
-// {{{ Database schema (profile_address, profile_jobs)
-/** The database for this is very unclear, so here is a little schema :
- * 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
- *
- * profile_addresses describes an Address, which
- * related to either a Profile, a Job or a Company:
- * - for a Profile:
- *   - `type` is set to 'home'
- *   - `pid` is set to the related profile pid
- *   - `id` is the id of the address in the list of those related to that profile
- *   - `jobid` is empty
- *
- * - for a Company:
- *   - `type` is set to 'hq'
- *   - `pid` is set to 0
- *   - `jobid` is set to the id of the company
- *   - `id` is set to 0 (only one address per Company)
- *
- * - for a Job:
- *   - `type` is set to 'job'
- *   - `pid` is set to the pid of the Profile of the related Job
- *   - `jobid` is set to the Company of the job (this information is redundant
- *              with that of the row of profile_job for the related job)
- *   - `id` is the id of the job to which we refer (i.e `profile_job.id`)
- *
- * For the documentation of the phone table, please see classes/phone.php.
- *
- * The possible relations are as follow:
- * An Address can be linked to a Company, a Profile, a Job
- * A Job is linked to a Company and a Profile
- */
-// }}}
-
 // {{{ 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 (
-                (($flags & Profile::ADDRESS_MAIN) && $addr->hasFlag('current'))
-                ||
-                (($flags & Profile::ADDRESS_POSTAL) && $addr->hasFlag('mail'))
-                ||
-                (($flags & Profile::ADDRESS_PERSO) && $addr->link_type == Address::LINK_PROFILE)
-                ||
-                (($flags & Profile::ADDRESS_PRO) && $addr->link_type == Address::LINK_JOB)
+        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)
             ) {
-                $res[] = $addr;
-                $nb++;
+                $addresses[] = $address;
+                ++$nb;
             }
             if ($limit != null && $nb == $limit) {
                 break;
             }
         }
-        return $res;
+        return $addresses;
     }
 
-    public static function fetchData(array $pids, ProfileVisibility $visibility)
+    public static function fetchData(array $pids, Visibility $visibility)
     {
-        $data = XDB::iterator('SELECT  pa.id, pa.pid, pa.flags, pa.type AS link_type,
-                                       IF(pa.type = \'home\', pid, IF(pa.type = \'job\', pa.id, jobid)) AS link_id,
-                                       pa.text, pa.postalCode, pa.latitude, pa.longitude, pa.comment,
-                                       gl.name AS locality, gas.name AS subAdministrativeArea,
-                                       ga.name AS administrativeArea, gc.countryFR AS country
-                                 FROM  profile_addresses AS pa
-                            LEFT JOIN  geoloc_localities AS gl ON (gl.id = pa.localityId)
-                            LEFT JOIN  geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId)
-                            LEFT JOIN  geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId)
-                            LEFT JOIN  geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId)
-                                WHERE  pa.pid in {?} AND pa.pub IN {?}
-                             ORDER BY  ' . XDB::formatCustomOrder('pid', $pids),
-                               $pids, $visibility->levels());
-
-        return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
+        $it = Address::iterate($pids, array(), array(), $visibility);
+        return PlIteratorUtils::subIterator($it->value(), PlIteratorUtils::arrayValueCallback('pid'));
     }
 
     public function addPhones(ProfilePhones $phones)
     {
         $p = $phones->get(Profile::PHONE_LINK_ADDRESS | Profile::PHONE_TYPE_ANY);
         foreach ($p as $phone) {
-            if ($phone->linkType() == Phone::LINK_ADDRESS && array_key_exists($phone->linkId(), $this->addresses)) {
-                $this->addresses[$phone->linkId()]->addPhone($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);
+                }
             }
         }
     }
@@ -728,9 +593,9 @@ class ProfilePhones extends ProfileField
         return $phones;
     }
 
-    public static function fetchData(array $pids, ProfileVisibility $visibility)
+    public static function fetchData(array $pids, Visibility $visibility)
     {
-        $it = Phone::iterate($pids, array(), array(), $visibility->levels());
+        $it = Phone::iterate($pids, array(), array(), $visibility);
         return PlIteratorUtils::subIterator($it->value(), PlIteratorUtils::arrayValueCallback('pid'));
     }
 }
@@ -748,21 +613,15 @@ class ProfileJobs extends ProfileField
         }
     }
 
-    public static function fetchData(array $pids, ProfileVisibility $visibility)
+    public static function fetchData(array $pids, Visibility $visibility)
     {
         CompanyList::preload($pids);
-        $data = XDB::iterator('SELECT  pj.id, pj.pid, pj.description, pj.url as user_site,
-                                       IF(pj.email_pub IN {?}, pj.email, NULL) AS user_email,
-                                       pj.jobid, pjse.name AS sector, pjsse.name AS subsector,
-                                       pjssse.name AS subsubsector
-                                 FROM  profile_job AS pj
-                            LEFT JOIN  profile_job_sector_enum AS pjse ON (pjse.id = pj.sectorid)
-                            LEFT JOIN  profile_job_subsector_enum AS pjsse ON (pjsse.id = pj.subsectorid)
-                            LEFT JOIN  profile_job_subsubsector_enum AS pjssse ON (pjssse.id = pj.subsubsectorid)
-                                WHERE  pj.pid IN {?} AND pj.pub IN {?}
-                             ORDER BY  ' . XDB::formatCustomOrder('pid', $pids) . ',
-                                       pj.id',
-                                 $visibility->levels(), $pids, $visibility->levels());
+        $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
+                            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'));
     }
 
@@ -784,8 +643,8 @@ class ProfileJobs extends ProfileField
     {
         $p = $phones->get(Profile::PHONE_LINK_JOB | Profile::PHONE_TYPE_ANY);
         foreach ($p as $phone) {
-            if ($phone->linkType() == Phone::LINK_JOB && array_key_exists($phone->linkId(), $this->jobs)) {
-                $this->jobs[$phone->linkId()]->addPhone($phone);
+            if ($phone->link_type == Phone::LINK_JOB && array_key_exists($phone->link_id, $this->jobs)) {
+                $this->jobs[$phone->link_id]->addPhone($phone);
             }
         }
     }
@@ -794,8 +653,8 @@ class ProfileJobs extends ProfileField
     {
         $a = $addresses->get(Profile::ADDRESS_PRO);
         foreach ($a as $address) {
-            if ($address->link_type == Address::LINK_JOB && array_key_exists($address->link_id, $this->jobs)) {
-                $this->jobs[$address->link_id]->setAddress($address);
+            if ($address->type == Address::LINK_JOB && array_key_exists($address->id, $this->jobs)) {
+                $this->jobs[$address->id]->setAddress($address);
             }
         }
     }
@@ -812,7 +671,7 @@ class ProfileJobs extends ProfileField
         $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);
+                $this->jobs[$term->jid]->addTerm($term);
             }
         }
     }
@@ -836,15 +695,16 @@ class ProfileJobTerms extends ProfileField
         return $this->jobterms;
     }
 
-    public static function fetchData(array $pids, ProfileVisibility $visibility)
+    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)
-                                WHERE  jt.pid IN {?} AND j.pub IN {?}
+                            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),
-                                 $pids, $visibility->levels());
+                                 $visibility->level(), $pids);
         return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
     }
 }
@@ -852,7 +712,7 @@ class ProfileJobTerms extends ProfileField
 // {{{ class ProfileMentoringTerms                    [ Field ]
 class ProfileMentoringTerms extends ProfileJobTerms
 {
-    public static function fetchData(array $pids, ProfileVisibility $visibility)
+    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
@@ -864,6 +724,105 @@ class ProfileMentoringTerms extends ProfileJobTerms
     }
 }
 // }}}
+// {{{ 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
 {
@@ -885,8 +844,7 @@ class CompanyList
         }
 
         $it = XDB::iterator('SELECT  pje.id, pje.name, pje.acronym, pje.url,
-                                     pa.flags, pa.text, pa.postalCode, pa.countryId,
-                                     pa.type, pa.pub
+                                     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 . '
@@ -904,9 +862,9 @@ class CompanyList
 
         // Add phones to hq
         if (count($newcompanies)) {
-            $it = Phone::iterate(array(), array(Phone::LINK_COMPANY), $newcompanies);
+            $it = Phone::iterate(array(), array(Phone::LINK_COMPANY), $newcompanies, Visibility::defaultForRead());
             while ($phone = $it->next()) {
-                self::$companies[$phone->linkId()]->setPhone($phone);
+                self::$companies[$phone->link_id]->setPhone($phone);
             }
         }
 
@@ -917,12 +875,16 @@ class CompanyList
 
     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:
 ?>